diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-07-20 09:55:51 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-07-20 09:55:51 +0000 |
commit | e8d2c2579383897a1dd7f9debd359abe8ae8373d (patch) | |
tree | c42be41678c2586d49a75cabce89322082698334 /spec/tooling/lib | |
parent | fc845b37ec3a90aaa719975f607740c22ba6a113 (diff) | |
download | gitlab-ce-e8d2c2579383897a1dd7f9debd359abe8ae8373d.tar.gz |
Add latest changes from gitlab-org/gitlab@14-1-stable-eev14.1.0-rc42
Diffstat (limited to 'spec/tooling/lib')
-rw-r--r-- | spec/tooling/lib/tooling/kubernetes_client_spec.rb | 91 |
1 files changed, 91 insertions, 0 deletions
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 |