generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest_test.go
148 lines (142 loc) · 4.86 KB
/
manifest_test.go
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
package e2e
import (
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func testManifestGenerate() {
cases := []struct {
Args []string
Expected string
}{
{
Args: []string{"--egress", "--allow"},
Expected: `apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: testrule
namespace: test
spec:
egress:
- toEndpoints:
- matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: l3-ingress-explicit-allow-all
endpointSelector:
matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: self`,
},
{
Args: []string{"--egress", "--deny"},
Expected: `apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: testrule
namespace: test
spec:
egressDeny:
- toEndpoints:
- matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: l3-ingress-explicit-allow-all
endpointSelector:
matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: self`,
},
{
Args: []string{"--ingress", "--allow"},
Expected: `apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: testrule
namespace: test
spec:
endpointSelector:
matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: l3-ingress-explicit-allow-all
ingress:
- fromEndpoints:
- matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: self`,
},
{
Args: []string{"--ingress", "--deny"},
Expected: `apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: testrule
namespace: test
spec:
endpointSelector:
matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: l3-ingress-explicit-allow-all
ingressDeny:
- fromEndpoints:
- matchLabels:
k8s:group: test
k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name: test
k8s:io.cilium.k8s.policy.cluster: default
k8s:io.cilium.k8s.policy.serviceaccount: default
k8s:io.kubernetes.pod.namespace: test
k8s:test: self`,
},
}
It("should generate manifests", func() {
from := "--from=test/" + onePodByLabelSelector(Default, "test", "test=self")
to := "--to=test/" + onePodByLabelSelector(Default, "test", "test=l3-ingress-explicit-allow-all")
for _, c := range cases {
args := append([]string{"manifest", "generate", "--name=testrule", from, to}, c.Args...)
result := strings.TrimSpace(string(runViewerSafe(Default, nil, args...)))
Expect(result).To(Equal(c.Expected), "compare failed.\nactual: %s\nexpected: %s", result, c.Expected)
}
})
}
func testManifestRange() {
expected := `From,test,self
To,test,l3-ingress-explicit-allow-all
To,test,l3-ingress-explicit-allow-all`
It("should list affected pods", func() {
from := "--from=test/" + onePodByLabelSelector(Default, "test", "test=self")
to := "--to=test/" + onePodByLabelSelector(Default, "test", "test=l3-ingress-explicit-allow-all")
result := runViewerSafe(Default, nil, "manifest", "range", from, to, "-o=json")
// remove hash suffix from pod names
result = jqSafe(Default, result, "-r", `[.[] | .name = (.name | split("-") | .[0:5] | join("-"))]`)
result = jqSafe(Default, result, "-r", `[.[] | .name = (.name | if startswith("self") then "self" else . end)]`)
result = jqSafe(Default, result, "-r", `.[] | [.part, .namespace, .name] | @csv`)
resultString := strings.Replace(string(result), `"`, "", -1)
Expect(resultString).To(Equal(expected), "compare failed.\nactual: %s\nexpected: %s", resultString, expected)
})
}