File: trigger_test.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (194 lines) | stat: -rw-r--r-- 6,777 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/kernel_v2/config/trigger")

describe VagrantPlugins::Kernel_V2::TriggerConfig do
  include_context "unit"

  subject { described_class.new }

  let(:machine) { double("machine") }

  def assert_invalid
    errors = subject.validate(machine)
    if !errors.values.any? { |v| !v.empty? }
      raise "No errors: #{errors.inspect}"
    end
  end

  def assert_valid
    errors = subject.validate(machine)
    if !errors.values.all? { |v| v.empty? }
      raise "Errors: #{errors.inspect}"
    end
  end

  before do
    env = double("env")
    allow(env).to receive(:root_path).and_return(nil)
    allow(machine).to receive(:env).and_return(env)
    allow(machine).to receive(:provider_config).and_return(nil)
    allow(machine).to receive(:provider_options).and_return({})
  end

  it "is valid with test defaults" do
    subject.finalize!
    assert_valid
  end

  let (:hash_block) { {info: "hi", run: {inline: "echo 'hi'"}} }
  let (:splat) { [:up, :destroy, :halt] }
  let (:arr) { [[:up, :destroy, :halt]] }

  describe "creating a before trigger" do
    it "creates a trigger with the splat syntax" do
      subject.before(:up, hash_block)
      bf_trigger = subject.instance_variable_get(:@_before_triggers)
      expect(bf_trigger.size).to eq(1)
      expect(bf_trigger.first).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "creates a trigger with the array syntax" do
      subject.before([:up], hash_block)
      bf_trigger = subject.instance_variable_get(:@_before_triggers)
      expect(bf_trigger.size).to eq(1)
      expect(bf_trigger.first).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "creates a trigger with the block syntax" do
      subject.before :up do |trigger|
        trigger.name = "rspec"
      end
      bf_trigger = subject.instance_variable_get(:@_before_triggers)
      expect(bf_trigger.size).to eq(1)
      expect(bf_trigger.first).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "creates multiple triggers with the splat syntax" do
      subject.before(splat, hash_block)
      bf_trigger = subject.instance_variable_get(:@_before_triggers)
      expect(bf_trigger.size).to eq(3)
      bf_trigger.map { |t| expect(t).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger) }
    end

    it "creates multiple triggers with the block syntax" do
      subject.before splat do |trigger|
        trigger.name = "rspec"
      end
      bf_trigger = subject.instance_variable_get(:@_before_triggers)
      expect(bf_trigger.size).to eq(3)
      bf_trigger.map { |t| expect(t).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger) }
    end

    it "creates multiple triggers with the array syntax" do
      subject.before(arr, hash_block)
      bf_trigger = subject.instance_variable_get(:@_before_triggers)
      expect(bf_trigger.size).to eq(3)
      bf_trigger.map { |t| expect(t).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger) }
    end
  end

  describe "creating an after trigger" do
    it "creates a trigger with the splat syntax" do
      subject.after(:up, hash_block)
      af_trigger = subject.instance_variable_get(:@_after_triggers)
      expect(af_trigger.size).to eq(1)
      expect(af_trigger.first).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "creates a trigger with the array syntax" do
      subject.after([:up], hash_block)
      af_trigger = subject.instance_variable_get(:@_after_triggers)
      expect(af_trigger.size).to eq(1)
      expect(af_trigger.first).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "creates a trigger with the block syntax" do
      subject.after :up do |trigger|
        trigger.name = "rspec"
      end
      af_trigger = subject.instance_variable_get(:@_after_triggers)
      expect(af_trigger.size).to eq(1)
      expect(af_trigger.first).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "creates multiple triggers with the splat syntax" do
      subject.after(splat, hash_block)
      af_trigger = subject.instance_variable_get(:@_after_triggers)
      expect(af_trigger.size).to eq(3)
      af_trigger.map { |t| expect(t).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger) }
    end

    it "creates multiple triggers with the block syntax" do
      subject.after splat do |trigger|
        trigger.name = "rspec"
      end
      af_trigger = subject.instance_variable_get(:@_after_triggers)
      expect(af_trigger.size).to eq(3)
      af_trigger.map { |t| expect(t).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger) }
    end

    it "creates multiple triggers with the array syntax" do
      subject.after(arr, hash_block)
      af_trigger = subject.instance_variable_get(:@_after_triggers)
      expect(af_trigger.size).to eq(3)
      af_trigger.map { |t| expect(t).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger) }
    end
  end

  describe "#create_trigger" do
    let(:command) { :up }
    let(:hash_block) { {info: "hi", run: {inline: "echo 'hi'"}} }

    it "returns a new VagrantConfigTrigger object if given a hash" do
      trigger = subject.create_trigger(command, hash_block)
      expect(trigger).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end

    it "returns a new VagrantConfigTrigger object if given a block" do
      block = Proc.new { |b| b.info = "test"}

      trigger = subject.create_trigger(command, block)
      expect(trigger).to be_a(VagrantPlugins::Kernel_V2::VagrantConfigTrigger)
    end
  end

  describe "#merge" do
    it "merges defined triggers" do
      a = described_class.new()
      b = described_class.new()

      a.before(splat, hash_block)
      a.after(arr, hash_block)
      b.before(splat, hash_block)
      b.after(arr, hash_block)

      result = a.merge(b)
      bf_trigger = result.instance_variable_get(:@_before_triggers)
      af_trigger = result.instance_variable_get(:@_after_triggers)

      expect(bf_trigger).to be_a(Array)
      expect(af_trigger).to be_a(Array)
      expect(bf_trigger.size).to eq(6)
      expect(af_trigger.size).to eq(6)
    end

    it "merges the other triggers if a class is empty" do
      a = described_class.new()
      b = described_class.new()

      a.before(splat, hash_block)
      a.after(arr, hash_block)

      b_bf_trigger = b.instance_variable_get(:@_before_triggers)
      b_af_trigger = b.instance_variable_get(:@_after_triggers)

      result = a.merge(b)
      bf_trigger = result.instance_variable_get(:@_before_triggers)
      af_trigger = result.instance_variable_get(:@_after_triggers)

      expect(bf_trigger.size).to eq(3)
      expect(af_trigger.size).to eq(3)
    end
  end
end