spec/kontena/cli/common_spec.rb in kontena-cli-0.15.5 vs spec/kontena/cli/common_spec.rb in kontena-cli-0.16.0.pre1

- old
+ new

@@ -7,39 +7,48 @@ include Kontena::Cli::Common end.new end before(:each) do - allow(subject).to receive(:settings).and_return({'current_server' => 'default', - 'servers' => [{}] - }) - allow(subject).to receive(:save_settings) + RSpec::Mocks.space.proxy_for(File).reset + Kontena::Cli::Config.reset_instance end + def mock_config(cfg_hash) + expect(File).to receive(:readable?).and_return(true) + expect(File).to receive(:exist?).and_return(true) + expect(File).to receive(:read).and_return(cfg_hash.to_json) + end + describe '#current_grid' do it 'returns nil by default' do - allow(subject).to receive(:settings).and_return({'current_server' => 'alias', - 'servers' => [ - {'name' => 'some_master', 'url' => 'some_master'}, - {'name' => 'alias', 'url' => 'someurl'} - ] - }) + mock_config({ + 'current_server' => 'alias', + 'servers' => [ + {'name' => 'some_master', 'url' => 'some_master'}, + {'name' => 'alias', 'url' => 'someurl'} + ] + }) + expect(subject.current_grid).to eq(nil) end it 'returns grid from env' do - expect(ENV).to receive(:[]).with('KONTENA_GRID').and_return('foo') + allow(ENV).to receive(:[]).with(anything).and_return(nil) + allow(ENV).to receive(:[]).with('DEBUG').and_call_original + allow(ENV).to receive(:[]).with('KONTENA_GRID').and_return('foo') expect(subject.current_grid).to eq('foo') end it 'returns grid from json' do - allow(subject).to receive(:settings).and_return({'current_server' => 'alias', - 'servers' => [ - {'name' => 'some_master', 'url' => 'some_master'}, - {'name' => 'alias', 'url' => 'someurl', 'grid' => 'foo_grid'} - ] - }) + mock_config({ + 'current_server' => 'alias', + 'servers' => [ + {'name' => 'some_master', 'url' => 'some_master'}, + {'name' => 'alias', 'url' => 'someurl', 'grid' => 'foo_grid'} + ] + }) expect(subject.current_grid).to eq('foo_grid') end it 'returns nil if settings are not present' do allow(subject).to receive(:current_master).and_raise(ArgumentError) @@ -53,104 +62,99 @@ subject.api_url }.to raise_error(ArgumentError) end it 'return url from env' do - expect(ENV).to receive(:[]).with('KONTENA_URL').and_return('https://domain.com') + allow(ENV).to receive(:[]).with(anything).and_return(nil) + allow(ENV).to receive(:[]).with('KONTENA_URL').and_return('https://domain.com') expect(subject.api_url).to eq('https://domain.com') end end describe '#require_token' do it 'raises error by default' do expect { subject.require_token }.to raise_error(ArgumentError) end - - it 'return token from env' do - expect(ENV).to receive(:[]).with('KONTENA_TOKEN').and_return('secret_token') - expect(subject.require_token).to eq('secret_token') - end end describe '#current_master' do it 'return correct master info' do - allow(subject).to receive(:settings).and_return({'current_server' => 'alias', - 'servers' => [ - {'name' => 'some_master', 'url' => 'some_master'}, - {'name' => 'alias', 'url' => 'someurl'} - ] - }) - + mock_config({ + 'current_server' => 'alias', + 'servers' => [ + {'name' => 'some_master', 'url' => 'some_master'}, + {'name' => 'alias', 'url' => 'someurl'} + ] + }) expect(subject.current_master['url']).to eq('someurl') expect(subject.current_master['name']).to eq('alias') end end describe '#settings' do it 'migrates old settings' do - RSpec::Mocks.space.proxy_for(subject).reset - - allow(File).to receive(:exists?).and_return(true) - allow(File).to receive(:read).and_return('{ - "server": { - "url": "https://master.domain.com:8443", - "grid": "my-grid", - "token": "kontena-token" + mock_config({ + "server" => { + "url" => "https://master.domain.com:8443", + "grid" => "my-grid", + "token" => "kontena-token" } - }') - expected_settings = { - 'current_server' => 'default', - 'servers' => [ - { - "url" => "https://master.domain.com:8443", - "grid" => "my-grid", - "token" => "kontena-token", - "name" => "default" - } - ] - } - expect(File).to receive(:write).with(subject.settings_filename, JSON.pretty_generate(expected_settings)) - - subject.settings + }) + expect(File).to receive(:write) do |filename, content| + expect(File.basename(filename)).to eq(".kontena_client.json") + config_hash = JSON.parse(content) + expect(config_hash['servers']).to be_kind_of(Array) + expect(config_hash['servers'].first).to be_kind_of(Hash) + expect(config_hash['servers'].first['name']).to eq("default") + expect(config_hash['servers'].first['url']).to eq("https://master.domain.com:8443") + expect(config_hash['servers'].first['token']).to eq("kontena-token") + expect(config_hash['servers'].first['grid']).to eq("my-grid") + expect(config_hash['current_server']).to be_kind_of(String) + expect(config_hash['current_server']).to eq("default") + end + subject.config.write end end describe '#error' do it 'prints error message to stderr if given and raise error' do - expect($stderr).to receive(:puts).with('error message!') - expect{subject.error('error message!')}.to raise_error(SystemExit) + begin + expect{subject.error('error message!')}.to output('error message').to_stderr + rescue SystemExit => ex + expect(ex.status).to be 1 + end end end describe '#confirm_command' do it 'returns true if input matches' do - allow(subject).to receive(:prompt).and_return('name-to-confirm') + allow(subject).to receive(:ask).and_return('name-to-confirm') expect(subject.confirm_command('name-to-confirm')).to be_truthy expect{subject.confirm_command('name-to-confirm')}.to_not raise_error end it 'raises error unless input matches' do - expect(subject).to receive(:prompt).and_return('wrong-name') + expect(subject).to receive(:ask).and_return('wrong-name') expect(subject).to receive(:error).with(/did not match/) subject.confirm_command('name-to-confirm') end end describe '#confirm' do it 'returns true if confirmed' do - allow(subject).to receive(:prompt).and_return('y') + allow(subject.prompt).to receive(:yes?).and_return(true) expect(subject.confirm).to be_truthy expect{subject.confirm}.to_not raise_error end it 'raises error unless confirmed' do - expect(subject).to receive(:prompt).and_return('ei') + expect(subject.prompt).to receive(:yes?).and_return(false) expect(subject).to receive(:error).with(/Aborted/) subject.confirm end end