Skip to content

Commit e13b02f

Browse files
committed
Test container provisioner plugin
1 parent 4a204fb commit e13b02f

File tree

4 files changed

+241
-133
lines changed

4 files changed

+241
-133
lines changed
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
require File.expand_path("../../../../base", __FILE__)
2+
3+
require Vagrant.source_root.join("plugins/provisioners/container/config")
4+
require Vagrant.source_root.join("plugins/kernel_v2/config/vm")
5+
6+
describe VagrantPlugins::ContainerProvisioner::Config do
7+
subject { described_class.new }
8+
9+
describe "#build_image" do
10+
it "stores them" do
11+
subject.build_image("foo")
12+
subject.build_image("bar", foo: :bar)
13+
subject.finalize!
14+
expect(subject.build_images.length).to eql(2)
15+
expect(subject.build_images[0]).to eql(["foo", {}])
16+
expect(subject.build_images[1]).to eql(["bar", { foo: :bar }])
17+
end
18+
end
19+
20+
describe "#images" do
21+
it "stores them in a set" do
22+
subject.images = ["1", "1", "2"]
23+
subject.finalize!
24+
expect(subject.images.to_a.sort).to eql(["1", "2"])
25+
end
26+
27+
it "overrides previously set images" do
28+
subject.images = ["3"]
29+
subject.images = ["1", "1", "2"]
30+
subject.finalize!
31+
expect(subject.images.to_a.sort).to eql(["1", "2"])
32+
end
33+
end
34+
35+
describe "#merge" do
36+
it "has all images to pull" do
37+
subject.pull_images("1")
38+
39+
other = described_class.new
40+
other.pull_images("2", "3")
41+
42+
result = subject.merge(other)
43+
expect(result.images.to_a.sort).to eq(
44+
["1", "2", "3"])
45+
end
46+
47+
it "has all the containers to run" do
48+
subject.run("foo", image: "bar", daemonize: false)
49+
subject.run("bar")
50+
51+
other = described_class.new
52+
other.run("foo", image: "foo")
53+
54+
result = subject.merge(other)
55+
result.finalize!
56+
57+
cs = result.containers
58+
expect(cs.length).to eq(2)
59+
expect(cs["foo"]).to eq({
60+
auto_assign_name: true,
61+
image: "foo",
62+
daemonize: false,
63+
restart: "always",
64+
})
65+
expect(cs["bar"]).to eq({
66+
auto_assign_name: true,
67+
image: "bar",
68+
daemonize: true,
69+
restart: "always",
70+
})
71+
end
72+
73+
it "has all the containers to build" do
74+
subject.build_image("foo")
75+
76+
other = described_class.new
77+
other.build_image("bar")
78+
79+
result = subject.merge(other)
80+
result.finalize!
81+
82+
images = result.build_images
83+
expect(images.length).to eq(2)
84+
expect(images[0]).to eq(["foo", {}])
85+
expect(images[1]).to eq(["bar", {}])
86+
end
87+
end
88+
89+
describe "#pull_images" do
90+
it "adds images to the list of images to build" do
91+
subject.pull_images("1")
92+
subject.pull_images("2", "3")
93+
subject.finalize!
94+
expect(subject.images.to_a.sort).to eql(["1", "2", "3"])
95+
end
96+
end
97+
98+
describe "#run" do
99+
it "runs the given image" do
100+
subject.run("foo")
101+
102+
subject.finalize!
103+
expect(subject.containers).to eql({
104+
"foo" => {
105+
auto_assign_name: true,
106+
daemonize: true,
107+
image: "foo",
108+
restart: "always",
109+
}
110+
})
111+
end
112+
113+
it "can not auto assign name" do
114+
subject.run("foo", auto_assign_name: false)
115+
116+
subject.finalize!
117+
expect(subject.containers).to eql({
118+
"foo" => {
119+
auto_assign_name: false,
120+
daemonize: true,
121+
image: "foo",
122+
restart: "always",
123+
}
124+
})
125+
end
126+
127+
it "can not daemonize" do
128+
subject.run("foo", daemonize: false)
129+
130+
subject.finalize!
131+
expect(subject.containers).to eql({
132+
"foo" => {
133+
auto_assign_name: true,
134+
daemonize: false,
135+
image: "foo",
136+
restart: "always",
137+
}
138+
})
139+
end
140+
end
141+
end

