From 7e9c479f7de77702622631cff2628a9c8dcbc627 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 19 Nov 2020 08:27:35 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-6-stable-ee --- spec/lib/gitlab/kubernetes/helm/api_spec.rb | 4 +- .../gitlab/kubernetes/helm/base_command_spec.rb | 38 ---- .../lib/gitlab/kubernetes/helm/certificate_spec.rb | 28 --- .../gitlab/kubernetes/helm/delete_command_spec.rb | 38 ---- .../gitlab/kubernetes/helm/init_command_spec.rb | 35 ---- .../gitlab/kubernetes/helm/install_command_spec.rb | 214 --------------------- .../gitlab/kubernetes/helm/patch_command_spec.rb | 114 ----------- spec/lib/gitlab/kubernetes/helm/pod_spec.rb | 121 ++++++------ .../gitlab/kubernetes/helm/reset_command_spec.rb | 50 ----- .../gitlab/kubernetes/helm/v2/base_command_spec.rb | 50 +++++ .../gitlab/kubernetes/helm/v2/certificate_spec.rb | 28 +++ .../kubernetes/helm/v2/delete_command_spec.rb | 38 ++++ .../gitlab/kubernetes/helm/v2/init_command_spec.rb | 35 ++++ .../kubernetes/helm/v2/install_command_spec.rb | 183 ++++++++++++++++++ .../kubernetes/helm/v2/patch_command_spec.rb | 87 +++++++++ .../kubernetes/helm/v2/reset_command_spec.rb | 50 +++++ .../gitlab/kubernetes/helm/v3/base_command_spec.rb | 44 +++++ .../kubernetes/helm/v3/delete_command_spec.rb | 35 ++++ .../kubernetes/helm/v3/install_command_spec.rb | 168 ++++++++++++++++ .../kubernetes/helm/v3/patch_command_spec.rb | 81 ++++++++ 20 files changed, 866 insertions(+), 575 deletions(-) delete mode 100644 spec/lib/gitlab/kubernetes/helm/base_command_spec.rb delete mode 100644 spec/lib/gitlab/kubernetes/helm/certificate_spec.rb delete mode 100644 spec/lib/gitlab/kubernetes/helm/delete_command_spec.rb delete mode 100644 spec/lib/gitlab/kubernetes/helm/init_command_spec.rb delete mode 100644 spec/lib/gitlab/kubernetes/helm/install_command_spec.rb delete mode 100644 spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb delete mode 100644 spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/base_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/certificate_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/delete_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/init_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/install_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/patch_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v2/reset_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v3/base_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v3/delete_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v3/install_command_spec.rb create mode 100644 spec/lib/gitlab/kubernetes/helm/v3/patch_command_spec.rb (limited to 'spec/lib/gitlab/kubernetes/helm') diff --git a/spec/lib/gitlab/kubernetes/helm/api_spec.rb b/spec/lib/gitlab/kubernetes/helm/api_spec.rb index bcc95bdbf2b..e022f5bd912 100644 --- a/spec/lib/gitlab/kubernetes/helm/api_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/api_spec.rb @@ -13,7 +13,7 @@ RSpec.describe Gitlab::Kubernetes::Helm::API do let(:files) { {} } let(:command) do - Gitlab::Kubernetes::Helm::InstallCommand.new( + Gitlab::Kubernetes::Helm::V2::InstallCommand.new( name: application_name, chart: 'chart-name', rbac: rbac, @@ -142,7 +142,7 @@ RSpec.describe Gitlab::Kubernetes::Helm::API do end context 'with a service account' do - let(:command) { Gitlab::Kubernetes::Helm::InitCommand.new(name: application_name, files: files, rbac: rbac) } + let(:command) { Gitlab::Kubernetes::Helm::V2::InitCommand.new(name: application_name, files: files, rbac: rbac) } context 'rbac-enabled cluster' do let(:rbac) { true } diff --git a/spec/lib/gitlab/kubernetes/helm/base_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/base_command_spec.rb deleted file mode 100644 index a7abd6ab1bf..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/base_command_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::BaseCommand do - subject(:base_command) do - test_class.new(rbac) - end - - let(:application) { create(:clusters_applications_helm) } - let(:rbac) { false } - - let(:test_class) do - Class.new(Gitlab::Kubernetes::Helm::BaseCommand) do - def initialize(rbac) - super( - name: 'test-class-name', - rbac: rbac, - files: { some: 'value' } - ) - end - end - end - - it_behaves_like 'helm command generator' do - let(:commands) { '' } - end - - describe '#pod_name' do - subject { base_command.pod_name } - - it { is_expected.to eq('install-test-class-name') } - end - - it_behaves_like 'helm command' do - let(:command) { base_command } - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/certificate_spec.rb b/spec/lib/gitlab/kubernetes/helm/certificate_spec.rb deleted file mode 100644 index b446c5e1149..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/certificate_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::Certificate do - describe '.generate_root' do - subject { described_class.generate_root } - - it 'generates a root CA that expires a long way in the future' do - expect(subject.cert.not_after).to be > 999.years.from_now - end - end - - describe '#issue' do - subject { described_class.generate_root.issue } - - it 'generates a cert that expires soon' do - expect(subject.cert.not_after).to be < 60.minutes.from_now - end - - context 'passing in INFINITE_EXPIRY' do - subject { described_class.generate_root.issue(expires_in: described_class::INFINITE_EXPIRY) } - - it 'generates a cert that expires a long way in the future' do - expect(subject.cert.not_after).to be > 999.years.from_now - end - end - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/delete_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/delete_command_spec.rb deleted file mode 100644 index ff2c2d76f22..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/delete_command_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::DeleteCommand do - subject(:delete_command) { described_class.new(name: app_name, rbac: rbac, files: files) } - - let(:app_name) { 'app-name' } - let(:rbac) { true } - let(:files) { {} } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm delete --purge app-name - EOS - end - end - - describe '#pod_name' do - subject { delete_command.pod_name } - - it { is_expected.to eq('uninstall-app-name') } - end - - it_behaves_like 'helm command' do - let(:command) { delete_command } - end - - describe '#delete_command' do - it 'deletes the release' do - expect(subject.delete_command).to eq('helm delete --purge app-name') - end - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb deleted file mode 100644 index d538ed12a07..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::InitCommand do - subject(:init_command) { described_class.new(name: application.name, files: files, rbac: rbac) } - - let(:application) { create(:clusters_applications_helm) } - let(:rbac) { false } - let(:files) { {} } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - helm init --tiller-tls --tiller-tls-verify --tls-ca-cert /data/helm/helm/config/ca.pem --tiller-tls-cert /data/helm/helm/config/cert.pem --tiller-tls-key /data/helm/helm/config/key.pem - EOS - end - end - - context 'on a rbac-enabled cluster' do - let(:rbac) { true } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - helm init --tiller-tls --tiller-tls-verify --tls-ca-cert /data/helm/helm/config/ca.pem --tiller-tls-cert /data/helm/helm/config/cert.pem --tiller-tls-key /data/helm/helm/config/key.pem --service-account tiller - EOS - end - end - end - - it_behaves_like 'helm command' do - let(:command) { init_command } - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb deleted file mode 100644 index 6ed7323c96f..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb +++ /dev/null @@ -1,214 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::InstallCommand do - subject(:install_command) do - described_class.new( - name: 'app-name', - chart: 'chart-name', - rbac: rbac, - files: files, - version: version, - repository: repository, - preinstall: preinstall, - postinstall: postinstall - ) - end - - let(:files) { { 'ca.pem': 'some file content' } } - let(:repository) { 'https://repository.example.com' } - let(:rbac) { false } - let(:version) { '1.2.3' } - let(:preinstall) { nil } - let(:postinstall) { nil } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_install_comand} - EOS - end - - let(:helm_install_comand) do - <<~EOS.squish - helm upgrade app-name chart-name - --install - --atomic - --cleanup-on-fail - --reset-values - --version 1.2.3 - --set rbac.create\\=false,rbac.enabled\\=false - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - - context 'when rbac is true' do - let(:rbac) { true } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_install_command} - EOS - end - - let(:helm_install_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --install - --atomic - --cleanup-on-fail - --reset-values - --version 1.2.3 - --set rbac.create\\=true,rbac.enabled\\=true - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - context 'when there is a pre-install script' do - let(:preinstall) { ['/bin/date', '/bin/true'] } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - /bin/date - /bin/true - #{helm_install_command} - EOS - end - - let(:helm_install_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --install - --atomic - --cleanup-on-fail - --reset-values - --version 1.2.3 - --set rbac.create\\=false,rbac.enabled\\=false - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - context 'when there is a post-install script' do - let(:postinstall) { ['/bin/date', "/bin/false\n"] } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_install_command} - /bin/date - /bin/false - EOS - end - - let(:helm_install_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --install - --atomic - --cleanup-on-fail - --reset-values - --version 1.2.3 - --set rbac.create\\=false,rbac.enabled\\=false - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - context 'when there is no ca.pem file' do - let(:files) { { 'file.txt': 'some content' } } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_install_command} - EOS - end - - let(:helm_install_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --install - --atomic - --cleanup-on-fail - --reset-values - --version 1.2.3 - --set rbac.create\\=false,rbac.enabled\\=false - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - context 'when there is no version' do - let(:version) { nil } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_install_command} - EOS - end - - let(:helm_install_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --install - --atomic - --cleanup-on-fail - --reset-values - --set rbac.create\\=false,rbac.enabled\\=false - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - it_behaves_like 'helm command' do - let(:command) { install_command } - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb deleted file mode 100644 index 487a38f286d..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb +++ /dev/null @@ -1,114 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::PatchCommand do - let(:files) { { 'ca.pem': 'some file content' } } - let(:repository) { 'https://repository.example.com' } - let(:rbac) { false } - let(:version) { '1.2.3' } - - subject(:patch_command) do - described_class.new( - name: 'app-name', - chart: 'chart-name', - rbac: rbac, - files: files, - version: version, - repository: repository - ) - end - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_upgrade_comand} - EOS - end - - let(:helm_upgrade_comand) do - <<~EOS.squish - helm upgrade app-name chart-name - --reuse-values - --version 1.2.3 - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - - context 'when rbac is true' do - let(:rbac) { true } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_upgrade_command} - EOS - end - - let(:helm_upgrade_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --reuse-values - --version 1.2.3 - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - context 'when there is no ca.pem file' do - let(:files) { { 'file.txt': 'some content' } } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - export HELM_HOST="localhost:44134" - tiller -listen ${HELM_HOST} -alsologtostderr & - helm init --client-only - helm repo add app-name https://repository.example.com - helm repo update - #{helm_upgrade_command} - EOS - end - - let(:helm_upgrade_command) do - <<~EOS.squish - helm upgrade app-name chart-name - --reuse-values - --version 1.2.3 - --namespace gitlab-managed-apps - -f /data/helm/app-name/config/values.yaml - EOS - end - end - end - - context 'when there is no version' do - let(:version) { nil } - - it { expect { patch_command }.to raise_error(ArgumentError, 'version is required') } - end - - describe '#pod_name' do - subject { patch_command.pod_name } - - it { is_expected.to eq 'install-app-name' } - end - - it_behaves_like 'helm command' do - let(:command) { patch_command } - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/pod_spec.rb b/spec/lib/gitlab/kubernetes/helm/pod_spec.rb index 54e3289dd25..6d97790fc8b 100644 --- a/spec/lib/gitlab/kubernetes/helm/pod_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/pod_spec.rb @@ -4,75 +4,84 @@ require 'spec_helper' RSpec.describe Gitlab::Kubernetes::Helm::Pod do describe '#generate' do - let(:app) { create(:clusters_applications_prometheus) } - let(:command) { app.install_command } - let(:namespace) { Gitlab::Kubernetes::Helm::NAMESPACE } - let(:service_account_name) { nil } + using RSpec::Parameterized::TableSyntax - subject { described_class.new(command, namespace, service_account_name: service_account_name) } + where(:helm_major_version, :expected_helm_version, :expected_command_env) do + 2 | '2.16.9' | [:TILLER_NAMESPACE] + 3 | '3.2.4' | nil + end - context 'with a command' do - it 'generates a Kubeclient::Resource' do - expect(subject.generate).to be_a_kind_of(Kubeclient::Resource) - end + with_them do + let(:cluster) { create(:cluster, helm_major_version: helm_major_version) } + let(:app) { create(:clusters_applications_prometheus, cluster: cluster) } + let(:command) { app.install_command } + let(:namespace) { Gitlab::Kubernetes::Helm::NAMESPACE } + let(:service_account_name) { nil } - it 'generates the appropriate metadata' do - metadata = subject.generate.metadata - expect(metadata.name).to eq("install-#{app.name}") - expect(metadata.namespace).to eq('gitlab-managed-apps') - expect(metadata.labels['gitlab.org/action']).to eq('install') - expect(metadata.labels['gitlab.org/application']).to eq(app.name) - end + subject { described_class.new(command, namespace, service_account_name: service_account_name) } - it 'generates a container spec' do - spec = subject.generate.spec - expect(spec.containers.count).to eq(1) - end + context 'with a command' do + it 'generates a Kubeclient::Resource' do + expect(subject.generate).to be_a_kind_of(Kubeclient::Resource) + end - it 'generates the appropriate specifications for the container' do - container = subject.generate.spec.containers.first - expect(container.name).to eq('helm') - expect(container.image).to eq('registry.gitlab.com/gitlab-org/cluster-integration/helm-install-image/releases/2.16.9-kube-1.13.12') - expect(container.env.count).to eq(3) - expect(container.env.map(&:name)).to match_array([:HELM_VERSION, :TILLER_NAMESPACE, :COMMAND_SCRIPT]) - expect(container.command).to match_array(["/bin/sh"]) - expect(container.args).to match_array(["-c", "$(COMMAND_SCRIPT)"]) - end + it 'generates the appropriate metadata' do + metadata = subject.generate.metadata + expect(metadata.name).to eq("install-#{app.name}") + expect(metadata.namespace).to eq('gitlab-managed-apps') + expect(metadata.labels['gitlab.org/action']).to eq('install') + expect(metadata.labels['gitlab.org/application']).to eq(app.name) + end - it 'includes a never restart policy' do - spec = subject.generate.spec - expect(spec.restartPolicy).to eq('Never') - end + it 'generates a container spec' do + spec = subject.generate.spec + expect(spec.containers.count).to eq(1) + end - it 'includes volumes for the container' do - container = subject.generate.spec.containers.first - expect(container.volumeMounts.first['name']).to eq('configuration-volume') - expect(container.volumeMounts.first['mountPath']).to eq("/data/helm/#{app.name}/config") - end + it 'generates the appropriate specifications for the container' do + container = subject.generate.spec.containers.first + expect(container.name).to eq('helm') + expect(container.image).to eq("registry.gitlab.com/gitlab-org/cluster-integration/helm-install-image/releases/#{expected_helm_version}-kube-1.13.12-alpine-3.12") + expect(container.env.map(&:name)).to include(:HELM_VERSION, :COMMAND_SCRIPT, *expected_command_env) + expect(container.command).to match_array(["/bin/sh"]) + expect(container.args).to match_array(["-c", "$(COMMAND_SCRIPT)"]) + end - it 'includes a volume inside the specification' do - spec = subject.generate.spec - expect(spec.volumes.first['name']).to eq('configuration-volume') - end + it 'includes a never restart policy' do + spec = subject.generate.spec + expect(spec.restartPolicy).to eq('Never') + end - it 'mounts configMap specification in the volume' do - volume = subject.generate.spec.volumes.first - expect(volume.configMap['name']).to eq("values-content-configuration-#{app.name}") - expect(volume.configMap['items'].first['key']).to eq(:'values.yaml') - expect(volume.configMap['items'].first['path']).to eq(:'values.yaml') - end + it 'includes volumes for the container' do + container = subject.generate.spec.containers.first + expect(container.volumeMounts.first['name']).to eq('configuration-volume') + expect(container.volumeMounts.first['mountPath']).to eq("/data/helm/#{app.name}/config") + end - it 'has no serviceAccountName' do - spec = subject.generate.spec - expect(spec.serviceAccountName).to be_nil - end + it 'includes a volume inside the specification' do + spec = subject.generate.spec + expect(spec.volumes.first['name']).to eq('configuration-volume') + end - context 'with a service_account_name' do - let(:service_account_name) { 'sa' } + it 'mounts configMap specification in the volume' do + volume = subject.generate.spec.volumes.first + expect(volume.configMap['name']).to eq("values-content-configuration-#{app.name}") + expect(volume.configMap['items'].first['key']).to eq(:'values.yaml') + expect(volume.configMap['items'].first['path']).to eq(:'values.yaml') + end - it 'uses the serviceAccountName provided' do + it 'has no serviceAccountName' do spec = subject.generate.spec - expect(spec.serviceAccountName).to eq(service_account_name) + expect(spec.serviceAccountName).to be_nil + end + + context 'with a service_account_name' do + let(:service_account_name) { 'sa' } + + it 'uses the serviceAccountName provided' do + spec = subject.generate.spec + expect(spec.serviceAccountName).to eq(service_account_name) + end end end end diff --git a/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb deleted file mode 100644 index 5a3ba59b8c0..00000000000 --- a/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::Kubernetes::Helm::ResetCommand do - subject(:reset_command) { described_class.new(name: name, rbac: rbac, files: files) } - - let(:rbac) { true } - let(:name) { 'helm' } - let(:files) { {} } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS - helm reset - kubectl delete replicaset -n gitlab-managed-apps -l name\\=tiller - kubectl delete clusterrolebinding tiller-admin - EOS - end - end - - context 'when there is a ca.pem file' do - let(:files) { { 'ca.pem': 'some file content' } } - - it_behaves_like 'helm command generator' do - let(:commands) do - <<~EOS1.squish + "\n" + <<~EOS2 - helm reset - --tls - --tls-ca-cert /data/helm/helm/config/ca.pem - --tls-cert /data/helm/helm/config/cert.pem - --tls-key /data/helm/helm/config/key.pem - EOS1 - kubectl delete replicaset -n gitlab-managed-apps -l name\\=tiller - kubectl delete clusterrolebinding tiller-admin - EOS2 - end - end - end - - describe '#pod_name' do - subject { reset_command.pod_name } - - it { is_expected.to eq('uninstall-helm') } - end - - it_behaves_like 'helm command' do - let(:command) { reset_command } - end -end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/base_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/base_command_spec.rb new file mode 100644 index 00000000000..3d2b36b9094 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/base_command_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::BaseCommand do + subject(:base_command) do + test_class.new(rbac) + end + + let(:application) { create(:clusters_applications_helm) } + let(:rbac) { false } + + let(:test_class) do + Class.new(described_class) do + def initialize(rbac) + super( + name: 'test-class-name', + rbac: rbac, + files: { some: 'value' } + ) + end + end + end + + describe 'HELM_VERSION' do + subject { described_class::HELM_VERSION } + + it { is_expected.to match /^2\.\d+\.\d+$/ } + end + + describe '#env' do + subject { base_command.env } + + it { is_expected.to include(TILLER_NAMESPACE: 'gitlab-managed-apps') } + end + + it_behaves_like 'helm command generator' do + let(:commands) { '' } + end + + describe '#pod_name' do + subject { base_command.pod_name } + + it { is_expected.to eq('install-test-class-name') } + end + + it_behaves_like 'helm command' do + let(:command) { base_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/certificate_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/certificate_spec.rb new file mode 100644 index 00000000000..a3f0fd9eb9b --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/certificate_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::Certificate do + describe '.generate_root' do + subject { described_class.generate_root } + + it 'generates a root CA that expires a long way in the future' do + expect(subject.cert.not_after).to be > 999.years.from_now + end + end + + describe '#issue' do + subject { described_class.generate_root.issue } + + it 'generates a cert that expires soon' do + expect(subject.cert.not_after).to be < 60.minutes.from_now + end + + context 'passing in INFINITE_EXPIRY' do + subject { described_class.generate_root.issue(expires_in: described_class::INFINITE_EXPIRY) } + + it 'generates a cert that expires a long way in the future' do + expect(subject.cert.not_after).to be > 999.years.from_now + end + end + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/delete_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/delete_command_spec.rb new file mode 100644 index 00000000000..4a3a41dba4a --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/delete_command_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::DeleteCommand do + subject(:delete_command) { described_class.new(name: app_name, rbac: rbac, files: files) } + + let(:app_name) { 'app-name' } + let(:rbac) { true } + let(:files) { {} } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm delete --purge app-name + EOS + end + end + + describe '#pod_name' do + subject { delete_command.pod_name } + + it { is_expected.to eq('uninstall-app-name') } + end + + it_behaves_like 'helm command' do + let(:command) { delete_command } + end + + describe '#delete_command' do + it 'deletes the release' do + expect(subject.delete_command).to eq('helm delete --purge app-name') + end + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/init_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/init_command_spec.rb new file mode 100644 index 00000000000..8ae78ada15c --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/init_command_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::InitCommand do + subject(:init_command) { described_class.new(name: application.name, files: files, rbac: rbac) } + + let(:application) { create(:clusters_applications_helm) } + let(:rbac) { false } + let(:files) { {} } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm init --tiller-tls --tiller-tls-verify --tls-ca-cert /data/helm/helm/config/ca.pem --tiller-tls-cert /data/helm/helm/config/cert.pem --tiller-tls-key /data/helm/helm/config/key.pem + EOS + end + end + + context 'on a rbac-enabled cluster' do + let(:rbac) { true } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm init --tiller-tls --tiller-tls-verify --tls-ca-cert /data/helm/helm/config/ca.pem --tiller-tls-cert /data/helm/helm/config/cert.pem --tiller-tls-key /data/helm/helm/config/key.pem --service-account tiller + EOS + end + end + end + + it_behaves_like 'helm command' do + let(:command) { init_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/install_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/install_command_spec.rb new file mode 100644 index 00000000000..250d1a82e7a --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/install_command_spec.rb @@ -0,0 +1,183 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::InstallCommand do + subject(:install_command) do + described_class.new( + name: 'app-name', + chart: 'chart-name', + rbac: rbac, + files: files, + version: version, + repository: repository, + preinstall: preinstall, + postinstall: postinstall + ) + end + + let(:files) { { 'ca.pem': 'some file content' } } + let(:repository) { 'https://repository.example.com' } + let(:rbac) { false } + let(:version) { '1.2.3' } + let(:preinstall) { nil } + let(:postinstall) { nil } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_comand} + EOS + end + + let(:helm_install_comand) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + + context 'when rbac is true' do + let(:rbac) { true } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_command} + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=true,rbac.enabled\\=true + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is a pre-install script' do + let(:preinstall) { ['/bin/date', '/bin/true'] } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + /bin/date + /bin/true + #{helm_install_command} + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is a post-install script' do + let(:postinstall) { ['/bin/date', "/bin/false\n"] } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_command} + /bin/date + /bin/false + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is no version' do + let(:version) { nil } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_command} + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + it_behaves_like 'helm command' do + let(:command) { install_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/patch_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/patch_command_spec.rb new file mode 100644 index 00000000000..98eb77d397c --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/patch_command_spec.rb @@ -0,0 +1,87 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::PatchCommand do + let(:files) { { 'ca.pem': 'some file content' } } + let(:repository) { 'https://repository.example.com' } + let(:rbac) { false } + let(:version) { '1.2.3' } + + subject(:patch_command) do + described_class.new( + name: 'app-name', + chart: 'chart-name', + rbac: rbac, + files: files, + version: version, + repository: repository + ) + end + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_comand} + EOS + end + + let(:helm_upgrade_comand) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + + context 'when rbac is true' do + let(:rbac) { true } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_command} + EOS + end + + let(:helm_upgrade_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is no version' do + let(:version) { nil } + + it { expect { patch_command }.to raise_error(ArgumentError, 'version is required') } + end + + describe '#pod_name' do + subject { patch_command.pod_name } + + it { is_expected.to eq 'install-app-name' } + end + + it_behaves_like 'helm command' do + let(:command) { patch_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v2/reset_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v2/reset_command_spec.rb new file mode 100644 index 00000000000..9e580cea397 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v2/reset_command_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V2::ResetCommand do + subject(:reset_command) { described_class.new(name: name, rbac: rbac, files: files) } + + let(:rbac) { true } + let(:name) { 'helm' } + let(:files) { {} } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm reset + kubectl delete replicaset -n gitlab-managed-apps -l name\\=tiller + kubectl delete clusterrolebinding tiller-admin + EOS + end + end + + context 'when there is a ca.pem file' do + let(:files) { { 'ca.pem': 'some file content' } } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS1.squish + "\n" + <<~EOS2 + helm reset + --tls + --tls-ca-cert /data/helm/helm/config/ca.pem + --tls-cert /data/helm/helm/config/cert.pem + --tls-key /data/helm/helm/config/key.pem + EOS1 + kubectl delete replicaset -n gitlab-managed-apps -l name\\=tiller + kubectl delete clusterrolebinding tiller-admin + EOS2 + end + end + end + + describe '#pod_name' do + subject { reset_command.pod_name } + + it { is_expected.to eq('uninstall-helm') } + end + + it_behaves_like 'helm command' do + let(:command) { reset_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v3/base_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v3/base_command_spec.rb new file mode 100644 index 00000000000..ad5ff13b4c9 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v3/base_command_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V3::BaseCommand do + subject(:base_command) do + test_class.new(rbac) + end + + let(:application) { create(:clusters_applications_helm) } + let(:rbac) { false } + + let(:test_class) do + Class.new(described_class) do + def initialize(rbac) + super( + name: 'test-class-name', + rbac: rbac, + files: { some: 'value' } + ) + end + end + end + + describe 'HELM_VERSION' do + subject { described_class::HELM_VERSION } + + it { is_expected.to match /^3\.\d+\.\d+$/ } + end + + it_behaves_like 'helm command generator' do + let(:commands) { '' } + end + + describe '#pod_name' do + subject { base_command.pod_name } + + it { is_expected.to eq('install-test-class-name') } + end + + it_behaves_like 'helm command' do + let(:command) { base_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v3/delete_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v3/delete_command_spec.rb new file mode 100644 index 00000000000..63e7a8d2f25 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v3/delete_command_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V3::DeleteCommand do + subject(:delete_command) { described_class.new(name: app_name, rbac: rbac, files: files) } + + let(:app_name) { 'app-name' } + let(:rbac) { true } + let(:files) { {} } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm uninstall app-name --namespace gitlab-managed-apps + EOS + end + end + + describe '#pod_name' do + subject { delete_command.pod_name } + + it { is_expected.to eq('uninstall-app-name') } + end + + it_behaves_like 'helm command' do + let(:command) { delete_command } + end + + describe '#delete_command' do + it 'deletes the release' do + expect(subject.delete_command).to eq('helm uninstall app-name --namespace gitlab-managed-apps') + end + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v3/install_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v3/install_command_spec.rb new file mode 100644 index 00000000000..2bf1f713b3f --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v3/install_command_spec.rb @@ -0,0 +1,168 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V3::InstallCommand do + subject(:install_command) do + described_class.new( + name: 'app-name', + chart: 'chart-name', + rbac: rbac, + files: files, + version: version, + repository: repository, + preinstall: preinstall, + postinstall: postinstall + ) + end + + let(:files) { { 'ca.pem': 'some file content' } } + let(:repository) { 'https://repository.example.com' } + let(:rbac) { false } + let(:version) { '1.2.3' } + let(:preinstall) { nil } + let(:postinstall) { nil } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_comand} + EOS + end + + let(:helm_install_comand) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + + context 'when rbac is true' do + let(:rbac) { true } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_command} + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=true,rbac.enabled\\=true + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is a pre-install script' do + let(:preinstall) { ['/bin/date', '/bin/true'] } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + /bin/date + /bin/true + #{helm_install_command} + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is a post-install script' do + let(:postinstall) { ['/bin/date', "/bin/false\n"] } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_command} + /bin/date + /bin/false + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --version 1.2.3 + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is no version' do + let(:version) { nil } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + #{helm_install_command} + EOS + end + + let(:helm_install_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --install + --atomic + --cleanup-on-fail + --reset-values + --set rbac.create\\=false,rbac.enabled\\=false + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + it_behaves_like 'helm command' do + let(:command) { install_command } + end +end diff --git a/spec/lib/gitlab/kubernetes/helm/v3/patch_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/v3/patch_command_spec.rb new file mode 100644 index 00000000000..2f22e0f2e77 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/v3/patch_command_spec.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Kubernetes::Helm::V3::PatchCommand do + let(:files) { { 'ca.pem': 'some file content' } } + let(:repository) { 'https://repository.example.com' } + let(:rbac) { false } + let(:version) { '1.2.3' } + + subject(:patch_command) do + described_class.new( + name: 'app-name', + chart: 'chart-name', + rbac: rbac, + files: files, + version: version, + repository: repository + ) + end + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_comand} + EOS + end + + let(:helm_upgrade_comand) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + + context 'when rbac is true' do + let(:rbac) { true } + + it_behaves_like 'helm command generator' do + let(:commands) do + <<~EOS + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_command} + EOS + end + + let(:helm_upgrade_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is no version' do + let(:version) { nil } + + it { expect { patch_command }.to raise_error(ArgumentError, 'version is required') } + end + + describe '#pod_name' do + subject { patch_command.pod_name } + + it { is_expected.to eq 'install-app-name' } + end + + it_behaves_like 'helm command' do + let(:command) { patch_command } + end +end -- cgit v1.2.1