Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Jen committed Aug 9, 2018
1 parent d101403 commit ca5dead
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 28 deletions.
32 changes: 32 additions & 0 deletions spec/unit/pdk/cli/test/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@
end
end

context 'with --puppet-dev' do
let(:puppet_env) do
{
ruby_version: '2.4.3',
gemset: { puppet: 'file://path/to/puppet' },
}
end

before(:each) do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with(hash_including(:'puppet-dev' => true)).and_return(puppet_env)
allow(PDK::Util::PuppetVersion).to receive(:fetch_puppet_dev).and_return(nil)
allow(PDK::Test::Unit).to receive(:invoke).and_return(0)
allow(PDK::CLI::Util).to receive(:module_version_check)
end

it 'activates puppet github source' do
expect(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet_env[:gemset])

expect {
test_unit_cmd.run_this(['--puppet-dev'])
}.to exit_zero
end

it 'activates resolved ruby version' do
expect(PDK::Util::RubyVersion).to receive(:use).with(puppet_env[:ruby_version])

expect {
test_unit_cmd.run_this(['--puppet-dev'])
}.to exit_zero
end
end

context 'when --puppet-version and --pe-version are specified' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{cannot specify.*--pe-version.*and.*--puppet-version}i))
Expand Down
115 changes: 115 additions & 0 deletions spec/unit/pdk/cli/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,51 @@
{ ruby_version: ruby_version, gem_version: Gem::Version.new(puppet_version) }
end

context 'when puppet-dev has been set' do
let(:options) { { :'puppet-dev' => true } }
let(:ruby_version) { '2.4.4' }
let(:puppet_version) { 'path/to/puppet' }

let(:version_result) do
{
gem_version: puppet_version,
ruby_version: ruby_version,
}
end

before(:each) do
allow(PDK::Util::PuppetVersion).to receive(:puppet_dev_path).and_return(puppet_version)
allow(PDK::Util::PuppetVersion).to receive(:puppet_dev_env).and_return(version_result)
end

it_behaves_like 'it returns a puppet environment'
end

context 'when PDK_PUPPET_DEV has been set' do
let(:options) { {} }
let(:ruby_version) { '2.4.4' }
let(:puppet_version) { 'path/to/puppet' }

let(:version_result) do
{
gem_version: puppet_version,
ruby_version: ruby_version,
}
end

before(:each) do
allow(PDK::Util::PuppetVersion).to receive(:puppet_dev_path).and_return(puppet_version)
allow(PDK::Util::PuppetVersion).to receive(:puppet_dev_env).and_return(version_result)
ENV['PDK_PUPPET_DEV'] = 'true'
end

after(:each) do
ENV.delete('PDK_PUPPET_DEV')
end

it_behaves_like 'it returns a puppet environment'
end

context 'when puppet-version has been set' do
let(:options) { { :'puppet-version' => '4.10.10' } }
let(:ruby_version) { '2.1.9' }
Expand Down Expand Up @@ -305,6 +350,76 @@
let(:cli_pe_version) { '2016.2' }
let(:env_pe_version) { '2017.3' }

context 'when --puppet-dev is set' do
let(:options) { { :'puppet-dev' => true } }

it 'is silent' do
expect(logger).not_to receive(:warn)

expect { validate_puppet_version_opts }.not_to raise_error
end

context 'when PDK_PUPPET_DEV is also set' do
before(:each) do
ENV['PDK_PUPPET_DEV'] = 'true'
end

after(:each) do
ENV.delete('PDK_PUPPET_DEV')
end

it 'warns about option precedence' do
expect(logger).to receive(:warn).with(%r{dev flag.*overrides.*environment}i)

validate_puppet_version_opts
end
end

context 'when --puppet-version is also set' do
let(:options) { { :'puppet-version' => cli_puppet_version, :'puppet-dev' => true } }

it 'exits with error' do
expect { validate_puppet_version_opts }.to raise_error(PDK::CLI::ExitWithError, %r{cannot specify.*flag.*and.*option}i)
end
end

context 'when PDK_PUPPET_VERSION is also set' do
before(:each) do
ENV['PDK_PUPPET_VERSION'] = env_puppet_version
end

after(:each) do
ENV.delete('PDK_PUPPET_VERSION')
end

it 'exits with error' do
expect { validate_puppet_version_opts }.to raise_error(PDK::CLI::ExitWithError, %r{cannot specify.*flag.*and.*environment}i)
end
end

context 'when --pe-version is also set' do
let(:options) { { :'pe-version' => cli_pe_version, :'puppet-dev' => true } }

it 'exits with error' do
expect { validate_puppet_version_opts }.to raise_error(PDK::CLI::ExitWithError, %r{cannot specify.*flag.*and.*option}i)
end
end

context 'when PDK_PE_VERSION is also set' do
before(:each) do
ENV['PDK_PE_VERSION'] = env_pe_version
end

after(:each) do
ENV.delete('PDK_PE_VERSION')
end

it 'exits with error' do
expect { validate_puppet_version_opts }.to raise_error(PDK::CLI::ExitWithError, %r{cannot specify.*flag.*and.*environment}i)
end
end
end

context 'when --puppet-version is set' do
let(:options) { { :'puppet-version' => cli_puppet_version } }

