forked from cloudfoundry-attic/bosh-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_deployment_spec.rb
161 lines (135 loc) · 4.93 KB
/
project_deployment_spec.rb
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
module Bosh::Workspace
describe ProjectDeployment do
subject { Bosh::Workspace::ProjectDeployment.new manifest_file }
let(:manifest_content) { manifest.to_yaml }
let(:manifest_file) { get_tmp_file_path(manifest_content, file_name) }
let(:file_name) { "foo.yml" }
let(:manifest) { :manifest }
describe ".new" do
context "deployment file does not exist" do
before do
allow(File).to receive(:exist?).with(manifest_file).and_return(false)
end
it { expect { subject }.to raise_error(/deployment file.+not exist/i) }
end
end
describe '#manifest' do
context 'with stub file' do
let!(:stub_file) do
path = "../../stubs/#{stub_filename}"
File.expand_path(path, manifest_file).tap do |file|
FileUtils.mkdir_p File.dirname(file)
IO.write(file, stub_content)
end
end
after { FileUtils.rm(stub_file) }
let(:stub_filename) { File.basename(manifest_file) }
let(:manifest) { { 'director_uuid' => 'DIRECTOR_UUID' } }
let(:stub_content) do
{
'name' => 'bar',
'director_uuid' => 'foo-uuid',
'meta' => { 'foo' => 'bar' }
}.to_yaml
end
it 'merges stub with manifest' do
expect(subject.manifest['director_uuid']).to eq('foo-uuid')
end
context 'stub with releases' do
let(:stub_content) { { 'releases' => [] }.to_yaml }
it 'raises an error' do
expect{ subject.manifest }.to raise_error /releases.+not allowed/
end
end
end
context 'with executable stub file' do
let(:stub_file) { asset_dir("manifests-repo/stubs/foobar.sh") }
let(:manifest_file) { asset_dir("manifests-repo/deployments/foobar.yml") }
let(:manifest) do
{
'name' => 'NAME',
'director_uuid' => 'DIRECTOR_UUID',
'meta' => { 'foo' => 'bar', 'bar' => 'foo' }
}
end
it 'merges stub output with manifest' do
expect(subject.manifest['name']).to eq('foobar')
expect(subject.manifest['director_uuid']).to eq('bar-uuid')
expect(subject.manifest['meta']).to eq({ 'foo' => 'foobar', 'bar' => 'foo' })
end
context 'which are invalid' do
let(:result) { Bosh::Exec::Result.new('foo.sh', 'foo: error:', exit_code) }
before do
expect(subject).to receive(:sh).and_yield(result).and_return(result)
end
context 'non valid yaml returned' do
let(:exit_code) { 0 }
it 'raises an error' do
expect{ subject.manifest }.to raise_error /mapping values are not allowed/
end
end
context 'failure during execution' do
let(:exit_code) { 1 }
it 'raises an error' do
expect{ subject.manifest }.to raise_error /foo: error/
end
end
end
end
context 'without stub file' do
let(:file_name) { 'bar.yml' }
let(:manifest) { { 'director_uuid' => 'litmus' } }
it 'reads manifest without errors' do
expect(subject.manifest['director_uuid']).to eq('litmus')
end
end
end
describe "#perform_validation" do
context "valid" do
it "validates" do
allow_any_instance_of(Schemas::ProjectDeployment)
.to receive(:validate).with(manifest)
expect(subject).to be_valid
end
end
context "invalid" do
it "has errors" do
allow_any_instance_of(Schemas::ProjectDeployment)
.to receive(:validate).with(manifest)
.and_raise(Membrane::SchemaValidationError.new("foo"))
expect(subject).to_not be_valid
expect(subject.errors).to include "foo"
end
end
end
describe "#director_uuid=" do
before { subject.director_uuid = "foo-bar" }
its(:director_uuid) { should eq "foo-bar" }
end
describe "attr readers" do
let(:manifest) { {
"name" => :name,
"director_uuid" => :director_uuid,
"templates" => :templates,
"releases" => :releases,
"stemcells" => :stemcells,
"meta" => :meta,
"domain_name" => :domain_name,
} }
let(:director_uuid) { uuid }
%w(name director_uuid templates releases stemcells meta domain_name)
.each do |attr|
its(attr.to_sym) { should eq attr.to_sym }
end
end
describe "#merged_file" do
it "creates parent directory" do
dir = File.dirname(subject.merged_file)
expect(File.directory?(dir)).to be true
end
it "retruns merged file" do
expect(subject.merged_file).to match(/\.deployments\/#{file_name}/)
end
end
end
end