From e8d2c2579383897a1dd7f9debd359abe8ae8373d Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Tue, 20 Jul 2021 09:55:51 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-1-stable-ee --- spec/tooling/danger/feature_flag_spec.rb | 20 ++++- spec/tooling/danger/project_helper_spec.rb | 43 +++++----- spec/tooling/lib/tooling/kubernetes_client_spec.rb | 91 ++++++++++++++++++++++ 3 files changed, 132 insertions(+), 22 deletions(-) (limited to 'spec/tooling') diff --git a/spec/tooling/danger/feature_flag_spec.rb b/spec/tooling/danger/feature_flag_spec.rb index 5e495cd43c6..7cae3e0a8b3 100644 --- a/spec/tooling/danger/feature_flag_spec.rb +++ b/spec/tooling/danger/feature_flag_spec.rb @@ -10,7 +10,7 @@ RSpec.describe Tooling::Danger::FeatureFlag do let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } - subject(:feature_flag) { fake_danger.new(git: fake_git) } + subject(:feature_flag) { fake_danger.new(helper: fake_helper) } describe '#feature_flag_files' do let(:feature_flag_files) do @@ -87,7 +87,11 @@ RSpec.describe Tooling::Danger::FeatureFlag do let(:feature_flag_path) { 'config/feature_flags/development/entry.yml' } let(:group) { 'group::source code' } let(:raw_yaml) do - YAML.dump('group' => group) + YAML.dump( + 'group' => group, + 'default_enabled' => true, + 'rollout_issue_url' => 'https://gitlab.com/gitlab-org/gitlab/-/issues/1' + ) end subject(:found) { described_class.new(feature_flag_path) } @@ -109,6 +113,18 @@ RSpec.describe Tooling::Danger::FeatureFlag do end end + describe '#default_enabled' do + it 'returns the default_enabled found in the YAML' do + expect(found.default_enabled).to eq(true) + end + end + + describe '#rollout_issue_url' do + it 'returns the rollout_issue_url found in the YAML' do + expect(found.rollout_issue_url).to eq('https://gitlab.com/gitlab-org/gitlab/-/issues/1') + end + end + describe '#group_match_mr_label?' do subject(:result) { found.group_match_mr_label?(mr_group_label) } diff --git a/spec/tooling/danger/project_helper_spec.rb b/spec/tooling/danger/project_helper_spec.rb index 7474709d255..8bcfac5a699 100644 --- a/spec/tooling/danger/project_helper_spec.rb +++ b/spec/tooling/danger/project_helper_spec.rb @@ -56,24 +56,26 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'ee/doc/foo' | [:unknown] 'ee/README' | [:unknown] - 'app/assets/foo' | [:frontend] - 'app/views/foo' | [:frontend] - 'public/foo' | [:frontend] - 'scripts/frontend/foo' | [:frontend] - 'spec/javascripts/foo' | [:frontend] - 'spec/frontend/bar' | [:frontend] - 'vendor/assets/foo' | [:frontend] - 'babel.config.js' | [:frontend] - 'jest.config.js' | [:frontend] - 'package.json' | [:frontend] - 'yarn.lock' | [:frontend] - 'config/foo.js' | [:frontend] - 'config/deep/foo.js' | [:frontend] - - 'ee/app/assets/foo' | [:frontend] - 'ee/app/views/foo' | [:frontend] - 'ee/spec/javascripts/foo' | [:frontend] - 'ee/spec/frontend/bar' | [:frontend] + 'app/assets/foo' | [:frontend] + 'app/views/foo' | [:frontend] + 'public/foo' | [:frontend] + 'scripts/frontend/foo' | [:frontend] + 'spec/javascripts/foo' | [:frontend] + 'spec/frontend/bar' | [:frontend] + 'spec/frontend_integration/bar' | [:frontend] + 'vendor/assets/foo' | [:frontend] + 'babel.config.js' | [:frontend] + 'jest.config.js' | [:frontend] + 'package.json' | [:frontend] + 'yarn.lock' | [:frontend] + 'config/foo.js' | [:frontend] + 'config/deep/foo.js' | [:frontend] + + 'ee/app/assets/foo' | [:frontend] + 'ee/app/views/foo' | [:frontend] + 'ee/spec/javascripts/foo' | [:frontend] + 'ee/spec/frontend/bar' | [:frontend] + 'ee/spec/frontend_integration/bar' | [:frontend] '.gitlab/ci/frontend.gitlab-ci.yml' | %i[frontend engineering_productivity] @@ -183,7 +185,8 @@ RSpec.describe Tooling::Danger::ProjectHelper do 'lib/generators/rails/usage_metric_definition_generator.rb' | [:backend, :product_intelligence] 'spec/lib/generators/usage_metric_definition_generator_spec.rb' | [:backend, :product_intelligence] 'config/metrics/schema.json' | [:product_intelligence] - 'app/assets/javascripts/tracking.js' | [:frontend, :product_intelligence] + 'app/assets/javascripts/tracking/foo.js' | [:frontend, :product_intelligence] + 'spec/frontend/tracking/foo.js' | [:frontend, :product_intelligence] 'spec/frontend/tracking_spec.js' | [:frontend, :product_intelligence] 'lib/gitlab/usage_database/foo.rb' | [:backend] end @@ -218,7 +221,7 @@ RSpec.describe Tooling::Danger::ProjectHelper do describe '.local_warning_message' do it 'returns an informational message with rules that can run' do - expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changelog, database, datateam, documentation, duplicate_yarn_dependencies, eslint, karma, pajamas, pipeline, prettier, product_intelligence, utility_css') + expect(described_class.local_warning_message).to eq('==> Only the following Danger rules can be run locally: changelog, database, documentation, duplicate_yarn_dependencies, eslint, gitaly, karma, pajamas, pipeline, prettier, product_intelligence, utility_css, vue_shared_documentation') end end diff --git a/spec/tooling/lib/tooling/kubernetes_client_spec.rb b/spec/tooling/lib/tooling/kubernetes_client_spec.rb index 636727401af..a7f50b0bb50 100644 --- a/spec/tooling/lib/tooling/kubernetes_client_spec.rb +++ b/spec/tooling/lib/tooling/kubernetes_client_spec.rb @@ -135,6 +135,52 @@ RSpec.describe Tooling::KubernetesClient do end end + describe '#cleanup_review_app_namespaces' do + let(:two_days_ago) { Time.now - 3600 * 24 * 2 } + let(:namespaces) { %w[review-abc-123 review-xyz-789] } + + subject { described_class.new(namespace: nil) } + + before do + allow(subject).to receive(:review_app_namespaces_created_before).with(created_before: two_days_ago).and_return(namespaces) + end + + shared_examples 'a kubectl command to delete namespaces older than given creation time' do + let(:wait) { true } + + specify do + expect(Gitlab::Popen).to receive(:popen_with_detail) + .with(["kubectl delete namespace " + + %(--now --ignore-not-found --wait=#{wait} #{namespaces.join(' ')})]) + .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true))) + + # We're not verifying the output here, just silencing it + expect { subject.cleanup_review_app_namespaces(created_before: two_days_ago) }.to output.to_stdout + end + end + + it_behaves_like 'a kubectl command to delete namespaces older than given creation time' + + it 'raises an error if the Kubernetes command fails' do + expect(Gitlab::Popen).to receive(:popen_with_detail) + .with(["kubectl delete namespace " + + %(--now --ignore-not-found --wait=true #{namespaces.join(' ')})]) + .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false))) + + expect { subject.cleanup_review_app_namespaces(created_before: two_days_ago) }.to raise_error(described_class::CommandFailedError) + end + + context 'with no namespaces found' do + let(:namespaces) { [] } + + it 'does not call #delete_namespaces_by_exact_names' do + expect(subject).not_to receive(:delete_namespaces_by_exact_names) + + subject.cleanup_review_app_namespaces(created_before: two_days_ago) + end + end + end + describe '#raw_resource_names' do it 'calls kubectl to retrieve the resource names' do expect(Gitlab::Popen).to receive(:popen_with_detail) @@ -200,4 +246,49 @@ RSpec.describe Tooling::KubernetesClient do it_behaves_like 'a kubectl command to retrieve resource names sorted by creationTimestamp' end end + + describe '#review_app_namespaces_created_before' do + let(:three_days_ago) { Time.now - 3600 * 24 * 3 } + let(:two_days_ago) { Time.now - 3600 * 24 * 2 } + let(:namespace_created_three_days_ago) { 'namespace-created-three-days-ago' } + let(:resource_type) { 'namespace' } + let(:raw_resources) do + { + items: [ + { + apiVersion: "v1", + kind: "Namespace", + metadata: { + creationTimestamp: three_days_ago, + name: namespace_created_three_days_ago, + labels: { + tls: 'review-apps-tls' + } + } + }, + { + apiVersion: "v1", + kind: "Namespace", + metadata: { + creationTimestamp: Time.now, + name: 'another-pvc', + labels: { + tls: 'review-apps-tls' + } + } + } + ] + }.to_json + end + + specify do + expect(Gitlab::Popen).to receive(:popen_with_detail) + .with(["kubectl get namespace " \ + "-l tls=review-apps-tls " \ + "--sort-by='{.metadata.creationTimestamp}' -o json"]) + .and_return(Gitlab::Popen::Result.new([], raw_resources, '', double(success?: true))) + + expect(subject.__send__(:review_app_namespaces_created_before, created_before: two_days_ago)).to contain_exactly(namespace_created_three_days_ago) + end + end end -- cgit v1.2.3