inspec/test/unit/bundles/inspect-habitat/profile_test.rb
Adam Leff 0342cca62e Adding a Habitat profile artifact creator
Two new commands have been created:

 * inspec habitat profile create /path/to/profile
 * inspec habitat profile upload /path/to/profile

The `create` command creates a Habitat artifact that contains the contents
of the Habitat profile found at the provided path. This will be used later
in some Habitat + InSpec integrations.

The `upload` command does the same create process but then uploads the
resulting artifact to the Habitat Depot.

Signed-off-by: Adam Leff <adam@leff.co>
2017-02-23 18:25:22 -05:00

118 lines
3.5 KiB
Ruby

require 'helper'
require 'mixlib/log'
require 'ostruct'
describe Habitat::Profile do
let(:profile) do
OpenStruct.new(
name: 'my_profile',
version: '1.2.3',
files: %w(file1 file2)
)
end
let(:subject) { Habitat::Profile.new('/path/to/profile', { 'log_level' => 'fatal' }) }
before do
Habitat::Log.level(:fatal)
end
describe '#validate_habitat_installed' do
it 'exits if hab --version fails' do
cmd = mock
cmd.stubs(:error?).returns(true)
cmd.stubs(:run_command)
cmd.stubs(:stdout)
cmd.stubs(:stderr)
Mixlib::ShellOut.expects(:new).with('hab --version').returns(cmd)
proc { subject.send(:validate_habitat_installed) }.must_raise SystemExit
end
end
describe '#validate_habitat_origin' do
it 'does not exit if the origin key exists' do
subject.expects(:habitat_origin).returns('12345')
subject.send(:validate_habitat_origin)
end
it 'exits if no origin key exists' do
subject.expects(:habitat_origin).returns(nil)
proc { subject.send(:validate_habitat_origin) }.must_raise SystemExit
end
end
describe '#validate_habitat_auth_token' do
it 'does not exit if the auth_token exists' do
subject.expects(:habitat_auth_token).returns('12345')
subject.send(:validate_habitat_auth_token)
end
it 'exits if no auth_token exists' do
subject.expects(:habitat_auth_token).returns(nil)
proc { subject.send(:validate_habitat_auth_token) }.must_raise SystemExit
end
end
describe '#build_hart' do
before do
subject.expects(:work_dir).at_least_once.returns(Dir.tmpdir)
end
it 'exits if the build fails' do
subject.expects(:system).returns(false)
proc { subject.send(:build_hart) }.must_raise SystemExit
end
it 'exits if more than one hart is created' do
subject.expects(:system).returns(true)
Dir.expects(:glob).returns(%w(hart1 hart2))
proc { subject.send(:build_hart) }.must_raise SystemExit
end
it 'exits if more than no hart is created' do
subject.expects(:system).returns(true)
Dir.expects(:glob).returns([])
proc { subject.send(:build_hart) }.must_raise SystemExit
end
it 'returns the hart filename' do
subject.expects(:system).returns(true)
Dir.expects(:glob).returns(%w(hart1))
subject.send(:build_hart).must_equal('hart1')
end
end
describe '#upload_hart' do
it 'exits if the upload failed' do
env = {
'TERM' => 'vt100',
'HAB_AUTH_TOKEN' => 'my_token',
'HAB_NONINTERACTIVE' => 'true',
}
cmd = mock
cmd.stubs(:run_command)
cmd.stubs(:error?).returns(true)
cmd.stubs(:stdout)
cmd.stubs(:stderr)
subject.expects(:habitat_auth_token).returns('my_token')
Mixlib::ShellOut.expects(:new).with("hab pkg upload my_hart", env: env).returns(cmd)
proc { subject.send(:upload_hart, 'my_hart') }.must_raise SystemExit
end
end
describe '#habitat_cli_config' do
it 'returns an empty hash if the CLI config does not exist' do
File.expects(:exist?).with(File.join(ENV['HOME'], '.hab', 'etc', 'cli.toml')).returns(false)
subject.send(:habitat_cli_config).must_equal({})
end
it 'returns parsed TOML from the hab config file' do
config_file = File.join(ENV['HOME'], '.hab', 'etc', 'cli.toml')
File.expects(:exist?).with(config_file).returns(true)
TOML.expects(:load_file).with(config_file).returns(foo: 1)
subject.send(:habitat_cli_config).must_equal(foo: 1)
end
end
end