Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit c10e513

Browse files
committed
Add some tests
1 parent 0ee18b7 commit c10e513

File tree

7 files changed

+614
-14
lines changed

7 files changed

+614
-14
lines changed

controllers/configmap.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ import (
99
v1 "k8s.io/api/core/v1"
1010
)
1111

12-
func (r *NginxIngressControllerReconciler) configMapForNginxIngressController(instance *k8sv1alpha1.NginxIngressController) *v1.ConfigMap {
12+
func (r *NginxIngressControllerReconciler) configMapForNginxIngressController(instance *k8sv1alpha1.NginxIngressController) (*v1.ConfigMap, error) {
1313
cm := &v1.ConfigMap{
1414
ObjectMeta: metav1.ObjectMeta{
1515
Name: instance.Name,
1616
Namespace: instance.Namespace,
1717
},
1818
Data: instance.Spec.ConfigMapData,
1919
}
20-
ctrl.SetControllerReference(instance, cm, r.Scheme)
21-
return cm
20+
if err := ctrl.SetControllerReference(instance, cm, r.Scheme); err != nil {
21+
return nil, err
22+
}
23+
return cm, nil
2224
}
2325

2426
func configMapMutateFn(cm *v1.ConfigMap, configMapData map[string]string) controllerutil.MutateFn {

controllers/ingressclass_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package controllers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
k8sv1alpha1 "github.com/nginxinc/nginx-ingress-operator/api/v1alpha1"
8+
networking "k8s.io/api/networking/v1beta1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
)
11+
12+
func TestIngressClassForNginxIngressController(t *testing.T) {
13+
instance := &k8sv1alpha1.NginxIngressController{
14+
Spec: k8sv1alpha1.NginxIngressControllerSpec{
15+
IngressClass: "nginx",
16+
},
17+
}
18+
expected := &networking.IngressClass{
19+
ObjectMeta: metav1.ObjectMeta{
20+
Name: "nginx",
21+
},
22+
Spec: networking.IngressClassSpec{
23+
Controller: "nginx.org/ingress-controller",
24+
},
25+
}
26+
27+
rec := &NginxIngressControllerReconciler{}
28+
29+
result := rec.ingressClassForNginxIngressController(instance)
30+
if diff := cmp.Diff(expected, result); diff != "" {
31+
t.Errorf("ingressClassForNginxIngressController() mismatch (-want +got):\n%s", diff)
32+
}
33+
}

controllers/nginxingresscontroller_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ func (r *NginxIngressControllerReconciler) Reconcile(ctx context.Context, req ct
230230
return ctrl.Result{}, err
231231
}
232232

233-
cm := r.configMapForNginxIngressController(instance)
233+
cm, err := r.configMapForNginxIngressController(instance)
234+
if err != nil {
235+
return ctrl.Result{}, err
236+
}
234237
res, err = controllerutil.CreateOrUpdate(ctx, r.Client, cm, configMapMutateFn(cm, instance.Spec.ConfigMapData))
235238
log.Info(fmt.Sprintf("ConfigMap %s %s", svc.Name, res))
236239
if err != nil {

controllers/rbac_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package controllers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
rbacv1 "k8s.io/api/rbac/v1"
8+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
)
10+
11+
func TestClusterRoleForNginxIngressController(t *testing.T) {
12+
name := "my-rcluster-role"
13+
expected := &rbacv1.ClusterRole{
14+
ObjectMeta: v1.ObjectMeta{
15+
Name: name,
16+
},
17+
Rules: []rbacv1.PolicyRule{
18+
{
19+
Verbs: []string{"get", "list", "watch"},
20+
APIGroups: []string{""},
21+
Resources: []string{"services", "endpoints"},
22+
},
23+
{
24+
Verbs: []string{"get", "list", "watch"},
25+
APIGroups: []string{""},
26+
Resources: []string{"secrets"},
27+
},
28+
{
29+
Verbs: []string{"get", "list", "watch", "update", "create"},
30+
APIGroups: []string{""},
31+
Resources: []string{"configmaps"},
32+
},
33+
{
34+
Verbs: []string{"list", "watch"},
35+
APIGroups: []string{""},
36+
Resources: []string{"pods"},
37+
},
38+
{
39+
Verbs: []string{"create", "patch"},
40+
APIGroups: []string{""},
41+
Resources: []string{"events"},
42+
},
43+
{
44+
Verbs: []string{"get", "list", "watch"},
45+
APIGroups: []string{"networking.k8s.io"},
46+
Resources: []string{"ingresses"},
47+
},
48+
{
49+
Verbs: []string{"update"},
50+
APIGroups: []string{"networking.k8s.io"},
51+
Resources: []string{"ingresses/status"},
52+
},
53+
{
54+
Verbs: []string{"get", "create"},
55+
APIGroups: []string{"networking.k8s.io"},
56+
Resources: []string{"ingressclasses"},
57+
},
58+
{
59+
Verbs: []string{"get", "list", "watch"},
60+
APIGroups: []string{"k8s.nginx.org"},
61+
Resources: []string{"virtualservers", "virtualserverroutes", "globalconfigurations", "transportservers", "policies"},
62+
},
63+
{
64+
Verbs: []string{"update"},
65+
APIGroups: []string{"k8s.nginx.org"},
66+
Resources: []string{"virtualservers/status", "virtualserverroutes/status", "policies/status", "transportservers/status"},
67+
},
68+
{
69+
Verbs: []string{"get", "list", "watch"},
70+
APIGroups: []string{"appprotect.f5.com"},
71+
Resources: []string{"aplogconfs", "appolicies", "apusersigs"},
72+
},
73+
},
74+
}
75+
76+
rec := &NginxIngressControllerReconciler{}
77+
78+
result := rec.clusterRoleForNginxIngressController(name)
79+
if diff := cmp.Diff(expected, result); diff != "" {
80+
t.Errorf("clusterRoleForNginxIngressController(%v) mismatch (-want +got):\n%s", name, diff)
81+
}
82+
}
83+
84+
func TestSubjectForServiceAccount(t *testing.T) {
85+
name := "my-sa"
86+
namespace := "my-nginx-ingress"
87+
expected := rbacv1.Subject{
88+
Kind: "ServiceAccount",
89+
Name: name,
90+
Namespace: namespace,
91+
}
92+
93+
result := subjectForServiceAccount(namespace, name)
94+
if diff := cmp.Diff(expected, result); diff != "" {
95+
t.Errorf("subjectForServiceAccount(%v, %v) mismatch (-want +got):\n%s", namespace, name, diff)
96+
}
97+
}
98+
99+
func TestClusterRoleBindingForNginxIngressController(t *testing.T) {
100+
name := "my-cluster-role-binding"
101+
expected := &rbacv1.ClusterRoleBinding{
102+
ObjectMeta: v1.ObjectMeta{
103+
Name: name,
104+
},
105+
RoleRef: rbacv1.RoleRef{
106+
Kind: "ClusterRole",
107+
Name: name,
108+
APIGroup: "rbac.authorization.k8s.io",
109+
},
110+
}
111+
112+
rec := &NginxIngressControllerReconciler{}
113+
114+
result := rec.clusterRoleBindingForNginxIngressController(name)
115+
if diff := cmp.Diff(expected, result); diff != "" {
116+
t.Errorf("clusterRoleBindingForNginxIngressController(%v) mismatch (-want +got):\n%s", name, diff)
117+
}
118+
}

controllers/scc_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
secv1 "github.com/openshift/api/security/v1"
9+
corev1 "k8s.io/api/core/v1"
10+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
)
12+
13+
func TestSccForNginxIngressController(t *testing.T) {
14+
var uid int64 = 101
15+
name := "my-nginx-ingress"
16+
allowPrivilegeEscalation := true
17+
18+
expected := &secv1.SecurityContextConstraints{
19+
ObjectMeta: v1.ObjectMeta{
20+
Name: name,
21+
},
22+
AllowHostPorts: false,
23+
AllowPrivilegedContainer: false,
24+
RunAsUser: secv1.RunAsUserStrategyOptions{
25+
Type: "MustRunAs",
26+
UID: &uid,
27+
},
28+
Users: nil,
29+
AllowHostDirVolumePlugin: false,
30+
AllowHostIPC: false,
31+
SELinuxContext: secv1.SELinuxContextStrategyOptions{
32+
Type: "MustRunAs",
33+
},
34+
ReadOnlyRootFilesystem: false,
35+
FSGroup: secv1.FSGroupStrategyOptions{
36+
Type: "MustRunAs",
37+
},
38+
SupplementalGroups: secv1.SupplementalGroupsStrategyOptions{
39+
Type: "MustRunAs",
40+
},
41+
Volumes: []secv1.FSType{"secret"},
42+
AllowHostPID: false,
43+
AllowHostNetwork: false,
44+
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
45+
RequiredDropCapabilities: []corev1.Capability{"ALL"},
46+
DefaultAddCapabilities: []corev1.Capability{"NET_BIND_SERVICE"},
47+
AllowedCapabilities: nil,
48+
}
49+
rec := &NginxIngressControllerReconciler{}
50+
51+
result := rec.sccForNginxIngressController(name)
52+
if diff := cmp.Diff(expected, result); diff != "" {
53+
t.Errorf("sccForNginxIngressController() mismatch (-want +got):\n%s", diff)
54+
}
55+
}
56+
57+
func TestUserForSCC(t *testing.T) {
58+
namespace := "my-nginx-ingress"
59+
name := "my-nginx-ingress-controller"
60+
expected := fmt.Sprintf("system:serviceaccount:%v:%v", namespace, name)
61+
62+
result := userForSCC(namespace, name)
63+
if expected != result {
64+
t.Errorf("userForSCC(%v, %v) returned %v but expected %v", namespace, name, result, expected)
65+
}
66+
}

controllers/service.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,3 @@ func serviceMutateFn(svc *corev1.Service, serviceType string, labels map[string]
5858
return nil
5959
}
6060
}
61-
62-
func updateService(svc *corev1.Service, instance *k8sv1alpha1.NginxIngressController) *corev1.Service {
63-
svc.Spec.Type = corev1.ServiceType(instance.Spec.ServiceType)
64-
if instance.Spec.Service != nil {
65-
svc.Labels = instance.Spec.Service.ExtraLabels
66-
} else {
67-
svc.Labels = nil
68-
}
69-
return svc
70-
}

0 commit comments

Comments
 (0)