vagrant-dsc 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/base.rb CHANGED
@@ -1,57 +1,54 @@
1
- require "tmpdir"
2
- require "rubygems"
3
-
4
- # Gems
5
- require "checkpoint"
6
- require "rspec/autorun"
7
-
8
- # Require Vagrant itself so we can reference the proper
9
- # classes to test.
10
- require "vagrant"
11
- require "vagrant/util/platform"
12
-
13
- # Add the test directory to the load path
14
- $:.unshift File.expand_path("../../", __FILE__)
15
-
16
- # Load in helpers
17
- require "vagrant/unit/support/dummy_communicator"
18
- require "vagrant/unit/support/dummy_provider"
19
- require "vagrant/unit/support/shared/base_context"
20
- require "vagrant/unit/support/shared/action_synced_folders_context"
21
- require "vagrant/unit/support/shared/capability_helpers_context"
22
- require "vagrant/unit/support/shared/plugin_command_context"
23
- require "vagrant/unit/support/shared/virtualbox_context"
24
-
25
- # Do not buffer output
26
- $stdout.sync = true
27
- $stderr.sync = true
28
-
29
- # Configure RSpec
30
- RSpec.configure do |c|
31
- c.expect_with :rspec, :stdlib
32
- c.treat_symbols_as_metadata_keys_with_true_values = true
33
-
34
- if Vagrant::Util::Platform.windows?
35
- c.filter_run_excluding :skip_windows
36
- else
37
- c.filter_run_excluding :windows
1
+ shared_context "unit" do
2
+ before(:each) do
3
+ # State to store the list of registered plugins that we have to
4
+ # unregister later.
5
+ @_plugins = []
6
+
7
+ # Create a thing to store our temporary files so that they aren't
8
+ # unlinked right away.
9
+ @_temp_files = []
38
10
  end
39
- end
40
11
 
41
- # Configure VAGRANT_CWD so that the tests never find an actual
42
- # Vagrantfile anywhere, or at least this minimizes those chances.
43
- ENV["VAGRANT_CWD"] = Dir.mktmpdir("vagrant")
12
+ # This helper creates a temporary file and returns a Pathname
13
+ # object pointed to it.
14
+ #
15
+ # @return [Pathname]
16
+ def temporary_file(contents=nil)
17
+ f = Tempfile.new("vagrant-unit")
44
18
 
45
- # Set the dummy provider to the default for tests
46
- ENV["VAGRANT_DEFAULT_PROVIDER"] = "dummy"
19
+ if contents
20
+ f.write(contents)
21
+ f.flush
22
+ end
47
23
 
48
- # Unset all host plugins so that we aren't executing subprocess things
49
- # to detect a host for every test.
50
- Vagrant.plugin("2").manager.registered.dup.each do |plugin|
51
- if plugin.components.hosts.to_hash.length > 0
52
- Vagrant.plugin("2").manager.unregister(plugin)
24
+ # Store the tempfile in an instance variable so that it is not
25
+ # garbage collected, so that the tempfile is not unlinked.
26
+ @_temp_files << f
27
+
28
+ return Pathname.new(f.path)
29
+ end
30
+
31
+ # Asserts that the current (config) validation run should fail.
32
+ # Any error message is sufficient.
33
+ def assert_invalid
34
+ errors = subject.validate(machine)
35
+ if !errors.values.any? { |v| !v.empty? }
36
+ raise "No errors: #{errors.inspect}"
37
+ end
38
+ end
39
+
40
+ # Asserts that the current (config) validation should fail with a specific message.
41
+ def assert_error(error)
42
+ errors = subject.validate(machine)
43
+ raise "Error #{error} was not raised" if !errors["dsc provisioner"].include? error
44
+ end
45
+
46
+ # Asserts that no failures should occur in the current (config) validation run.
47
+ def assert_valid
48
+ errors = subject.validate(machine)
49
+ if !errors.values.all? { |v| v.empty? }
50
+ raise "Errors: #{errors.inspect}"
51
+ end
53
52
  end
54
- end
55
53
 
56
- # Disable checkpoint
57
- Checkpoint.disable!
54
+ end
@@ -1,289 +1,147 @@
1
1
  require 'spec_helper'
2
2
  require 'vagrant-dsc/provisioner'
3
3
  require 'vagrant-dsc/config'
4
- require 'rspec/its'
4
+ require 'base'
5
5
 
6
6
  describe VagrantPlugins::DSC::Config do
7
- # include_context "unit"
7
+ include_context "unit"
8
8
  let(:instance) { described_class.new }
