Skip to content

Commit

Permalink
standalone-unit-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 42dfc93 commit deda75a
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
98 changes: 98 additions & 0 deletions k8sutils/redis-standalone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package k8sutils

import (
"os"
"path/filepath"
"testing"

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)
}
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
86 changes: 86 additions & 0 deletions tests/testdata/redis-standalone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: Redis
metadata:
name: redis-standalone
namespace: redis
labels:
app: redis-standalone
annotations:
opstreelabs.in/redis: "true"
spec:
redisConfig:
additionalRedisConfig: redis-external-config
podSecurityContext:
runAsUser: 1000
fsGroup: 1000
kubernetesConfig:
image: quay.io/opstree/redis:v7.0.12
imagePullPolicy: IfNotPresent
imagePullSecrets:
- name: mysecret
resources:
requests:
cpu: 101m
memory: 128Mi
limits:
cpu: 101m
memory: 128Mi
redisSecret:
name: redis-secret
key: password
ignoreAnnotations:
- "opstreelabs.in/ignore"
redisExporter:
enabled: true
image: quay.io/opstree/redis-exporter:v1.44.0
imagePullPolicy: Always
resources:
requests:
cpu: 100m
memory: 128Mi
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
storage:
volumeClaimTemplate:
spec:
storageClassName: standard
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
nodeSelector:
node-role.kubernetes.io/infra: worker
priorityClassName: high-priority
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/infra
operator: In
values:
- worker
tolerations:
- key: "node-role.kubernetes.io/infra"
operator: "Exists"
effect: "NoSchedule"
- key: "node-role.kubernetes.io/infra"
operator: "Exists"
effect: "NoExecute"
serviceAccountName: redis-sa
terminationGracePeriodSeconds: 30

0 comments on commit deda75a

Please sign in to comment.