Skip to content

Commit

Permalink
tests for hana check (#2177)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoipm authored Jul 8, 2024
1 parent 67be6a5 commit c4496e7
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions internal/cmd/alpha/hana/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package hana

import (
"context"
"github.com/kyma-project/cli.v3/internal/btp/operator"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
kube_fake "github.com/kyma-project/cli.v3/internal/kube/fake"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
dynamic_fake "k8s.io/client-go/dynamic/fake"
"testing"
)

func Test_checkHanaInstance(t *testing.T) {
t.Run("ready", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHana := fixTestHanaServiceInstance(name, namespace, nil)
config := fixCheckConfig(name, namespace, testHana)
err := checkHanaInstance(&config)
require.Nil(t, err)
})
t.Run("not found", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHana := fixTestHanaServiceInstance("other-name", namespace, nil)
config := fixCheckConfig(name, namespace, testHana)
err := checkHanaInstance(&config)
require.NotNil(t, err)
errMsg := err.String()
require.Contains(t, errMsg, "failed to get resource data")
require.Contains(t, errMsg, "serviceinstances.services.cloud.sap.com \"test-name\" not found")
require.Contains(t, errMsg, "Make sure that Hana was provisioned")
})
t.Run("not ready", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHana := fixTestHanaServiceInstance(name, namespace, &map[string]interface{}{})
config := fixCheckConfig(name, namespace, testHana)
err := checkHanaInstance(&config)
require.NotNil(t, err)
errMsg := err.String()
require.Contains(t, errMsg, "hana instance is not ready")
require.Contains(t, errMsg, "Wait for provisioning of Hana resources")
require.Contains(t, errMsg, "Check if Hana resources started without errors")
})
}

func Test_checkHanaBinding(t *testing.T) {
t.Run("ready", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHanaBinding := fixTestHanaServiceBinding(name, namespace, nil)
config := fixCheckConfig(name, namespace, testHanaBinding)
err := checkHanaBinding(&config)
require.Nil(t, err)
})
t.Run("not found", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHanaBinding := fixTestHanaServiceBinding("other-name", namespace, nil)
config := fixCheckConfig(name, namespace, testHanaBinding)
err := checkHanaBinding(&config)
require.NotNil(t, err)
errMsg := err.String()
require.Contains(t, errMsg, "failed to get resource data")
require.Contains(t, errMsg, "servicebindings.services.cloud.sap.com \"test-name\" not found")
require.Contains(t, errMsg, "Make sure that Hana was provisioned")
})
t.Run("not ready", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHanaBinding := fixTestHanaServiceBinding(name, namespace, &map[string]interface{}{})
config := fixCheckConfig(name, namespace, testHanaBinding)
err := checkHanaBinding(&config)
require.NotNil(t, err)
errMsg := err.String()
require.Contains(t, errMsg, "hana binding is not ready")
require.Contains(t, errMsg, "Wait for provisioning of Hana resources")
require.Contains(t, errMsg, "Check if Hana resources started without errors")
})
}

func Test_checkHanaBindingURL(t *testing.T) {
t.Run("ready", func(t *testing.T) {
name := "test-name"
urlName := name + "-url"
namespace := "test-namespace"
testHanaBinding := fixTestHanaServiceBinding(urlName, namespace, nil)
config := fixCheckConfig(name, namespace, testHanaBinding)
err := checkHanaBindingURL(&config)
require.Nil(t, err)
})
t.Run("not found", func(t *testing.T) {
name := "test-name"
namespace := "test-namespace"
testHanaBinding := fixTestHanaServiceBinding("other-name", namespace, nil)
config := fixCheckConfig(name, namespace, testHanaBinding)
err := checkHanaBindingURL(&config)
require.NotNil(t, err)
errMsg := err.String()
require.Contains(t, errMsg, "failed to get resource data")
require.Contains(t, errMsg, "servicebindings.services.cloud.sap.com \"test-name-url\" not found")
require.Contains(t, errMsg, "Make sure that Hana was provisioned")
})
t.Run("not ready", func(t *testing.T) {
name := "test-name"
urlName := name + "-url"
namespace := "test-namespace"
testHanaBinding := fixTestHanaServiceBinding(urlName, namespace, &map[string]interface{}{})
config := fixCheckConfig(name, namespace, testHanaBinding)
err := checkHanaBindingURL(&config)
require.NotNil(t, err)
errMsg := err.String()
require.Contains(t, errMsg, "hana URL binding is not ready")
require.Contains(t, errMsg, "Wait for provisioning of Hana resources")
require.Contains(t, errMsg, "Check if Hana resources started without errors")
})
}

func fixCheckConfig(name string, namespace string, objects ...runtime.Object) hanaCheckConfig {
scheme := runtime.NewScheme()
scheme.AddKnownTypes(operator.GVRServiceInstance.GroupVersion())
dynamic := dynamic_fake.NewSimpleDynamicClient(scheme, objects...)
config := hanaCheckConfig{
KymaConfig: &cmdcommon.KymaConfig{Ctx: context.Background()},
KubeClientConfig: cmdcommon.KubeClientConfig{KubeClient: &kube_fake.FakeKubeClient{TestDynamicInterface: dynamic}},
name: name,
namespace: namespace,
timeout: 0,
}
return config
}

func fixTestHanaServiceInstance(name, namespace string, status *map[string]interface{}) *unstructured.Unstructured {
return fixTestHanaService("ServiceInstance", name, namespace, status)
}

func fixTestHanaServiceBinding(name, namespace string, status *map[string]interface{}) *unstructured.Unstructured {
return fixTestHanaService("ServiceBinding", name, namespace, status)
}

func fixTestHanaService(kind, name, namespace string, status *map[string]interface{}) *unstructured.Unstructured {
if status == nil {
status = &map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"type": "Succeeded",
"status": string(metav1.ConditionTrue),
},
map[string]interface{}{
"type": "Ready",
"status": string(metav1.ConditionTrue),
},
},
"ready": "True",
}
}
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "services.cloud.sap.com/v1",
"kind": kind,
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
"status": *status,
},
}
}

0 comments on commit c4496e7

Please sign in to comment.