9
-
10
- before { subject.finalize! }
11
-
12
- def assert_invalid
13
- errors = subject.validate(machine)
14
- if !errors.values.any? { |v| !v.empty? }
15
- raise "No errors: #{errors.inspect}"
16
- end
17
- end
18
-
19
- def assert_valid
20
- errors = subject.validate(machine)
21
- if !errors.values.all? { |v| v.empty? }
22
- raise "Errors: #{errors.inspect}"
23
- end
24
- end
9
+ let(:machine) { double("machine") }
25
10
 
26
11
  def valid_defaults
27
- subject.image = "foo"
12
+ # subject.prop = value
28
13
  end
29
14
 
30
-
31
15
  describe "defaults" do
32
16
 
17
+ before do
18
+ env = double("environment", root_path: "/tmp/vagrant-dsc-path")
19
+ config = double("config")
20
+ machine.stub(config: config, env: env)
21
+
22
+ allow(machine).to receive(:root_path).and_return("/c/foo")
23
+ end
24
+
33
25
  # before do
34
26
  # # By default lets be Linux for validations
35
27
  # Vagrant::Util::Platform.stub(linux: true)
36
28
  # end
37
29
 
38
- subject do
39
- instance.tap do |o|
40
- o.finalize!
41
- end
42
- end
43
-
44
- its("manifest_file") { expect = "default.ps1" }
45
- its("manifests_path") { expect = "." }
46
- its("configuration_name") { expect = "default" }
47
- its("mof_file") { expect be_nil }
48
- its("module_path") { expect be_nil }
49
- its("options") { expect = [] }
50
- its("facter") { expect = {} }
51
- its("synced_folder_type") { expect be_nil }
52
- its("temp_dir") { expect match /^\/tmp\/vagrant-dsc-*/ }
53
- its("working_directory") { expect be_nil }
30
+ before { subject.finalize! }
31
+
32
+ its("configuration_file") { expect = "default.ps1" }
33
+ its("manifests_path") { expect = "." }
34
+ its("configuration_name") { expect = "default" }
35
+ its("mof_path") { expect be_nil }
36
+ its("module_path") { expect be_nil }
37
+ its("options") { expect = [] }
38
+ its("configuration_params") { expect = {} }
39
+ its("synced_folder_type") { expect be_nil }
40
+ its("temp_dir") { expect match /^\/tmp\/vagrant-dsc-*/ }
41
+ its("working_directory") { expect be_nil }
54
42
  end
55
- end
56
-
57
-
58
- # require_relative "../base"
59
-
60
- # require "vagrant/util/platform"
61
-
62
- # require Vagrant.source_root.join("plugins/providers/docker/config")
63
-
64
- # describe VagrantPlugins::DockerProvider::Config do
65
- # include_context "unit"
66
-
67
- # let(:machine) { double("machine") }
68
-
69
- # let(:build_dir) do
70
- # temporary_dir.tap do |dir|
71
- # dir.join("Dockerfile").open("w") do |f|
72
- # f.write("Hello")
73
- # end
74
- # end
75
- # end
76
-
77
- # def assert_invalid
78
- # errors = subject.validate(machine)
79
- # if !errors.values.any? { |v| !v.empty? }
80
- # raise "No errors: #{errors.inspect}"
81
- # end
82
- # end
83
-
84
- # def assert_valid
85
- # errors = subject.validate(machine)
86
- # if !errors.values.all? { |v| v.empty? }
87
- # raise "Errors: #{errors.inspect}"
88
- # end
89
- # end
90
-
91
- # def valid_defaults
92
- # subject.image = "foo"
93
- # end
94
-
95
- # describe "defaults" do
96
- # before { subject.finalize! }
97
-
98
- # its(:build_dir) { should be_nil }
99
- # its(:expose) { should eq([]) }
100
- # its(:cmd) { should eq([]) }
101
- # its(:env) { should eq({}) }
102
- # its(:force_host_vm) { should be_false }
103
- # its(:host_vm_build_dir_options) { should be_nil }
104
- # its(:image) { should be_nil }
105
- # its(:name) { should be_nil }
106
- # its(:privileged) { should be_false }
107
- # its(:vagrant_machine) { should be_nil }
108
- # its(:vagrant_vagrantfile) { should be_nil }
109
- # end
110
-
111
- # before do
112
- # # By default lets be Linux for validations
113
- # Vagrant::Util::Platform.stub(linux: true)
114
- # end
115
-
116
- # it "should be invalid if both build dir and image are set" do
117
- # subject.build_dir = build_dir
118
- # subject.image = "foo"
119
- # subject.finalize!
120
- # assert_invalid
121
- # end
122
-
123
- # describe "#build_dir" do
124
- # it "should be valid if not set with image" do
125
- # subject.build_dir = nil
126
- # subject.image = "foo"
127
- # subject.finalize!
128
- # assert_valid
129
- # end
130
43
 
