debian-mirror-gitlab/spec/lib/gitlab/kubernetes/network_policy_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

236 lines
5.8 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Kubernetes::NetworkPolicy do
2020-05-24 23:13:21 +05:30
let(:policy) do
described_class.new(
name: name,
namespace: namespace,
2020-11-24 15:15:51 +05:30
selector: selector,
2020-05-24 23:13:21 +05:30
ingress: ingress,
2020-11-24 15:15:51 +05:30
labels: labels
2020-05-24 23:13:21 +05:30
)
end
2020-10-24 23:57:45 +05:30
let(:resource) do
::Kubeclient::Resource.new(
2020-11-24 15:15:51 +05:30
kind: Gitlab::Kubernetes::NetworkPolicy::KIND,
2020-10-24 23:57:45 +05:30
metadata: { name: name, namespace: namespace },
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
)
end
2020-11-24 15:15:51 +05:30
let(:selector) { pod_selector }
let(:labels) { nil }
2020-05-24 23:13:21 +05:30
let(:name) { 'example-name' }
let(:namespace) { 'example-namespace' }
let(:pod_selector) { { matchLabels: { role: 'db' } } }
let(:ingress) do
[
{
from: [
{ namespaceSelector: { matchLabels: { project: 'myproject' } } }
]
}
]
end
let(:egress) do
[
{
ports: [{ port: 5978 }]
}
]
end
2020-11-24 15:15:51 +05:30
include_examples 'network policy common specs'
2020-10-24 23:57:45 +05:30
2020-05-24 23:13:21 +05:30
describe '.from_yaml' do
let(:manifest) do
2020-06-23 00:09:42 +05:30
<<~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
2020-05-24 23:13:21 +05:30
POLICY
end
subject { Gitlab::Kubernetes::NetworkPolicy.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
2020-06-23 00:09:42 +05:30
<<~POLICY
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
project: myproject
2020-05-24 23:13:21 +05:30
POLICY
end
it { is_expected.to be_nil }
end
context 'with manifest without spec' do
let(:manifest) do
2020-06-23 00:09:42 +05:30
<<~POLICY
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: example-name
namespace: example-namespace
2020-05-24 23:13:21 +05:30
POLICY
end
it { is_expected.to be_nil }
end
context 'with disallowed class' do
let(:manifest) do
2020-06-23 00:09:42 +05:30
<<~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
2020-05-24 23:13:21 +05:30
POLICY
end
it { is_expected.to be_nil }
end
end
describe '.from_resource' do
let(:resource) do
::Kubeclient::Resource.new(
2020-06-23 00:09:42 +05:30
metadata: {
name: name, namespace: namespace, creationTimestamp: '2020-04-14T00:08:30Z',
labels: { app: 'foo' }, resourceVersion: '4990'
},
2020-05-24 23:13:21 +05:30
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
)
end
2020-10-24 23:57:45 +05:30
2020-05-24 23:13:21 +05:30
let(:generated_resource) do
::Kubeclient::Resource.new(
2020-11-24 15:15:51 +05:30
kind: Gitlab::Kubernetes::NetworkPolicy::KIND,
2020-06-23 00:09:42 +05:30
metadata: { name: name, namespace: namespace, labels: { app: 'foo' } },
2020-05-24 23:13:21 +05:30
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
)
end
subject { Gitlab::Kubernetes::NetworkPolicy.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: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: 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: '4990' }
)
end
it { is_expected.to be_nil }
end
2021-09-30 23:02:18 +05:30
context 'with environment_ids' do
subject { Gitlab::Kubernetes::NetworkPolicy.from_resource(resource, [1, 2, 3]) }
it 'includes environment_ids in as_json result' do
expect(subject.as_json).to include(environment_ids: [1, 2, 3])
end
end
2020-05-24 23:13:21 +05:30
end
2020-11-24 15:15:51 +05:30
describe '#resource' do
subject { policy.resource }
let(:resource) do
{
kind: Gitlab::Kubernetes::NetworkPolicy::KIND,
metadata: { name: name, namespace: namespace },
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
}
end
it { is_expected.to eq(resource) }
context 'with labels' do
let(:labels) { { app: 'foo' } }
let(:resource) do
{
kind: Gitlab::Kubernetes::NetworkPolicy::KIND,
metadata: { name: name, namespace: namespace, labels: { app: 'foo' } },
spec: { podSelector: pod_selector, policyTypes: %w(Ingress), ingress: ingress, egress: nil }
}
end
it { is_expected.to eq(resource) }
end
end
2020-05-24 23:13:21 +05:30
end