Skip to content

Commit

Permalink
add test for modules managed command (#2164)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoipm authored Jul 2, 2024
1 parent 5266a3f commit 461b3ee
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 7 deletions.
7 changes: 0 additions & 7 deletions internal/communitymodules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"net/http"
"strings"
)
Expand Down Expand Up @@ -103,12 +102,6 @@ func ManagedModules(client cmdcommon.KubeClientConfig, cfg cmdcommon.KymaConfig)

// getManagedList gets a list of all managed modules from the Kyma CR
func getManagedList(client cmdcommon.KubeClientConfig, cfg cmdcommon.KymaConfig) ([]string, clierror.Error) {
GVRKyma := schema.GroupVersionResource{
Group: "operator.kyma-project.io",
Version: "v1beta2",
Resource: "kymas",
}

resp, err := client.KubeClient.Dynamic().Resource(GVRKyma).Namespace("kyma-system").
Get(cfg.Ctx, "default", metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
Expand Down
124 changes: 124 additions & 0 deletions internal/communitymodules/modules_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package communitymodules

import (
"context"
"encoding/json"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
kube_fake "github.com/kyma-project/cli.v3/internal/kube/fake"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
dynamic_fake "k8s.io/client-go/dynamic/fake"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -86,9 +94,125 @@ func Test_modulesCatalog(t *testing.T) {
})
}

func Test_ManagedModules(t *testing.T) {
t.Run("ok", func(t *testing.T) {
expectedResult := moduleMap{
"module1": row{
Name: "module1",
Managed: "True",
},
"module2": row{
Name: "module2",
Managed: "True",
},
"module3": row{
Name: "module3",
Managed: "True",
},
}

testKyma := fixTestKyma()

scheme := runtime.NewScheme()
scheme.AddKnownTypes(GVRKyma.GroupVersion(), testKyma)
dynamic := dynamic_fake.NewSimpleDynamicClient(scheme, testKyma)

kubeClient := &kube_fake.FakeKubeClient{
TestKubernetesInterface: nil,
TestDynamicInterface: dynamic,
}

kymaConfig := cmdcommon.KymaConfig{
Ctx: context.Background(),
}

modules, err := ManagedModules(cmdcommon.KubeClientConfig{
Kubeconfig: "",
KubeClient: kubeClient,
}, kymaConfig)

assert.Equal(t, expectedResult, modules)
assert.Nil(t, err)
})
t.Run("kyma cr not found", func(t *testing.T) {
expectedResult := moduleMap{}

testKyma := fixTestKyma()

scheme := runtime.NewScheme()
scheme.AddKnownTypes(GVRKyma.GroupVersion(), testKyma)
dynamic := dynamic_fake.NewSimpleDynamicClient(scheme)

kubeClient := &kube_fake.FakeKubeClient{
TestKubernetesInterface: nil,
TestDynamicInterface: dynamic,
}

kymaConfig := cmdcommon.KymaConfig{
Ctx: context.Background(),
}

modules, err := ManagedModules(cmdcommon.KubeClientConfig{
Kubeconfig: "",
KubeClient: kubeClient,
}, kymaConfig)

assert.Equal(t, expectedResult, modules)
assert.Nil(t, err)
})
}

func Test_installedModules(t *testing.T) {
t.Run("ok", func(t *testing.T) {
})
}

func fixHttpResponseHandler(status int, response string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(status)
w.Write([]byte(response))
}
}

func fixTestKyma() *unstructured.Unstructured {
b := []byte(`
{
"apiVersion": "operator.kyma-project.io/v1beta2",
"kind": "Kyma",
"metadata": {
"managedFields": [
{
"fieldsV1": {
"f:spec": {
"f:modules": {
".": {},
"k:{\"name\":\"module1\"}": {
".": {},
"f:customResourcePolicy": {},
"f:name": {}
},
"k:{\"name\":\"module3\"}": {
".": {},
"f:customResourcePolicy": {},
"f:name": {}
},
"k:{\"name\":\"module2\"}": {
".": {},
"f:customResourcePolicy": {},
"f:name": {}
}
}
}
}
}
],
"name": "default",
"namespace": "kyma-system"
}
}
`)
f := make(map[string]interface{})
_ = json.Unmarshal(b, &f)
u := &unstructured.Unstructured{Object: f}
return u
}
10 changes: 10 additions & 0 deletions internal/communitymodules/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package communitymodules

import "k8s.io/apimachinery/pkg/runtime/schema"

// This structure contains only the fields currently in use.
type Modules []Module

Expand All @@ -13,3 +15,11 @@ type Version struct {
ManagerPath string `json:"managerPath,omitempty"`
Repository string `json:"repository,omitempty"`
}

var (
GVRKyma = schema.GroupVersionResource{
Group: "operator.kyma-project.io",
Version: "v1beta2",
Resource: "kymas",
}
)

0 comments on commit 461b3ee

Please sign in to comment.