Skip to content

Commit

Permalink
Rewrite tests to only verify the thing they care about
Browse files Browse the repository at this point in the history
Ideally this should make the tests less brittle, where as a single change would mean updating 17-20 different hashes.
  • Loading branch information
zzak authored and rafaelfranca committed Apr 15, 2024
1 parent 9cf1d86 commit 7ddf9b4
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 467 deletions.
10 changes: 0 additions & 10 deletions test/buildkite_config/test_annotate.rb

This file was deleted.

179 changes: 116 additions & 63 deletions test/buildkite_config/test_docker_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "buildkite_config"

class TestDockerBuild < TestCase
def test_builder
def test_builder_with_ruby_config_using_string_version
pipeline = PipelineFixture.new do
use Buildkite::Config::DockerBuild

Expand All @@ -13,37 +13,112 @@ def test_builder
end
end

expected = { "steps" =>
[{ "label" => ":docker: builder:3.2",
"key" => "docker-image-builder-3-2",
"agents" => { "queue" => "builder" },
"env" =>
{ "BUNDLER" => nil,
"RUBYGEMS" => nil,
"RUBY_IMAGE" => "builder:3.2",
"encrypted_0fb9444d0374_key" => nil,
"encrypted_0fb9444d0374_iv" => nil },
"timeout_in_minutes" => 15,
"plugins" =>
[{ "artifacts#v1.0" => { "download" => ".dockerignore" } },
{ "artifacts#v1.0" =>
{ "download" =>
[".buildkite/.empty",
".buildkite/docker-compose.yml",
".buildkite/Dockerfile",
".buildkite/Dockerfile.beanstalkd",
".buildkite/mysql-initdb.d",
".buildkite/runner"],
"compressed" => ".buildkite.tgz" } },
{ "docker-compose#v1.0" =>
{ "build" => "base",
"config" => ".buildkite/docker-compose.yml",
"env" => ["PRE_STEPS", "RACK"],
"image-name" => "builder-3-2-local",
"cache-from" => ["base:buildkite-config-base:builder-3-2-br-main"],
"push" => ["base:buildkite-config-base:builder-3-2-br-"],
"image-repository" => "buildkite-config-base" } }] }] }
assert_equal expected, pipeline.to_h
%w[label key env].each do |key|
assert_includes pipeline.to_h["steps"][0], key
end

assert_equal ":docker: builder:3.2", pipeline.to_h["steps"][0]["label"]
assert_equal "docker-image-builder-3-2", pipeline.to_h["steps"][0]["key"]
assert_equal "builder:3.2", pipeline.to_h["steps"][0]["env"]["RUBY_IMAGE"]
end

def test_builder_artifacts
pipeline = PipelineFixture.new do
use Buildkite::Config::DockerBuild

build_context.stub(:rails_version, Gem::Version.new("7.1")) do
builder Buildkite::Config::RubyConfig.new(version: "3.2")
end
end

assert_includes pipeline.to_h["steps"][0], "plugins"
plugins = pipeline.to_h["steps"][0]["plugins"]

artifacts = plugins.select { |plugin|
plugin.key?("artifacts#v1.0")
}
assert_equal ".dockerignore", artifacts[0]["artifacts#v1.0"]["download"]

download = artifacts[1]["artifacts#v1.0"]
assert_equal %w[
.buildkite/.empty
.buildkite/docker-compose.yml
.buildkite/Dockerfile
.buildkite/Dockerfile.beanstalkd
.buildkite/mysql-initdb.d
.buildkite/runner
], download["download"]
assert_equal ".buildkite.tgz", download["compressed"]
end

def test_builder_compose_plugin
pipeline = PipelineFixture.new do
use Buildkite::Config::DockerBuild

build_context.stub(:rails_version, Gem::Version.new("7.1")) do
builder Buildkite::Config::RubyConfig.new(version: "3.2")
end
end

plugins = pipeline.to_h["steps"][0]["plugins"]

compose = plugins.find { |plugin|
plugin.key?("docker-compose#v1.0")
}.fetch("docker-compose#v1.0")