Diff for: test/unit/plugins/provisioners/docker/config_test.rb

-133
Original file line numberDiff line numberDiff line change
@@ -7,139 +7,6 @@
77
describe VagrantPlugins::DockerProvisioner::Config do
88
subject { described_class.new }
99

10-
describe "#build_image" do
11-
it "stores them" do
12-
subject.build_image("foo")
13-
subject.build_image("bar", foo: :bar)
14-
subject.finalize!
15-
expect(subject.build_images.length).to eql(2)
16-
expect(subject.build_images[0]).to eql(["foo", {}])
17-
expect(subject.build_images[1]).to eql(["bar", { foo: :bar }])
18-
end
19-
end
20-
21-
describe "#images" do
22-
it "stores them in a set" do
23-
subject.images = ["1", "1", "2"]
24-
subject.finalize!
25-
expect(subject.images.to_a.sort).to eql(["1", "2"])
26-
end
27-
28-
it "overrides previously set images" do
29-
subject.images = ["3"]
30-
subject.images = ["1", "1", "2"]
31-
subject.finalize!
32-
expect(subject.images.to_a.sort).to eql(["1", "2"])
33-
end
34-
end
35-
36-
describe "#merge" do
37-
it "has all images to pull" do
38-
subject.pull_images("1")
39-
40-
other = described_class.new
41-
other.pull_images("2", "3")
42-
43-
result = subject.merge(other)
44-
expect(result.images.to_a.sort).to eq(
45-
["1", "2", "3"])
46-
end
47-
48-
it "has all the containers to run" do
49-
subject.run("foo", image: "bar", daemonize: false)
50-
subject.run("bar")
51-
52-
other = described_class.new
53-
other.run("foo", image: "foo")
54-
55-
result = subject.merge(other)
56-
result.finalize!
57-
58-
cs = result.containers
59-
expect(cs.length).to eq(2)
60-
expect(cs["foo"]).to eq({
61-
auto_assign_name: true,
62-
image: "foo",
63-
daemonize: false,
64-
restart: "always",
65-
})
66-
expect(cs["bar"]).to eq({
67-
auto_assign_name: true,
68-
image: "bar",
69-
daemonize: true,
70-
restart: "always",
71-
})
72-
end
73-
74-
it "has all the containers to build" do
75-
subject.build_image("foo")
76-
77-
other = described_class.new
78-
other.build_image("bar")
79-
80-
result = subject.merge(other)
81-
result.finalize!
82-
83-
images = result.build_images
84-
expect(images.length).to eq(2)
85-
expect(images[0]).to eq(["foo", {}])
86-
expect(images[1]).to eq(["bar", {}])
87-
end
88-
end
89-
90-
describe "#pull_images" do
91-
it "adds images to the list of images to build" do
92-
subject.pull_images("1")
93-
subject.pull_images("2", "3")
94-
subject.finalize!
95-
expect(subject.images.to_a.sort).to eql(["1", "2", "3"])
96-
end
97-
end
98-
99-
describe "#run" do
100-
it "runs the given image" do
101-
subject.run("foo")
102-
103-
subject.finalize!
104-
expect(subject.containers).to eql({
105-
"foo" => {
106-
auto_assign_name: true,
107-
daemonize: true,
108-
image: "foo",
109-
restart: "always",
110-
}
111-
})
112-
end
113-
114-
it "can not auto assign name" do
115-
subject.run("foo", auto_assign_name: false)
116-
117-
subject.finalize!
118-
expect(subject.containers).to eql({
119-
"foo" => {
120-
auto_assign_name: false,
121-
daemonize: true,
122-
image: "foo",
123-
restart: "always",
124-
}
125-
})
126-
end
127-
128-
it "can not daemonize" do
129-
subject.run("foo", daemonize: false)
130-
131-
subject.finalize!
132-
expect(subject.containers).to eql({
133-
"foo" => {
134-
auto_assign_name: true,
135-
daemonize: false,
136-
image: "foo",
137-
restart: "always",
138-
}
139-
})
140-
end
141-
end
142-
14310
describe "#post_install_provision" do
14411
it "raises an error if 'docker' provisioner was provided" do
14512
expect {subject.post_install_provision("myprov", :type=>"docker", :inline=>"echo 'hello'")}

