diff --git a/spec/kaiser_spec.rb b/spec/kaiser_spec.rb index ae39a4c6..28dc00b4 100644 --- a/spec/kaiser_spec.rb +++ b/spec/kaiser_spec.rb @@ -15,7 +15,7 @@ shared_examples 'full help' do it 'prints the full help message' do - unwrapped_output = cmd_stdout.gsub("\n", ' ') + unwrapped_output = cmd_stdout.tr("\n", ' ') Kaiser::SUB_COMMANDS.keys.each do |name| expect(unwrapped_output).to include "- #{name}" @@ -68,8 +68,8 @@ unwrapped_output = lines[0, args_line].join # Remove all newlines because Optimist will wordwrap according to terminal size - unwrapped_output.gsub!("\n", ' ') - unwrapped_usage = subject.usage.gsub("\n", ' ') + unwrapped_output.tr!("\n", ' ') + unwrapped_usage = subject.usage.tr("\n", ' ') # Now we can check is it's the same as usage. expect(unwrapped_output).to eq unwrapped_usage diff --git a/spec/kaiserfile_spec.rb b/spec/kaiserfile_spec.rb index 1f545f8d..e403a7ec 100644 --- a/spec/kaiserfile_spec.rb +++ b/spec/kaiserfile_spec.rb @@ -2,11 +2,154 @@ require 'kaiser/kaiserfile' -RSpec.describe Kaiser::Kaiserfile, fixture_dir: 'kaiserfile' do - it 'defines dockerfile' do - cd 'app' do +RSpec.describe Kaiser::Kaiserfile do + # Defaults + let(:kaiserfile_name) { 'Kaiserfile' } + let(:dockerfile_name) { 'Dockerfile' } + + let(:kaiserfile) { '' } + let(:dockerfile) { '' } + + before do + allow(File).to receive(:exist?) { true } + allow(File).to receive(:read).with(kaiserfile_name) { kaiserfile } + allow(File).to receive(:read).with(dockerfile_name) { dockerfile } + end + + context '#dockerfile' do + let(:dockerfile_name) { 'Dockerfile.kaiser' } + let(:dockerfile) { 'FROM ruby:alpine' } + let(:kaiserfile) { 'dockerfile "Dockerfile.kaiser"' } + + it 'loads the specified dockerfile' do kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') expect(kaiserfile.docker_file_contents).to match(/FROM ruby:alpine/) end end + + context '#attach_mount' do + let(:kaiserfile) { 'attach_mount "bin", "/app/bin"' } + + it 'records the required mount attachments' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.attach_mounts).to contain_exactly(['bin', '/app/bin']) + end + end + + context '#db' do + context 'with a simple db script' do + let(:kaiserfile) { <<-KAISERFILE } + db 'somedb:version', + data_dir: '/var/lib/somedb/data', + port: 1234 + + KAISERFILE + + it 'sets up the database variable correctly' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.database[:image]).to eq 'somedb:version' + expect(kaiserfile.database[:data_dir]).to eq '/var/lib/somedb/data' + expect(kaiserfile.database[:port]).to eq 1234 + end + end + + context 'with a simple db script with a startup command' do + let(:kaiserfile) { <<-KAISERFILE } + db 'somedb:version', + data_dir: '/var/lib/dbdb', + port: 1414, + commands: 'start_db' + KAISERFILE + + it 'sets up the database variable with commands' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.database[:image]).to eq 'somedb:version' + expect(kaiserfile.database[:data_dir]).to eq '/var/lib/dbdb' + expect(kaiserfile.database[:port]).to eq 1414 + expect(kaiserfile.database[:commands]).to eq 'start_db' + end + end + + context 'with a simple db script with a waitscript' do + let(:kaiserfile) { <<-KAISERFILE } + db 'somedb:version', + data_dir: '/var/lib/dbdb', + port: 1414, + waitscript: 'sh wait_for_db.sh' + KAISERFILE + + it 'sets up the database variable with waitscript commands' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.database[:image]).to eq 'somedb:version' + expect(kaiserfile.database[:data_dir]).to eq '/var/lib/dbdb' + expect(kaiserfile.database[:port]).to eq 1414 + expect(kaiserfile.database[:waitscript]).to eq 'sh wait_for_db.sh' + end + end + + context 'with a simple db script with a waitscript and parameters to the waitscript' do + let(:kaiserfile) { <<-KAISERFILE } + db 'somedb:version', + data_dir: '/var/lib/dbdb', + port: 1414, + waitscript: 'sh wait_for_db.sh', + waitscript_params: '-e PORT=1414' + KAISERFILE + + it 'sets up the database variable with waitscript command params' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.database[:image]).to eq 'somedb:version' + expect(kaiserfile.database[:data_dir]).to eq '/var/lib/dbdb' + expect(kaiserfile.database[:port]).to eq 1414 + expect(kaiserfile.database[:waitscript]).to eq 'sh wait_for_db.sh' + expect(kaiserfile.database[:waitscript_params]).to eq '-e PORT=1414' + end + end + end + + context '#expose' do + let(:kaiserfile) { 'expose 1337' } + + it 'records the port used by the application' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.port).to eq 1337 + end + end + + context '#app_params' do + let(:kaiserfile) { 'app_params "--user me"' } + + it 'records the parameters to the docker container' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.params).to eq '--user me' + end + end + + context '#db_reset_command' do + let(:kaiserfile) { 'db_reset_command "explosion"' } + + it 'records the database reset command' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.database_reset_command).to eq 'explosion' + end + end + + context '#type' do + context 'when http is specified' do + let(:kaiserfile) { 'type :http' } + it 'sets server type to http' do + kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile') + expect(kaiserfile.server_type).to eq :http + end + end + + context 'when anything else specified' do + let(:kaiserfile) { 'type :meow' } + it 'throws an error' do + expect do + Kaiser::Kaiserfile.new('Kaiserfile') + end.to raise_error 'Valid server types are: [:http]' + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5f686120..2a283a7e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -41,6 +41,9 @@ config.after :each do |example| remove 'app' if example.metadata[:fixture_dir] - Aruba::RSpec.teardown + begin + Aruba::RSpec.teardown + rescue StandardError => _e + end end end