@@ -3,55 +3,116 @@ package permissions
33import (
44 "testing"
55
6+ "github.com/d4l3k/messagediff"
67 "github.com/jetstack/preflight/pkg/agent"
78 "github.com/jetstack/preflight/pkg/datagatherer/k8s"
9+ rbac "k8s.io/api/rbac/v1"
10+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
811 "k8s.io/apimachinery/pkg/runtime/schema"
912)
1013
11- func TestGenerate (t * testing.T ) {
12- inputDatagatherers := []agent.DataGatherer {
14+ func TestGenerateRBAC (t * testing.T ) {
15+ // Use these test cases to check if Generate function is correct
16+ testCases := []struct {
17+ // expectedClusterRoles is the collection of ClusterRole
18+ expectedClusterRoles []rbac.ClusterRole
19+ dataGatherers []agent.DataGatherer
20+ description string
21+ }{
1322 {
14- Name : "k8s/pods" ,
15- Kind : "k8s-dynamic" ,
16- Config : & k8s.ConfigDynamic {
17- GroupVersionResource : schema.GroupVersionResource {
18- Version : "v1" ,
19- Resource : "pods" ,
23+ description : "Generate RBAC struct for pods datagatherer" ,
24+ dataGatherers : []agent.DataGatherer {
25+ {
26+ Name : "k8s/pods" ,
27+ Kind : "k8s-dynamic" ,
28+ Config : & k8s.ConfigDynamic {
29+ GroupVersionResource : schema.GroupVersionResource {
30+ Version : "v1" ,
31+ Resource : "pods" ,
32+ },
33+ },
34+ },
35+ {
36+ Name : "k8s/secrets" ,
37+ Kind : "k8s-dynamic" ,
38+ Config : & k8s.ConfigDynamic {
39+ GroupVersionResource : schema.GroupVersionResource {
40+ Version : "v1" ,
41+ Resource : "secrets" ,
42+ },
43+ },
44+ },
45+ {
46+ Name : "k8s/awspcaissuer" ,
47+ Kind : "k8s-dynamic" ,
48+ Config : & k8s.ConfigDynamic {
49+ GroupVersionResource : schema.GroupVersionResource {
50+ Group : "awspca.cert-manager.io" ,
51+ Version : "v1" ,
52+ Resource : "awspcaissuers" ,
53+ },
54+ },
2055 },
2156 },
22- },
23- {
24- Name : "k8s/secrets" ,
25- Kind : "k8s-dynamic" ,
26- Config : & k8s.ConfigDynamic {
27- GroupVersionResource : schema.GroupVersionResource {
28- Version : "v1" ,
29- Resource : "secrets" ,
57+ expectedClusterRoles : []rbac.ClusterRole {
58+ {
59+ TypeMeta : metav1.TypeMeta {
60+ Kind : "ClusterRole" ,
61+ APIVersion : "rbac.authorization.k8s.io/v1" ,
62+ },
63+ ObjectMeta : metav1.ObjectMeta {
64+ Name : "jetstack-secure-agent-pods-reader" ,
65+ },
66+ Rules : []rbac.PolicyRule {
67+ {
68+ Verbs : []string {"get" , "list" , "watch" },
69+ APIGroups : []string {"" },
70+ Resources : []string {"pods" },
71+ },
72+ },
73+ },
74+ {
75+ TypeMeta : metav1.TypeMeta {
76+ Kind : "ClusterRole" ,
77+ APIVersion : "rbac.authorization.k8s.io/v1" ,
78+ },
79+ ObjectMeta : metav1.ObjectMeta {
80+ Name : "jetstack-secure-agent-secrets-reader" ,
81+ },
82+ Rules : []rbac.PolicyRule {
83+ {
84+ Verbs : []string {"get" , "list" , "watch" },
85+ APIGroups : []string {"" },
86+ Resources : []string {"secrets" },
87+ },
88+ },
89+ },
90+ {
91+ TypeMeta : metav1.TypeMeta {
92+ Kind : "ClusterRole" ,
93+ APIVersion : "rbac.authorization.k8s.io/v1" ,
94+ },
95+ ObjectMeta : metav1.ObjectMeta {
96+ Name : "jetstack-secure-agent-awspcaissuers-reader" ,
97+ },
98+ Rules : []rbac.PolicyRule {
99+ {
100+ Verbs : []string {"get" , "list" , "watch" },
101+ APIGroups : []string {"awspca.cert-manager.io" },
102+ Resources : []string {"awspcaissuers" },
103+ },
104+ },
30105 },
31106 },
32107 },
108+ // Try adding more test cases
33109 }
34110
35- expectedOutput := `apiVersion: rbac.authorization.k8s.io/v1
36- kind: ClusterRole
37- metadata:
38- name: jetstack-secure-agent-pods-reader
39- rules:
40- - apiGroups: [""]
41- resources: ["pods"]
42- verbs: ["get", "list", "watch"]
43- ---
44- apiVersion: rbac.authorization.k8s.io/v1
45- kind: ClusterRole
46- metadata:
47- name: jetstack-secure-agent-secrets-reader
48- rules:
49- - apiGroups: [""]
50- resources: ["secrets"]
51- verbs: ["get", "list", "watch"]`
52-
53- if output := Generate (inputDatagatherers ); output != expectedOutput {
54- t .Fatalf ("unexpected output \n %s \n expected: \n %s" , output , expectedOutput )
111+ for _ , input := range testCases {
112+ got := GenerateRoles (input .dataGatherers )
113+ if diff , equal := messagediff .PrettyDiff (input .expectedClusterRoles , got ); ! equal {
114+ t .Errorf ("%s:\n %s" , input .description , diff )
115+ t .Fatalf ("unexpected difference in RBAC cluster role: \n got \n %v\n want\n %v" , got , input .expectedClusterRoles )
116+ }
55117 }
56-
57118}
0 commit comments