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

adding unit test cases for helm charts #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/operator-chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ appVersion: "1.16.0"

dependencies:
- name: operator-chart
version: 1.0.9
version: 1.0.10
repository: https://github.com/flipkart-incubator/hbase-k8s-operator/helm-charts
Binary file modified examples/operator-chart/charts/operator-chart-1.0.10.tgz
Binary file not shown.
18 changes: 18 additions & 0 deletions examples/operator-chart/example_values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: operator-hbase
namespace: hbase-operator-ns
replicaCount: 1
serviceAccountName: hbase-operator-controller-manager
managerRoleName: hbase-operator-manager-role
managerRoleBindingName: hbase-operator-manager-rolebinding
leaderElectionRoleName: hbase-operator-leader-election-role
namespaces:
- hbase-standalone-ns
- hbase-cluster-ns
- hbase-tenant-ns
image:
hbase_operator:
image_name: hbase-operator
tag: v1.0.0
kube_rbac_proxy:
image_name: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
18 changes: 0 additions & 18 deletions examples/operator-chart/values.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
name: operator-hbase
namespace: hbase-operator-ns
replicaCount: 1
serviceAccountName: hbase-operator-controller-manager
managerRoleName: hbase-operator-manager-role
managerRoleBindingName: hbase-operator-manager-rolebinding
leaderElectionRoleName: hbase-operator-leader-election-role
namespaces:
- hbase-standalone-ns
- hbase-cluster-ns
- hbase-tenant-ns
image:
hbase_operator:
image_name: hbase-operator
tag: v1.0.0
kube_rbac_proxy:
image_name: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
60 changes: 60 additions & 0 deletions examples/tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module chart-testing

go 1.19

require (
github.com/gruntwork-io/terratest v0.40.22
k8s.io/api v0.20.6
)

