|
| 1 | +require 'spec_helper' |
| 2 | +require 'gitlab/docker_helper' |
| 3 | + |
| 4 | +RSpec.describe DockerHelper do |
| 5 | + describe '.authenticate' do |
| 6 | + before do |
| 7 | + stdin_mock = double(puts: true, close: true) |
| 8 | + stdout_mock = double(read: true) |
| 9 | + stderr_mock = double(read: true) |
| 10 | + wait_thr_mock = double(value: double(success?: true)) |
| 11 | + |
| 12 | + allow(Open3).to receive(:popen3).with({}, "docker", "login", any_args).and_yield(stdin_mock, stdout_mock, stderr_mock, wait_thr_mock) |
| 13 | + end |
| 14 | + |
| 15 | + context 'when a registry is not specified' do |
| 16 | + it 'runs the command to login to docker.io' do |
| 17 | + expect(Open3).to receive(:popen3).with({}, "docker", "login", "--username=dummy-username", "--password-stdin", "") |
| 18 | + |
| 19 | + described_class.authenticate(username: 'dummy-username', password: 'dummy-password') |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'when a registry is specified' do |
| 24 | + it 'runs the command to login to specified registry' do |
| 25 | + expect(Open3).to receive(:popen3).with({}, "docker", "login", "--username=dummy-username", "--password-stdin", "registry.gitlab.com") |
| 26 | + |
| 27 | + described_class.authenticate(username: 'dummy-username', password: 'dummy-password', registry: 'registry.gitlab.com') |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe '.build' do |
| 33 | + shared_examples 'docker build command invocation' do |
| 34 | + end |
| 35 | + |
| 36 | + before do |
| 37 | + stdout_stderr_mock = double(gets: nil) |
| 38 | + status_mock = double(value: double(success?: true)) |
| 39 | + |
| 40 | + allow(described_class).to receive(:create_builder).and_return(true) |
| 41 | + |
| 42 | + allow(Open3).to receive(:popen2e).with("docker", "buildx", "build", any_args).and_yield(nil, stdout_stderr_mock, status_mock) |
| 43 | + end |
| 44 | + |
| 45 | + context 'when a single platform is specified' do |
| 46 | + context 'when push is not explicitly disabled' do |
| 47 | + let(:expected_args) { %w[docker buildx build /tmp/foo -t sample:value --platform=linux/amd64 --push] } |
| 48 | + |
| 49 | + it 'calls docker build command with correct arguments' do |
| 50 | + expect(Open3).to receive(:popen2e).with(*expected_args) |
| 51 | + |
| 52 | + described_class.build('/tmp/foo', 'sample', 'value') |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'when push is explicitly disabled' do |
| 57 | + let(:expected_args) { %w[docker buildx build /tmp/foo -t sample:value --platform=linux/amd64] } |
| 58 | + |
| 59 | + it 'calls docker build command with correct arguments' do |
| 60 | + expect(Open3).to receive(:popen2e).with(*expected_args) |
| 61 | + |
| 62 | + described_class.build('/tmp/foo', 'sample', 'value', push: false) |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context 'when multiple platforms are specified via env vars' do |
| 68 | + before do |
| 69 | + stub_env_var('DOCKER_BUILD_PLATFORMS', 'linux/arm64') |
| 70 | + end |
| 71 | + |
| 72 | + let(:expected_args) { %w[docker buildx build /tmp/foo -t sample:value --platform=linux/amd64,linux/arm64 --push] } |
| 73 | + |
| 74 | + it 'calls docker build command with correct arguments' do |
| 75 | + expect(Open3).to receive(:popen2e).with(*expected_args) |
| 76 | + |
| 77 | + described_class.build('/tmp/foo', 'sample', 'value') |
| 78 | + end |
| 79 | + |
| 80 | + context 'even if push is explicitly disabled' do |
| 81 | + let(:expected_args) { %w[docker buildx build /tmp/foo -t sample:value --platform=linux/amd64,linux/arm64 --push] } |
| 82 | + |
| 83 | + it 'calls docker build command with correct arguments' do |
| 84 | + expect(Open3).to receive(:popen2e).with(*expected_args) |
| 85 | + |
| 86 | + described_class.build('/tmp/foo', 'sample', 'value', push: false) |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + context 'when build_args are specified' do |
| 92 | + let(:expected_args) { %w[docker buildx build /tmp/foo -t sample:value --platform=linux/amd64 --push --build-arg='FOO=BAR'] } |
| 93 | + |
| 94 | + it 'calls docker build command with correct arguments' do |
| 95 | + expect(Open3).to receive(:popen2e).with(*expected_args) |
| 96 | + |
| 97 | + described_class.build('/tmp/foo', 'sample', 'value', buildargs: ["FOO=BAR"]) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe '.create_builder' do |
| 103 | + before do |
| 104 | + allow(described_class).to receive(:cleanup_existing_builder).and_return(true) |
| 105 | + |
| 106 | + stdout_stderr_mock = double(gets: nil) |
| 107 | + status_mock = double(value: double(success?: true)) |
| 108 | + allow(Open3).to receive(:popen2e).with("docker", "buildx", "create", any_args).and_return([nil, stdout_stderr_mock, status_mock]) |
| 109 | + end |
| 110 | + |
| 111 | + it 'calls docker buildx create command with correct arguments' do |
| 112 | + expect(Open3).to receive(:popen2e).with(*%w[docker buildx create --bootstrap --use --name omnibus-gitlab-builder]) |
| 113 | + |
| 114 | + described_class.create_builder |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + describe '.cleanup_existing_builder' do |
| 119 | + context 'when no builder instance exist' do |
| 120 | + before do |
| 121 | + status_mock = double(value: double(success?: false)) |
| 122 | + allow(Open3).to receive(:popen2e).with("docker", "buildx", "ls", any_args).and_return([nil, nil, status_mock]) |
| 123 | + end |
| 124 | + |
| 125 | + it 'does not call docker buildx rm' do |
| 126 | + expect(Open3).not_to receive(:popen2e).with(*%w[docker buildx rm --force omnibus-gitlab-builder]) |
| 127 | + |
| 128 | + described_class.cleanup_existing_builder |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + context 'when builder instance exists' do |
| 133 | + before do |
| 134 | + status_mock = double(value: double(success?: true)) |
| 135 | + allow(Open3).to receive(:popen2e).with("docker", "buildx", "ls", any_args).and_return([nil, nil, status_mock]) |
| 136 | + allow(Open3).to receive(:popen2e).with("docker", "buildx", "rm", any_args).and_return([nil, nil, status_mock]) |
| 137 | + end |
| 138 | + |
| 139 | + it 'calls docker buildx rm command with correct arguments' do |
| 140 | + expect(Open3).to receive(:popen2e).with(*%w[docker buildx rm --force omnibus-gitlab-builder]) |
| 141 | + |
| 142 | + described_class.cleanup_existing_builder |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments