providers/docker: config validation and specs

This commit is contained in:
Mitchell Hashimoto 2014-04-18 19:52:02 -07:00
parent 0094a58227
commit 79793ab130
3 changed files with 104 additions and 4 deletions

View File

@ -1,3 +1,5 @@
require "pathname"
module VagrantPlugins
module DockerProvider
class Config < Vagrant.plugin("2", :config)
@ -110,6 +112,21 @@ module VagrantPlugins
def validate(machine)
errors = _detected_errors
if @build_dir && @image
errors << I18n.t("docker_provider.errors.config.both_build_and_image")
end
if !@build_dir && !@image
errors << I18n.t("docker_provider.errors.config.build_dir_or_image")
end
if @build_dir
build_dir_pn = Pathname.new(@build_dir)
if !build_dir_pn.directory? || !build_dir_pn.join("Dockerfile").file?
errors << I18n.t("docker_provider.errors.config.build_dir_invalid")
end
end
@links.each do |link|
parts = link.split(":")
if parts.length != 2 || parts[0] == "" || parts[1] == ""
@ -118,8 +135,12 @@ module VagrantPlugins
end
end
# TODO: Detect if base image has a CMD / ENTRYPOINT set before erroring out
errors << I18n.t("docker_provider.errors.config.cmd_not_set") if @cmd == UNSET_VALUE
if @vagrant_vagrantfile
vf_pn = Pathname.new(@vagrant_vagrantfile)
if !vf_pn.file?
errors << I18n.t("docker_provider.errors.config.invalid_vagrantfile")
end
end
{ "docker provider" => errors }
end

View File

@ -92,8 +92,16 @@ en:
and notify them to not use this communicator for anything except the
"docker" provider.
config:
both_build_and_image: |-
Only one of "build_dir" or "image" can be set
build_dir_invalid: |-
"build_dir" must exist and contain a Dockerfile
build_dir_or_image: |-
One of "build_dir" or "image" must be set
invalid_link: |-
Invalid link (should be 'name:alias'): "%{link}"
invalid_vagrantfile: |-
"vagrant_vagrantfile" must point to a Vagrantfile that exists.
docker_provider_nfs_without_privileged: |-
You've configured a NFS synced folder but didn't enable privileged
mode for the container. Please set the `privileged` option to true

View File

@ -5,8 +5,18 @@ require "vagrant/util/platform"
require Vagrant.source_root.join("plugins/providers/docker/config")
describe VagrantPlugins::DockerProvider::Config do
include_context "unit"
let(:machine) { double("machine") }
let(:build_dir) do
temporary_dir.tap do |dir|
dir.join("Dockerfile").open("w") do |f|
f.write("Hello")
end
end
end
def assert_invalid
errors = subject.validate(machine)
if !errors.values.any? { |v| !v.empty? }
@ -21,6 +31,10 @@ describe VagrantPlugins::DockerProvider::Config do
end
end
def valid_defaults
subject.image = "foo"
end
describe "defaults" do
before { subject.finalize! }
@ -39,12 +53,53 @@ describe VagrantPlugins::DockerProvider::Config do
Vagrant::Util::Platform.stub(linux: true)
end
it "should be valid by default" do
it "should be invalid if both build dir and image are set" do
subject.build_dir = build_dir
subject.image = "foo"
subject.finalize!
assert_invalid
end
describe "#build_dir" do
it "should be valid if not set with image" do
subject.build_dir = nil
subject.image = "foo"
subject.finalize!
assert_valid
end
it "should be valid with a valid directory" do
subject.build_dir = build_dir
subject.finalize!
assert_valid
end
it "should be invalid with a directory that doesn't have a Dockerfile" do
subject.build_dir = temporary_dir.to_s
subject.finalize!
assert_invalid
end
end
describe "#image" do
it "should be valid if set" do
subject.image = "foo"
subject.finalize!
assert_valid
end
it "should be invalid if not set" do
subject.image = nil
subject.finalize!
assert_invalid
end
end
describe "#link" do
before do
valid_defaults
end
it "should be valid with good links" do
subject.link "foo:bar"
subject.link "db:blah"
@ -93,4 +148,20 @@ describe VagrantPlugins::DockerProvider::Config do
end
end
end
describe "#vagrant_vagrantfile" do
before { valid_defaults }
it "should be valid if set to a file" do
subject.vagrant_vagrantfile = temporary_file.to_s
subject.finalize!
assert_valid
end
it "should not be valid if set to a non-existent place" do
subject.vagrant_vagrantfile = "/i/shouldnt/exist"
subject.finalize!
assert_invalid
end
end
end