From a06ecc5a9518ff1ee1e81c51fd4e341ad3a9279a Mon Sep 17 00:00:00 2001 From: Matthias Baur Date: Tue, 14 Jan 2020 14:29:22 +0100 Subject: [PATCH] move-to-concat - Move runner configuration from exec to concat::fragment This uses the foundation laid in e99ffe0a9bbf9636d390e28ce62d9d44c255dd39 and moves the runner configuration from execs to concat::fragments. This was done to be able to change (parts) of the runner configuration after the inital Puppet run which is not possible with the exec because there is no config update logic in the gitlab-runner binary. Sadly this drops the support for autoregistering a runner on a Gitlab instance as discussed in https://github.com/voxpupuli/puppet-gitlab_ci_runner/issues/18. The registration can be done with Bolt task provide in https://github.com/voxpupuli/puppet-gitlab_ci_runner/pull/73. --- .sync.yml | 3 + REFERENCE.md | 88 ++++++++----- manifests/init.pp | 3 - manifests/runner.pp | 116 +++++++++------- spec/acceptance/class_spec.rb | 130 ++++++++++++++++++ spec/acceptance/gitlab_ci_runner.rb | 30 ----- spec/acceptance/runner_spec.rb | 130 ++++++++++++++++++ spec/classes/gitlab_ci_runner_spec.rb | 32 ----- spec/defines/runner_spec.rb | 182 +++++++++++++++++--------- spec/spec_helper.rb | 2 + spec/spec_helper_local.rb | 6 + 11 files changed, 516 insertions(+), 206 deletions(-) create mode 100644 spec/acceptance/class_spec.rb delete mode 100644 spec/acceptance/gitlab_ci_runner.rb create mode 100644 spec/acceptance/runner_spec.rb create mode 100644 spec/spec_helper_local.rb diff --git a/.sync.yml b/.sync.yml index b1abc44..e80eeef 100644 --- a/.sync.yml +++ b/.sync.yml @@ -13,3 +13,6 @@ Gemfile: optional: ':test': - gem: 'toml-rb' +spec/spec_helper.rb: + spec_overrides: + - require 'spec_helper_local' diff --git a/REFERENCE.md b/REFERENCE.md index 0fe5228..673f89d 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -18,7 +18,7 @@ _Private Classes_ **Defined types** -* [`gitlab_ci_runner::runner`](#gitlab_ci_runnerrunner): This module installs and configures Gitlab CI Runners. +* [`gitlab_ci_runner::runner`](#gitlab_ci_runnerrunner): This configures a Gitlab CI runner. **Data types** @@ -181,18 +181,67 @@ Default value: '/etc/gitlab-runner/config.toml' ### gitlab_ci_runner::runner -This module installs and configures Gitlab CI Runners. +This configures a Gitlab CI runner. #### Examples -##### Simple runner registration +##### Add a simple runner ```puppet -gitlab_ci_runner::runner { example_runner: +gitlab_ci_runner::runner { 'testrunner': + config => { + 'url' => 'https://gitlab.com', + 'token' => '123456789abcdefgh', # Note this is different from the registration token used by `gitlab-runner register` + 'executor' => 'shell', + }, +} +``` + +##### Add a autoscaling runner with DigitalOcean as IaaS + +```puppet +gitlab_ci_runner::runner { 'autoscale-runner': config => { - 'registration-token' => 'gitlab-token', - 'url' => 'https://gitlab.com', - 'tag-list' => 'docker,aws', + url => 'https://gitlab.com', + token => 'RUNNER_TOKEN', # Note this is different from the registration token used by `gitlab-runner register` + name => 'autoscale-runner', + executor => 'docker+machine', + limit => 10, + docker => { + image => 'ruby:2.6', + }, + machine => { + OffPeakPeriods => [ + '* * 0-9,18-23 * * mon-fri *', + '* * * * * sat,sun *', + ], + OffPeakIdleCount => 1, + OffPeakIdleTime => 1200, + IdleCount => 5, + IdleTime => 600, + MaxBuilds => 100, + MachineName => 'auto-scale-%s', + MachineDriver => 'digitalocean', + MachineOptions => [ + 'digitalocean-image=coreos-stable', + 'digitalocean-ssh-user=core', + 'digitalocean-access-token=DO_ACCESS_TOKEN', + 'digitalocean-region=nyc2', + 'digitalocean-size=4gb', + 'digitalocean-private-networking', + 'engine-registry-mirror=http://10.11.12.13:12345', + ], + }, + cache => { + 'Type' => 's3', + s3 => { + ServerAddress => 's3-eu-west-1.amazonaws.com', + AccessKey => 'AMAZON_S3_ACCESS_KEY', + SecretKey => 'AMAZON_S3_SECRET_KEY', + BucketName => 'runner', + Insecure => false, + }, + }, }, } ``` @@ -207,30 +256,7 @@ Data type: `Hash` Hash with configuration options. See https://docs.gitlab.com/runner/configuration/advanced-configuration.html for all possible options. - -##### `ensure` - -Data type: `Enum['present', 'absent']` - -If the runner should be 'present' or 'absent'. Will register/unregister the runner from Gitlab. - -Default value: 'present' - -##### `runner_name` - -Data type: `String[1]` - -The name of the runner. - -Default value: $title - -##### `binary` - -Data type: `String[1]` - -The name of the Gitlab runner binary. - -Default value: 'gitlab-runner' +If you omit the 'name' configuration, we will automatically use the $title of this define class. ## Data types diff --git a/manifests/init.pp b/manifests/init.pp index 981b03e..08cdcae 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -94,16 +94,13 @@ $runners.each |$runner_name,$config| { $_config = merge($runner_defaults, $config) - $ensure = $_config['ensure'] $title = $_config['name'] ? { undef => $runner_name, default => $_config['name'], } gitlab_ci_runner::runner { $title: - ensure => $ensure, config => $_config - ['ensure', 'name'], - binary => $package_name, require => Class['gitlab_ci_runner::config'], notify => Class['gitlab_ci_runner::service'], } diff --git a/manifests/runner.pp b/manifests/runner.pp index dc6f894..109207a 100644 --- a/manifests/runner.pp +++ b/manifests/runner.pp @@ -1,65 +1,83 @@ -# @summary This module installs and configures Gitlab CI Runners. +# @summary This configures a Gitlab CI runner. # -# @example Simple runner registration -# gitlab_ci_runner::runner { example_runner: +# @example Add a simple runner +# gitlab_ci_runner::runner { 'testrunner': +# config => { +# 'url' => 'https://gitlab.com', +# 'token' => '123456789abcdefgh', # Note this is different from the registration token used by `gitlab-runner register` +# 'executor' => 'shell', +# }, +# } +# +# @example Add a autoscaling runner with DigitalOcean as IaaS +# gitlab_ci_runner::runner { 'autoscale-runner': # config => { -# 'registration-token' => 'gitlab-token', -# 'url' => 'https://gitlab.com', -# 'tag-list' => 'docker,aws', +# url => 'https://gitlab.com', +# token => 'RUNNER_TOKEN', # Note this is different from the registration token used by `gitlab-runner register` +# name => 'autoscale-runner', +# executor => 'docker+machine', +# limit => 10, +# docker => { +# image => 'ruby:2.6', +# }, +# machine => { +# OffPeakPeriods => [ +# '* * 0-9,18-23 * * mon-fri *', +# '* * * * * sat,sun *', +# ], +# OffPeakIdleCount => 1, +# OffPeakIdleTime => 1200, +# IdleCount => 5, +# IdleTime => 600, +# MaxBuilds => 100, +# MachineName => 'auto-scale-%s', +# MachineDriver => 'digitalocean', +# MachineOptions => [ +# 'digitalocean-image=coreos-stable', +# 'digitalocean-ssh-user=core', +# 'digitalocean-access-token=DO_ACCESS_TOKEN', +# 'digitalocean-region=nyc2', +# 'digitalocean-size=4gb', +# 'digitalocean-private-networking', +# 'engine-registry-mirror=http://10.11.12.13:12345', +# ], +# }, +# cache => { +# 'Type' => 's3', +# s3 => { +# ServerAddress => 's3-eu-west-1.amazonaws.com', +# AccessKey => 'AMAZON_S3_ACCESS_KEY', +# SecretKey => 'AMAZON_S3_SECRET_KEY', +# BucketName => 'runner', +# Insecure => false, +# }, +# }, # }, # } # # @param config # Hash with configuration options. # See https://docs.gitlab.com/runner/configuration/advanced-configuration.html for all possible options. -# @param ensure -# If the runner should be 'present' or 'absent'. Will register/unregister the runner from Gitlab. -# @param runner_name -# The name of the runner. -# @param binary -# The name of the Gitlab runner binary. +# If you omit the 'name' configuration, we will automatically use the $title of this define class. # define gitlab_ci_runner::runner ( - Hash $config, - Enum['present', 'absent'] $ensure = 'present', - String[1] $runner_name = $title, - String[1] $binary = 'gitlab-runner', + Hash $config, ) { - # Set resource name as name for the runner and replace under_scores for later use - $_name = regsubst($runner_name, '_', '-', 'G') + include gitlab_ci_runner - # To be able to use all parameters as command line arguments, - # we have to transform the configuration into something the gitlab-runner - # binary accepts: - # * Always prefix the options with '--' - # * Always join option names and values with '=' - # - # In the end, flatten thewhole array and join all elements with a space as delimiter - $__config = $config.map |$item| { - # Ensure all keys use '-' instead of '_'. Needed for e.g. build_dir. - $key = regsubst($item[0], '_', '-', 'G') + $config_path = $gitlab_ci_runner::config_path + + # Use title parameter if config hash doesn't contain one. + $_config = $config['name'] ? { + undef => merge($config, { name => $title }), + default => $config, + } - # If the value ($item[1]) is an Array multiple elements are added for each item - if $item[1] =~ Array { - $item[1].map |$nested| { - "--${key}=${nested}" - } - } else { - "--${key}=${item[1]}" - } - }.flatten.join(' ') + $__config = { runners => [ $_config ], } - if $ensure == 'absent' { - # Execute gitlab ci multirunner unregister - exec {"Unregister_runner_${title}": - command => "/usr/bin/${binary} unregister -n ${_name}", - onlyif => "/bin/grep \'${_name}\' /etc/gitlab-runner/config.toml", - } - } else { - # Execute gitlab ci multirunner register - exec {"Register_runner_${title}": - command => "/usr/bin/${binary} register -n ${__config}", - unless => "/bin/grep -F \'${_name}\' /etc/gitlab-runner/config.toml", - } + concat::fragment { "${config_path} - ${title}": + target => $config_path, + order => 2, + content => inline_template("<%= require 'toml-rb'; TomlRB.dump(@__config) %>"), } } diff --git a/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb new file mode 100644 index 0000000..85d09ba --- /dev/null +++ b/spec/acceptance/class_spec.rb @@ -0,0 +1,130 @@ +require 'spec_helper_acceptance' + +describe 'gitlab_ci_runner class' do + context 'default parameters' do + it 'idempotently with no errors' do + pp = <<-EOS + include gitlab_ci_runner + EOS + + shell('/opt/puppetlabs/puppet/bin/gem install toml-rb') + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe package('gitlab-runner') do + it { is_expected.to be_installed } + end + + describe service('gitlab-runner') do + it { is_expected.to be_running } + it { is_expected.to be_enabled } + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain '# MANAGED BY PUPPET' } + end + end + + context 'concurrent => 20' do + it 'idempotently with no errors' do + pp = <<-EOS + class { 'gitlab_ci_runner': + concurrent => 20, + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain 'concurrent = 20' } + end + end + + context 'log_level => error' do + it 'idempotently with no errors' do + pp = <<-EOS + class { 'gitlab_ci_runner': + log_level => 'error', + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain 'log_level = "error"' } + end + end + + context 'log_format => text' do + it 'idempotently with no errors' do + pp = <<-EOS + class { 'gitlab_ci_runner': + log_format => 'text', + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain 'log_format = "text"' } + end + end + + context 'check_interval => 42' do + it 'idempotently with no errors' do + pp = <<-EOS + class { 'gitlab_ci_runner': + check_interval => 42, + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain 'check_interval = 42' } + end + end + + context 'sentry_dsn => https://123abc@localhost/1' do + it 'idempotently with no errors' do + pp = <<-EOS + class { 'gitlab_ci_runner': + sentry_dsn => 'https://123abc@localhost/1', + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain 'sentry_dsn = "https://123abc@localhost/1"' } + end + end + + context 'listen_address => localhost:9252' do + it 'idempotently with no errors' do + pp = <<-EOS + class { 'gitlab_ci_runner': + listen_address => 'localhost:9252', + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain 'listen_address = "localhost:9252"' } + end + end +end diff --git a/spec/acceptance/gitlab_ci_runner.rb b/spec/acceptance/gitlab_ci_runner.rb deleted file mode 100644 index a82551f..0000000 --- a/spec/acceptance/gitlab_ci_runner.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'spec_helper_acceptance' - -describe 'gitlab_ci_runner class' do - context 'default parameters' do - it 'idempotently with no errors' do - pp = <<-EOS - class { 'gitlab_ci_runner': - runners => { - test_runner => {} - }, - runner_defaults => { - url => 'https://git.example.com/ci', - registration-token => '1234567890abcdef', - executor => 'docker', - docker-image => 'ubuntu:trusty' - } - } - EOS - - apply_manifest(pp, catch_failures: true) - apply_manifest(pp, catch_changes: true) - - shell('sleep 15') # give it some time to start up - end - - describe package('gitlab-runner') do - it { is_expected.to be_installed } - end - end -end diff --git a/spec/acceptance/runner_spec.rb b/spec/acceptance/runner_spec.rb new file mode 100644 index 0000000..6eb254b --- /dev/null +++ b/spec/acceptance/runner_spec.rb @@ -0,0 +1,130 @@ +require 'spec_helper_acceptance' + +describe 'gitlab_ci_runner::runner define' do + context 'simple runner' do + it 'idempotently with no errors' do + pp = <<-EOS + gitlab_ci_runner::runner { 'testrunner': + config => { + 'url' => 'https://gitlab.com', + 'token' => '123456789abcdefgh', + 'executor' => 'shell', + }, + } + EOS + + shell('/opt/puppetlabs/puppet/bin/gem install toml-rb') + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe package('gitlab-runner') do + it { is_expected.to be_installed } + end + + describe service('gitlab-runner') do + it { is_expected.to be_running } + it { is_expected.to be_enabled } + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain '[[runners]]' } + it { is_expected.to contain 'name = "testrunner"' } + it { is_expected.to contain 'url = "https://gitlab.com"' } + it { is_expected.to contain 'token = "123456789abcdefgh"' } + it { is_expected.to contain 'executor = "shell"' } + end + end + + context 'autoscaling runner with DigitalOcean as IaaS' do + it 'idempotently with no errors' do + pp = <<-EOS + gitlab_ci_runner::runner { 'autoscale-runner': + config => { + url => 'https://gitlab.com', + token => '123456789abcdefgh', + name => 'autoscale-runner', + executor => 'docker+machine', + limit => 10, + docker => { + image => 'ruby:2.6', + }, + machine => { + 'OffPeakPeriods' => [ + '* * 0-9,18-23 * * mon-fri *', + '* * * * * sat,sun *', + ], + 'OffPeakIdleCount' => 1, + 'OffPeakIdleTime' => 1200, + 'IdleCount' => 5, + 'IdleTime' => 600, + 'MaxBuilds' => 100, + 'MachineName' => 'auto-scale-%s', + 'MachineDriver' => 'digitalocean', + 'MachineOptions' => [ + 'digitalocean-image=coreos-stable', + 'digitalocean-ssh-user=core', + 'digitalocean-access-token=DO_ACCESS_TOKEN', + 'digitalocean-region=nyc2', + 'digitalocean-size=4gb', + 'digitalocean-private-networking', + 'engine-registry-mirror=http://10.11.12.13:12345', + ], + }, + cache => { + 'Type' => 's3', + s3 => { + 'ServerAddress' => 's3-eu-west-1.amazonaws.com', + 'AccessKey' => 'AMAZON_S3_ACCESS_KEY', + 'SecretKey' => 'AMAZON_S3_SECRET_KEY', + 'BucketName' => 'runner', + 'Insecure' => false, + }, + }, + }, + } + EOS + + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe package('gitlab-runner') do + it { is_expected.to be_installed } + end + + describe service('gitlab-runner') do + it { is_expected.to be_running } + it { is_expected.to be_enabled } + end + + describe file('/etc/gitlab-runner/config.toml') do + it { is_expected.to contain '[[runners]]' } + it { is_expected.to contain 'url = "https://gitlab.com"' } + it { is_expected.to contain 'token = "123456789abcdefgh"' } + it { is_expected.to contain 'name = "autoscale-runner"' } + it { is_expected.to contain 'executor = "docker+machine"' } + it { is_expected.to contain 'limit = 10' } + it { is_expected.to contain '[runners.docker]' } + it { is_expected.to contain 'image = "ruby:2.6"' } + it { is_expected.to contain '[runners.machine]' } + it { is_expected.to contain 'OffPeakPeriods = ["* * 0-9,18-23 * * mon-fri *", "* * * * * sat,sun *"]' } + it { is_expected.to contain 'OffPeakIdleCount = 1' } + it { is_expected.to contain 'OffPeakIdleTime = 1200' } + it { is_expected.to contain 'IdleCount = 5' } + it { is_expected.to contain 'IdleTime = 600' } + it { is_expected.to contain 'MaxBuilds = 100' } + it { is_expected.to contain 'MachineName = "auto-scale-%s"' } + it { is_expected.to contain 'MachineDriver = "digitalocean"' } + it { is_expected.to contain 'MachineOptions = ["digitalocean-image=coreos-stable", "digitalocean-ssh-user=core", "digitalocean-access-token=DO_ACCESS_TOKEN", "digitalocean-region=nyc2", "digitalocean-size=4gb", "digitalocean-private-networking", "engine-registry-mirror=http://10.11.12.13:12345"]' } + it { is_expected.to contain '[runners.cache]' } + it { is_expected.to contain 'Type = "s3"' } + it { is_expected.to contain '[runners.cache.s3]' } + it { is_expected.to contain 'ServerAddress = "s3-eu-west-1.amazonaws.com"' } + it { is_expected.to contain 'AccessKey = "AMAZON_S3_ACCESS_KEY"' } + it { is_expected.to contain 'SecretKey = "AMAZON_S3_SECRET_KEY"' } + it { is_expected.to contain 'BucketName = "runner"' } + it { is_expected.to contain 'Insecure = false' } + end + end +end diff --git a/spec/classes/gitlab_ci_runner_spec.rb b/spec/classes/gitlab_ci_runner_spec.rb index a20655b..a61cdce 100644 --- a/spec/classes/gitlab_ci_runner_spec.rb +++ b/spec/classes/gitlab_ci_runner_spec.rb @@ -155,38 +155,6 @@ end end - context 'with ensure => present' do - let(:params) do - super().merge( - 'runners' => { - 'test_runner' => { - 'ensure' => 'present' - } - } - ) - end - - it { is_expected.to contain_gitlab_ci_runner__runner('test_runner') } - it { is_expected.to contain_exec('Register_runner_test_runner').with('command' => %r{/usr/bin/[^ ]+ register }) } - it { is_expected.not_to contain_exec('Register_runner_test_runner').with('command' => %r{--ensure=}) } - end - - context 'with ensure => absent' do - let(:params) do - super().merge( - 'runners' => { - 'test_runner' => { - 'ensure' => 'absent' - } - } - ) - end - - it { is_expected.to contain_gitlab_ci_runner__runner('test_runner') } - it { is_expected.to contain_exec('Unregister_runner_test_runner').with('command' => %r{/usr/bin/[^ ]+ unregister }) } - it { is_expected.not_to contain_exec('Unregister_runner_test_runner').with('command' => %r{--ensure=}) } - end - # puppetlabs-docker doesn't support CentOS 6 anymore. unless facts[:os]['name'] == 'CentOS' && facts[:os]['release']['major'] == '6' context 'with manage_docker => true' do diff --git a/spec/defines/runner_spec.rb b/spec/defines/runner_spec.rb index 7fdaac0..58dad67 100644 --- a/spec/defines/runner_spec.rb +++ b/spec/defines/runner_spec.rb @@ -5,104 +5,164 @@ context "on #{os}" do let(:facts) { facts } let(:title) { 'testrunner' } - let(:default_params) do - { - config: { - 'registration-token' => 'gitlab-token', - 'url' => 'https://gitlab.com' - } - } - end - context 'with default params' do - let(:params) { default_params } + context 'with simple shell runner' do + let(:params) do + { + config: { + url: 'https://gitlab.com', + token: '123456789abcdefgh', + executor: 'shell' + } + } + end it { is_expected.to compile.with_all_deps } it do - is_expected.to contain_exec('Register_runner_testrunner').with( - command: '/usr/bin/gitlab-runner register -n --registration-token=gitlab-token --url=https://gitlab.com', - unless: "/bin/grep -F 'testrunner' /etc/gitlab-runner/config.toml" + verify_concat_fragment_exact_contents( + catalogue, + '/etc/gitlab-runner/config.toml - testrunner', + [ + '[[runners]]', + 'name = "testrunner"', + 'url = "https://gitlab.com"', + 'token = "123456789abcdefgh"', + 'executor = "shell"' + ] ) end end - context 'with ensure => absent' do + context 'with autoscaling runner with DigitalOcean as IaaS' do let(:params) do - default_params.merge(ensure: 'absent') + { + config: { + url: 'https://gitlab.com', + token: '123456789abcdefgh', + name: 'autoscale-runner', + executor: 'docker+machine', + limit: 10, + docker: { + image: 'ruby:2.6' + }, + machine: { + OffPeakPeriods: [ + '* * 0-9,18-23 * * mon-fri *', + '* * * * * sat,sun *' + ], + OffPeakIdleCount: 1, + OffPeakIdleTime: 1200, + IdleCount: 5, + IdleTime: 600, + MaxBuilds: 100, + MachineName: 'auto-scale-%s', + MachineDriver: 'digitalocean', + MachineOptions: [ + 'digitalocean-image=coreos-stable', + 'digitalocean-ssh-user=core', + 'digitalocean-access-token=DO_ACCESS_TOKEN', + 'digitalocean-region=nyc2', + 'digitalocean-size=4gb', + 'digitalocean-private-networking', + 'engine-registry-mirror=http://10.11.12.13:12345' + ] + }, + cache: { + Type: 's3', + s3: { + ServerAddress: 's3-eu-west-1.amazonaws.com', + AccessKey: 'AMAZON_S3_ACCESS_KEY', + SecretKey: 'AMAZON_S3_SECRET_KEY', + BucketName: 'runner', + Insecure: false + } + } + } + } end it { is_expected.to compile.with_all_deps } it do - is_expected.to contain_exec('Unregister_runner_testrunner').with( - command: '/usr/bin/gitlab-runner unregister -n testrunner', - onlyif: "/bin/grep 'testrunner' /etc/gitlab-runner/config.toml" + verify_concat_fragment_exact_contents( + catalogue, + '/etc/gitlab-runner/config.toml - testrunner', + [ + '[[runners]]', + 'url = "https://gitlab.com"', + 'token = "123456789abcdefgh"', + 'name = "autoscale-runner"', + 'executor = "docker+machine"', + 'limit = 10', + '[runners.docker]', + 'image = "ruby:2.6"', + '[runners.machine]', + 'OffPeakPeriods = ["* * 0-9,18-23 * * mon-fri *", "* * * * * sat,sun *"]', + 'OffPeakIdleCount = 1', + 'OffPeakIdleTime = 1200', + 'IdleCount = 5', + 'IdleTime = 600', + 'MaxBuilds = 100', + 'MachineName = "auto-scale-%s"', + 'MachineDriver = "digitalocean"', + 'MachineOptions = ["digitalocean-image=coreos-stable", "digitalocean-ssh-user=core", "digitalocean-access-token=DO_ACCESS_TOKEN", "digitalocean-region=nyc2", "digitalocean-size=4gb", "digitalocean-private-networking", "engine-registry-mirror=http://10.11.12.13:12345"]', + '[runners.cache]', + 'Type = "s3"', + '[runners.cache.s3]', + 'ServerAddress = "s3-eu-west-1.amazonaws.com"', + 'AccessKey = "AMAZON_S3_ACCESS_KEY"', + 'SecretKey = "AMAZON_S3_SECRET_KEY"', + 'BucketName = "runner"', + 'Insecure = false' + ] ) end end - context 'with runner_name/title including a _' do - let(:title) { 'test_runner' } - let(:params) { default_params } - - context 'with ensure => present' do - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_exec('Register_runner_test_runner').with_unless(%r{^/bin/grep -F 'test-runner'}) } - end - - context 'with ensure => absent' do - let(:params) do - super().merge(ensure: 'absent') - end - - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_exec('Unregister_runner_test_runner').with_onlyif(%r{^/bin/grep 'test-runner'}) } - end - end - - context 'with binary => special-gitlab-runner' do - let(:params) { default_params.merge(binary: 'special-gitlab-runner') } - - context 'with ensure => present' do - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_exec('Register_runner_testrunner').with_command(%r{^/usr/bin/special-gitlab-runner}) } - end - - context 'with ensure => absent' do - let(:params) do - super().merge(ensure: 'absent') - end - - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_exec('Unregister_runner_testrunner').with_command(%r{^/usr/bin/special-gitlab-runner}) } - end - end - - context 'with config having a key containing _' do + context 'with name not included in config' do let(:params) do { config: { - build_dir: '/tmp' } } end it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_exec('Register_runner_testrunner').with_command(%r{--build-dir=/tmp}) } + + it do + verify_concat_fragment_exact_contents( + catalogue, + '/etc/gitlab-runner/config.toml - testrunner', + [ + '[[runners]]', + 'name = "testrunner"' + ] + ) + end end - context 'with config having a key which has an array as value' do + context 'with name included in config' do let(:params) do { config: { - 'docker-volumes' => ['test0:/test0', 'test1:/test1'] + name: 'foo-runner' } } end it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_exec('Register_runner_testrunner').with_command(%r{--docker-volumes=test0:/test0 --docker-volumes=test1:/test1}) } + + it do + verify_concat_fragment_exact_contents( + catalogue, + '/etc/gitlab-runner/config.toml - testrunner', + [ + '[[runners]]', + 'name = "foo-runner"' + ] + ) + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 93f417a..b016665 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -59,3 +59,5 @@ RSpec::Puppet::Coverage.report! end end + +require 'spec_helper_local' diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb new file mode 100644 index 0000000..4987b1b --- /dev/null +++ b/spec/spec_helper_local.rb @@ -0,0 +1,6 @@ +# Taken from https://github.com/theforeman/puppet-puppet/blob/143199e1f529581f138fcd9c8edea2990ea4a69c/spec/spec_helper.rb +def verify_concat_fragment_exact_contents(subject, title, expected_lines) + is_expected.to contain_concat__fragment(title) + content = subject.resource('concat::fragment', title).send(:parameters)[:content] + expect(content.split(%r{\n}).reject { |line| line =~ %r{(^#|^$|^\s+#)} }).to match_array(expected_lines) +end