Skip to content

Commit faba790

Browse files
committed
Finish adding objects and agent labels tests
1 parent 300b803 commit faba790

File tree

3 files changed

+391
-3
lines changed

3 files changed

+391
-3
lines changed

internal/controller/provisioner/objects_test.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,86 @@ func TestBuildNginxResourceObjects_OpenShift(t *testing.T) {
785785
g.Expect(roleBinding.GetLabels()).To(Equal(expLabels))
786786
}
787787

788+
func TestBuildNginxResourceObjects_DataplaneKeySecret(t *testing.T) {
789+
t.Parallel()
790+
g := NewWithT(t)
791+
792+
agentTLSSecret := &corev1.Secret{
793+
ObjectMeta: metav1.ObjectMeta{
794+
Name: agentTLSTestSecretName,
795+
Namespace: ngfNamespace,
796+
},
797+
Data: map[string][]byte{"tls.crt": []byte("tls")},
798+
}
799+
dataplaneKeySecret := &corev1.Secret{
800+
ObjectMeta: metav1.ObjectMeta{
801+
Name: "dataplane-key-secret",
802+
Namespace: ngfNamespace,
803+
},
804+
Data: map[string][]byte{"dataplane.key": []byte("keydata")},
805+
}
806+
fakeClient := fake.NewFakeClient(agentTLSSecret, dataplaneKeySecret)
807+
808+
dataplaneKeySecretName := "dataplane-key-secret" //nolint:gosec // not credentials
809+
810+
provisioner := &NginxProvisioner{
811+
cfg: Config{
812+
GatewayPodConfig: &config.GatewayPodConfig{
813+
Namespace: ngfNamespace,
814+
},
815+
AgentTLSSecretName: agentTLSTestSecretName,
816+
NginxOneConsoleTelemetryConfig: config.NginxOneConsoleTelemetryConfig{
817+
DataplaneKeySecretName: dataplaneKeySecretName,
818+
EndpointHost: "my.endpoint.com",
819+
EndpointPort: 443,
820+
EndpointTLSSkipVerify: false,
821+
},
822+
},
823+
k8sClient: fakeClient,
824+
baseLabelSelector: metav1.LabelSelector{
825+
MatchLabels: map[string]string{
826+
"app": "nginx",
827+
},
828+
},
829+
}
830+
831+
gateway := &gatewayv1.Gateway{
832+
ObjectMeta: metav1.ObjectMeta{
833+
Name: "gw",
834+
Namespace: "default",
835+
},
836+
}
837+
838+
resourceName := "gw-nginx"
839+
objects, err := provisioner.buildNginxResourceObjects(resourceName, gateway, &graph.EffectiveNginxProxy{})
840+
g.Expect(err).ToNot(HaveOccurred())
841+
g.Expect(objects).To(HaveLen(7)) // 2 secrets, 2 configmaps, serviceaccount, service, deployment
842+
843+
// Find the dataplane key secret
844+
var found bool
845+
for _, obj := range objects {
846+
if s, ok := obj.(*corev1.Secret); ok {
847+
if s.GetName() == controller.CreateNginxResourceName(resourceName, dataplaneKeySecretName) {
848+
found = true
849+
g.Expect(s.Data).To(HaveKey("dataplane.key"))
850+
g.Expect(s.Data["dataplane.key"]).To(Equal([]byte("keydata")))
851+
}
852+
}
853+
}
854+
g.Expect(found).To(BeTrue())
855+
856+
// Check deployment mounts the secret
857+
dep, ok := objects[6].(*appsv1.Deployment)
858+
g.Expect(ok).To(BeTrue())
859+
g.Expect(dep).ToNot(BeNil())
860+
container := dep.Spec.Template.Spec.Containers[0]
861+
g.Expect(container.VolumeMounts).To(ContainElement(corev1.VolumeMount{
862+
Name: "agent-dataplane-key",
863+
MountPath: "/etc/nginx-agent/secrets/dataplane.key",
864+
SubPath: "dataplane.key",
865+
}))
866+
}
867+
788868
func TestGetAndUpdateSecret_NotFound(t *testing.T) {
789869
t.Parallel()
790870
g := NewWithT(t)
@@ -989,6 +1069,50 @@ func TestBuildNginxResourceObjectsForDeletion_OpenShift(t *testing.T) {
9891069
validateMeta(roleBinding, deploymentNSName.Name)
9901070
}
9911071

1072+
func TestBuildNginxResourceObjectsForDeletion_DataplaneKeySecret(t *testing.T) {
1073+
t.Parallel()
1074+
g := NewWithT(t)
1075+
1076+
dataplaneKeySecretName := "dataplane-key-secret" //nolint:gosec // not credentials
1077+
1078+
provisioner := &NginxProvisioner{
1079+
cfg: Config{
1080+
NginxOneConsoleTelemetryConfig: config.NginxOneConsoleTelemetryConfig{
1081+
DataplaneKeySecretName: dataplaneKeySecretName,
1082+
},
1083+
AgentTLSSecretName: agentTLSTestSecretName,
1084+
},
1085+
}
1086+
1087+
deploymentNSName := types.NamespacedName{
1088+
Name: "gw-nginx",
1089+
Namespace: "default",
1090+
}
1091+
1092+
objects := provisioner.buildNginxResourceObjectsForDeletion(deploymentNSName)
1093+
1094+
// Should include the dataplane key secret in the objects list
1095+
// Default: deployment, daemonset, service, serviceaccount, 2 configmaps, agentTLSSecret, dataplaneKeySecret
1096+
g.Expect(objects).To(HaveLen(8))
1097+
1098+
validateMeta := func(obj client.Object, name string) {
1099+
g.Expect(obj.GetName()).To(Equal(name))
1100+
g.Expect(obj.GetNamespace()).To(Equal(deploymentNSName.Namespace))
1101+
}
1102+
1103+
// Validate the dataplane key secret is present
1104+
found := false
1105+
for _, obj := range objects {
1106+
if s, ok := obj.(*corev1.Secret); ok {
1107+
if s.GetName() == controller.CreateNginxResourceName(deploymentNSName.Name, dataplaneKeySecretName) {
1108+
validateMeta(s, controller.CreateNginxResourceName(deploymentNSName.Name, dataplaneKeySecretName))
1109+
found = true
1110+
}
1111+
}
1112+
}
1113+
g.Expect(found).To(BeTrue())
1114+
}
1115+
9921116
func TestSetIPFamily(t *testing.T) {
9931117
t.Parallel()
9941118
g := NewWithT(t)
@@ -1070,6 +1194,46 @@ func TestBuildNginxConfigMaps_WorkerConnections(t *testing.T) {
10701194
g.Expect(bootstrapCM.Data["main.conf"]).To(ContainSubstring("worker_connections 2048;"))
10711195
}
10721196

1197+
func TestBuildNginxConfigMaps_AgentFields(t *testing.T) {
1198+
t.Parallel()
1199+
g := NewWithT(t)
1200+
1201+
provisioner := &NginxProvisioner{
1202+
cfg: Config{
1203+
GatewayPodConfig: &config.GatewayPodConfig{
1204+
Namespace: "default",
1205+
ServiceName: "test-service",
1206+
},
1207+
AgentLabels: map[string]string{
1208+
"key1": "val1",
1209+
"key2": "val2",
1210+
},
1211+
NginxOneConsoleTelemetryConfig: config.NginxOneConsoleTelemetryConfig{
1212+
DataplaneKeySecretName: "dataplane-key-secret",
1213+
EndpointHost: "console.example.com",
1214+
EndpointPort: 443,
1215+
EndpointTLSSkipVerify: false,
1216+
},
1217+
},
1218+
}
1219+
objectMeta := metav1.ObjectMeta{Name: "test", Namespace: "default"}
1220+
1221+
nProxyCfgEmpty := &graph.EffectiveNginxProxy{}
1222+
1223+
configMaps := provisioner.buildNginxConfigMaps(objectMeta, nProxyCfgEmpty, "test-bootstrap", "test-agent", true, true)
1224+
g.Expect(configMaps).To(HaveLen(2))
1225+
1226+
agentCM, ok := configMaps[1].(*corev1.ConfigMap)
1227+
g.Expect(ok).To(BeTrue())
1228+
data := agentCM.Data["nginx-agent.conf"]
1229+
1230+
g.Expect(data).To(ContainSubstring("key1: val1"))
1231+
g.Expect(data).To(ContainSubstring("key2: val2"))
1232+
g.Expect(data).To(ContainSubstring("host: console.example.com"))
1233+
g.Expect(data).To(ContainSubstring("port: 443"))
1234+
g.Expect(data).To(ContainSubstring("skip_verify: false"))
1235+
}
1236+
10731237
func TestBuildReadinessProbe(t *testing.T) {
10741238
t.Parallel()
10751239

0 commit comments

Comments
 (0)