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 e2e test for readonly manifestwork via gRPC #130

Merged
merged 1 commit into from
Jun 17, 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
119 changes: 118 additions & 1 deletion test/e2e/pkg/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"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"
"k8s.io/apimachinery/pkg/util/rand"
workv1 "open-cluster-management.io/api/work/v1"

"github.com/openshift-online/maestro/pkg/api"
"github.com/openshift-online/maestro/pkg/api/openapi"
Expand Down Expand Up @@ -198,7 +203,7 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() {
})
})

Context("Resource ReadOnly UpdateStrategy Tests", func() {
Context("Resource ReadOnly UpdateStrategy Tests via restful api", func() {

It("create a sample deployment in the target cluster", func() {
nginxDeploy := &appsv1.Deployment{}
Expand Down Expand Up @@ -266,4 +271,116 @@ var _ = Describe("Resources", Ordered, Label("e2e-tests-resources"), func() {
})
})

Context("Resource ReadOnly UpdateStrategy Tests via gRPC", func() {
workName := "work-readonly-" + rand.String(5)
secretName := "auth"
It("create a sample screct in the target cluster", func() {
_, err := kubeClient.CoreV1().Secrets("default").Create(context.Background(), &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: "default",
},
Data: map[string][]byte{
"token": []byte("token"),
},
}, metav1.CreateOptions{})
Expect(err).ShouldNot(HaveOccurred())
})

It("post the resource bundle via gRPC client", func() {
obj := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Secret",
"metadata": map[string]interface{}{
"namespace": "default",
"name": "auth",
},
},
}
objectStr, _ := obj.MarshalJSON()
manifest := workv1.Manifest{}
manifest.Raw = objectStr
_, err := workClient.ManifestWorks(consumer_name).Create(ctx, &workv1.ManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: workName,
},
Spec: workv1.ManifestWorkSpec{
Workload: workv1.ManifestsTemplate{
Manifests: []workv1.Manifest{
{
RawExtension: runtime.RawExtension{
Raw: []byte("{\"apiVersion\":\"v1\",\"kind\":\"Secret\",\"metadata\":{\"name\":\"auth\",\"namespace\":\"default\"}}"),
},
},
},
},
ManifestConfigs: []workv1.ManifestConfigOption{
{
ResourceIdentifier: workv1.ResourceIdentifier{
Resource: "secrets",
Name: secretName,
Namespace: "default",
},
FeedbackRules: []workv1.FeedbackRule{
{
Type: workv1.JSONPathsType,
JsonPaths: []workv1.JsonPath{
{
Name: "credential",
Path: ".data",
},
},
},
},
UpdateStrategy: &workv1.UpdateStrategy{
Type: workv1.UpdateStrategyTypeReadOnly,
},
},
},
},
}, metav1.CreateOptions{})
Expect(err).ShouldNot(HaveOccurred())
})

It("get the resource status back", func() {
Eventually(func() error {
work, err := workClient.ManifestWorks(consumer_name).Get(ctx, workName, metav1.GetOptions{})
if err != nil {
return err
}

manifest := work.Status.ResourceStatus.Manifests
if len(manifest) > 0 && len(manifest[0].StatusFeedbacks.Values) != 0 {
feedback := manifest[0].StatusFeedbacks.Values
if feedback[0].Name == "credential" && *feedback[0].Value.JsonRaw == "{\"token\":\"dG9rZW4=\"}" {
return nil
}
return fmt.Errorf("the result %v is not expected", feedback[0])
}

return fmt.Errorf("manifest should be empty")
}, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred())
})

It("delete the readonly resource", func() {
err := workClient.ManifestWorks(consumer_name).Delete(ctx, workName, metav1.DeleteOptions{})
Expect(err).ShouldNot(HaveOccurred())

err = kubeClient.CoreV1().Secrets("default").Delete(context.Background(), secretName, metav1.DeleteOptions{})
Expect(err).ShouldNot(HaveOccurred())

Eventually(func() error {
_, err := kubeClient.CoreV1().Secrets("default").Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
return fmt.Errorf("auth secret still exists")
}, 1*time.Minute, 1*time.Second).ShouldNot(HaveOccurred())
})
})

})
31 changes: 3 additions & 28 deletions test/e2e/pkg/sourceclient_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package e2e_test

