Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test : Add Unit Test for redis-standalone.yaml #683

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 308 additions & 0 deletions k8sutils/redis-standalone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
package k8sutils

import (
"os"
"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"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/utils/pointer"
)

func Test_generateRedisStandaloneParams(t *testing.T) {
path := filepath.Join("..", "tests", "testdata", "redis-standalone.yaml")
expected := statefulSetParameters{
Replicas: pointer.Int32(1),
ClusterMode: false,
NodeConfVolume: false,
// Metadata: metav1.ObjectMeta{
// Name: "redis-standalone",
// Namespace: "redis",
// Labels: map[string]string{
// "app": "redis-standalone"},
// Annotations: map[string]string{
// "opstreelabs.in.redis": "true"},
// },
NodeSelector: map[string]string{
"node-role.kubernetes.io/infra": "worker"},
PodSecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(1000),
FSGroup: pointer.Int64(1000),
},
PriorityClassName: "high-priority",
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "node-role.kubernetes.io/infra",
Operator: corev1.NodeSelectorOpIn,
Values: []string{"worker"},
},
},
},
},
},
},
},
Tolerations: &[]corev1.Toleration{
{
Key: "node-role.kubernetes.io/infra",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
},
{
Key: "node-role.kubernetes.io/infra",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
},
},
PersistentVolumeClaim: corev1.PersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: pointer.String("standard"),
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("1Gi"),
},
},
},
},
EnableMetrics: true,
ImagePullSecrets: &[]corev1.LocalObjectReference{{Name: "mysecret"}},
ExternalConfig: pointer.String("redis-external-config"),
ServiceAccountName: pointer.String("redis-sa"),
TerminationGracePeriodSeconds: pointer.Int64(30),
IgnoreAnnotations: []string{"opstreelabs.in/ignore"},
}

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 := 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)
}

func Test_generateRedisStandaloneInitContainerParams(t *testing.T) {
path := filepath.Join("..", "tests", "testdata", "redis-standalone.yaml")
expected := initContainerParameters{
Enabled: pointer.Bool(true),
Image: "quay.io/opstree/redis-operator-restore:latest",
ImagePullPolicy: corev1.PullPolicy("Always"),
Resources: &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"),
},
},
Role: "standalone",
Command: []string{"/bin/bash", "-c", "/app/restore.bash"},
Arguments: []string{"--restore-from", "redis-standalone-restore"},
PersistenceEnabled: pointer.Bool(true),
AdditionalEnvVariable: &[]corev1.EnvVar{
{
Name: "CLUSTER_NAME",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "env-secrets",
},
Key: "CLUSTER_NAME",
},
},
},
{
Name: "CLUSTER_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "env-secrets",
},
Key: "CLUSTER_NAMESPACE",
},
},
},
},
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 := generateRedisStandaloneInitContainerParams(input)
assert.EqualValues(t, expected, actual, "Expected %+v, got %+v", expected, actual)
}
1 change: 0 additions & 1 deletion k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type statefulSetParameters struct {
ServiceAccountName *string
UpdateStrategy appsv1.StatefulSetUpdateStrategy
RecreateStatefulSet bool
InitContainers *[]redisv1beta2.InitContainer
TerminationGracePeriodSeconds *int64
IgnoreAnnotations []string
}
Expand Down
Loading