baha 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +15 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +156 -0
- data/Rakefile +21 -0
- data/baha.gemspec +31 -0
- data/bin/baha +5 -0
- data/example/.gitignore +1 -0
- data/example/base/init.sh.erb +11 -0
- data/example/base/test-template.erb +22 -0
- data/example/example.yml +54 -0
- data/example/rvm/image.yml +33 -0
- data/example/rvm/init.sh.erb +12 -0
- data/lib/baha/builder.rb +130 -0
- data/lib/baha/cli.rb +69 -0
- data/lib/baha/config.rb +142 -0
- data/lib/baha/container_options/cmd.rb +33 -0
- data/lib/baha/container_options/entrypoint.rb +10 -0
- data/lib/baha/container_options/env.rb +21 -0
- data/lib/baha/container_options/exposed_ports.rb +35 -0
- data/lib/baha/container_options/invalid_option_error.rb +15 -0
- data/lib/baha/container_options/option.rb +59 -0
- data/lib/baha/container_options/volumes.rb +24 -0
- data/lib/baha/container_options.rb +38 -0
- data/lib/baha/image.rb +154 -0
- data/lib/baha/log.rb +80 -0
- data/lib/baha/pre_build/command.rb +51 -0
- data/lib/baha/pre_build/download.rb +28 -0
- data/lib/baha/pre_build/template.rb +48 -0
- data/lib/baha/pre_build.rb +47 -0
- data/lib/baha/version.rb +3 -0
- data/lib/baha/workspace.rb +13 -0
- data/lib/baha.rb +5 -0
- data/spec/builder_spec.rb +103 -0
- data/spec/config_spec.rb +93 -0
- data/spec/container_options/cmd_spec.rb +46 -0
- data/spec/container_options/entrypoint_spec.rb +32 -0
- data/spec/container_options/env_spec.rb +26 -0
- data/spec/container_options/exposed_ports_spec.rb +32 -0
- data/spec/container_options/option_spec.rb +43 -0
- data/spec/container_options/volumes_spec.rb +25 -0
- data/spec/fixtures/base_image.yml +5 -0
- data/spec/fixtures/config_build.yml +12 -0
- data/spec/fixtures/config_build_image.yml +10 -0
- data/spec/fixtures/config_eachimage.yml +12 -0
- data/spec/fixtures/config_embedded.yml +11 -0
- data/spec/fixtures/config_include.yml +7 -0
- data/spec/fixtures/config_ssl.yml +13 -0
- data/spec/fixtures/config_sslpath.yml +11 -0
- data/spec/helpers/docker_helpers.rb +31 -0
- data/spec/image_spec.rb +167 -0
- data/spec/log_spec.rb +89 -0
- data/spec/options_spec.rb +52 -0
- data/spec/pre_build/command_spec.rb +69 -0
- data/spec/pre_build/download_spec.rb +43 -0
- data/spec/pre_build/template_spec.rb +55 -0
- data/spec/pre_build_spec.rb +29 -0
- data/spec/spec_helper.rb +39 -0
- metadata +255 -0
data/spec/image_spec.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'baha/image'
|
3
|
+
|
4
|
+
describe Baha::Image do
|
5
|
+
let(:config) do
|
6
|
+
Baha::Config.new({
|
7
|
+
'defaults' => {
|
8
|
+
'parent' => 'ubuntu:14.04.1',
|
9
|
+
'repository' => 'docker.example.com/baha',
|
10
|
+
'maintainer' => 'Ishmael <ishmael@example.com>',
|
11
|
+
'bind' => '/.baha',
|
12
|
+
'command' => ['/bin/bash','/.baha/init.sh']
|
13
|
+
}
|
14
|
+
})
|
15
|
+
end
|
16
|
+
let(:image) do
|
17
|
+
{
|
18
|
+
'parent' => 'ubuntu:14.04.1',
|
19
|
+
'name' => 'base',
|
20
|
+
'tag' => '1.0.0',
|
21
|
+
'maintainer' => '"Captain Ahab" <ahab@example.com>',
|
22
|
+
'bind' => '/.data',
|
23
|
+
'command' => ['/bin/bash','/.data/setup.sh']
|
24
|
+
}
|
25
|
+
end
|
26
|
+
subject { described_class.new(config,image) }
|
27
|
+
|
28
|
+
describe "#inspect" do
|
29
|
+
its(:inspect) { should match(/^Baha::Image<(@[a-z0-9_]+=.*)+>$/) }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#parent_id" do
|
33
|
+
let(:parent) { double('parent') }
|
34
|
+
it 'returns parent full id if available' do
|
35
|
+
allow(Baha::Image).to receive(:get_image!).and_return(parent)
|
36
|
+
allow(parent).to receive(:id).and_return('AAAA')
|
37
|
+
expect(subject.parent_id).to eq('AAAA')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#parse_name" do
|
42
|
+
subject { Baha::Image.parse_name(@name)}
|
43
|
+
it 'parses full name' do
|
44
|
+
@name = "docker.example.com/repo/name:tag"
|
45
|
+
is_expected.to eq({:ns=>"docker.example.com/repo", :name=>"name", :tag=>"tag"})
|
46
|
+
end
|
47
|
+
it 'parses local name' do
|
48
|
+
@name = "name:tag"
|
49
|
+
is_expected.to eq({:ns=>nil, :name=>"name", :tag=>"tag"})
|
50
|
+
end
|
51
|
+
it 'sets default tag to latest' do
|
52
|
+
@name = "name"
|
53
|
+
is_expected.to eq({:ns=>nil, :name=>"name", :tag=>"latest"})
|
54
|
+
end
|
55
|
+
it 'raises error on invalid name' do
|
56
|
+
expect { Baha::Image.parse_name("@@@@@@") }.to raise_error(ArgumentError)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#parse_with_default" do
|
61
|
+
subject { Baha::Image.parse_with_default(@name,'default.example.com/baha')}
|
62
|
+
it 'does not override repos' do
|
63
|
+
@name = "docker.example.com/repo/name:tag"
|
64
|
+
is_expected.to eq({:ns=>"docker.example.com/repo", :name=>"name", :tag=>"tag"})
|
65
|
+
end
|
66
|
+
it 'sets default namespace' do
|
67
|
+
@name = "name:tag"
|
68
|
+
is_expected.to eq({:ns=>"default.example.com/baha", :name=>"name", :tag=>"tag"})
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when constructed" do
|
73
|
+
its(:parent) { is_expected.to eq({:ns=>"docker.example.com/baha", :name=>"ubuntu", :tag=>"14.04.1"}) }
|
74
|
+
its(:image) { is_expected.to eq({:ns=>"docker.example.com/baha", :name=>"base", :tag=>"1.0.0"}) }
|
75
|
+
its(:maintainer) { is_expected.to eq('"Captain Ahab" <ahab@example.com>') }
|
76
|
+
its(:options) { is_expected.to be_a(Hash) }
|
77
|
+
its(:bind) { is_expected.to eq('/.data')}
|
78
|
+
its(:command) { is_expected.to eq(['/bin/bash','/.data/setup.sh']) }
|
79
|
+
its(:env) { is_expected.to include(
|
80
|
+
{:parent => {:ns=>"docker.example.com/baha", :name=>"ubuntu", :tag=>"14.04.1"}},
|
81
|
+
{:maintainer => "\"Captain Ahab\" <ahab@example.com>"},
|
82
|
+
{:bind => '/.data'},
|
83
|
+
{:name => 'base'},
|
84
|
+
{:tag => '1.0.0'},
|
85
|
+
:workspace) }
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#get_image!" do
|
89
|
+
let(:image) do
|
90
|
+
{
|
91
|
+
:ns => 'docker.example.com/baha',
|
92
|
+
:name => 'base',
|
93
|
+
:tag => '1.0.0'
|
94
|
+
}
|
95
|
+
end
|
96
|
+
subject { Baha::Image.get_image!(image) }
|
97
|
+
context 'when image exists locally' do
|
98
|
+
before :example do
|
99
|
+
mock_registry([
|
100
|
+
{:id => 'BBBB', :name => 'base', :tag => '1.0.0', :pulled => true, :parent => 'AAAA', :tags => ['base:1.0.0']}
|
101
|
+
])
|
102
|
+
end
|
103
|
+
it { should_not be_nil }
|
104
|
+
end
|
105
|
+
context 'when base image exists' do
|
106
|
+
before :example do
|
107
|
+
mock_registry([
|
108
|
+
{:id => 'BBBB', :name => 'base', :tag => '1.0.0', :parent => 'AAAA', :tags => ['base:1.0.0']}
|
109
|
+
])
|
110
|
+
end
|
111
|
+
it { should_not be_nil }
|
112
|
+
end
|
113
|
+
context 'when base image exists remotely' do
|
114
|
+
before :example do
|
115
|
+
mock_registry([
|
116
|
+
{:id => 'BBBB', :name => 'base', :tag => '1.0.0', :parent => 'AAAA', :tags => ['docker.example.com/baha/base:1.0.0']}
|
117
|
+
])
|
118
|
+
end
|
119
|
+
it { should_not be_nil }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "#needs_update?" do
|
124
|
+
context 'when parent does not exist' do
|
125
|
+
before :example do
|
126
|
+
mock_registry([])
|
127
|
+
end
|
128
|
+
it { expect{ subject.needs_update? }.to raise_error(Baha::ImageNotFoundError) }
|
129
|
+
end
|
130
|
+
context 'when base does not exist' do
|
131
|
+
before :example do
|
132
|
+
mock_registry([
|
133
|
+
{:id => 'AAAA', :name => 'ubuntu', :tag => '14.04.1', :tags => ['ubuntu:14.04.1']},
|
134
|
+
{:id => 'BBBB', :name => 'base', :tag => '1.0.0', :tags => ['base:1.0.0'], :not_found => true}
|
135
|
+
])
|
136
|
+
end
|
137
|
+
it { expect(subject.needs_update?).to be(true) }
|
138
|
+
end
|
139
|
+
context 'when parent has changed' do
|
140
|
+
before :example do
|
141
|
+
mock_registry([
|
142
|
+
{:id => 'AAAA', :name => 'ubuntu', :tag => '14.04.1', :tags => ['ubuntu:14.04.1']},
|
143
|
+
{:id => 'CCCC', :name => 'base', :tag => '1.0.0', :parent => 'BBBB', :tags => ['docker.example.com/baha/base:1.0.0']}
|
144
|
+
])
|
145
|
+
end
|
146
|
+
it { expect(subject.needs_update?).to be(true) }
|
147
|
+
end
|
148
|
+
context 'when tags have changed' do
|
149
|
+
before :example do
|
150
|
+
mock_registry([
|
151
|
+
{:id => 'AAAA', :name => 'ubuntu', :tag => '14.04.1', :tags => ['ubuntu:14.04.1']},
|
152
|
+
{:id => 'CCCC', :name => 'base', :tag => '1.0.0', :parent => 'BBBB', :tags => ['docker.example.com/baha/base:0.9.0']}
|
153
|
+
])
|
154
|
+
end
|
155
|
+
it { expect(subject.needs_update?).to be(true) }
|
156
|
+
end
|
157
|
+
context 'when up to date' do
|
158
|
+
before :example do
|
159
|
+
mock_registry([
|
160
|
+
{:id => 'AAAA', :name => 'ubuntu', :tag => '14.04.1', :tags => ['ubuntu:14.04.1']},
|
161
|
+
{:id => 'BBBB', :name => 'base', :tag => '1.0.0', :parent => 'AAAA', :tags => ['docker.example.com/baha/base:1.0.0']}
|
162
|
+
])
|
163
|
+
end
|
164
|
+
it { expect(subject.needs_update?).to be(false) }
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
data/spec/log_spec.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'baha/log'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
shared_examples "a logger" do |level|
|
6
|
+
subject { Baha::Log.for_name("specs") }
|
7
|
+
before do
|
8
|
+
Baha::Log.level = :debug
|
9
|
+
end
|
10
|
+
let(:loglevel) { level.to_s.upcase }
|
11
|
+
let(:regex) {
|
12
|
+
Regexp.new("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3} \\[\\s*#{loglevel}\\] specs -- Hello$")
|
13
|
+
}
|
14
|
+
context "when given a block" do
|
15
|
+
it 'prints a valid message' do
|
16
|
+
subject.send(level) { "Hello" }
|
17
|
+
expect(io.string).to match(regex)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context "when given a string" do
|
21
|
+
it 'prints a valid message' do
|
22
|
+
subject.send(level,"Hello")
|
23
|
+
expect(io.string).to match(regex)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
context "when given an object" do
|
27
|
+
it 'prints object.inspect' do
|
28
|
+
subject.send(level,{ :hello => 'world' })
|
29
|
+
expect(io.string).to match("specs -- {:hello=>\"world\"}")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
context "when given an exception" do
|
33
|
+
it 'prints a exception backtrace' do
|
34
|
+
begin
|
35
|
+
raise ArgumentError.new("Hello")
|
36
|
+
rescue Exception => e
|
37
|
+
subject.send(level,e)
|
38
|
+
end
|
39
|
+
expect(io.string).to match(/specs -- Hello \(ArgumentError\)\n((.+?):(\d+)(|:in `(.+)')\n)+/)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe Baha::Log do
|
45
|
+
subject { Baha::Log }
|
46
|
+
|
47
|
+
before do
|
48
|
+
Baha::Log.level = :error
|
49
|
+
end
|
50
|
+
|
51
|
+
it { expect(Baha::Log.level).to eq(:error) }
|
52
|
+
|
53
|
+
describe "#level=" do
|
54
|
+
it 'sets level' do
|
55
|
+
Baha::Log.level = "WARN"
|
56
|
+
expect(Baha::Log.level).to eq(:warn)
|
57
|
+
end
|
58
|
+
it 'raises error on invalid input type' do
|
59
|
+
expect { Baha::Log.level = 123 }.to raise_error(ArgumentError)
|
60
|
+
end
|
61
|
+
it 'raises error on invalid level' do
|
62
|
+
expect { Baha::Log.level = 'NOTALEVEL' }.to raise_error(ArgumentError)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#for_name" do
|
67
|
+
subject { Baha::Log.for_name("specs") }
|
68
|
+
it { expect(subject).not_to be_nil }
|
69
|
+
its(:progname) { should eq("specs") }
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with log instance' do
|
73
|
+
let(:io) { StringIO.new }
|
74
|
+
before do
|
75
|
+
Baha::Log.logfile = io
|
76
|
+
end
|
77
|
+
it_behaves_like "a logger", :debug
|
78
|
+
it_behaves_like "a logger", :info
|
79
|
+
it_behaves_like "a logger", :warn
|
80
|
+
it_behaves_like "a logger", :error
|
81
|
+
it_behaves_like "a logger", :fatal
|
82
|
+
describe "#close!" do
|
83
|
+
before do
|
84
|
+
subject.close!
|
85
|
+
end
|
86
|
+
it { expect(io.closed?).to eq(true) }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'baha/container_options'
|
3
|
+
|
4
|
+
describe Baha::ContainerOptions do
|
5
|
+
describe 'parse_option' do
|
6
|
+
it 'parses Cmd' do
|
7
|
+
expect(Baha::ContainerOptions::parse_option(:cmd,'/bin/bash -l')).to be_a(Baha::ContainerOptions::Cmd)
|
8
|
+
end
|
9
|
+
it 'parses Entrypoint' do
|
10
|
+
expect(Baha::ContainerOptions::parse_option(:entrypoint,['/bin/bash','-l'])).to be_a(Baha::ContainerOptions::Entrypoint)
|
11
|
+
end
|
12
|
+
it 'parses Env' do
|
13
|
+
expect(Baha::ContainerOptions::parse_option(:env,{'key' => 'value'})).to be_a(Baha::ContainerOptions::Env)
|
14
|
+
end
|
15
|
+
it 'parses raw option' do
|
16
|
+
expect(Baha::ContainerOptions::parse_option(:hostname,'host.local')).to be_a(Baha::ContainerOptions::Option)
|
17
|
+
end
|
18
|
+
it 'parses Volumes' do
|
19
|
+
expect(Baha::ContainerOptions::parse_option(:volumes,['/mnt/media'])).to be_a(Baha::ContainerOptions::Volumes)
|
20
|
+
end
|
21
|
+
it 'parses ExposedPorts' do
|
22
|
+
expect(Baha::ContainerOptions::parse_option(:exposedports,[8080,'8443/tcp'])).to be_a(Baha::ContainerOptions::ExposedPorts)
|
23
|
+
end
|
24
|
+
it 'validates values' do
|
25
|
+
expect{ Baha::ContainerOptions::parse_option(:exposedports,:notavalidvalue) }.to raise_error(Baha::ContainerOptions::InvalidOptionError)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
describe 'parse_options' do
|
29
|
+
it 'parses all options' do
|
30
|
+
expected = {
|
31
|
+
:entrypoint => Baha::ContainerOptions::Entrypoint.new('/bin/bash'),
|
32
|
+
:cmd => Baha::ContainerOptions::Cmd.new('-l'),
|
33
|
+
:volumes => Baha::ContainerOptions::Volumes.new(['/mnt/data']),
|
34
|
+
:env => Baha::ContainerOptions::Env.new({'ENVKEY' => 'Hello'}),
|
35
|
+
:exposedports => Baha::ContainerOptions::ExposedPorts.new([8080,'8443/tcp']),
|
36
|
+
:hostname => Baha::ContainerOptions::Option.new('hostname','localhost.localdomain')
|
37
|
+
}
|
38
|
+
actual = Baha::ContainerOptions::parse_options({
|
39
|
+
'entrypoint' => '/bin/bash',
|
40
|
+
'cmd' => '-l',
|
41
|
+
'volumes' => ['/mnt/data'],
|
42
|
+
'env' => {'ENVKEY' => 'Hello'},
|
43
|
+
'exposedports' => [ 8080, '8443/tcp' ],
|
44
|
+
'hostname' => 'localhost.localdomain'
|
45
|
+
})
|
46
|
+
expect(actual).to eql(expected)
|
47
|
+
end
|
48
|
+
it 'returns empty map on nil options' do
|
49
|
+
expect(Baha::ContainerOptions::parse_options(nil)).to eql({})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'baha/pre_build'
|
5
|
+
require 'baha/pre_build/command'
|
6
|
+
|
7
|
+
describe Baha::PreBuild::Module::Command do
|
8
|
+
describe "#execute" do
|
9
|
+
let(:cmd) { ['echo','Hello'] }
|
10
|
+
let(:test) { 'test' }
|
11
|
+
let(:stdin) { double('in') }
|
12
|
+
let(:output) { double('output') }
|
13
|
+
let(:thread) { double('wait') }
|
14
|
+
let(:exit_status) { double('exit') }
|
15
|
+
let(:config) { double('config') }
|
16
|
+
let(:image) { double('image') }
|
17
|
+
before do
|
18
|
+
allow(image).to receive(:workspace).and_return(Pathname.new('/tmp'))
|
19
|
+
allow(output).to receive(:each).and_yield("OUTPUT LINE\n")
|
20
|
+
allow(thread).to receive(:value).and_return(exit_status)
|
21
|
+
allow(Open3).to receive(:popen2e).with(cmd,:chdir=>'/tmp').and_yield(stdin,output,thread)
|
22
|
+
end
|
23
|
+
subject { {'command' => cmd, :image => image, :config => config } }
|
24
|
+
it 'runs command' do
|
25
|
+
Baha::PreBuild::Module.execute(subject)
|
26
|
+
expect(Open3).to have_received(:popen2e).with(cmd,:chdir=>'/tmp')
|
27
|
+
end
|
28
|
+
context "when creates given" do
|
29
|
+
subject { {'command' => cmd,'creates' => 'file', :image => image, :config => config } }
|
30
|
+
context "and file exists" do
|
31
|
+
it 'it does not execute' do
|
32
|
+
allow_any_instance_of(Pathname).to receive(:exist?).and_return(true)
|
33
|
+
Baha::PreBuild::Module.execute(subject)
|
34
|
+
expect(Open3).not_to have_received(:popen2e).with(cmd)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
context "and file does not exist" do
|
38
|
+
it 'it executes' do
|
39
|
+
allow_any_instance_of(Pathname).to receive(:exist?).and_return(false)
|
40
|
+
Baha::PreBuild::Module.execute(subject)
|
41
|
+
expect(Open3).to have_received(:popen2e).with(cmd,:chdir=>'/tmp')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
context "when onlyif given" do
|
46
|
+
subject { {'command' => cmd,'only_if' => test, :image => image, :config => config } }
|
47
|
+
before do
|
48
|
+
allow(Open3).to receive(:popen2e).with(test,:chdir=>'/tmp').and_yield(stdin,output,thread)
|
49
|
+
allow(Open3).to receive(:popen2e).with(cmd,:chdir=>'/tmp').and_yield(stdin,output,thread)
|
50
|
+
end
|
51
|
+
context "and returns fail" do
|
52
|
+
it 'it does not execute' do
|
53
|
+
allow(exit_status).to receive(:success?).and_return(false)
|
54
|
+
Baha::PreBuild::Module.execute(subject)
|
55
|
+
expect(Open3).to have_received(:popen2e).with(test,:chdir=>'/tmp')
|
56
|
+
expect(Open3).not_to have_received(:popen2e).with(cmd,:chdir=>'/tmp')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
context "and returns success" do
|
60
|
+
it 'it executes' do
|
61
|
+
allow(exit_status).to receive(:success?).and_return(true)
|
62
|
+
Baha::PreBuild::Module.execute(subject)
|
63
|
+
expect(Open3).to have_received(:popen2e).with(test,:chdir=>'/tmp')
|
64
|
+
expect(Open3).to have_received(:popen2e).with(cmd,:chdir=>'/tmp')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'baha/pre_build'
|
5
|
+
require 'baha/pre_build/download'
|
6
|
+
|
7
|
+
describe Baha::PreBuild::Module::Download do
|
8
|
+
let(:file) {
|
9
|
+
double("file")
|
10
|
+
}
|
11
|
+
let(:url) {
|
12
|
+
double("url")
|
13
|
+
}
|
14
|
+
let(:config) {
|
15
|
+
double('config')
|
16
|
+
}
|
17
|
+
let(:image) { double('image') }
|
18
|
+
subject {
|
19
|
+
{:config => config, :image => image, 'download' => 'http://www.google.com/', 'file' => 'url' }
|
20
|
+
}
|
21
|
+
|
22
|
+
describe "#execute" do
|
23
|
+
before do
|
24
|
+
allow(file).to receive(:write)
|
25
|
+
allow(url).to receive(:read).and_return('content')
|
26
|
+
allow(Baha::PreBuild::Module::Download).to receive(:open).with('http://www.google.com/','rb').and_yield(url)
|
27
|
+
allow(File).to receive(:open)
|
28
|
+
allow(File).to receive(:open).with(Pathname.new('/tmp/url'),'w').and_yield(file)
|
29
|
+
allow(image).to receive(:workspace).and_return(Pathname.new('/tmp'))
|
30
|
+
end
|
31
|
+
it 'downloads url to file' do
|
32
|
+
Baha::PreBuild::Module.execute(subject)
|
33
|
+
expect(file).to have_received(:write).with("content")
|
34
|
+
end
|
35
|
+
context "when file exists" do
|
36
|
+
it 'does not download file' do
|
37
|
+
allow_any_instance_of(Pathname).to receive(:exist?).and_return(true)
|
38
|
+
Baha::PreBuild::Module.execute(subject)
|
39
|
+
expect(file).not_to have_received(:write)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'baha/pre_build'
|
5
|
+
require 'baha/pre_build/template'
|
6
|
+
|
7
|
+
describe Baha::PreBuild::Module::Template do
|
8
|
+
let(:dest) {
|
9
|
+
double("dest")
|
10
|
+
}
|
11
|
+
let(:config) {
|
12
|
+
double('config')
|
13
|
+
}
|
14
|
+
let(:image) { double('image') }
|
15
|
+
subject {
|
16
|
+
{:config => config, :image => image, 'template' => '/tmp/file', 'dest' => 'dest.txt' }
|
17
|
+
}
|
18
|
+
|
19
|
+
describe "#execute" do
|
20
|
+
before do
|
21
|
+
allow(image).to receive(:workspace).and_return(Pathname.new('/tmp'))
|
22
|
+
allow(image).to receive(:env).and_return({:name => 'test'})
|
23
|
+
allow(config).to receive(:resolve_file).and_return(nil)
|
24
|
+
allow(config).to receive(:resolve_file).with('/tmp/file').and_return('/tmp/file')
|
25
|
+
allow(config).to receive(:resolve_file).with('include.erb').and_return('/tmp/include.erb')
|
26
|
+
allow(File).to receive(:read).with('/tmp/file').and_return("<%= name %>")
|
27
|
+
allow(File).to receive(:read).with('/tmp/include.erb').and_return("included")
|
28
|
+
allow(File).to receive(:open).with(Pathname.new("/tmp/dest.txt"),"w").and_yield(dest)
|
29
|
+
allow(dest).to receive(:write)
|
30
|
+
end
|
31
|
+
it 'writes template content to dest' do
|
32
|
+
Baha::PreBuild::Module.execute(subject)
|
33
|
+
expect(dest).to have_received(:write).with("test")
|
34
|
+
end
|
35
|
+
context 'with render function' do
|
36
|
+
before do
|
37
|
+
allow(File).to receive(:read).with('/tmp/file').and_return("<%= render 'include.erb' %>")
|
38
|
+
end
|
39
|
+
it 'includes render file' do
|
40
|
+
Baha::PreBuild::Module.execute(subject)
|
41
|
+
expect(dest).to have_received(:write).with("included")
|
42
|
+
end
|
43
|
+
it 'raises error when not found' do
|
44
|
+
allow(config).to receive(:resolve_file).with('include.erb').and_return(nil)
|
45
|
+
expect { Baha::PreBuild::Module.execute(subject) }.to raise_error(ArgumentError)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
context "when template not found" do
|
49
|
+
it 'raises ArgumentError' do
|
50
|
+
allow(config).to receive(:resolve_file).and_return(nil)
|
51
|
+
expect { Baha::PreBuild::Module.execute(subject) }.to raise_error(ArgumentError)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'baha/config'
|
3
|
+
require 'baha/pre_build'
|
4
|
+
|
5
|
+
describe Baha::Builder do
|
6
|
+
let(:task) do
|
7
|
+
{ 'download' => '', 'url' => 'http://www.google.com', 'file' => 'out.txt' }
|
8
|
+
end
|
9
|
+
let(:badtask) do
|
10
|
+
{ 'no_such_module' => 'throws error' }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#execute" do
|
14
|
+
context 'with valid task' do
|
15
|
+
before do
|
16
|
+
allow(Baha::PreBuild::Module).to receive(:module_download)
|
17
|
+
Baha::PreBuild::Module.execute(task)
|
18
|
+
end
|
19
|
+
it 'exeecutes module' do
|
20
|
+
expect(Baha::PreBuild::Module).to have_received(:module_download).with(instance_of(Baha::PreBuild::Module))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
context 'with invalid task' do
|
24
|
+
it do
|
25
|
+
expect { Baha::PreBuild::Module.execute(badtask) }.to raise_error(Baha::PreBuild::ModuleNotFoundError)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
add_filter '/spec/'
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'bundler/setup'
|
7
|
+
Bundler.setup
|
8
|
+
|
9
|
+
require 'baha'
|
10
|
+
require 'helpers/docker_helpers.rb'
|
11
|
+
require 'rspec/its'
|
12
|
+
require 'rspec/mocks'
|
13
|
+
require 'pathname'
|
14
|
+
require 'stringio'
|
15
|
+
|
16
|
+
def fixture_path
|
17
|
+
Pathname.new(File.expand_path(File.join(__FILE__, '..', 'fixtures')))
|
18
|
+
end
|
19
|
+
def fixture(filename)
|
20
|
+
file = fixture_path + filename
|
21
|
+
raise ArgumentError.new("File #{filename} could not be found") unless file.exist?
|
22
|
+
file
|
23
|
+
end
|
24
|
+
|
25
|
+
RSpec.configure do |config|
|
26
|
+
# config.color_enabled = true
|
27
|
+
config.order = "random"
|
28
|
+
|
29
|
+
config.include DockerHelpers
|
30
|
+
|
31
|
+
# Forbid old 'should' syntax
|
32
|
+
config.expect_with :rspec do |c|
|
33
|
+
c.syntax = :expect
|
34
|
+
end
|
35
|
+
|
36
|
+
config.before(:each) do
|
37
|
+
Baha::Log.logfile = StringIO.new
|
38
|
+
end
|
39
|
+
end
|