diff options
Diffstat (limited to 'spec/lib/gitlab/kubernetes')
8 files changed, 252 insertions, 93 deletions
diff --git a/spec/lib/gitlab/kubernetes/helm/api_spec.rb b/spec/lib/gitlab/kubernetes/helm/api_spec.rb index 1f925fd45af..0e4179d5887 100644 --- a/spec/lib/gitlab/kubernetes/helm/api_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/api_spec.rb @@ -17,7 +17,8 @@ describe Gitlab::Kubernetes::Helm::API do name: application_name, chart: 'chart-name', rbac: rbac, - files: files + files: files, + local_tiller_enabled: true ) end @@ -142,7 +143,7 @@ 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::InitCommand.new(name: application_name, files: files, rbac: rbac, local_tiller_enabled: true) } 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 index 2a4a911cf38..f9bcb8abdb1 100644 --- a/spec/lib/gitlab/kubernetes/helm/base_command_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/base_command_spec.rb @@ -11,25 +11,14 @@ describe Gitlab::Kubernetes::Helm::BaseCommand do let(:rbac) { false } let(:test_class) do - Class.new do - include Gitlab::Kubernetes::Helm::BaseCommand - + Class.new(Gitlab::Kubernetes::Helm::BaseCommand) do def initialize(rbac) - @rbac = rbac - end - - def name - "test-class-name" - end - - def rbac? - @rbac - end - - def files - { - some: 'value' - } + super( + name: 'test-class-name', + rbac: rbac, + files: { some: 'value' }, + local_tiller_enabled: false + ) 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 index 95d60c18d56..2bf8b294821 100644 --- a/spec/lib/gitlab/kubernetes/helm/delete_command_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/delete_command_spec.rb @@ -3,11 +3,12 @@ require 'spec_helper' describe Gitlab::Kubernetes::Helm::DeleteCommand do - subject(:delete_command) { described_class.new(name: app_name, rbac: rbac, files: files) } + subject(:delete_command) { described_class.new(name: app_name, rbac: rbac, files: files, local_tiller_enabled: local_tiller_enabled) } let(:app_name) { 'app-name' } let(:rbac) { true } let(:files) { {} } + let(:local_tiller_enabled) { true } it_behaves_like 'helm command generator' do let(:commands) do @@ -21,9 +22,7 @@ describe Gitlab::Kubernetes::Helm::DeleteCommand do end context 'tillerless feature disabled' do - before do - stub_feature_flags(managed_apps_local_tiller: false) - end + let(:local_tiller_enabled) { false } it_behaves_like 'helm command generator' do let(:commands) do diff --git a/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb index 05d9b63d12b..61b8eb30b42 100644 --- a/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/init_command_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Gitlab::Kubernetes::Helm::InitCommand do - subject(:init_command) { described_class.new(name: application.name, files: files, rbac: rbac) } + subject(:init_command) { described_class.new(name: application.name, files: files, rbac: rbac, local_tiller_enabled: false) } let(:application) { create(:clusters_applications_helm) } let(:rbac) { false } diff --git a/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb index abd29e97505..6fc91300f5b 100644 --- a/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/install_command_spec.rb @@ -12,7 +12,8 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do version: version, repository: repository, preinstall: preinstall, - postinstall: postinstall + postinstall: postinstall, + local_tiller_enabled: local_tiller_enabled ) end @@ -22,6 +23,7 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do let(:version) { '1.2.3' } let(:preinstall) { nil } let(:postinstall) { nil } + let(:local_tiller_enabled) { true } it_behaves_like 'helm command generator' do let(:commands) do @@ -51,9 +53,7 @@ describe Gitlab::Kubernetes::Helm::InstallCommand do end context 'tillerless feature disabled' do - before do - stub_feature_flags(managed_apps_local_tiller: false) - end + let(:local_tiller_enabled) { false } let(:tls_flags) do <<~EOS.squish diff --git a/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb index eee842fa7d6..8d965a25f84 100644 --- a/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb @@ -7,6 +7,7 @@ describe Gitlab::Kubernetes::Helm::PatchCommand do let(:repository) { 'https://repository.example.com' } let(:rbac) { false } let(:version) { '1.2.3' } + let(:local_tiller_enabled) { true } subject(:patch_command) do described_class.new( @@ -15,14 +16,13 @@ describe Gitlab::Kubernetes::Helm::PatchCommand do rbac: rbac, files: files, version: version, - repository: repository + repository: repository, + local_tiller_enabled: local_tiller_enabled ) end context 'when local tiller feature is disabled' do - before do - stub_feature_flags(managed_apps_local_tiller: false) - end + let(:local_tiller_enabled) { false } let(:tls_flags) do <<~EOS.squish diff --git a/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb index 981bb4e4abf..3773c428713 100644 --- a/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb +++ b/spec/lib/gitlab/kubernetes/helm/reset_command_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Gitlab::Kubernetes::Helm::ResetCommand do - subject(:reset_command) { described_class.new(name: name, rbac: rbac, files: files) } + subject(:reset_command) { described_class.new(name: name, rbac: rbac, files: files, local_tiller_enabled: false) } let(:rbac) { true } let(:name) { 'helm' } diff --git a/spec/lib/gitlab/kubernetes/network_policy_spec.rb b/spec/lib/gitlab/kubernetes/network_policy_spec.rb index f23d215a9a1..5a920d78436 100644 --- a/spec/lib/gitlab/kubernetes/network_policy_spec.rb +++ b/spec/lib/gitlab/kubernetes/network_policy_spec.rb @@ -39,28 +39,30 @@ describe Gitlab::Kubernetes::NetworkPolicy do describe '.from_yaml' do let(:manifest) do - <<-POLICY -apiVersion: networking.k8s.io/v1 -kind: NetworkPolicy -metadata: - name: example-name - namespace: example-namespace -spec: - podSelector: - matchLabels: - role: db - policyTypes: - - Ingress - ingress: - - from: - - namespaceSelector: - matchLabels: - project: myproject + <<~POLICY + apiVersion: networking.k8s.io/v1 + kind: NetworkPolicy + metadata: + name: example-name + namespace: example-namespace + labels: + app: foo + spec: + podSelector: + matchLabels: + role: db + policyTypes: + - Ingress + ingress: + - from: + - namespaceSelector: + matchLabels: + project: myproject POLICY end let(:resource) do ::Kubeclient::Resource.new( - metadata: { name: name, namespace: namespace }, + metadata: { name: name, namespace: namespace, labels: { app: 'foo' } }, spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil } ) end @@ -83,20 +85,20 @@ spec: context 'with manifest without metadata' do let(:manifest) do - <<-POLICY -apiVersion: networking.k8s.io/v1 -kind: NetworkPolicy -spec: - podSelector: - matchLabels: - role: db - policyTypes: - - Ingress - ingress: - - from: - - namespaceSelector: - matchLabels: - project: myproject + <<~POLICY + apiVersion: networking.k8s.io/v1 + kind: NetworkPolicy + spec: + podSelector: + matchLabels: + role: db + policyTypes: + - Ingress + ingress: + - from: + - namespaceSelector: + matchLabels: + project: myproject POLICY end @@ -105,12 +107,12 @@ spec: context 'with manifest without spec' do let(:manifest) do - <<-POLICY -apiVersion: networking.k8s.io/v1 -kind: NetworkPolicy -metadata: - name: example-name - namespace: example-namespace + <<~POLICY + apiVersion: networking.k8s.io/v1 + kind: NetworkPolicy + metadata: + name: example-name + namespace: example-namespace POLICY end @@ -119,24 +121,24 @@ metadata: context 'with disallowed class' do let(:manifest) do - <<-POLICY -apiVersion: networking.k8s.io/v1 -kind: NetworkPolicy -metadata: - name: example-name - namespace: example-namespace - creationTimestamp: 2020-04-14T00:08:30Z -spec: - podSelector: - matchLabels: - role: db - policyTypes: - - Ingress - ingress: - - from: - - namespaceSelector: - matchLabels: - project: myproject + <<~POLICY + apiVersion: networking.k8s.io/v1 + kind: NetworkPolicy + metadata: + name: example-name + namespace: example-namespace + creationTimestamp: 2020-04-14T00:08:30Z + spec: + podSelector: + matchLabels: + role: db + policyTypes: + - Ingress + ingress: + - from: + - namespaceSelector: + matchLabels: + project: myproject POLICY end @@ -147,13 +149,16 @@ spec: describe '.from_resource' do let(:resource) do ::Kubeclient::Resource.new( - metadata: { name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z', resourceVersion: '4990' }, + metadata: { + name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z', + labels: { app: 'foo' }, resourceVersion: '4990' + }, spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil } ) end let(:generated_resource) do ::Kubeclient::Resource.new( - metadata: { name: name, namespace: namespace }, + metadata: { name: name, namespace: namespace, labels: { app: 'foo' } }, spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil } ) end @@ -213,7 +218,9 @@ spec: metadata: { name: name, namespace: namespace }, spec: { podSelector: pod_selector, policyTypes: %w(Ingress Egress), ingress: ingress, egress: egress } }.deep_stringify_keys - ) + ), + is_autodevops: false, + is_enabled: true } end @@ -221,4 +228,167 @@ spec: it { is_expected.to eq(json_policy) } end + + describe '#autodevops?' do + subject { policy.autodevops? } + + let(:chart) { nil } + let(:policy) do + described_class.new( + name: name, + namespace: namespace, + labels: { chart: chart }, + pod_selector: pod_selector, + ingress: ingress + ) + end + + it { is_expected.to be false } + + context 'with non-autodevops chart' do + let(:chart) { 'foo' } + + it { is_expected.to be false } + end + + context 'with autodevops chart' do + let(:chart) { 'auto-deploy-app-0.6.0' } + + it { is_expected.to be true } + end + end + + describe '#enabled?' do + subject { policy.enabled? } + + let(:pod_selector) { nil } + let(:policy) do + described_class.new( + name: name, + namespace: namespace, + pod_selector: pod_selector, + ingress: ingress + ) + end + + it { is_expected.to be true } + + context 'with empty pod_selector' do + let(:pod_selector) { {} } + + it { is_expected.to be true } + end + + context 'with nil matchLabels in pod_selector' do + let(:pod_selector) { { matchLabels: nil } } + + it { is_expected.to be true } + end + + context 'with empty matchLabels in pod_selector' do + let(:pod_selector) { { matchLabels: {} } } + + it { is_expected.to be true } + end + + context 'with disabled_by label in matchLabels in pod_selector' do + let(:pod_selector) do + { matchLabels: { Gitlab::Kubernetes::NetworkPolicy::DISABLED_BY_LABEL => 'gitlab' } } + end + + it { is_expected.to be false } + end + end + + describe '#enable' do + subject { policy.enabled? } + + let(:pod_selector) { nil } + let(:policy) do + described_class.new( + name: name, + namespace: namespace, + pod_selector: pod_selector, + ingress: ingress + ) + end + + before do + policy.enable + end + + it { is_expected.to be true } + + context 'with empty pod_selector' do + let(:pod_selector) { {} } + + it { is_expected.to be true } + end + + context 'with nil matchLabels in pod_selector' do + let(:pod_selector) { { matchLabels: nil } } + + it { is_expected.to be true } + end + + context 'with empty matchLabels in pod_selector' do + let(:pod_selector) { { matchLabels: {} } } + + it { is_expected.to be true } + end + + context 'with disabled_by label in matchLabels in pod_selector' do + let(:pod_selector) do + { matchLabels: { Gitlab::Kubernetes::NetworkPolicy::DISABLED_BY_LABEL => 'gitlab' } } + end + + it { is_expected.to be true } + end + end + + describe '#disable' do + subject { policy.enabled? } + + let(:pod_selector) { nil } + let(:policy) do + described_class.new( + name: name, + namespace: namespace, + pod_selector: pod_selector, + ingress: ingress + ) + end + + before do + policy.disable + end + + it { is_expected.to be false } + + context 'with empty pod_selector' do + let(:pod_selector) { {} } + + it { is_expected.to be false } + end + + context 'with nil matchLabels in pod_selector' do + let(:pod_selector) { { matchLabels: nil } } + + it { is_expected.to be false } + end + + context 'with empty matchLabels in pod_selector' do + let(:pod_selector) { { matchLabels: {} } } + + it { is_expected.to be false } + end + + context 'with disabled_by label in matchLabels in pod_selector' do + let(:pod_selector) do + { matchLabels: { Gitlab::Kubernetes::NetworkPolicy::DISABLED_BY_LABEL => 'gitlab' } } + end + + it { is_expected.to be false } + end + end end |