summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/kubernetes/cilium_network_policy_spec.rb
blob: 9600a70a95d2c95536dbefac5377cb0b58db4422 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Kubernetes::CiliumNetworkPolicy do
  let(:policy) do
    described_class.new(
      name: name,
      namespace: namespace,
      creation_timestamp: '2020-04-14T00:08:30Z',
      endpoint_selector: endpoint_selector,
      ingress: ingress,
      egress: egress,
      description: description
    )
  end

  let(:resource) do
    ::Kubeclient::Resource.new(
      kind: partial_class_name,
      apiVersion: "cilium.io/v2",
      metadata: { name: name, namespace: namespace, resourceVersion: resource_version },
      spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: nil }
    )
  end

  let(:name) { 'example-name' }
  let(:namespace) { 'example-namespace' }
  let(:endpoint_selector) { { matchLabels: { role: 'db' } } }
  let(:description) { 'example-description' }
  let(:partial_class_name) { described_class.name.split('::').last }
  let(:resource_version) { 101 }
  let(:ingress) do
    [
      {
        fromEndpoints: [
          { matchLabels: { project: 'myproject' } }
        ]
      }
    ]
  end

  let(:egress) do
    [
      {
        ports: [{ port: 5978 }]
      }
    ]
  end

  include_examples 'network policy common specs' do
    let(:selector) { endpoint_selector}
    let(:policy) do
      described_class.new(
        name: name,
        namespace: namespace,
        selector: selector,
        ingress: ingress,
        labels: labels,
        resource_version: resource_version
      )
    end

    let(:spec) { { endpointSelector: selector, ingress: ingress, egress: nil } }
    let(:metadata) { { name: name, namespace: namespace, resourceVersion: resource_version } }
  end

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

    it { is_expected.to eq(resource) }
  end

  describe '.from_yaml' do
    let(:manifest) do
      <<~POLICY
        apiVersion: cilium.io/v2
        kind: CiliumNetworkPolicy
        metadata:
          name: example-name
          namespace: example-namespace
          resourceVersion: 101
        spec:
          endpointSelector:
            matchLabels:
              role: db
          ingress:
          - fromEndpoints:
            - matchLabels:
                project: myproject
      POLICY
    end

    subject { Gitlab::Kubernetes::CiliumNetworkPolicy.from_yaml(manifest)&.generate }

    it { is_expected.to eq(resource) }

    context 'with nil manifest' do
      let(:manifest) { nil }

      it { is_expected.to be_nil }
    end

    context 'with invalid manifest' do
      let(:manifest) { "\tfoo: bar" }

      it { is_expected.to be_nil }
    end

    context 'with manifest without metadata' do
      let(:manifest) do
        <<~POLICY
        apiVersion: cilium.io/v2
        kind: CiliumNetworkPolicy
        spec:
          endpointSelector:
            matchLabels:
              role: db
          ingress:
          - fromEndpoints:
              matchLabels:
                project: myproject
        POLICY
      end

      it { is_expected.to be_nil }
    end

    context 'with manifest without spec' do
      let(:manifest) do
        <<~POLICY
        apiVersion: cilium.io/v2
        kind: CiliumNetworkPolicy
        metadata:
          name: example-name
          namespace: example-namespace
        POLICY
      end

      it { is_expected.to be_nil }
    end

    context 'with disallowed class' do
      let(:manifest) do
        <<~POLICY
        apiVersion: cilium.io/v2
        kind: CiliumNetworkPolicy
        metadata:
          name: example-name
          namespace: example-namespace
          creationTimestamp: 2020-04-14T00:08:30Z
        spec:
          endpointSelector:
            matchLabels:
              role: db
          ingress:
          - fromEndpoints:
              matchLabels:
                project: myproject
        POLICY
      end

      it { is_expected.to be_nil }
    end
  end

  describe '.from_resource' do
    let(:resource) do
      ::Kubeclient::Resource.new(
        metadata: {
          name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z',
          labels: { app: 'foo' }, resourceVersion: resource_version
        },
        spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: nil, labels: nil, description: nil }
      )
    end

    let(:generated_resource) do
      ::Kubeclient::Resource.new(
        kind: partial_class_name,
        apiVersion: "cilium.io/v2",
        metadata: { name: name, namespace: namespace, resourceVersion: resource_version, labels: { app: 'foo' } },
        spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: nil }
      )
    end

    subject { Gitlab::Kubernetes::CiliumNetworkPolicy.from_resource(resource)&.generate }

    it { is_expected.to eq(generated_resource) }

    context 'with nil resource' do
      let(:resource) { nil }

      it { is_expected.to be_nil }
    end

    context 'with resource without metadata' do
      let(:resource) do
        ::Kubeclient::Resource.new(
          spec: { endpointSelector: endpoint_selector, ingress: ingress, egress: nil, labels: nil, description: nil }
        )
      end

      it { is_expected.to be_nil }
    end

    context 'with resource without spec' do
      let(:resource) do
        ::Kubeclient::Resource.new(
          metadata: { name: name, namespace: namespace, uid: '128cf288-7de4-11ea-aceb-42010a800089', resourceVersion: resource_version }
        )
      end

      it { is_expected.to be_nil }
    end
  end
end