forked from grafana/tanka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_test.go
147 lines (131 loc) · 4.55 KB
/
diff_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
package kubernetes
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/grafana/tanka/pkg/kubernetes/client"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
)
// TestSeparate checks that separate properly separates resources:
//
// - cluster-wide resources are always `live`
// - resources with missing namespaces:
// - `soon` if their namespace is included in the Jsonnet
// - `live` otherwise, to cause a helpful error message
// for the user that the namespace is indeed missing
// - `namespace: ""` should be properly translated into the default namespace
func TestSeparate(t *testing.T) {
cases := []struct {
name string
// state returned from Jsonnet
state manifest.List
// namespaces that exist
namespaces []string
// the default namespace (no value -> implicit default '')
defaultNs string
// resources that can be checked with the cluster
live manifest.List
// resources depending on a condition that will be met on next apply
soon manifest.List
}{
{
name: "multi",
defaultNs: "default",
namespaces: []string{
"default",
"kube-system",
"custom",
},
state: manifest.List{
// cluster-wide resources: always live
m("rbac.authorization.k8s.io/v1", "ClusterRole", "globalRole", ""),
m("rbac.authorization.k8s.io/v1", "ClusterRoleBinding", "binding", "whydoihaveanamespace"),
// default, existing namespace: `live`
m("apps/v1", "Deployment", "loki", ""),
m("apps/v1", "Deployment", "grafana", "default"),
// custom, existing namespace: `live`
m("apps/v1", "Deployment", "cortex", "custom"),
// custom, soon existing namespace:
m("v1", "Namespace", "monitoring", ""), // `live`
m("apps/v1", "Deployment", "prometheus", "monitoring"), // `soon`
// custom, missing namespace: `live`
m("apps/v1", "Deployment", "metrictank", "metrics"),
},
live: manifest.List{
m("rbac.authorization.k8s.io/v1", "ClusterRole", "globalRole", ""),
m("rbac.authorization.k8s.io/v1", "ClusterRoleBinding", "binding", "whydoihaveanamespace"),
m("apps/v1", "Deployment", "loki", ""),
m("apps/v1", "Deployment", "grafana", "default"),
m("apps/v1", "Deployment", "cortex", "custom"),
m("v1", "Namespace", "monitoring", ""),
m("apps/v1", "Deployment", "metrictank", "metrics"),
},
soon: manifest.List{
m("apps/v1", "Deployment", "prometheus", "monitoring"),
},
},
{
name: "default/soon",
defaultNs: "grafana",
namespaces: []string{"default", "kube-system"}, // `grafana` missing
state: manifest.List{
m("v1", "Namespace", "grafana", ""), // `grafana` created during apply
m("apps/v1", "Deployment", "prometheus", "grafana"),
m("apps/v1", "Deployment", "cortex", ""), // implicit default `""`
},
live: manifest.List{
m("v1", "Namespace", "grafana", ""),
},
soon: manifest.List{
m("apps/v1", "Deployment", "prometheus", "grafana"),
m("apps/v1", "Deployment", "cortex", ""),
},
},
{
name: "default/missing",
defaultNs: "grafana",
namespaces: []string{"default", "kube-system"}, // `grafana` missing
state: manifest.List{
// m("", "Namespace", "grafana", ""), <- `grafana` NOT created
m("apps/v1", "Deployment", "prometheus", "grafana"),
m("apps/v1", "Deployment", "cortex", ""), // implicit default (`""`)
},
live: manifest.List{
// `live`, so user notices missing ns
m("apps/v1", "Deployment", "prometheus", "grafana"),
m("apps/v1", "Deployment", "cortex", ""),
},
},
}
// static set of resources for this test (usually obtained using
// `client.Resources()`)
staticResources := client.Resources{
{APIGroup: "", Kind: "Namespace", Namespaced: false},
{APIGroup: "apps/v1", Kind: "Deployment", Namespaced: true},
{APIGroup: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole", Namespaced: false},
{APIGroup: "rbac.authorization.k8s.io/v1", Kind: "ClusterRoleBinding", Namespaced: false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
namespaces := make(map[string]bool)
for _, n := range c.namespaces {
namespaces[n] = true
}
live, soon := separate(c.state, c.defaultNs, separateOpts{
namespaces: namespaces,
resources: staticResources,
})
assert.ElementsMatch(t, c.live, live, "live")
assert.ElementsMatch(t, c.soon, soon, "soon")
})
}
}
func m(apiVersion, kind, name, namespace string) manifest.Manifest {
return manifest.Manifest{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
}
}