131
- # it "should be valid with a valid directory" do
132
- # subject.build_dir = build_dir
133
- # subject.finalize!
134
- # assert_valid
135
- # end
44
+ describe "derived settings" do
136
45
 
137
- # it "should be invalid with a directory that doesn't have a Dockerfile" do
138
- # subject.build_dir = temporary_dir.to_s
139
- # subject.finalize!
140
- # assert_invalid
141
- # end
142
- # end
143
-
144
- # describe "#expose" do
145
- # before do
146
- # valid_defaults
147
- # end
148
-
149
- # it "uniqs the ports" do
150
- # subject.expose = [1, 1, 4, 5]
151
- # subject.finalize!
152
- # assert_valid
153
-
154
- # expect(subject.expose).to eq([1, 4, 5])
155
- # end
156
- # end
157
-
158
- # describe "#image" do
159
- # it "should be valid if set" do
160
- # subject.image = "foo"
161
- # subject.finalize!
162
- # assert_valid
163
- # end
164
-
165
- # it "should be invalid if not set" do
166
- # subject.image = nil
167
- # subject.finalize!
168
- # assert_invalid
169
- # end
170
- # end
171
-
172
- # describe "#link" do
173
- # before do
174
- # valid_defaults
175
- # end
176
-
177
- # it "should be valid with good links" do
178
- # subject.link "foo:bar"
179
- # subject.link "db:blah"
180
- # subject.finalize!
181
- # assert_valid
182
- # end
183
-
184
- # it "should be invalid if not name:alias" do
185
- # subject.link "foo"
186
- # subject.finalize!
187
- # assert_invalid
188
- # end
46
+ it "should derive 'configuration_name' from 'configuration_file' automatically" do
47
+ subject.configuration_file = "manifests/MyWebsite.ps1"
48
+ subject.finalize!
49
+ expect(subject.configuration_name).to eq("MyWebsite")
50
+ end
189
51
 
190
- # it "should be invalid if too many colons" do
191
- # subject.link "foo:bar:baz"
192
- # subject.finalize!
193
- # assert_invalid
194
- # end
195
- # end
52
+ it "should derive 'configuration_name' from 'configuration_file' automatically, when given multi-level file path" do
53
+ subject.configuration_file = "manifests/foo/MyWebsite.ps1"
54
+ subject.finalize!
55
+ expect(subject.configuration_name).to eq("MyWebsite")
56
+ end
196
57
 
197
- # describe "#merge" do
198
- # let(:one) { described_class.new }
199
- # let(:two) { described_class.new }
58
+ it "should derive 'configuration_name' from 'configuration_file' automatically, when given no multi-level file path" do
59
+ subject.configuration_file = "MyWebsite.ps1"
60
+ subject.finalize!
61
+ expect(subject.configuration_name).to eq("MyWebsite")
62
+ end
200
63
 
201
- # subject { one.merge(two) }
64
+ it "should detect the fully qualified path to the manifest automatically" do
65
+ env = double("environment", root_path: "")
66
+ config = double("config")
67
+ machine.stub(config: config, env: env)
68
+ allow(machine).to receive(:root_path).and_return(".")
202
69
 
203
- # context "#build_dir and #image" do
204
- # it "overrides image if build_dir is set previously" do
205
- # one.build_dir = "foo"
206
- # two.image = "bar"
70
+ subject.configuration_file = "manifests/MyWebsite.ps1"
207
71
 
208
- # expect(subject.build_dir).to be_nil
209
- # expect(subject.image).to eq("bar")
210
- # end
72
+ subject.finalize!
73
+ subject.validate(machine)
211
74
 
212
- # it "overrides image if build_dir is set previously" do
213
- # one.image = "foo"
214
- # two.build_dir = "bar"
75
+ basePath = File.absolute_path(File.join(File.dirname(__FILE__), '../../'))
76
+ expect(subject.expanded_configuration_file.to_s).to eq("#{basePath}/manifests/MyWebsite.ps1")
77
+ end
78
+ end
215
79
 
216
- # expect(subject.image).to be_nil
217
- # expect(subject.build_dir).to eq("bar")
218
- # end
80
+ describe "validate" do
81
+ before { subject.finalize! }
219
82
 
220
- # it "preserves if both set" do
221
- # one.image = "foo"
222
- # two.image = "baz"
223
- # two.build_dir = "bar"
83
+ before do
84
+ env = double("environment", root_path: "")
85
+ config = double("config")
86
+ machine.stub(config: config, env: env)
224
87
 
