diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5bbf504..a576085 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,16 @@ jobs: strategy: matrix: os: - - "ubuntu-2004" + - "almalinux-8" + - "almalinux-9" + - "amazonlinux-2023" + - "centos-stream-9" + - "debian-12" + - "fedora-latest" + - "rockylinux-8" + - "rockylinux-9" + - "ubuntu-2204" + - "ubuntu-2404" suite: - "default" fail-fast: false @@ -37,8 +46,8 @@ jobs: steps: - name: Check out code uses: actions/checkout@v7 - - name: Install Chef - uses: actionshub/chef-install@6.0.0 + - name: Install Cinc Workstation + uses: sous-chefs/.github/.github/actions/install-workstation@9.0.1 - name: Dokken uses: actionshub/test-kitchen@3.0.0 env: diff --git a/.github/workflows/conventional-commits.yml b/.github/workflows/conventional-commits.yml index 51d2f1b..64f2293 100644 --- a/.github/workflows/conventional-commits.yml +++ b/.github/workflows/conventional-commits.yml @@ -11,4 +11,6 @@ name: conventional-commits jobs: conventional-commits: + permissions: + pull-requests: read uses: sous-chefs/.github/.github/workflows/conventional-commits.yml@9.0.1 diff --git a/.github/workflows/copilot-setup-steps.yml b/.github/workflows/copilot-setup-steps.yml index 91d1e83..bd02e25 100644 --- a/.github/workflows/copilot-setup-steps.yml +++ b/.github/workflows/copilot-setup-steps.yml @@ -21,4 +21,4 @@ jobs: - name: Install Chef uses: actionshub/chef-install@main - name: Install cookbooks - run: berks install + run: chef install Policyfile.rb diff --git a/.github/workflows/prevent-file-change.yml b/.github/workflows/prevent-file-change.yml index 37bc165..7869c99 100644 --- a/.github/workflows/prevent-file-change.yml +++ b/.github/workflows/prevent-file-change.yml @@ -11,6 +11,8 @@ name: prevent-file-change jobs: prevent-file-change: + permissions: + pull-requests: write uses: sous-chefs/.github/.github/workflows/prevent-file-change.yml@9.0.1 secrets: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..6188300 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,2 @@ +require: + - cookstyle diff --git a/Berksfile b/Berksfile deleted file mode 100644 index e09849c..0000000 --- a/Berksfile +++ /dev/null @@ -1,7 +0,0 @@ -source 'https://supermarket.chef.io' - -metadata - -group :integration do - cookbook 'test', path: 'test/fixtures/cookbooks/test' -end diff --git a/Policyfile.rb b/Policyfile.rb new file mode 100644 index 0000000..73b4300 --- /dev/null +++ b/Policyfile.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +name 'github' + +run_list 'test::default' +named_run_list :default, 'test::default' + +cookbook 'github', path: '.' +cookbook 'test', path: 'test/fixtures/cookbooks/test' diff --git a/kitchen.dokken.yml b/kitchen.dokken.yml index 47eff95..a8c3185 100644 --- a/kitchen.dokken.yml +++ b/kitchen.dokken.yml @@ -1,10 +1,14 @@ driver: name: dokken privileged: true - chef_version: <%= ENV['CHEF_VERSION'] || 'current' %> + chef_image: cincproject/cinc + chef_version: <%= ENV['CHEF_VERSION'] || 'latest' %> transport: { name: dokken } -provisioner: { name: dokken } +provisioner: + name: dokken + product_name: cinc + chef_binary: /opt/cinc/bin/cinc-client platforms: - name: almalinux-8 @@ -22,36 +26,11 @@ platforms: image: dokken/amazonlinux-2023 pid_one_command: /usr/lib/systemd/systemd - - name: centos-7 - driver: - image: dokken/centos-7 - pid_one_command: /usr/lib/systemd/systemd - - - name: centos-stream-8 - driver: - image: dokken/centos-stream-8 - pid_one_command: /usr/lib/systemd/systemd - - name: centos-stream-9 driver: image: dokken/centos-stream-9 pid_one_command: /usr/lib/systemd/systemd - - name: debian-9 - driver: - image: dokken/debian-9 - pid_one_command: /bin/systemd - - - name: debian-10 - driver: - image: dokken/debian-10 - pid_one_command: /bin/systemd - - - name: debian-11 - driver: - image: dokken/debian-11 - pid_one_command: /bin/systemd - - name: debian-12 driver: image: dokken/debian-12 @@ -62,26 +41,6 @@ platforms: image: dokken/fedora-latest pid_one_command: /usr/lib/systemd/systemd - - name: opensuse-leap-15 - driver: - image: dokken/opensuse-leap-15 - pid_one_command: /usr/lib/systemd/systemd - - - name: oraclelinux-7 - driver: - image: dokken/oraclelinux-7 - pid_one_command: /usr/lib/systemd/systemd - - - name: oraclelinux-8 - driver: - image: dokken/oraclelinux-8 - pid_one_command: /usr/lib/systemd/systemd - - - name: oraclelinux-9 - driver: - image: dokken/oraclelinux-9 - pid_one_command: /usr/lib/systemd/systemd - - name: rockylinux-8 driver: image: dokken/rockylinux-8 @@ -92,22 +51,12 @@ platforms: image: dokken/rockylinux-9 pid_one_command: /usr/lib/systemd/systemd - - name: ubuntu-18.04 - driver: - image: dokken/ubuntu-18.04 - pid_one_command: /bin/systemd - - - name: ubuntu-20.04 - driver: - image: dokken/ubuntu-20.04 - pid_one_command: /bin/systemd - - name: ubuntu-22.04 driver: image: dokken/ubuntu-22.04 pid_one_command: /bin/systemd - - name: ubuntu-23.04 + - name: ubuntu-24.04 driver: - image: dokken/ubuntu-23.04 + image: dokken/ubuntu-24.04 pid_one_command: /bin/systemd diff --git a/kitchen.yml b/kitchen.yml index 120aea4..9e7c16c 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -3,17 +3,25 @@ driver: provisioner: name: chef_infra - product_name: chef + product_name: cinc enforce_idempotency: true multiple_converge: 2 deprecations_as_errors: true - chef_license: accept-no-persist verifier: name: inspec platforms: - - name: ubuntu-20.04 + - name: almalinux-8 + - name: almalinux-9 + - name: amazonlinux-2023 + - name: centos-stream-9 + - name: debian-12 + - name: fedora-latest + - name: rockylinux-8 + - name: rockylinux-9 + - name: ubuntu-22.04 + - name: ubuntu-24.04 suites: - name: default diff --git a/libraries/github_archive.rb b/libraries/github_archive.rb index 09f1d78..60753b7 100644 --- a/libraries/github_archive.rb +++ b/libraries/github_archive.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'open-uri' require 'uri' require 'fileutils' diff --git a/libraries/github_asset.rb b/libraries/github_asset.rb index ba6cc1b..6e828de 100644 --- a/libraries/github_asset.rb +++ b/libraries/github_asset.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'net/http' require 'open-uri' require 'uri' @@ -6,6 +8,15 @@ module GithubCB class Asset + module JsonSerialization + private + + def sawyer_options + super.merge(serializer: Sawyer::Serializer.json) + end + end + private_constant :JsonSerialization + class << self def asset_cache ::File.join(Chef::Config[:file_cache_path], 'github_assets') @@ -38,15 +49,17 @@ def initialize(fqrn, options = {}) def asset_url(options) require 'octokit' - Octokit.configure do |c| - c.login = options[:user] - c.access_token = options[:token] - end + # Some embedded Ruby runtimes expose Yajl without Sawyer's dump/load API. + client = Octokit::Client.new( + login: options[:user] || false, + access_token: options[:token] || false, + connection_options: Octokit.connection_options.dup + ).extend(JsonSerialization) page = 1 release = nil loop do - releases = Octokit.releases(fqrn, per_page: 100, page: page) + releases = client.releases(fqrn, per_page: 100, page: page) release = releases.find { |r| r.tag_name == tag_name } break if release || releases.empty? diff --git a/libraries/github_errors.rb b/libraries/github_errors.rb index ee507bc..7d588f2 100644 --- a/libraries/github_errors.rb +++ b/libraries/github_errors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module GithubCB class GHError < StandardError; end diff --git a/resources/archive.rb b/resources/archive.rb index 9196b20..4be9b61 100644 --- a/resources/archive.rb +++ b/resources/archive.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + unified_mode true default_action :extract diff --git a/resources/asset.rb b/resources/asset.rb index d3a0d0a..6ce7cdb 100644 --- a/resources/asset.rb +++ b/resources/asset.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + unified_mode true default_action :download diff --git a/resources/deploy.rb b/resources/deploy.rb index 2794715..176f52c 100644 --- a/resources/deploy.rb +++ b/resources/deploy.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + unified_mode true default_action :deploy diff --git a/spec/libraries/github_archive_spec.rb b/spec/libraries/github_archive_spec.rb index ecc8e73..7c1ddda 100644 --- a/spec/libraries/github_archive_spec.rb +++ b/spec/libraries/github_archive_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # require 'spec_helper' # # describe GithubCB::Archive do diff --git a/spec/libraries/github_asset_spec.rb b/spec/libraries/github_asset_spec.rb index f4c3625..b1e13bc 100644 --- a/spec/libraries/github_asset_spec.rb +++ b/spec/libraries/github_asset_spec.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +require 'spec_helper' require 'octokit' require_relative '../../libraries/github_asset' @@ -5,7 +8,7 @@ subject(:asset) { described_class.new(repository, release: 'v1.2.3', name: 'example.zip') } let(:repository) { 'example/project' } - let(:configuration) { Struct.new(:login, :access_token).new } + let(:client) { instance_double(Octokit::Client) } let(:download_url) { 'https://api.github.com/repos/example/project/releases/assets/123' } let(:release_asset) { double('release asset', name: 'example.zip', rels: { self: double(href: download_url) }) } let(:assets_response) { double(data: [release_asset]) } @@ -13,48 +16,66 @@ let(:other_release) { double('other release', tag_name: 'v2.0.0') } before do - allow(Octokit).to receive(:configure).and_yield(configuration) + allow(Octokit::Client).to receive(:new).and_return(client) end it 'returns a matching first-page asset without requesting more pages' do - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).once.and_return([release]) + expect(Octokit::Client).to receive(:new).with(hash_including(login: 'example-user', access_token: 'example-token')).and_return(client) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).once.and_return([release]) expect(asset.asset_url(user: 'example-user', token: 'example-token')).to eq(download_url) - expect(configuration.login).to eq('example-user') - expect(configuration.access_token).to eq('example-token') + end + + it 'uses JSON when the embedded Sawyer default cannot provide a serializer' do + allow(Sawyer::Serializer).to receive(:any_json).and_raise(NameError, 'Yajl does not implement dump') + allow(Octokit).to receive(:instance_variable_get).and_call_original + allow(Octokit).to receive(:instance_variable_get).with(:@login).and_return('other-user') + allow(Octokit).to receive(:instance_variable_get).with(:@access_token).and_return('other-token') + allow(Octokit::Client).to receive(:new).and_wrap_original do |constructor, options| + real_client = constructor.call(options) + allow(real_client).to receive(:releases) do + expect(real_client.login).to be_falsey + expect(real_client.access_token).to be_falsey + expect(real_client.agent.decode_body('{"version":"v1.2.3"}')).to eq(version: 'v1.2.3') + [release] + end + real_client + end + + expect(asset.asset_url({})).to eq(download_url) end it 'finds an older release on a later page' do - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release]) - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([release]) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release]) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([release]) expect(asset.asset_url({})).to eq(download_url) end it 'raises ReleaseNotFound after reaching an empty page' do - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release]) - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([]) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release]) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_return([]) expect { asset.asset_url({}) }.to raise_error(GithubCB::ReleaseNotFound, 'release not found') end it 'raises AssetNotFound when the matching release has no matching asset' do - allow(Octokit).to receive(:releases).and_return([release]) + allow(client).to receive(:releases).and_return([release]) allow(assets_response).to receive(:data).and_return([]) expect { asset.asset_url({}) }.to raise_error(GithubCB::AssetNotFound, 'asset not found') end it 'propagates errors from a later release page' do - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release]) - expect(Octokit).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_raise(Octokit::Unauthorized) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 1).ordered.and_return([other_release]) + expect(client).to receive(:releases).with(repository, per_page: 100, page: 2).ordered.and_raise(Octokit::Unauthorized) expect { asset.asset_url({}) }.to raise_error(Octokit::Unauthorized) end it 'does not discard an authenticated draft or prerelease returned by the API' do allow(release).to receive_messages(draft: true, prerelease: true) - allow(Octokit).to receive(:releases).and_return([release]) + allow(client).to receive(:releases).and_return([release]) expect(asset.asset_url(user: 'example-user', token: 'example-token')).to eq(download_url) end diff --git a/spec/resources/deploy_spec.rb b/spec/resources/deploy_spec.rb new file mode 100644 index 0000000..94b5498 --- /dev/null +++ b/spec/resources/deploy_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'github_deploy' do + step_into :github_deploy + platform 'ubuntu', '22.04' + + context 'deploy action' do + recipe do + github_deploy 'sous-chefs/test-repo' do + version 'v1.0.0' + path '/opt/deployments/test-repo' + owner 'deploy' + group 'deploy' + end + end + + it { is_expected.to create_directory('/opt/deployments/test-repo/releases') } + it { is_expected.to create_directory('/opt/deployments/test-repo/shared') } + it { is_expected.to create_link('/opt/deployments/test-repo/current') } + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8b0212c..7409940 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,37 +1,12 @@ -# require 'rubygems' -# require 'bundler' -# require 'rspec' -# require 'spork' -# require 'chef' -# -# Spork.prefork do -# Dir[File.join(File.expand_path('../spec/support/**/*.rb', __dir__))].each { |f| require f } -# -# RSpec.configure do |config| -# config.expect_with :rspec do |c| -# c.syntax = :expect -# end -# -# config.mock_with :rspec -# config.treat_symbols_as_metadata_keys_with_true_values = true -# config.filter_run focus: true -# config.run_all_when_everything_filtered = true -# -# config.before(:each) do -# Chef::Config[:file_cache_path] = File.join(tmp_path, 'chef_cache') -# FileUtils.rm_rf(tmp_path) -# FileUtils.mkdir_p(tmp_path) -# FileUtils.mkdir_p(Chef::Config[:file_cache_path]) -# end -# end -# -# def tmp_path -# File.expand_path(File.join(File.dirname(__FILE__), '../tmp')) -# end -# end -# -# Spork.each_run do -# Dir["#{File.expand_path('..', File.dirname(__FILE__))}/libraries/*.rb"].sort.each do |path| -# require_relative "../libraries/#{File.basename(path, '.rb')}" -# end -# end +# frozen_string_literal: true + +require 'chefspec' +require 'chefspec/policyfile' + +RSpec.configure do |config| + config.color = true + config.formatter = :documentation + config.log_level = :warn + config.platform = 'ubuntu' + config.version = '22.04' +end diff --git a/test/fixtures/cookbooks/test/metadata.rb b/test/fixtures/cookbooks/test/metadata.rb index 691cb42..31e98c0 100644 --- a/test/fixtures/cookbooks/test/metadata.rb +++ b/test/fixtures/cookbooks/test/metadata.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + name 'test' maintainer 'Sous Chefs' maintainer_email 'help@sous-chefs.org' diff --git a/test/fixtures/cookbooks/test/recipes/default.rb b/test/fixtures/cookbooks/test/recipes/default.rb index ce54375..b81862f 100644 --- a/test/fixtures/cookbooks/test/recipes/default.rb +++ b/test/fixtures/cookbooks/test/recipes/default.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +apt_update if platform_family?('debian') + github_asset 'Precompiled.zip' do repo 'elixir-lang/elixir' release 'v1.12.2' diff --git a/test/integration/default/controls/default.rb b/test/integration/default/controls/default.rb index 22e1412..af65372 100644 --- a/test/integration/default/controls/default.rb +++ b/test/integration/default/controls/default.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + cache_dir = inspec.file('/opt/kitchen').exist? ? '/opt/kitchen/cache' : '/tmp/kitchen/cache' control 'github-cookbook' do