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

add test for modules managed command #2164

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
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",
}
)