require (
cloud.google.com/go v0.83.0 // indirect
github.com/aws/aws-sdk-go v1.40.56 // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
github.com/go-logr/logr v0.2.0 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/googleapis/gnostic v0.4.1 // indirect
github.com/gruntwork-io/go-commons v0.8.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/otp v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/urfave/cli v1.22.2 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/apimachinery v0.20.6 // indirect
k8s.io/client-go v0.20.6 // indirect
k8s.io/klog/v2 v2.4.0 // indirect
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.0.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
674 changes: 674 additions & 0 deletions examples/tests/go.sum

Large diffs are not rendered by default.

240 changes: 240 additions & 0 deletions examples/tests/operator_sa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
package test

import (
"testing"
"fmt"
"strings"

json "encoding/json"

corev1 "k8s.io/api/core/v1"
appsv1 "k8s.io/api/apps/v1"
rbac "k8s.io/api/rbac/v1"

"github.com/gruntwork-io/terratest/modules/helm"
)

var additionalNamespaceKey = "ADDITIONAL_WATCH_NAMESPACES"
var serviceAccountName = "hbase-operator-controller-manager"
var namespaceName = "hbase-operator-ns"
var deploymentName = "operator-hbase"
var operatorConfigMapName = "hbase-operator-config"
var replicaCount = "1"
var managerRoleName = "hbase-operator-manager-role"
var managerRoleBindingName = "hbase-operator-manager-rolebinding"
var leaderElectionRoleName = "hbase-operator-leader-election-role"
var leaderElectionRoleBindingName = "hbase-operator-leader-election-rolebinding"
var kubeRbacProxyImageName = "gcr.io/kubebuilder/kube-rbac-proxy"
var kubeRbacProxyImageVersion = "v0.8.0"
var operatorImageName = "hbase-operator"
var operatorImageVersion = "v1.0.0"
var namespacesEscaped = "\"hbase-standalone-ns\\,hbase-cluster-ns\\,hbase-tenant-ns\""
var namespaces = "hbase-standalone-ns,hbase-cluster-ns,hbase-tenant-ns"

func TestOperatorServiceAccountHappyCase(t *testing.T) {
helmChartPath := "../operator-chart"
_ = "--values " + helmChartPath + "/empty_values.yaml"

options := &helm.Options{
SetValues: map[string]string{
"image.kube_rbac_proxy.image_name": kubeRbacProxyImageName,
"image.kube_rbac_proxy.tag": kubeRbacProxyImageVersion,
"image.hbase_operator.image_name": operatorImageName,
"image.hbase_operator.tag": operatorImageVersion,
"name": deploymentName,
"namespace": namespaceName,
"replicaCount": replicaCount,
"managerRoleName": managerRoleName,
"managerRoleBindingName": managerRoleBindingName,
"leaderElectionRoleName": leaderElectionRoleName,
"serviceAccountName": serviceAccountName,
"namespaces": namespacesEscaped,
},
}

output := helm.RenderTemplate(t, options, helmChartPath, "operator-example", []string{})

k8sObjects := strings.Split(output, "---")

// Service Account
var sa corev1.ServiceAccount
helm.UnmarshalK8SYaml(t, k8sObjects[1], &sa)

if sa.Name != serviceAccountName {
t.Fatalf("Rendered service account name (%s) is not expected (%s)", sa.Name, serviceAccountName)
}

if sa.Namespace != namespaceName {
t.Fatalf("Rendered namespace (%s) is not expected (%s)", sa.Namespace, namespaceName)
}

// ConfigMap for operator
var configmap corev1.ConfigMap
helm.UnmarshalK8SYaml(t, k8sObjects[3], &configmap)

if configmap.Name != operatorConfigMapName {
t.Fatalf("Rendered configmap name (%s) is not expected (%s)", configmap.Name, operatorConfigMapName)
}

if configmap.Namespace != namespaceName {
t.Fatalf("Rendered configmap namespace (%s) is not expected (%s)", configmap.Namespace, namespaceName)
}

if val, ok := configmap.Data[additionalNamespaceKey]; ok {
if val != namespaces {
t.Fatalf("Rendered configmap name (%s) key (%s) value (%s) is not expected (%s)", configmap.Name, additionalNamespaceKey, val, namespaces)
}
} else {
t.Fatalf("Rendered configmap name (%s) does not contain key (%s)", configmap.Name, additionalNamespaceKey)
}

// Leader role
var leaderRole rbac.Role
helm.UnmarshalK8SYaml(t, k8sObjects[4], &leaderRole)

if leaderRole.Name != leaderElectionRoleName {
t.Fatalf("Rendered leaderrole name (%s) is not expected (%s)", leaderRole.Name, leaderElectionRoleName)
}

if leaderRole.Namespace != namespaceName {
t.Fatalf("Rendered leaderrole namespace (%s) is not expected (%s)", leaderRole.Namespace, namespaceName)
}

// Manager role
var managerRole rbac.Role
helm.UnmarshalK8SYaml(t, k8sObjects[5], &managerRole)

if managerRole.Name != managerRoleName {
t.Fatalf("Rendered managerrole name (%s) is not expected (%s)", managerRole.Name, managerRoleName)
}

if managerRole.Namespace != namespaceName {
t.Fatalf("Rendered managerrole namespace (%s) is not expected (%s)", managerRole.Namespace, namespaceName)
}


// Role binding leader role
var leaderRoleBinding rbac.RoleBinding
helm.UnmarshalK8SYaml(t, k8sObjects[6], &leaderRoleBinding)
if leaderRoleBinding.Name != leaderElectionRoleBindingName {
t.Fatalf("Rendered leaderrolebinding name (%s) is not expected (%s)", leaderRoleBinding.Name, leaderElectionRoleBindingName)
}

if leaderRoleBinding.Namespace != namespaceName {
t.Fatalf("Rendered leaderrolebinding namespace (%s) is not expected (%s)", leaderRoleBinding.Namespace, namespaceName)
}

if leaderRoleBinding.RoleRef.Kind != "Role" {
t.Fatalf("Rendered leaderrolebinding roleref kind (%s) is not expected (%s)", leaderRoleBinding.RoleRef.Kind, "Role")
}

if leaderRoleBinding.RoleRef.Name != leaderElectionRoleName {
t.Fatalf("Rendered leaderrolebinding roleref name (%s) is not expected (%s)", leaderRoleBinding.RoleRef.Name, leaderElectionRoleName)
}

if leaderRoleBinding.Subjects[0].Kind != "ServiceAccount" {
t.Fatalf("Rendered leaderrolebinding subject kind (%s) is not expected (%s)", leaderRoleBinding.Subjects[0].Kind, "ServiceAccount")
}

if leaderRoleBinding.Subjects[0].Name != serviceAccountName {
t.Fatalf("Rendered leaderrolebinding subject name (%s) is not expected (%s)", leaderRoleBinding.Subjects[0].Name, serviceAccountName)
}

if leaderRoleBinding.Subjects[0].Namespace != namespaceName {
t.Fatalf("Rendered leaderrolebinding subject namespace (%s) is not expected (%s)", leaderRoleBinding.Subjects[0].Namespace, namespaceName)
}


// Role binding manager role
var managerRoleBinding rbac.RoleBinding
helm.UnmarshalK8SYaml(t, k8sObjects[7], &managerRoleBinding)
if managerRoleBinding.Name != managerRoleBindingName {
t.Fatalf("Rendered managerrolebinding name (%s) is not expected (%s)", managerRoleBinding.Name, managerRoleBindingName)
}

if managerRoleBinding.Namespace != namespaceName {
t.Fatalf("Rendered managerrolebinding namespace (%s) is not expected (%s)", managerRoleBinding.Namespace, namespaceName)
}

if managerRoleBinding.RoleRef.Kind != "Role" {
t.Fatalf("Rendered managerrolebinding roleref kind (%s) is not expected (%s)", managerRoleBinding.RoleRef.Kind, "Role")
}

if managerRoleBinding.RoleRef.Name != managerRoleName {
t.Fatalf("Rendered managerrolebinding roleref name (%s) is not expected (%s)", managerRoleBinding.RoleRef.Name, managerRoleName)
}

if managerRoleBinding.Subjects[0].Kind != "ServiceAccount" {
t.Fatalf("Rendered managerrolebinding subject kind (%s) is not expected (%s)", managerRoleBinding.Subjects[0].Kind, "ServiceAccount")
}

if managerRoleBinding.Subjects[0].Name != serviceAccountName {
t.Fatalf("Rendered managerrolebinding subject name (%s) is not expected (%s)", managerRoleBinding.Subjects[0].Name, serviceAccountName)
}

if managerRoleBinding.Subjects[0].Namespace != namespaceName {
t.Fatalf("Rendered managerrolebinding subject namespace (%s) is not expected (%s)", managerRoleBinding.Subjects[0].Namespace, namespaceName)
}

sss, _ := json.MarshalIndent(managerRoleBinding, "", "\t")
fmt.Print(string(sss))

// Deployment of operator
var deployment appsv1.Deployment
helm.UnmarshalK8SYaml(t, k8sObjects[8], &deployment)

if deployment.Name != deploymentName {
t.Fatalf("Rendered deployment name (%s) is not expected (%s)", deployment.Name, deploymentName)
}

if deployment.Namespace != namespaceName {
t.Fatalf("Rendered deployment namespace (%s) is not expected (%s)", deployment.Namespace, namespaceName)
}

actualReplicaCount := fmt.Sprint(*deployment.Spec.Replicas)
if actualReplicaCount != replicaCount {
t.Fatalf("Rendered deployment replicas (%s) is not expected (%s)", actualReplicaCount, replicaCount)
}

//fmt.Printf("%s", "-----------")
//fmt.Printf("%s", k8sObjects[8])
//fmt.Printf("%s", "-----------")

//sss, _ := json.MarshalIndent(deployment, "", "\t")
//fmt.Print(string(sss))
}


/*func TestOperatorServiceAccountMissingCase(t *testing.T) {
helmChartPath := "../operator-chart"

options := &helm.Options{
SetValues: map[string]string{
"image.kube_rbac_proxy.image_name": kubeRbacProxyImageName,
"image.kube_rbac_proxy.tag": kubeRbacProxyImageVersion,
"image.hbase_operator.image_name": operatorImageName,
"image.hbase_operator.tag": operatorImageVersion,
"name": deploymentName,
"namespace": "",
"replicaCount": replicaCount,
"managerRoleName": managerRoleName,
"managerRoleBindingName": managerRoleBindingName,
"leaderElectionRoleName": leaderElectionRoleName,
"serviceAccountName": "",
},
}

output := helm.RenderTemplate(t, options, helmChartPath, "operator-example", []string{})
k8sObjects := strings.Split(output, "---")

var sa corev1.ServiceAccount
helm.UnmarshalK8SYaml(t, k8sObjects[1], &sa)

if sa.Name != "" {
t.Fatalf("Rendered service account name (%s) is not expected (%s)", sa.Name, serviceAccountName)
}

if sa.Namespace != "" {
t.Fatalf("Rendered namespace (%s) is not expected (%s)", sa.Namespace, namespaceName)
}
}*/
3 changes: 1 addition & 2 deletions helm-charts/hbase-chart/templates/_hbasecluster.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ spec:
{{- $arg2 := list .Values.configuration.hadoopLogPath .Values.configuration.hadoopConfigMountPath .Values.configuration.hadoopHomePath }}
{{- $args := list $arg1 $arg2 }}
{{- $probescripts := list $nnprobescript "" }}
{{- $initscripts := list $initnnscript $initzkfcscript }}
{{- $data := dict "Values" .Values "root" .Values.deployments.namenode "scripts" $scripts "initContainers" $initContainers "args" $args "initscripts" $initscripts "probescripts" $probescripts "portsArr" $portsArr "isPodServiceRequired" $isPodServiceRequired "podManagementPolicy" $podManagementPolicy }}
{{- $data := dict "Values" .Values "root" .Values.deployments.namenode "scripts" $scripts "initContainers" $initContainers "args" $args "probescripts" $probescripts "portsArr" $portsArr "isPodServiceRequired" $isPodServiceRequired "podManagementPolicy" $podManagementPolicy }}
{{- include "hbasecluster.component" $data | indent 4 }}
{{- end }}
2 changes: 1 addition & 1 deletion helm-charts/hbase-chart/templates/meta/_component.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
- /bin/bash
- -c
- |
{{- index $.initscripts $index }}
{{- include .templateName . | indent 6 }}
args:
{{- range index $.args $index }}
- {{ . }}
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/operator-chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v2
apiVersion: v1
name: operator-chart
description: Hbase Operator helm chart for kubernetes

Expand Down