Diff for: test/unit/plugins/provisioners/podman/config_test.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require File.expand_path("../../../../base", __FILE__)
2+
3+
require Vagrant.source_root.join("plugins/provisioners/podman/config")
4+
require Vagrant.source_root.join("plugins/provisioners/podman/provisioner")
5+
require Vagrant.source_root.join("plugins/kernel_v2/config/vm")
6+
7+
describe VagrantPlugins::PodmanProvisioner::Config do
8+
subject { described_class.new }
9+
10+
describe "#post_install_provision" do
11+
it "raises an error if 'podman' provisioner was provided" do
12+
expect {subject.post_install_provision("myprov", :type=>"podman", :inline=>"echo 'hello'")}
13+
.to raise_error(VagrantPlugins::PodmanProvisioner::PodmanError)
14+
end
15+
16+
it "setups a basic provisioner" do
17+
prov = double()
18+
mock_provisioner = "mock"
19+
mock_provisioners = [mock_provisioner]
20+
21+
allow(VagrantPlugins::Kernel_V2::VMConfig).to receive(:new).
22+
and_return(prov)
23+
allow(prov).to receive(:provision).and_return(mock_provisioners)
24+
allow(prov).to receive(:provisioners).and_return(mock_provisioners)
25+
26+
subject.post_install_provision("myprov", :inline=>"echo 'hello'")
27+
expect(subject.post_install_provisioner).to eq(mock_provisioner)
28+
end
29+
end
30+
end
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require File.expand_path("../../../../base", __FILE__)
2+
3+
require Vagrant.source_root.join("plugins/provisioners/podman/provisioner")
4+
5+
describe VagrantPlugins::PodmanProvisioner::Provisioner do
6+
include_context "unit"
7+
subject { described_class.new(machine, config, installer, client) }
8+
9+
let(:iso_env) do
10+
# We have to create a Vagrantfile so there is a root path
11+
env = isolated_environment
12+
env.vagrantfile("")
13+
env.create_vagrant_env
14+
end
15+
16+
let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
17+
let(:config) { double("config") }
18+
let(:communicator) { double("comm") }
19+
let(:guest) { double("guest") }
20+
let(:client) { double("client") }
21+
let(:installer) { double("installer") }
22+
let(:hook) { double("hook") }
23+
24+
before do
25+
allow(machine).to receive(:communicate).and_return(communicator)
26+
allow(machine).to receive(:guest).and_return(guest)
27+
28+
allow(communicator).to receive(:execute).and_return(true)
29+
allow(communicator).to receive(:upload).and_return(true)
30+
31+
allow(guest).to receive(:capability?).and_return(false)
32+
allow(guest).to receive(:capability).and_return(false)
33+
34+
allow(client).to receive(:start_service).and_return(true)
35+
allow(client).to receive(:daemon_running?).and_return(true)
36+
37+
allow(config).to receive(:images).and_return(Set.new)
38+
allow(config).to receive(:build_images).and_return(Set.new)
39+
allow(config).to receive(:containers).and_return(Hash.new)
40+
end
41+
42+
describe "#provision" do
43+
let(:provisioner) do
44+
prov = VagrantPlugins::Kernel_V2::VagrantConfigProvisioner.new("spec-test", :shell)
45+
prov.config = {}
46+
prov
47+
end
48+
49+
it "invokes a post_install_provisioner if defined and podman is installed" do
50+
allow(installer).to receive(:ensure_installed).and_return(true)
51+
allow(config).to receive(:post_install_provisioner).and_return(provisioner)
52+
allow(machine).to receive(:env).and_return(iso_env)
53+
allow(machine.env).to receive(:hook).and_return(true)
54+
55+
expect(machine.env).to receive(:hook).with(:run_provisioner, anything)
56+
subject.provision()
57+
end
58+
59+
it "does not invoke post_install_provisioner if not defined" do
60+
allow(installer).to receive(:ensure_installed).and_return(true)
61+
allow(config).to receive(:post_install_provisioner).and_return(nil)
62+
allow(machine).to receive(:env).and_return(iso_env)
63+
allow(machine.env).to receive(:hook).and_return(true)
64+
65+
expect(machine.env).not_to receive(:hook).with(:run_provisioner, anything)
66+
subject.provision()
67+
end
68+
end
69+
70+
end

0 commit comments

Comments
 (0)