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

tests: Add E2E test for net-attach-def lifecycle #14

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
81 changes: 81 additions & 0 deletions e2e/net-attach-def_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package e2e

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"

selfservicev1 "github.com/AlonaKaplan/selfserviceoverlay/api/v1"
)

const overlayNetworkKind = "OverlayNetwork"

var _ = Describe("OverlayNetwork controller, net-attach-def lifecycle", func() {
It("should create and delete net-attach-def according to OverlayNetwork state", func() {
By("Create test OverlayNetwork instance")
ovrlyNet := newTestOverlayNetwork(TestsNamespace, "test")
Expect(RuntimeClient.Create(context.Background(), ovrlyNet)).To(Succeed())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run the tests a project admin and not cluster-admin.

Chatgpt example of how it should be done-
Create a kubeconfig file for the project admin. Save it as kubeconfig-admin.yaml:
yaml
Copy code
apiVersion: v1
kind: Config
clusters:

  • name: your-cluster-name
    cluster:
    server: https://your-cluster-server
    certificate-authority-data:
    users:
  • name: project-admin
    user:
    client-certificate-data:
    client-key-data:
    contexts:
  • name: project-admin-context
    context:
    cluster: your-cluster-name
    user: project-admin
    current-context: project-admin-context
    Replace placeholders like your-cluster-name, https://your-cluster-server, , , and with the actual values.

Update your test code to use this custom kubeconfig file:
go
Copy code
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
)

func getClientForProjectAdmin() (kubernetes.Interface, error) {
adminKubeconfigPath := "/path/to/kubeconfig-admin.yaml" // Update with the correct path

// Use the in-cluster config if running within a cluster, otherwise use the specified kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", adminKubeconfigPath)
if err != nil {
	return nil, err
}

mgr, err := ctrl.NewManager(config, ctrlOptions)
if err != nil {
	return nil, err
}

return client.New(mgr.GetConfig(), client.Options{Scheme: mgr.GetScheme(), Mapper: mgr.GetRESTMapper()})

}


By("Assert a corresponding net-attach-def has been created")
nad := &netv1.NetworkAttachmentDefinition{}
nadKey := types.NamespacedName{Namespace: ovrlyNet.Namespace, Name: ovrlyNet.Name}
Eventually(func() error {
return RuntimeClient.Get(context.Background(), nadKey, nad)
}).Should(Succeed(), "1m", "3s")

By("Assert overlay-network corresponding net-attach-def object")
assertOverlayNetworkNetAttachDef(ovrlyNet, nad)

By("Delete test OverlayNetwork instance")
Expect(RuntimeClient.Delete(context.Background(), ovrlyNet)).To(Succeed())

By("Assert the corresponding net-attach-def is deleted by and not exist")
Eventually(func() bool {
return errors.IsNotFound(RuntimeClient.Get(context.Background(), nadKey, nad))
}).Should(BeTrue(), "1m", "3s",
"the overlay-network corresponding net-attach-def should be disposed")
})
})

func assertOverlayNetworkNetAttachDef(overlyNet *selfservicev1.OverlayNetwork, nad *netv1.NetworkAttachmentDefinition) {
expectedNetAttachName := overlyNet.Namespace + "/" + overlyNet.Name
expectedCniNetConf := fmt.Sprintf(`{
"name":"test",
"type":"ovn-k8s-cni-overlay",
"netAttachDefName": "%s",
"topology":"layer2"
}`, expectedNetAttachName)

Expect(nad.Spec.Config).To(MatchJSON(expectedCniNetConf))

expectedOwnerReference := metav1.OwnerReference{
APIVersion: selfservicev1.GroupVersion.String(), // "self.service.ovn.org/netv1",
Kind: overlayNetworkKind,
Name: overlyNet.Name,
UID: overlyNet.UID,
}

Expect(nad.ObjectMeta.OwnerReferences).To(Equal([]metav1.OwnerReference{expectedOwnerReference}))
}

func newTestOverlayNetwork(namespace, name string) *selfservicev1.OverlayNetwork {
return &selfservicev1.OverlayNetwork{
TypeMeta: metav1.TypeMeta{
Kind: overlayNetworkKind,
APIVersion: selfservicev1.GroupVersion.Version,
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: selfservicev1.OverlayNetworkSpec{},
}
}