Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
cover kaiserfile class with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsiaw committed Jan 15, 2020
1 parent 11afb9b commit 3f97f1e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 7 deletions.
6 changes: 3 additions & 3 deletions spec/kaiser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand Down
149 changes: 146 additions & 3 deletions spec/kaiserfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3f97f1e

Please sign in to comment.