Skip to content

Commit

Permalink
Test container provisioner plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
soapy1 committed Mar 30, 2020
1 parent 4a204fb commit e13b02f
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 133 deletions.
141 changes: 141 additions & 0 deletions test/unit/plugins/provisioners/container/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/provisioners/container/config")
require Vagrant.source_root.join("plugins/kernel_v2/config/vm")

describe VagrantPlugins::ContainerProvisioner::Config do
subject { described_class.new }

describe "#build_image" do
it "stores them" do
subject.build_image("foo")
subject.build_image("bar", foo: :bar)
subject.finalize!
expect(subject.build_images.length).to eql(2)
expect(subject.build_images[0]).to eql(["foo", {}])
expect(subject.build_images[1]).to eql(["bar", { foo: :bar }])
end
end

describe "#images" do
it "stores them in a set" do
subject.images = ["1", "1", "2"]
subject.finalize!
expect(subject.images.to_a.sort).to eql(["1", "2"])
end

it "overrides previously set images" do
subject.images = ["3"]
subject.images = ["1", "1", "2"]
subject.finalize!
expect(subject.images.to_a.sort).to eql(["1", "2"])
end
end

describe "#merge" do
it "has all images to pull" do
subject.pull_images("1")

other = described_class.new
other.pull_images("2", "3")

result = subject.merge(other)
expect(result.images.to_a.sort).to eq(
["1", "2", "3"])
end

it "has all the containers to run" do
subject.run("foo", image: "bar", daemonize: false)
subject.run("bar")

other = described_class.new
other.run("foo", image: "foo")

result = subject.merge(other)
result.finalize!

cs = result.containers
expect(cs.length).to eq(2)
expect(cs["foo"]).to eq({
auto_assign_name: true,
image: "foo",
daemonize: false,
restart: "always",
})
expect(cs["bar"]).to eq({
auto_assign_name: true,
image: "bar",
daemonize: true,
restart: "always",
})
end

it "has all the containers to build" do
subject.build_image("foo")

other = described_class.new
other.build_image("bar")

result = subject.merge(other)
result.finalize!

images = result.build_images
expect(images.length).to eq(2)
expect(images[0]).to eq(["foo", {}])
expect(images[1]).to eq(["bar", {}])
end
end

describe "#pull_images" do
it "adds images to the list of images to build" do
subject.pull_images("1")
subject.pull_images("2", "3")
subject.finalize!
expect(subject.images.to_a.sort).to eql(["1", "2", "3"])
end
end

describe "#run" do
it "runs the given image" do
subject.run("foo")

subject.finalize!
expect(subject.containers).to eql({
"foo" => {
auto_assign_name: true,
daemonize: true,
image: "foo",
restart: "always",
}
})
end

it "can not auto assign name" do
subject.run("foo", auto_assign_name: false)

subject.finalize!
expect(subject.containers).to eql({
"foo" => {
auto_assign_name: false,
daemonize: true,
image: "foo",
restart: "always",
}
})
end

it "can not daemonize" do
subject.run("foo", daemonize: false)

subject.finalize!
expect(subject.containers).to eql({
"foo" => {
auto_assign_name: true,
daemonize: false,
image: "foo",
restart: "always",
}
})
end
end
end
133 changes: 0 additions & 133 deletions test/unit/plugins/provisioners/docker/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,139 +7,6 @@
describe VagrantPlugins::DockerProvisioner::Config do
subject { described_class.new }

describe "#build_image" do
it "stores them" do
subject.build_image("foo")
subject.build_image("bar", foo: :bar)
subject.finalize!
expect(subject.build_images.length).to eql(2)
expect(subject.build_images[0]).to eql(["foo", {}])
expect(subject.build_images[1]).to eql(["bar", { foo: :bar }])
end
end

describe "#images" do
it "stores them in a set" do
subject.images = ["1", "1", "2"]
subject.finalize!
expect(subject.images.to_a.sort).to eql(["1", "2"])
end

it "overrides previously set images" do
subject.images = ["3"]
subject.images = ["1", "1", "2"]
subject.finalize!
expect(subject.images.to_a.sort).to eql(["1", "2"])
end
end

describe "#merge" do
it "has all images to pull" do
subject.pull_images("1")

other = described_class.new
other.pull_images("2", "3")

result = subject.merge(other)
expect(result.images.to_a.sort).to eq(
["1", "2", "3"])
end

it "has all the containers to run" do
subject.run("foo", image: "bar", daemonize: false)
subject.run("bar")

other = described_class.new
other.run("foo", image: "foo")

result = subject.merge(other)
result.finalize!

cs = result.containers
expect(cs.length).to eq(2)
expect(cs["foo"]).to eq({
auto_assign_name: true,
image: "foo",
daemonize: false,
restart: "always",
})
expect(cs["bar"]).to eq({
auto_assign_name: true,
image: "bar",
daemonize: true,
restart: "always",
})
end

it "has all the containers to build" do
subject.build_image("foo")

