Skip to content

Commit

Permalink
more test
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Oct 28, 2023
1 parent deda75a commit 79a2f0f
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 16 deletions.
131 changes: 131 additions & 0 deletions k8sutils/redis-standalone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"

common "github.com/OT-CONTAINER-KIT/redis-operator/api"
redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -96,3 +97,133 @@ func Test_generateRedisStandaloneParams(t *testing.T) {
actual := generateRedisStandaloneParams(input)
assert.EqualValues(t, expected, actual, "Expected %+v, got %+v", expected, actual)
}

func Test_generateRedisStandaloneContainerParams(t *testing.T) {
path := filepath.Join("..", "tests", "testdata", "redis-standalone.yaml")
expected := containerParameters{
Image: "quay.io/opstree/redis:v7.0.12",
ImagePullPolicy: corev1.PullPolicy("IfNotPresent"),
Resources: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("101m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("101m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
SecurityContext: &corev1.SecurityContext{
RunAsUser: pointer.Int64(1000),
RunAsGroup: pointer.Int64(1000),
RunAsNonRoot: pointer.Bool(true),
ReadOnlyRootFilesystem: pointer.Bool(true),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
Add: []corev1.Capability{"NET_BIND_SERVICE"},
},
},
RedisExporterImage: "quay.io/opstree/redis-exporter:v1.44.0",
RedisExporterImagePullPolicy: corev1.PullPolicy("Always"),
RedisExporterResources: &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
corev1.ResourceMemory: resource.MustParse("128Mi"),
},
},
RedisExporterEnv: &[]corev1.EnvVar{
{
Name: "REDIS_EXPORTER_INCL_SYSTEM_METRICS",
Value: "true",
},
{
Name: "UI_PROPERTIES_FILE_NAME",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "game-demo",
},
Key: "ui_properties_file_name",
},
},
},
{
Name: "SECRET_USERNAME",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "mysecret",
},
Key: "username",
},
},
},
},
Role: "standalone",
EnabledPassword: pointer.Bool(true),
SecretName: pointer.String("redis-secret"),
SecretKey: pointer.String("password"),
PersistenceEnabled: pointer.Bool(true),
TLSConfig: &redisv1beta2.TLSConfig{
TLSConfig: common.TLSConfig{
CaKeyFile: "ca.key",
CertKeyFile: "tls.crt",
KeyFile: "tls.key",
Secret: corev1.SecretVolumeSource{
SecretName: "redis-tls-cert",
},
},
},
ACLConfig: &redisv1beta2.ACLConfig{
Secret: &corev1.SecretVolumeSource{
SecretName: "acl-secret",
},
},
EnvVars: &[]corev1.EnvVar{
{
Name: "CUSTOM_ENV_VAR_1",
Value: "custom_value_1",
},
{
Name: "CUSTOM_ENV_VAR_2",
Value: "custom_value_2",
},
},
AdditionalVolume: []corev1.Volume{
{
Name: "example-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "example-configmap",
},
},
},
},
},
AdditionalMountPath: []corev1.VolumeMount{
{
MountPath: "/config",
Name: "example-config",
},
},
}

data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("Failed to read file %s: %v", path, err)
}

input := &redisv1beta2.Redis{}
err = yaml.UnmarshalStrict(data, input)
if err != nil {
t.Fatalf("Failed to unmarshal file %s: %v", path, err)
}

actual := generateRedisStandaloneContainerParams(input)
assert.EqualValues(t, expected, actual, "Expected %+v, got %+v", expected, actual)
}
61 changes: 45 additions & 16 deletions tests/testdata/redis-standalone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ spec:
additionalRedisConfig: redis-external-config
podSecurityContext:
runAsUser: 1000
fsGroup: 1000
fsGroup: 1000
securityContext:
runAsUser: 1000
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
add: ["NET_BIND_SERVICE"]
kubernetesConfig:
image: quay.io/opstree/redis:v7.0.12
imagePullPolicy: IfNotPresent
Expand Down Expand Up @@ -41,20 +49,19 @@ spec:
limits:
cpu: 100m
memory: 128Mi
# Environment Variables for Redis Exporter
# env:
# - name: REDIS_EXPORTER_INCL_SYSTEM_METRICS
# value: "true"
# - name: UI_PROPERTIES_FILE_NAME
# valueFrom:
# configMapKeyRef:
# name: game-demo
# key: ui_properties_file_name
# - name: SECRET_USERNAME
# valueFrom:
# secretKeyRef:
# name: mysecret
# key: username
env:
- name: REDIS_EXPORTER_INCL_SYSTEM_METRICS
value: "true"
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
storage:
volumeClaimTemplate:
spec:
Expand All @@ -63,6 +70,14 @@ spec:
resources:
requests:
storage: 1Gi
volumeMount:
volume:
- name: example-config
configMap:
name: example-configmap
mountPath:
- mountPath: /config
name: example-config
nodeSelector:
node-role.kubernetes.io/infra: worker
priorityClassName: high-priority
Expand All @@ -83,4 +98,18 @@ spec:
operator: "Exists"
effect: "NoExecute"
serviceAccountName: redis-sa
terminationGracePeriodSeconds: 30
terminationGracePeriodSeconds: 30
acl:
secret:
secretName: acl-secret
TLS:
ca: ca.key
cert: tls.crt
key: tls.key
secret:
secretName: redis-tls-cert
env:
- name: CUSTOM_ENV_VAR_1
value: "custom_value_1"
- name: CUSTOM_ENV_VAR_2
value: "custom_value_2"

0 comments on commit 79a2f0f

Please sign in to comment.