%w[image-name cache-from push build config env image-repository].each do |key|
assert_includes compose, key
end

assert_equal "3-2-local", compose["image-name"]
assert_equal ["base:buildkite-config-base:3-2-br-main"], compose["cache-from"]
assert_equal ["base:buildkite-config-base:3-2-br-"], compose["push"]

assert_equal "base", compose["build"]
assert_equal ".buildkite/docker-compose.yml", compose["config"]
assert_includes compose["env"], "PRE_STEPS"
assert_includes compose["env"], "RACK"
assert_equal "buildkite-config-base", compose["image-repository"]
end

def test_builder_timeout_default
pipeline = PipelineFixture.new do
use Buildkite::Config::DockerBuild

build_context.stub(:rails_version, Gem::Version.new("7.1")) do
builder Buildkite::Config::RubyConfig.new(version: "3.2")
end
end

assert_includes pipeline.to_h["steps"][0], "timeout_in_minutes"
assert_equal 15, pipeline.to_h["steps"][0]["timeout_in_minutes"]
end

def test_builder_agents_default
pipeline = PipelineFixture.new do
use Buildkite::Config::DockerBuild

build_context.stub(:rails_version, Gem::Version.new("7.1")) do
builder Buildkite::Config::RubyConfig.new(version: "3.2")
end
end

assert_includes pipeline.to_h["steps"][0], "agents"
assert_equal({ "queue" => "builder" }, pipeline.to_h["steps"][0]["agents"])
end

def test_builder_env_default
pipeline = PipelineFixture.new do
use Buildkite::Config::DockerBuild

build_context.stub(:rails_version, Gem::Version.new("7.1")) do
builder Buildkite::Config::RubyConfig.new(version: "3.2")
end
end
assert_includes pipeline.to_h["steps"][0]["env"], "BUNDLER"
assert_includes pipeline.to_h["steps"][0]["env"], "RUBYGEMS"
assert_includes pipeline.to_h["steps"][0]["env"], "encrypted_0fb9444d0374_key"
assert_includes pipeline.to_h["steps"][0]["env"], "encrypted_0fb9444d0374_iv"
end

def test_builder_skip
Expand All @@ -68,36 +143,14 @@ def test_builder_gem_version
end
end

expected = { "steps" =>
[{ "label" => ":docker: ruby:1.9.3",
"key" => "docker-image-ruby-1-9-3",
"agents" => { "queue" => "builder" },
"env" =>
{ "BUNDLER" => nil,
"RUBYGEMS" => nil,
"RUBY_IMAGE" => "ruby:1.9.3",
"encrypted_0fb9444d0374_key" => nil,
"encrypted_0fb9444d0374_iv" => nil },
"timeout_in_minutes" => 15,
"plugins" =>
[{ "artifacts#v1.0" => { "download" => ".dockerignore" } },
{ "artifacts#v1.0" =>
{ "download" =>
[".buildkite/.empty",
".buildkite/docker-compose.yml",
".buildkite/Dockerfile",
".buildkite/Dockerfile.beanstalkd",
".buildkite/mysql-initdb.d",
".buildkite/runner"],
"compressed" => ".buildkite.tgz" } },
{ "docker-compose#v1.0" =>
{ "build" => "base",
"config" => ".buildkite/docker-compose.yml",
"env" => ["PRE_STEPS", "RACK"],
"image-name" => "ruby-1-9-3-local",
"cache-from" => ["base:buildkite-config-base:ruby-1-9-3-br-main"],
"push" => ["base:buildkite-config-base:ruby-1-9-3-br-"],
"image-repository" => "buildkite-config-base" } }] }] }
assert_equal expected, pipeline.to_h
plugins = pipeline.to_h["steps"][0]["plugins"]

compose = plugins.find { |plugin|
plugin.key?("docker-compose#v1.0")
}.fetch("docker-compose#v1.0")

assert_equal "ruby-1-9-3-local", compose["image-name"]
assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-main"], compose["cache-from"]
assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-"], compose["push"]
end
end
Loading

0 comments on commit 7ddf9b4

Please sign in to comment.