summaryrefslogtreecommitdiff
path: root/spec/tooling
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
commita09983ae35713f5a2bbb100981116d31ce99826e (patch)
tree2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/tooling
parent18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff)
downloadgitlab-ce-a09983ae35713f5a2bbb100981116d31ce99826e.tar.gz
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/tooling')
-rw-r--r--spec/tooling/lib/tooling/helm3_client_spec.rb133
-rw-r--r--spec/tooling/lib/tooling/kubernetes_client_spec.rb111
-rw-r--r--spec/tooling/lib/tooling/test_file_finder_spec.rb74
3 files changed, 317 insertions, 1 deletions
diff --git a/spec/tooling/lib/tooling/helm3_client_spec.rb b/spec/tooling/lib/tooling/helm3_client_spec.rb
new file mode 100644
index 00000000000..f12bae051f0
--- /dev/null
+++ b/spec/tooling/lib/tooling/helm3_client_spec.rb
@@ -0,0 +1,133 @@
+# frozen_string_literal: true
+
+require_relative '../../../../tooling/lib/tooling/helm3_client'
+
+RSpec.describe Tooling::Helm3Client do
+ let(:namespace) { 'review-apps' }
+ let(:release_name) { 'my-release' }
+ let(:raw_helm_list_page1) do
+ <<~OUTPUT
+ [
+ {"name":"review-qa-60-reor-1mugd1","namespace":"#{namespace}","revision":1,"updated":"2020-04-03 17:27:10.245952 +0800 +08","status":"failed","chart":"gitlab-1.1.3","app_version":"12.9.2"},
+ {"name":"review-7846-fix-s-261vd6","namespace":"#{namespace}","revision":2,"updated":"2020-04-02 17:27:12.245952 +0800 +08","status":"deployed","chart":"gitlab-1.1.3","app_version":"12.9.2"},
+ {"name":"review-7867-snowp-lzo3iy","namespace":"#{namespace}","revision":1,"updated":"2020-04-02 15:27:12.245952 +0800 +08","status":"deployed","chart":"gitlab-1.1.3","app_version":"12.9.1"},
+ {"name":"review-6709-group-2pzeec","namespace":"#{namespace}","revision":2,"updated":"2020-04-01 21:27:12.245952 +0800 +08","status":"failed","chart":"gitlab-1.1.3","app_version":"12.9.1"}
+ ]
+ OUTPUT
+ end
+ let(:raw_helm_list_page2) do
+ <<~OUTPUT
+ [
+ {"name":"review-6709-group-t40qbv","namespace":"#{namespace}","revision":2,"updated":"2020-04-01 11:27:12.245952 +0800 +08","status":"deployed","chart":"gitlab-1.1.3","app_version":"12.9.1"}
+ ]
+ OUTPUT
+ end
+ let(:raw_helm_list_empty) do
+ <<~OUTPUT
+ []
+ OUTPUT
+ end
+
+ subject { described_class.new(namespace: namespace) }
+
+ describe '#releases' do
+ it 'raises an error if the Helm command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.releases.to_a }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls helm list with default arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ subject.releases.to_a
+ end
+
+ it 'calls helm list with extra arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json --deployed)])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ subject.releases(args: ['--deployed']).to_a
+ end
+
+ it 'returns a list of Release objects' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json --deployed)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true)))
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_empty, '', double(success?: true)))
+
+ releases = subject.releases(args: ['--deployed']).to_a
+
+ expect(releases.size).to eq(1)
+ expect(releases[0]).to have_attributes(
+ name: 'review-6709-group-t40qbv',
+ revision: 2,
+ last_update: Time.parse('2020-04-01 11:27:12.245952 +0800 +08'),
+ status: 'deployed',
+ chart: 'gitlab-1.1.3',
+ app_version: '12.9.1',
+ namespace: namespace
+ )
+ end
+
+ it 'automatically paginates releases' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page1, '', double(success?: true)))
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 256 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true)))
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 512 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_empty, '', double(success?: true)))
+ releases = subject.releases.to_a
+
+ expect(releases.size).to eq(5)
+ expect(releases.last.name).to eq('review-6709-group-t40qbv')
+ end
+ end
+
+ describe '#delete' do
+ it 'raises an error if the Helm command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls helm uninstall with default arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(subject.delete(release_name: release_name)).to eq('')
+ end
+
+ context 'with multiple release names' do
+ let(:release_name) { %w[my-release my-release-2] }
+
+ it 'raises an error if the Helm command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name.join(' ')})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls helm uninstall with multiple release names' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name.join(' ')})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(subject.delete(release_name: release_name)).to eq('')
+ end
+ end
+ end
+end
diff --git a/spec/tooling/lib/tooling/kubernetes_client_spec.rb b/spec/tooling/lib/tooling/kubernetes_client_spec.rb
new file mode 100644
index 00000000000..fdd56aa0189
--- /dev/null
+++ b/spec/tooling/lib/tooling/kubernetes_client_spec.rb
@@ -0,0 +1,111 @@
+# frozen_string_literal: true
+
+require_relative '../../../../tooling/lib/tooling/kubernetes_client'
+
+RSpec.describe Tooling::KubernetesClient do
+ let(:namespace) { 'review-apps' }
+ let(:release_name) { 'my-release' }
+ let(:pod_for_release) { "pod-my-release-abcd" }
+ let(:raw_resource_names_str) { "NAME\nfoo\n#{pod_for_release}\nbar" }
+ let(:raw_resource_names) { raw_resource_names_str.lines.map(&:strip) }
+
+ subject { described_class.new(namespace: namespace) }
+
+ describe 'RESOURCE_LIST' do
+ it 'returns the correct list of resources separated by commas' do
+ expect(described_class::RESOURCE_LIST).to eq('ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa,crd')
+ end
+ end
+
+ describe '#cleanup' do
+ before do
+ allow(subject).to receive(:raw_resource_names).and_return(raw_resource_names)
+ end
+
+ it 'raises an error if the Kubernetes command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with(["kubectl delete #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" --now --ignore-not-found --include-uninitialized --wait=true -l release="#{release_name}")])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.cleanup(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls kubectl with the correct arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with(["kubectl delete #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" --now --ignore-not-found --include-uninitialized --wait=true -l release="#{release_name}")])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(kubectl delete --namespace "#{namespace}" --ignore-not-found #{pod_for_release})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ # We're not verifying the output here, just silencing it
+ expect { subject.cleanup(release_name: release_name) }.to output.to_stdout
+ end
+
+ context 'with multiple releases' do
+ let(:release_name) { %w[my-release my-release-2] }
+
+ it 'raises an error if the Kubernetes command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with(["kubectl delete #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" --now --ignore-not-found --include-uninitialized --wait=true -l 'release in (#{release_name.join(', ')})')])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.cleanup(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls kubectl with the correct arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with(["kubectl delete #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" --now --ignore-not-found --include-uninitialized --wait=true -l 'release in (#{release_name.join(', ')})')])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(kubectl delete --namespace "#{namespace}" --ignore-not-found #{pod_for_release})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ # We're not verifying the output here, just silencing it
+ expect { subject.cleanup(release_name: release_name) }.to output.to_stdout
+ end
+ end
+
+ context 'with `wait: false`' do
+ it 'raises an error if the Kubernetes command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with(["kubectl delete #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" --now --ignore-not-found --include-uninitialized --wait=false -l release="#{release_name}")])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.cleanup(release_name: release_name, wait: false) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls kubectl with the correct arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with(["kubectl delete #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" --now --ignore-not-found --include-uninitialized --wait=false -l release="#{release_name}")])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(kubectl delete --namespace "#{namespace}" --ignore-not-found #{pod_for_release})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ # We're not verifying the output here, just silencing it
+ expect { subject.cleanup(release_name: release_name, wait: false) }.to output.to_stdout
+ 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)
+ .with(["kubectl get #{described_class::RESOURCE_LIST} " +
+ %(--namespace "#{namespace}" -o name)])
+ .and_return(Gitlab::Popen::Result.new([], raw_resource_names_str, '', double(success?: true)))
+
+ expect(subject.__send__(:raw_resource_names)).to eq(raw_resource_names)
+ end
+ end
+end
diff --git a/spec/tooling/lib/tooling/test_file_finder_spec.rb b/spec/tooling/lib/tooling/test_file_finder_spec.rb
index 9c33f20877b..64b55b9b1d6 100644
--- a/spec/tooling/lib/tooling/test_file_finder_spec.rb
+++ b/spec/tooling/lib/tooling/test_file_finder_spec.rb
@@ -3,7 +3,7 @@
require_relative '../../../../tooling/lib/tooling/test_file_finder'
RSpec.describe Tooling::TestFileFinder do
- subject { Tooling::TestFileFinder.new(file) }
+ subject { described_class.new(file) }
describe '#test_files' do
context 'when given non .rb files' do
@@ -88,6 +88,78 @@ RSpec.describe Tooling::TestFileFinder do
end
end
+ context 'when given a factory file' do
+ let(:file) { 'spec/factories/users.rb' }
+
+ it 'returns spec/factories_spec.rb file' do
+ expect(subject.test_files).to contain_exactly('spec/factories_spec.rb')
+ end
+ end
+
+ context 'when given an ee factory file' do
+ let(:file) { 'ee/spec/factories/users.rb' }
+
+ it 'returns spec/factories_spec.rb file' do
+ expect(subject.test_files).to contain_exactly('spec/factories_spec.rb')
+ end
+ end
+
+ context 'when given db/structure.sql' do
+ let(:file) { 'db/structure.sql' }
+
+ it 'returns spec/db/schema_spec.rb' do
+ expect(subject.test_files).to contain_exactly('spec/db/schema_spec.rb')
+ end
+ end
+
+ context 'when given an initializer' do
+ let(:file) { 'config/initializers/action_mailer_hooks.rb' }
+
+ it 'returns the matching initializer spec' do
+ expect(subject.test_files).to contain_exactly('spec/initializers/action_mailer_hooks_spec.rb')
+ end
+ end
+
+ context 'when given a haml view' do
+ let(:file) { 'app/views/admin/users/_user.html.haml' }
+
+ it 'returns the matching view spec' do
+ expect(subject.test_files).to contain_exactly('spec/views/admin/users/_user.html.haml_spec.rb')
+ end
+ end
+
+ context 'when given a haml view in ee/' do
+ let(:file) { 'ee/app/views/admin/users/_user.html.haml' }
+
+ it 'returns the matching view spec' do
+ expect(subject.test_files).to contain_exactly('ee/spec/views/admin/users/_user.html.haml_spec.rb')
+ end
+ end
+
+ context 'when given a migration file' do
+ let(:file) { 'db/migrate/20191023152913_add_default_and_free_plans.rb' }
+
+ it 'returns the matching migration spec' do
+ test_files = %w[
+ spec/migrations/add_default_and_free_plans_spec.rb
+ spec/migrations/20191023152913_add_default_and_free_plans_spec.rb
+ ]
+ expect(subject.test_files).to contain_exactly(*test_files)
+ end
+ end
+
+ context 'when given a post-migration file' do
+ let(:file) { 'db/post_migrate/20200608072931_backfill_imported_snippet_repositories.rb' }
+
+ it 'returns the matching migration spec' do
+ test_files = %w[
+ spec/migrations/backfill_imported_snippet_repositories_spec.rb
+ spec/migrations/20200608072931_backfill_imported_snippet_repositories_spec.rb
+ ]
+ expect(subject.test_files).to contain_exactly(*test_files)
+ end
+ end
+
context 'with foss_test_only: true' do
subject { Tooling::TestFileFinder.new(file, foss_test_only: true) }