summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/kubernetes/network_policy_common_shared_examples.rb
blob: f018ece0d469b259f549633cd1ba585a4312dfc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

RSpec.shared_examples 'network policy common specs' do
  let(:name) { 'example-name' }
  let(:namespace) { 'example-namespace' }
  let(:labels) { nil }

  describe '#generate' do
    subject { policy.generate }

    it { is_expected.to eq(Kubeclient::Resource.new(policy.resource)) }
  end

  describe 'as_json' do
    let(:json_policy) do
      {
        name: name,
        namespace: namespace,
        creation_timestamp: nil,
        manifest: YAML.dump(policy.resource.deep_stringify_keys),
        is_autodevops: false,
        is_enabled: true
      }
    end

    subject { policy.as_json }

    it { is_expected.to eq(json_policy) }
  end

  describe 'autodevops?' do
    subject { policy.autodevops? }

    let(:labels) { { chart: chart } }
    let(:chart) { nil }

    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(:selector) { nil }

    it { is_expected.to be true }

    context 'with empty selector' do
      let(:selector) { {} }

      it { is_expected.to be true }
    end

    context 'with nil matchLabels in selector' do
      let(:selector) { { matchLabels: nil } }

      it { is_expected.to be true }
    end

    context 'with empty matchLabels in selector' do
      let(:selector) { { matchLabels: {} } }

      it { is_expected.to be true }
    end

    context 'with disabled_by label in matchLabels in selector' do
      let(:selector) do
        { matchLabels: { Gitlab::Kubernetes::NetworkPolicyCommon::DISABLED_BY_LABEL => 'gitlab' } }
      end

      it { is_expected.to be false }
    end
  end

  describe 'enable' do
    subject { policy.enabled? }

    let(:selector) { nil }

    before do
      policy.enable
    end

    it { is_expected.to be true }

    context 'with empty selector' do
      let(:selector) { {} }

      it { is_expected.to be true }
    end

    context 'with nil matchLabels in selector' do
      let(:selector) { { matchLabels: nil } }

      it { is_expected.to be true }
    end

    context 'with empty matchLabels in selector' do
      let(:selector) { { matchLabels: {} } }

      it { is_expected.to be true }
    end

    context 'with disabled_by label in matchLabels in selector' do
      let(:selector) do
        { matchLabels: { Gitlab::Kubernetes::NetworkPolicyCommon::DISABLED_BY_LABEL => 'gitlab' } }
      end

      it { is_expected.to be true }
    end
  end

  describe 'disable' do
    subject { policy.enabled? }

    let(:selector) { nil }

    before do
      policy.disable
    end

    it { is_expected.to be false }

    context 'with empty selector' do
      let(:selector) { {} }

      it { is_expected.to be false }
    end

    context 'with nil matchLabels in selector' do
      let(:selector) { { matchLabels: nil } }

      it { is_expected.to be false }
    end

    context 'with empty matchLabels in selector' do
      let(:selector) { { matchLabels: {} } }

      it { is_expected.to be false }
    end

    context 'with disabled_by label in matchLabels in selector' do
      let(:selector) do
        { matchLabels: { Gitlab::Kubernetes::NetworkPolicyCommon::DISABLED_BY_LABEL => 'gitlab' } }
      end

      it { is_expected.to be false }
    end
  end
end