225
- # expect(subject.image).to eq("baz")
226
- # expect(subject.build_dir).to eq("bar")
227
- # end
228
- # end
88
+ allow(machine).to receive(:root_path).and_return("/path/to/vagrant")
89
+ end
229
90
 
230
- # context "env vars" do
231
- # it "should merge the values" do
232
- # one.env["foo"] = "bar"
233
- # two.env["bar"] = "baz"
91
+ # before do
92
+ # # By default lets be Linux for validations
93
+ # Vagrant::Util::Platform.stub(linux: true)
94
+ # end
234
95
 
235
- # expect(subject.env).to eq({
236
- # "foo" => "bar",
237
- # "bar" => "baz",
238
- # })
239
- # end
240
- # end
96
+ # it "should disallow absolute module paths" do
97
+ # end
241
98
 
242
- # context "exposed ports" do
243
- # it "merges the exposed ports" do
244
- # one.expose << 1234
245
- # two.expose = [42, 54]
99
+ it "should generate a module path on the host machine relative to the Vagrantfile" do
100
+ subject.module_path = "foo/modules"
101
+ expect(subject.expanded_module_paths('/path/to/vagrant/')).to eq(["/path/to/vagrant/foo/modules"])
102
+ end
246
103
 
247
- # expect(subject.expose).to eq([
248
- # 1234, 42, 54])
249
- # end
250
- # end
104
+ it "should generate module paths on the host machine relative to the Vagrantfile" do
105
+ subject.module_path = ["dont/exist", "also/dont/exist"]
106
+ expect(subject.expanded_module_paths('/path/to/vagrant/')).to eq(["/path/to/vagrant/dont/exist", "/path/to/vagrant/also/dont/exist"])
107
+ end
251
108
 
252
- # context "links" do
253
- # it "should merge the links" do
254
- # one.link "foo"
255
- # two.link "bar"
109
+ it "should be invalid if 'manifests_path' is not a real directory" do
110
+ subject.manifests_path = "/i/do/not/exist"
111
+ assert_invalid
112
+ assert_error("\"Path to DSC Manifest folder does not exist: /i/do/not/exist\"")
113
+ end
256
114
 
257
- # expect(subject._links).to eq([
258
- # "foo", "bar"])
259
- # end
260
- # end
261
- # end
115
+ it "should be invalid if 'configuration_file' is not a real file" do
116
+ subject.manifests_path = "/"
117
+ subject.configuration_file = "notexist.pp"
118
+ assert_invalid
119
+ assert_error("\"Path to DSC Manifest does not exist: /notexist.pp\"")
120
+ end
262
121
 
263
- # describe "#vagrant_machine" do
264
- # before { valid_defaults }
122
+ it "should be invalid if 'module_path' is not a real directory" do
123
+ subject.module_path = "/i/dont/exist"
124
+ assert_invalid
125
+ assert_error("\"Path to DSC Modules does not exist: /i/dont/exist\"")
126
+ end
265
127
 
266
- # it "should convert to a symbol" do
267
- # subject.vagrant_machine = "foo"
268
- # subject.finalize!
269
- # assert_valid
270
- # expect(subject.vagrant_machine).to eq(:foo)
271
- # end
272
- # end
128
+ it "should be invalid if 'configuration_file' and 'mof_path' provided" do
129
+ mof = File.new(temporary_file)
130
+ man = File.new(temporary_file)
273
131
 
274
- # describe "#vagrant_vagrantfile" do
275
- # before { valid_defaults }
132
+ subject.configuration_file = File.basename(man)
133
+ subject.mof_path = File.basename(mof)
134
+ expect { subject.finalize! }.to raise_error("\"Cannot provide configuration_file and mof_path at the same time. Please provide only one of the two.\"")
135
+ # assert_error("\"Cannot provide configuration_file and mof_path at the same time. Please provide only one of the two.\"")
136
+ end
276
137
 
277
- # it "should be valid if set to a file" do
278
- # subject.vagrant_vagrantfile = temporary_file.to_s
279
- # subject.finalize!
280
- # assert_valid
281
- # end
138
+ it "should be valid if 'configuration_file' is a real file" do
139
+ file = File.new(temporary_file)
282
140
 
283
- # it "should not be valid if set to a non-existent place" do
284
- # subject.vagrant_vagrantfile = "/i/shouldnt/exist"
285
- # subject.finalize!
286
- # assert_invalid
287
- # end
288
- # end
289
- # end
141
+ subject.configuration_file = File.basename(file)
142
+ subject.manifests_path = File.dirname(file)
143
+ subject.module_path = File.dirname(file)
144
+ assert_valid
145
+ end
146
+ end
147
+ end