forked from openstack-k8s-operators/horizon-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for TLS mounts and volumes
Signed-off-by: Brendan Shephard <bshephar@redhat.com>
- Loading branch information
Showing
4 changed files
with
131 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package horizon | ||
|
||
import ( | ||
"testing" | ||
|
||
horizonv1 "github.com/openstack-k8s-operators/horizon-operator/api/v1beta1" | ||
"github.com/openstack-k8s-operators/lib-common/modules/common/tls" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type testCase struct { | ||
name string | ||
instance *horizonv1.Horizon | ||
expectedVolumes []corev1.Volume | ||
expectedMounts []corev1.VolumeMount | ||
expectedError bool | ||
errorContains string | ||
} | ||
|
||
func TestFormatTLSOptions(t *testing.T) { | ||
|
||
var tlsSecretName string = "generic-tls-secret" | ||
var defaultMode int32 = 256 | ||
testCases := []testCase{ | ||
{ | ||
name: "Valid TLS Configuration", | ||
instance: &horizonv1.Horizon{ | ||
Spec: horizonv1.HorizonSpec{ | ||
HorizonSpecCore: horizonv1.HorizonSpecCore{ | ||
TLS: tls.SimpleService{ | ||
GenericService: tls.GenericService{ | ||
SecretName: &tlsSecretName, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedVolumes: []corev1.Volume{ | ||
{ | ||
Name: "horizon-tls-certs", | ||
VolumeSource: corev1.VolumeSource{ | ||
Secret: &corev1.SecretVolumeSource{ | ||
SecretName: "generic-tls-secret", | ||
DefaultMode: &defaultMode, | ||
}, | ||
}, | ||
}, | ||
{Name: "combined-ca-bundle", | ||
VolumeSource: corev1.VolumeSource{ | ||
Secret: &corev1.SecretVolumeSource{ | ||
SecretName: "combined-ca-bundle", | ||
DefaultMode: &defaultMode, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
expectedMounts: []corev1.VolumeMount{ | ||
{ | ||
Name: "horizon-tls-certs", | ||
MountPath: "/var/lib/config-data/tls/certs/horizon.crt", | ||
ReadOnly: true, | ||
SubPath: "tls.crt", | ||
MountPropagation: nil, | ||
SubPathExpr: "", | ||
}, | ||
{ | ||
Name: "combined-ca-bundle", | ||
ReadOnly: true, | ||
SubPath: "tls-ca-bundle.pem", | ||
MountPath: "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", | ||
MountPropagation: nil, | ||
SubPathExpr: "", | ||
}, | ||
{ | ||
Name: "horizon-tls-certs", | ||
ReadOnly: true, | ||
SubPath: "tls.key", | ||
MountPath: "/var/lib/config-data/tls/private/horizon.key", | ||
MountPropagation: nil, | ||
SubPathExpr: "", | ||
}, | ||
}, | ||
expectedError: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
options := &TLSRequiredOptions{ | ||
containerPort: &corev1.ContainerPort{}, | ||
livenessProbe: &corev1.Probe{ProbeHandler: corev1.ProbeHandler{HTTPGet: &corev1.HTTPGetAction{}}}, | ||
readinessProbe: &corev1.Probe{ProbeHandler: corev1.ProbeHandler{HTTPGet: &corev1.HTTPGetAction{}}}, | ||
startupProbe: &corev1.Probe{ProbeHandler: corev1.ProbeHandler{HTTPGet: &corev1.HTTPGetAction{}}}, | ||
} | ||
|
||
volumes, mounts, err := options.formatTLSOptions(tc.instance) | ||
|
||
if tc.expectedError { | ||
assert.Error(t, err) | ||
if tc.errorContains != "" { | ||
assert.Contains(t, err.Error(), tc.errorContains) | ||
} | ||
} else { | ||
assert.NoError(t, err) | ||
for _, elem := range volumes { | ||
assert.Contains(t, tc.expectedVolumes, elem) | ||
} | ||
for _, elem := range mounts { | ||
assert.Contains(t, tc.expectedMounts, elem) | ||
} | ||
|
||
assert.Equal(t, corev1.URISchemeHTTPS, options.livenessProbe.HTTPGet.Scheme) | ||
assert.Equal(t, corev1.URISchemeHTTPS, options.readinessProbe.HTTPGet.Scheme) | ||
assert.Equal(t, corev1.URISchemeHTTPS, options.startupProbe.HTTPGet.Scheme) | ||
assert.Equal(t, int32(HorizonPortTLS), options.containerPort.ContainerPort) | ||
|
||
} | ||
}) | ||
} | ||
} |