Skip to content

Commit

Permalink
(UT): GetChaosRunnerENV and GetChaosMonitorENV Unit Test Cases (#79)
Browse files Browse the repository at this point in the history
* Added specs for getChaosRunnerENV

Signed-off-by: suresh pathipati <[email protected]>
  • Loading branch information
sureshpathipati authored and Chandan Kumar committed Oct 31, 2019
1 parent 4346f14 commit 70dc933
Showing 1 changed file with 121 additions and 1 deletion.
122 changes: 121 additions & 1 deletion pkg/controller/chaosengine/chaosengine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package chaosengine
import (
"fmt"
"testing"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
litmuschaosv1alpha1 "github.com/litmuschaos/chaos-operator/pkg/apis/litmuschaos/v1alpha1"
chaosTypes "github.com/litmuschaos/chaos-operator/pkg/controller/types"
)
Expand Down Expand Up @@ -302,3 +304,121 @@ func TestInitializeApplicationInfo(t *testing.T) {
})
}
}
func TestGetChaosRunnerENV(t *testing.T) {
fakeEngineName := "Fake Engine"
fakeNameSpace := "Fake NameSpace"
fakeServiceAcc := "Fake Service Account"
fakeAppLabel := "Fake Label"
fakeAExList := []string{"fake string"}

tests := map[string]struct {
instance *litmuschaosv1alpha1.ChaosEngine
aExList []string
expectedResult []corev1.EnvVar
}{
"Test Positive": {
instance: &litmuschaosv1alpha1.ChaosEngine{
ObjectMeta: metav1.ObjectMeta{
Name: fakeEngineName,
Namespace: fakeNameSpace,
},
Spec: litmuschaosv1alpha1.ChaosEngineSpec{
ChaosServiceAccount: fakeServiceAcc,
Appinfo: litmuschaosv1alpha1.ApplicationParams{
Applabel: fakeAppLabel,
},
},
},
aExList: fakeAExList,
expectedResult: []corev1.EnvVar{
{
Name: "CHAOSENGINE",
Value: fakeEngineName,
},
{
Name: "APP_LABEL",
Value: fakeAppLabel,
},
{
Name: "APP_NAMESPACE",
Value: fakeNameSpace,
},
{
Name: "EXPERIMENT_LIST",
Value: fmt.Sprint(strings.Join(fakeAExList, ",")),
},
{
Name: "CHAOS_SVC_ACC",
Value: fakeServiceAcc,
},
},
},
}
for name, mock := range tests {
name, mock := name, mock
t.Run(name, func(t *testing.T) {
actualResult := getChaosRunnerENV(mock.instance, mock.aExList)
if len(actualResult) != 5 {
t.Fatalf("Test %q failed: expected array length to be 5", name)
}
for index, result := range actualResult {
if result.Value != mock.expectedResult[index].Value {
t.Fatalf("Test %q failed: actual result %q, received result %q", name, result, mock.expectedResult[index])
}
}
})
}
}


func TestGetChaosMonitorENV(t *testing.T) {
fakeEngineName := "Fake Engine"
fakeNameSpace := "fake NameSpace"
fakeAUUID := types.UID("fake UUID")

tests := map[string]struct {
instance *litmuschaosv1alpha1.ChaosEngine
aUUID types.UID
expectedResult []corev1.EnvVar
}{
"Test Positive": {
instance: &litmuschaosv1alpha1.ChaosEngine{
ObjectMeta: metav1.ObjectMeta {
Name: fakeEngineName,
Namespace: fakeNameSpace,
},
},

aUUID: fakeAUUID,
expectedResult: []corev1.EnvVar{
{
Name: "CHAOSENGINE",
Value: fakeEngineName,
},
{
Name: "APP_UUID",
Value: string(fakeAUUID),
},
{
Name: "APP_NAMESPACE",
Value: fakeNameSpace,
},
},
},
}
for name, mock := range tests {
name, mock := name, mock
t.Run(name, func(t *testing.T) {
actualResult := getChaosMonitorENV(mock.instance, mock.aUUID)
if len(actualResult) != 3 {
t.Fatalf("Test %q failed: expected array length to be 3", name)
}
for index, result := range actualResult {
if result.Value != mock.expectedResult[index].Value {
t.Fatalf("Test %q failed: actual result %q, received result %q", name, result, mock.expectedResult[index])
}
}
})
}
}

0 comments on commit 70dc933

Please sign in to comment.