Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m00g3n committed Jan 19, 2024
1 parent 1494728 commit ae0fed6
Show file tree
Hide file tree
Showing 6 changed files with 4,507 additions and 89 deletions.
15 changes: 12 additions & 3 deletions cp_mod_migration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
. "github.com/onsi/gomega"

v293 "github.tools.sap/framefrog/cp-mod-migrator/pkg/cproxy/api/v294"
"github.tools.sap/framefrog/cp-mod-migrator/pkg/extract"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -15,15 +17,22 @@ import (

func TestCproxy(t *testing.T) {
RegisterFailHandler(Fail)
defer GinkgoRecover()

Expect(loadCMs(&cms)).ShouldNot(HaveOccurred(), "unable to load test data")
mockedClient = newClient(t, cms)

RunSpecs(t, "Cproxy Suite")
}

var (
externalDependencyDataPath = "./hack/cproxy/crd.yaml"

testEnv *envtest.Environment
k8sClient client.Client
config *rest.Config
testEnv *envtest.Environment
k8sClient client.Client
config *rest.Config
mockedClient extract.Client
cms []corev1.ConfigMap
)

func getK8sClient() (client.Client, error) {
Expand Down
14 changes: 14 additions & 0 deletions cp_mod_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
. "github.com/onsi/gomega"
migration "github.tools.sap/framefrog/cp-mod-migrator/pkg"
v294 "github.tools.sap/framefrog/cp-mod-migrator/pkg/cproxy/api/v294"
"github.tools.sap/framefrog/cp-mod-migrator/pkg/extract"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -51,6 +52,19 @@ var _ = Describe("cp-mod-migrator", Ordered, func() {
Expect(k8sClient.Delete(ctx, &cr)).ShouldNot(HaveOccurred())
})

It("should cover all existing cases", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

for range cms {
var cr v294.ConnectivityProxy
err := extract.GetCPConfiguration(ctx, &cr, mockedClient)
Expect(err).ShouldNot(HaveOccurred())
Expect(k8sClient.Create(ctx, &cr)).ShouldNot(HaveOccurred())
deleteObjs(ctx, &cr)
}
})

It("should migrate data", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
51 changes: 50 additions & 1 deletion cp_mod_migration_utilz_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package main_test

import (
"errors"
"io"
"os"
"testing"

. "github.com/onsi/gomega"

"github.com/stretchr/testify/mock"
v294 "github.tools.sap/framefrog/cp-mod-migrator/pkg/cproxy/api/v294"
"github.tools.sap/framefrog/cp-mod-migrator/pkg/extract"
"github.tools.sap/framefrog/cp-mod-migrator/pkg/mocks"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -74,3 +79,47 @@ func cp(name, namespace string) v294.ConnectivityProxy {
},
}
}

func loadCMs(data *[]corev1.ConfigMap) error {
file, err := os.Open("../../hack/testdata/configmaps.yaml")
if err != nil {
return err
}

decoder := yaml.NewYAMLOrJSONDecoder(file, 2048)
var cm corev1.ConfigMap
for {
err := decoder.Decode(&cm)
if errors.Is(err, io.EOF) {
break
}

if err != nil {
return err
}

*data = append(*data, cm)
}
return nil
}

func newClient(t *testing.T, cms []corev1.ConfigMap) extract.Client {
var index int
runFn := func(args mock.Arguments) {
cm := args.Get(2).(*corev1.ConfigMap)
*cm = cms[index]
if index == len(cms)-1 {
index = 0
return
}
index++
}

client := mocks.NewClient(t)
client.On("Get", mock.Anything, mock.Anything, mock.Anything).
Run(runFn).
Return(nil).
Times(len(cms))

return client
}
Loading

0 comments on commit ae0fed6

Please sign in to comment.