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

Commit

Permalink
add build platform and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsiaw committed Mar 29, 2024
1 parent 6606928 commit c394082
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: default
jobs:
rspec:
container:
image: ruby:2.7
image: ruby:3.0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions lib/kaiser/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def services
@services ||= Config.kaiserfile.services.map { |name, info| Service.new(envname, name, info) }
end

def force_platform
Config.kaiserfile.platform || ''
end

def db_port
Config.config[:envs][envname][:db_port]
end
Expand Down
20 changes: 15 additions & 5 deletions lib/kaiser/cmds/up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ def execute(opts)
end
end

def build_cmd
platform_args = ''
platform_args = "--platform=#{force_platform}" if force_platform.length != 0
build_args = docker_build_args.map { |k, v| "--build-arg #{k}=#{v}" }
[
'docker build',
"-t kaiser:#{envname}-#{current_branch}",
"-f #{tmp_dockerfile_name} #{Config.work_dir}",
platform_args,
"#{build_args.join(' ')}"
]
end

def setup_app
Config.info_out.puts 'Setting up application'
File.write(tmp_dockerfile_name, docker_file_contents)
build_args = docker_build_args.map { |k, v| "--build-arg #{k}=#{v}" }
CommandRunner.run! Config.out, "docker build
-t kaiser:#{envname}-#{current_branch}
-f #{tmp_dockerfile_name} #{Config.work_dir}
#{build_args.join(' ')}"

CommandRunner.run! Config.out, build_cmd.join("\n\t")
FileUtils.rm(tmp_dockerfile_name)
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/kaiser/kaiserfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def db(image,
}
end

def force_platform(platform_name)
@platform = platform_name
end

def expose(port)
@port = port
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kaiser/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Kaiser
VERSION = '0.6.7'
VERSION = '0.7.0'
end
61 changes: 61 additions & 0 deletions spec/cmds/up_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

RSpec.describe Kaiser::Cmds::Up do
describe "cmd" do
it "generates correctly" do
kaiserfile = double(Kaiser::Kaiserfile)
kaiserconfig = double(Kaiser::Config)

allow(Kaiser::Config).to receive(:kaiserfile).and_return(kaiserfile)
allow(Kaiser::Config).to receive(:config).and_return(kaiserconfig)

allow(Kaiser::Config).to receive(:work_dir).and_return('somedir')

envs = double("Hash")
allow(envs).to receive(:[]).with('somedir').and_return('meow')

allow(Kaiser::Config.kaiserfile).to receive(:docker_build_args).and_return({})
allow(Kaiser::Config.config).to receive(:[]).with(:envnames).and_return(envs)

obj = described_class.new
allow(obj).to receive(:current_branch).and_return('master')


allow(kaiserfile).to receive(:platform).and_return('')

expect(obj.build_cmd[0]).to eq "docker build"
expect(obj.build_cmd[1]).to eq "-t kaiser:meow-master"
expect(obj.build_cmd[2]).to eq "-f /meow-dockerfile somedir"
expect(obj.build_cmd[3].to_s).to eq ""
end

it "generates the appropriate build platform" do
kaiserfile = double(Kaiser::Kaiserfile)
kaiserconfig = double(Kaiser::Config)

allow(Kaiser::Config).to receive(:kaiserfile).and_return(kaiserfile)
allow(Kaiser::Config).to receive(:config).and_return(kaiserconfig)

allow(Kaiser::Config).to receive(:work_dir).and_return('somedir')

envs = double("Hash")
allow(envs).to receive(:[]).with('somedir').and_return('meow')

allow(kaiserfile).to receive(:platform).and_return('linux/catcpu')

allow(Kaiser::Config.kaiserfile).to receive(:docker_build_args).and_return({})
allow(Kaiser::Config.config).to receive(:[]).with(:envnames).and_return(envs)

obj = described_class.new
allow(obj).to receive(:current_branch).and_return('master')

allow(kaiserfile).to receive(:platform).and_return('linux/catcpu')

expect(obj.build_cmd[0]).to eq "docker build"
expect(obj.build_cmd[1]).to eq "-t kaiser:meow-master"
expect(obj.build_cmd[2]).to eq "-f /meow-dockerfile somedir"
expect(obj.build_cmd[3]).to eq "--platform=linux/catcpu"
end

end
end
20 changes: 20 additions & 0 deletions spec/kaiserfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@
end
end

context '#force_platform' do
context 'when force_platform is specified' do
let(:kaiserfile_contents) { "force_platform 'catcpu'" }

it 'sets platform' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.platform).to eq('catcpu')
end
end

context 'when force_platform is not specified' do
let(:kaiserfile_contents) { "" }

it 'platform is nil' do
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
expect(kaiserfile.platform).to be_nil
end
end
end

context '#service' do
context 'when service is specified' do
let(:kaiserfile_contents) { "service 'santaclaus'" }
Expand Down
2 changes: 1 addition & 1 deletion spec/plugins/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
kaiserfile = Kaiser::Kaiserfile.new('Kaiserfile')
allow(kaiserfile).to receive(:require).with('kaiser/databases/somebase') { true }

expect(db_class).to receive(:new) { db_driver }.with(hello: :meow)
expect(db_class).to receive(:new) { db_driver }.with({hello: :meow})
expect(db_driver).to receive(:options_hash) { { port: 1234 } }
expect(db_driver).to receive(:image_name) { '' }
expect(kaiserfile).to receive(:db).with('', port: 1234)
Expand Down

0 comments on commit c394082

Please sign in to comment.