-
Notifications
You must be signed in to change notification settings - Fork 35
/
create-projects_test.go
125 lines (119 loc) · 4.03 KB
/
create-projects_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
package create_projects
import (
"fmt"
"testing"
"github.com/opendevstack/ods-core/tests/utils"
projectClientV1 "github.com/openshift/client-go/project/clientset/versioned/typed/project/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rbacv1client "k8s.io/client-go/kubernetes/typed/rbac/v1"
)
func TestCreateNakedOCPProjectsThruShellScripts(t *testing.T) {
err := utils.RemoveAllTestOCProjects()
if err != nil {
t.Fatal("Unable to remove test projects")
}
stdout, stderr, err := utils.RunScriptFromBaseDir("create-projects/create-projects.sh", []string{
fmt.Sprintf("--project=%s", utils.PROJECT_NAME),
}, []string{})
if err != nil {
t.Fatalf(
"Execution of `create-project.sh` failed: \nStdOut: %s\nStdErr: %s",
stdout,
stderr)
}
CheckProjectSetup(t)
}
func CheckProjectSetup(t *testing.T) {
config, err := utils.GetOCClient()
if err != nil {
t.Fatalf("Error creating OC config: %s", err)
}
client, err := projectClientV1.NewForConfig(config)
if err != nil {
t.Fatalf("Error creating Project client: %s", err)
}
projects, err := client.Projects().List(metav1.ListOptions{})
if err != nil {
t.Fatalf("Cannot list projects: %s", err)
}
expectedProjects := []string{utils.PROJECT_NAME_CD, utils.PROJECT_NAME_TEST, utils.PROJECT_NAME_DEV}
for _, expectedProject := range expectedProjects {
if err = utils.FindProject(projects, expectedProject); err != nil {
t.Fatal(err)
}
}
rbacV1Client, err := rbacv1client.NewForConfig(config)
if err != nil {
t.Fatalf("Cannot initialize RBAC Client: %s", err)
}
roleBindings, _ := rbacV1Client.RoleBindings(utils.PROJECT_NAME_CD).List(metav1.ListOptions{})
expectedRoleBindings := []utils.RoleBinding{
{
SubjectName: "jenkins",
SubjectType: "ServiceAccount",
Namespace: utils.PROJECT_NAME_CD,
RoleName: "edit",
},
{
SubjectName: fmt.Sprintf("system:serviceaccounts:%s", utils.PROJECT_NAME_DEV),
SubjectType: "Group",
Namespace: "",
RoleName: "system:image-puller",
},
{
SubjectName: fmt.Sprintf("system:serviceaccounts:%s", utils.PROJECT_NAME_TEST),
SubjectType: "Group",
Namespace: "",
RoleName: "system:image-puller",
},
}
for _, expectedRoleBinding := range expectedRoleBindings {
if err = utils.FindRoleBinding(roleBindings, expectedRoleBinding.SubjectName, expectedRoleBinding.SubjectType, expectedRoleBinding.Namespace, expectedRoleBinding.RoleName); err != nil {
t.Error(err)
}
}
roleBindings, _ = rbacV1Client.RoleBindings(utils.PROJECT_NAME_DEV).List(metav1.ListOptions{})
expectedRoleBindings = []utils.RoleBinding{
{
SubjectName: "default",
SubjectType: "ServiceAccount",
Namespace: utils.PROJECT_NAME_DEV,
RoleName: "system:image-builder",
}, {
SubjectName: fmt.Sprintf("system:serviceaccounts:%s", utils.PROJECT_NAME_TEST),
SubjectType: "Group",
Namespace: "",
RoleName: "system:image-puller",
}, {
SubjectName: "jenkins",
SubjectType: "ServiceAccount",
Namespace: utils.PROJECT_NAME_CD,
RoleName: "admin",
},
}
for _, expectedRoleBinding := range expectedRoleBindings {
if err = utils.FindRoleBinding(roleBindings, expectedRoleBinding.SubjectName, expectedRoleBinding.SubjectType, expectedRoleBinding.Namespace, expectedRoleBinding.RoleName); err != nil {
t.Error(err)
}
}
roleBindings, _ = rbacV1Client.RoleBindings(utils.PROJECT_NAME_TEST).List(metav1.ListOptions{})
expectedRoleBindings = []utils.RoleBinding{
{
SubjectName: "default",
SubjectType: "ServiceAccount",
Namespace: utils.PROJECT_NAME_TEST,
RoleName: "system:image-builder",
}, {
SubjectName: "jenkins",
SubjectType: "ServiceAccount",
Namespace: utils.PROJECT_NAME_CD,
RoleName: "admin",
},
}
for _, expectedRoleBinding := range expectedRoleBindings {
if err = utils.FindRoleBinding(roleBindings, expectedRoleBinding.SubjectName, expectedRoleBinding.SubjectType, expectedRoleBinding.Namespace, expectedRoleBinding.RoleName); err != nil {
t.Error(err)
}
}
fmt.Println("WARNING: Seeding special and default permission groups is not tested yet!")
}