import (
"context"
"encoding/json"
"fmt"
"time"
Expand All @@ -21,41 +20,18 @@ import (

workv1 "open-cluster-management.io/api/work/v1"

"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc"
"open-cluster-management.io/sdk-go/pkg/cloudevents/work"
"open-cluster-management.io/sdk-go/pkg/cloudevents/work/common"
"open-cluster-management.io/sdk-go/pkg/cloudevents/work/source/codec"
)

var _ = Describe("gRPC Source ManifestWork Client Test", func() {
Context("Watch work status with gRPC source ManifestWork client", func() {
var ctx context.Context
var cancel context.CancelFunc

var sourceID string
var grpcOptions *grpc.GRPCOptions

var watchedWorks []*workv1.ManifestWork

BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())

sourceID = "sourceclient-test" + rand.String(5)

watchedWorks = []*workv1.ManifestWork{}

grpcOptions = grpc.NewGRPCOptions()
grpcOptions.URL = grpcServerAddress

workClient, err := work.NewClientHolderBuilder(grpcOptions).
WithClientID(fmt.Sprintf("%s-watcher", sourceID)).
WithSourceID(sourceID).
WithCodecs(codec.NewManifestBundleCodec()).
WithWorkClientWatcherStore(grpcsource.NewRESTFullAPIWatcherStore(apiClient, sourceID)).
WithResyncEnabled(false).
NewSourceClientHolder(ctx)
Expect(err).ShouldNot(HaveOccurred())

watcher, err := workClient.ManifestWorks(consumer_name).Watch(ctx, metav1.ListOptions{})
Expect(err).ShouldNot(HaveOccurred())

Expand Down Expand Up @@ -84,10 +60,6 @@ var _ = Describe("gRPC Source ManifestWork Client Test", func() {
}()
})

AfterEach(func() {
cancel()
})

It("The work status should be watched", func() {
workClient, err := work.NewClientHolderBuilder(grpcOptions).
WithClientID(fmt.Sprintf("%s-client", sourceID)).
Expand All @@ -103,6 +75,9 @@ var _ = Describe("gRPC Source ManifestWork Client Test", func() {
_, err = workClient.ManifestWorks(consumer_name).Create(ctx, NewManifestWork(workName), metav1.CreateOptions{})
Expect(err).ShouldNot(HaveOccurred())

// wait for few seconds to ensure the creation is finished
<-time.After(5 * time.Second)

By("list the works")
works, err := workClient.ManifestWorks(consumer_name).List(ctx, metav1.ListOptions{})
Expect(err).ShouldNot(HaveOccurred())
Expand Down
28 changes: 27 additions & 1 deletion test/e2e/pkg/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e_test

import (
"context"
"crypto/tls"
"flag"
"fmt"
Expand All @@ -10,11 +11,16 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc"
"open-cluster-management.io/sdk-go/pkg/cloudevents/work"
"open-cluster-management.io/sdk-go/pkg/cloudevents/work/source/codec"

"github.com/openshift-online/maestro/pkg/api/openapi"
"github.com/openshift-online/maestro/pkg/client/cloudevents/grpcsource"
"github.com/openshift-online/maestro/test"
)

Expand All @@ -27,6 +33,11 @@ var (
apiClient *openapi.APIClient
helper *test.Helper
T *testing.T
workClient *work.ClientHolder
grpcOptions *grpc.GRPCOptions
cancel context.CancelFunc
ctx context.Context
sourceID string
)

func TestE2E(t *testing.T) {
Expand Down Expand Up @@ -87,8 +98,23 @@ var _ = BeforeSuite(func() {
if consumer_name == "" {
panic("consumer_id is not provided")
}

ctx, cancel = context.WithCancel(context.Background())

sourceID = "sourceclient-test" + rand.String(5)
grpcOptions = grpc.NewGRPCOptions()
grpcOptions.URL = grpcServerAddress

workClient, err = work.NewClientHolderBuilder(grpcOptions).
WithClientID(fmt.Sprintf("%s-watcher", sourceID)).
WithSourceID(sourceID).
WithCodecs(codec.NewManifestBundleCodec()).
WithWorkClientWatcherStore(grpcsource.NewRESTFullAPIWatcherStore(apiClient, sourceID)).
WithResyncEnabled(false).
NewSourceClientHolder(ctx)
Expect(err).ShouldNot(HaveOccurred())
})

var _ = AfterSuite(func() {
// later...
cancel()
})
Loading