other = described_class.new
other.build_image("bar")

result = subject.merge(other)
result.finalize!

images = result.build_images
expect(images.length).to eq(2)
expect(images[0]).to eq(["foo", {}])
expect(images[1]).to eq(["bar", {}])
end
end

describe "#pull_images" do
it "adds images to the list of images to build" do
subject.pull_images("1")
subject.pull_images("2", "3")
subject.finalize!
expect(subject.images.to_a.sort).to eql(["1", "2", "3"])
end
end

describe "#run" do
it "runs the given image" do
subject.run("foo")

subject.finalize!
expect(subject.containers).to eql({
"foo" => {
auto_assign_name: true,
daemonize: true,
image: "foo",
restart: "always",
}
})
end

it "can not auto assign name" do
subject.run("foo", auto_assign_name: false)

subject.finalize!
expect(subject.containers).to eql({
"foo" => {
auto_assign_name: false,
daemonize: true,
image: "foo",
restart: "always",
}
})
end

it "can not daemonize" do
subject.run("foo", daemonize: false)

subject.finalize!
expect(subject.containers).to eql({
"foo" => {
auto_assign_name: true,
daemonize: false,
image: "foo",
restart: "always",
}
})
end
end

describe "#post_install_provision" do
it "raises an error if 'docker' provisioner was provided" do
expect {subject.post_install_provision("myprov", :type=>"docker", :inline=>"echo 'hello'")}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/plugins/provisioners/podman/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/provisioners/podman/config")
require Vagrant.source_root.join("plugins/provisioners/podman/provisioner")
require Vagrant.source_root.join("plugins/kernel_v2/config/vm")

describe VagrantPlugins::PodmanProvisioner::Config do
subject { described_class.new }

describe "#post_install_provision" do
it "raises an error if 'podman' provisioner was provided" do
expect {subject.post_install_provision("myprov", :type=>"podman", :inline=>"echo 'hello'")}
.to raise_error(VagrantPlugins::PodmanProvisioner::PodmanError)
end

it "setups a basic provisioner" do
prov = double()
mock_provisioner = "mock"
mock_provisioners = [mock_provisioner]

allow(VagrantPlugins::Kernel_V2::VMConfig).to receive(:new).
and_return(prov)
allow(prov).to receive(:provision).and_return(mock_provisioners)
allow(prov).to receive(:provisioners).and_return(mock_provisioners)

subject.post_install_provision("myprov", :inline=>"echo 'hello'")
expect(subject.post_install_provisioner).to eq(mock_provisioner)
end
end
end
70 changes: 70 additions & 0 deletions test/unit/plugins/provisioners/podman/provisioner_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require File.expand_path("../../../../base", __FILE__)

require Vagrant.source_root.join("plugins/provisioners/podman/provisioner")

describe VagrantPlugins::PodmanProvisioner::Provisioner do
include_context "unit"
subject { described_class.new(machine, config, installer, client) }

let(:iso_env) do
# We have to create a Vagrantfile so there is a root path
env = isolated_environment
env.vagrantfile("")
env.create_vagrant_env
end

let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) }
let(:config) { double("config") }
let(:communicator) { double("comm") }
let(:guest) { double("guest") }
let(:client) { double("client") }
let(:installer) { double("installer") }
let(:hook) { double("hook") }

before do
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:guest).and_return(guest)

allow(communicator).to receive(:execute).and_return(true)
allow(communicator).to receive(:upload).and_return(true)

allow(guest).to receive(:capability?).and_return(false)
allow(guest).to receive(:capability).and_return(false)

allow(client).to receive(:start_service).and_return(true)
allow(client).to receive(:daemon_running?).and_return(true)

allow(config).to receive(:images).and_return(Set.new)
allow(config).to receive(:build_images).and_return(Set.new)
allow(config).to receive(:containers).and_return(Hash.new)
end

describe "#provision" do
let(:provisioner) do
prov = VagrantPlugins::Kernel_V2::VagrantConfigProvisioner.new("spec-test", :shell)
prov.config = {}
prov
end

it "invokes a post_install_provisioner if defined and podman is installed" do
allow(installer).to receive(:ensure_installed).and_return(true)
allow(config).to receive(:post_install_provisioner).and_return(provisioner)
allow(machine).to receive(:env).and_return(iso_env)
allow(machine.env).to receive(:hook).and_return(true)

expect(machine.env).to receive(:hook).with(:run_provisioner, anything)
subject.provision()
end

it "does not invoke post_install_provisioner if not defined" do
allow(installer).to receive(:ensure_installed).and_return(true)
allow(config).to receive(:post_install_provisioner).and_return(nil)
allow(machine).to receive(:env).and_return(iso_env)
allow(machine.env).to receive(:hook).and_return(true)

expect(machine.env).not_to receive(:hook).with(:run_provisioner, anything)
subject.provision()
end
end

end

0 comments on commit e13b02f

Please sign in to comment.