@@ -37,6 +37,7 @@ import (
3737 appsv1 "k8s.io/api/apps/v1"
3838 corev1 "k8s.io/api/core/v1"
3939 rbacv1 "k8s.io/api/rbac/v1"
40+ crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
4041 apierr "k8s.io/apimachinery/pkg/api/errors"
4142 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4243)
@@ -254,7 +255,7 @@ func CreateRandomE2ETestNamespace() *corev1.Namespace {
254255
255256 testNamespaceName := "gitops-e2e-test-" + randomVal
256257
257- ns := CreateNamespace (string ( testNamespaceName ) )
258+ ns := CreateNamespace (testNamespaceName )
258259 return ns
259260}
260261
@@ -329,23 +330,26 @@ func CreateManagedNamespaceWithCleanupFunc(name string, managedByNamespace strin
329330func nsDeletionFunc (ns * corev1.Namespace ) func () {
330331
331332 return func () {
333+ DeleteNamespace (ns )
334+ }
332335
333- // If you are debugging an E2E test and want to prevent its namespace from being deleted when the test ends (so that you can examine the state of resources in the namespace) you can set E2E_DEBUG_SKIP_CLEANUP env var.
334- if os .Getenv ("E2E_DEBUG_SKIP_CLEANUP" ) != "" {
335- GinkgoWriter .Println ("Skipping namespace cleanup as E2E_DEBUG_SKIP_CLEANUP is set" )
336- return
337- }
338-
339- k8sClient , _ , err := utils .GetE2ETestKubeClientWithError ()
340- Expect (err ).ToNot (HaveOccurred ())
341- err = k8sClient .Delete (context .Background (), ns , & client.DeleteOptions {PropagationPolicy : ptr .To (metav1 .DeletePropagationForeground )})
336+ }
342337
343- // Error shouldn't occur, UNLESS it's because the NS no longer exists
344- if err != nil && ! apierr .IsNotFound (err ) {
345- Expect (err ).ToNot (HaveOccurred ())
346- }
338+ func DeleteNamespace (ns * corev1.Namespace ) {
339+ // If you are debugging an E2E test and want to prevent its namespace from being deleted when the test ends (so that you can examine the state of resources in the namespace) you can set E2E_DEBUG_SKIP_CLEANUP env var.
340+ if os .Getenv ("E2E_DEBUG_SKIP_CLEANUP" ) != "" {
341+ GinkgoWriter .Println ("Skipping namespace cleanup as E2E_DEBUG_SKIP_CLEANUP is set" )
342+ return
347343 }
348344
345+ k8sClient , _ , err := utils .GetE2ETestKubeClientWithError ()
346+ Expect (err ).ToNot (HaveOccurred ())
347+ err = k8sClient .Delete (context .Background (), ns , & client.DeleteOptions {PropagationPolicy : ptr .To (metav1 .DeletePropagationForeground )})
348+
349+ // Error shouldn't occur, UNLESS it's because the NS no longer exists
350+ if err != nil && ! apierr .IsNotFound (err ) {
351+ Expect (err ).ToNot (HaveOccurred ())
352+ }
349353}
350354
351355// EnvNonOLM checks if NON_OLM var is set; this variable is set when testing on GitOps operator that is not installed via OLM
@@ -804,7 +808,8 @@ var testReportMap = map[string]testReportEntry{} // acquire testReportLock befor
804808// - Namespace parameter may be a string, *Namespace, or Namespace
805809func OutputDebugOnFail (namespaceParams ... any ) {
806810
807- // Convert parameter to string of namespace name
811+ // Convert parameter to string of namespace name:
812+ // - You can specify Namespace, *Namespae, or string, and we will convert it to string namespace
808813 namespaces := []string {}
809814 for _ , param := range namespaceParams {
810815
@@ -852,7 +857,7 @@ func OutputDebugOnFail(namespaceParams ...any) {
852857
853858 kubectlOutput , err := osFixture .ExecCommandWithOutputParam (false , "kubectl" , "get" , "all" , "-n" , namespace )
854859 if err != nil {
855- GinkgoWriter .Println ("unable to extract operator logs for namespace " , namespace , err , kubectlOutput )
860+ GinkgoWriter .Println ("unable to list " , namespace , err , kubectlOutput )
856861 continue
857862 }
858863
@@ -862,6 +867,18 @@ func OutputDebugOnFail(namespaceParams ...any) {
862867 GinkgoWriter .Println (kubectlOutput )
863868 GinkgoWriter .Println ("----------------------------------------------------------------" )
864869
870+ kubectlOutput , err = osFixture .ExecCommandWithOutputParam (false , "kubectl" , "get" , "deployments" , "-n" , namespace , "-o" , "yaml" )
871+ if err != nil {
872+ GinkgoWriter .Println ("unable to list" , namespace , err , kubectlOutput )
873+ continue
874+ }
875+
876+ GinkgoWriter .Println ("" )
877+ GinkgoWriter .Println ("----------------------------------------------------------------" )
878+ GinkgoWriter .Println ("'kubectl get deployments -n " + namespace + " -o yaml" )
879+ GinkgoWriter .Println (kubectlOutput )
880+ GinkgoWriter .Println ("----------------------------------------------------------------" )
881+
865882 }
866883
867884 kubectlOutput , err := osFixture .ExecCommandWithOutputParam (false , "kubectl" , "get" , "argocds" , "-A" , "-o" , "yaml" )
@@ -877,6 +894,37 @@ func OutputDebugOnFail(namespaceParams ...any) {
877894
878895}
879896
897+ // EnsureRunningOnOpenShift should be called if a test requires OpenShift (for example, it uses Route CR).
898+ func EnsureRunningOnOpenShift () {
899+
900+ runningOnOpenShift := RunningOnOpenShift ()
901+
902+ if ! runningOnOpenShift {
903+ Skip ("This test requires the cluster to be OpenShift" )
904+ return
905+ }
906+
907+ Expect (runningOnOpenShift ).To (BeTrueBecause ("this test is marked as requiring an OpenShift cluster, and we have detected the cluster is OpenShift" ))
908+
909+ }
910+
911+ // RunningOnOpenShift returns true if the cluster is an OpenShift cluster, false otherwise.
912+ func RunningOnOpenShift () bool {
913+ k8sClient , _ := utils .GetE2ETestKubeClient ()
914+
915+ crdList := crdv1.CustomResourceDefinitionList {}
916+ Expect (k8sClient .List (context .Background (), & crdList )).To (Succeed ())
917+
918+ openshiftAPIsFound := 0
919+ for _ , crd := range crdList .Items {
920+ if strings .Contains (crd .Spec .Group , "openshift.io" ) {
921+ openshiftAPIsFound ++
922+ }
923+ }
924+ return openshiftAPIsFound > 5 // I picked 5 as an arbitrary number, could also just be 1
925+ }
926+
927+ //nolint:unused
880928func outputPodLog (podSubstring string ) {
881929 k8sClient , _ , err := utils .GetE2ETestKubeClientWithError ()
882930 if err != nil {
0 commit comments