Expand Down
78 changes: 50 additions & 28 deletions spec/unit/pdk/cli/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,56 @@
end
end

context 'with --puppet-dev' do
let(:puppet_env) do
{
ruby_version: '2.4.3',
gemset: { puppet: 'file://path/to/puppet' },
}
end

before(:each) do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with(hash_including(:'puppet-dev' => true)).and_return(puppet_env)
allow(PDK::Util::PuppetVersion).to receive(:fetch_puppet_dev).and_return(nil)
end

it 'activates puppet github source' do
expect(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet_env[:gemset])

expect {
PDK::CLI.run(['validate', '--puppet-dev'])
}.to exit_zero
end

it 'activates resolved ruby version' do
expect(PDK::Util::RubyVersion).to receive(:use).with(puppet_env[:ruby_version])

expect {
PDK::CLI.run(['validate', '--puppet-dev'])
}.to exit_zero
end
end

context 'with both --puppet-version and --puppet-dev' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{cannot specify.*--puppet-dev.*and.*--puppet-version}i))

expect {
PDK::CLI.run(%w[validate --puppet-version 4.10.10 --puppet-dev])
}.to exit_nonzero
end
end

context 'with both --pe-version and --puppet-dev' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{cannot specify.*--puppet-dev.*and.*--pe-version}i))

expect {
PDK::CLI.run(%w[validate --pe-version 2018.1 --puppet-dev])
}.to exit_nonzero
end
end

context 'with both --puppet-version and --pe-version' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{cannot specify.*--pe-version.*and.*--puppet-version}i))
Expand Down Expand Up @@ -221,32 +271,4 @@
}.to exit_zero
end
end

context 'with --puppet-dev' do
it 'activates resolved puppet version' do
expect {
PDK::CLI.run(['validate', '--puppet-dev'])
}.to exit_zero
end
end

context 'with both --puppet-version and --puppet-dev' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{cannot specify.*--puppet-dev.*and.*--puppet-version}i))

expect {
PDK::CLI.run(%w[validate --puppet-version 4.10.10 --puppet-dev])
}.to exit_nonzero
end
end

context 'with both --pe-version and --puppet-dev' do
it 'exits with an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{cannot specify.*--puppet-dev.*and.*--pe-version}i))

expect {
PDK::CLI.run(%w[validate --pe-version 2018.1 --puppet-dev])
}.to exit_nonzero
end
end
end
98 changes: 98 additions & 0 deletions spec/unit/pdk/util/puppet_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,104 @@
end
end

describe '.fetch_puppet_dev' do
context 'if puppet source is not cloned yet' do
before(:each) do
allow(PDK::Util::Git).to receive(:remote_repo?).with(anything).and_return(false)
end

context 'and fails to connect to github' do
let(:clone_results) do
{
stdout: 'foo',
stderr: 'bar',
exit_code: 1,
}
end

before(:each) do
allow(PDK::Util).to receive(:cachedir).and_return(File.join('path', 'to'))
allow(PDK::Util::Git).to receive(:git).with('clone', anything, anything).and_return(clone_results)
end

it 'raises an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{foo}))
expect(logger).to receive(:error).with(a_string_matching(%r{bar}))
expect {
described_class.fetch_puppet_dev
}.to raise_error(PDK::CLI::FatalError, a_string_matching(%r{Unable to clone git repository}i))
end
end

context 'and successfully connects to github' do
let(:clone_results) do
{
stdout: 'foo',
stderr: 'bar',
exit_code: 0,
}
end

before(:each) do
allow(PDK::Util).to receive(:cachedir).and_return(File.join('path', 'to'))
allow(PDK::Util::Git).to receive(:git).with('clone', anything, anything).and_return(clone_results)
end

it 'exits cleanly' do
expect(described_class.fetch_puppet_dev).to eq(nil)
end
end
end

context 'if puppet source is already cloned' do
before(:each) do
allow(PDK::Util::Git).to receive(:remote_repo?).with(anything).and_return(true)
end

context 'and fails to connect to github' do
let(:fetch_results) do
{
stdout: 'foo',
stderr: 'bar',
exit_code: 1,
}
end

before(:each) do
allow(PDK::Util).to receive(:cachedir).and_return('/path/to/')
allow(PDK::Util::Git).to receive(:git).with('fetch', anything, anything).and_return(fetch_results)
end

it 'raises an error' do
expect(logger).to receive(:error).with(a_string_matching(%r{foo}))
expect(logger).to receive(:error).with(a_string_matching(%r{bar}))
expect {
described_class.fetch_puppet_dev
}.to raise_error(PDK::CLI::FatalError, a_string_matching(%r{Unable to fetch updates for git repository}i))
end
end

context 'and successfully connects to github' do
let(:clone_results) do
{
stdout: 'foo',
stderr: 'bar',
exit_code: 0,
}
end

before(:each) do
allow(PDK::Util).to receive(:cachedir).and_return('/path/to/')
allow(PDK::Util::Git).to receive(:git).with('fetch', anything, anything).and_return(clone_results)
end

it 'exits cleanly' do
expect(described_class.fetch_puppet_dev).to eq(nil)
end
end
end
end

describe '.find_gem_for' do
context 'when running from a package install' do
include_context 'is a package install'
Expand Down

0 comments on commit ca5dead

Please sign in to comment.