github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/e2e.go (about)

     1  /*
     2  Copyright 2019 The Interconnectedcloud Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package e2e
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  	"testing"
    24  
    25  	"github.com/onsi/ginkgo"
    26  	"github.com/onsi/ginkgo/config"
    27  	"github.com/onsi/ginkgo/reporters"
    28  	"github.com/onsi/gomega"
    29  	"k8s.io/klog"
    30  
    31  	"github.com/interconnectedcloud/qdr-operator/test/e2e/framework"
    32  	"github.com/interconnectedcloud/qdr-operator/test/e2e/framework/ginkgowrapper"
    33  	e2elog "github.com/interconnectedcloud/qdr-operator/test/e2e/framework/log"
    34  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    35  )
    36  
    37  // There are certain operations we only want to run once per overall test invocation
    38  // (such as deleting old namespaces, or verifying that all system pods are running.
    39  // Because of the way Ginkgo runs tests in parallel, we must use SynchronizedBeforeSuite
    40  // to ensure that these operations only run on the first parallel Ginkgo node.
    41  //
    42  // This function takes two parameters: one function which runs on only the first Ginkgo node,
    43  // returning an opaque byte array, and then a second function which runs on all Ginkgo nodes,
    44  // accepting the byte array.
    45  var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
    46  	// Run only on Ginkgo node 1
    47  
    48  	c, err := framework.LoadClientset()
    49  	if err != nil {
    50  		klog.Fatal("Error loading client: ", err)
    51  	}
    52  
    53  	// Delete any namespaces except those created by the system. This ensures no
    54  	// lingering resources are left over from a previous test run.
    55  	if framework.TestContext.CleanStart {
    56  		deleted, err := framework.DeleteNamespaces(c, nil, /* deleteFilter */
    57  			[]string{
    58  				metav1.NamespaceSystem,
    59  				metav1.NamespaceDefault,
    60  				metav1.NamespacePublic,
    61  			})
    62  		if err != nil {
    63  			e2elog.Failf("Error deleting orphaned namespaces: %v", err)
    64  		}
    65  		klog.Infof("Waiting for deletion of the following namespaces: %v", deleted)
    66  		if err := framework.WaitForNamespacesDeleted(c, deleted, framework.NamespaceCleanupTimeout); err != nil {
    67  			e2elog.Failf("Failed to delete orphaned namespaces %v: %v", deleted, err)
    68  		}
    69  	}
    70  
    71  	return nil
    72  
    73  }, func(data []byte) {
    74  	// Run on all Ginkgo nodes
    75  })
    76  
    77  // Similar to SynchronizedBeforeSuite, we want to run some operations only once (such as collecting cluster logs).
    78  // Here, the order of functions is reversed; first, the function which runs everywhere,
    79  // and then the function that only runs on the first Ginkgo node.
    80  var _ = ginkgo.SynchronizedAfterSuite(func() {
    81  	// Run on all Ginkgo nodes
    82  	e2elog.Logf("Running AfterSuite actions on all nodes")
    83  	framework.RunCleanupActions()
    84  }, func() {
    85  
    86  })
    87  
    88  func RunE2ETests(t *testing.T) {
    89  
    90  	gomega.RegisterFailHandler(ginkgowrapper.Fail)
    91  	// Disable skipped tests unless they are explicitly requested.
    92  	if config.GinkgoConfig.FocusString == "" && config.GinkgoConfig.SkipString == "" {
    93  		config.GinkgoConfig.SkipString = `\[Flaky\]|\[Feature:.+\]`
    94  	}
    95  
    96  	// Run tests through the Ginkgo runner with output to console + JUnit for Jenkins
    97  	var r []ginkgo.Reporter
    98  	if framework.TestContext.ReportDir != "" {
    99  		// TODO: we should probably only be trying to create this directory once
   100  		// rather than once-per-Ginkgo-node.
   101  		if err := os.MkdirAll(framework.TestContext.ReportDir, 0755); err != nil {
   102  			klog.Errorf("Failed creating report directory: %v", err)
   103  		} else {
   104  			r = append(r, reporters.NewJUnitReporter(path.Join(framework.TestContext.ReportDir, fmt.Sprintf("junit_%v%02d.xml", framework.TestContext.ReportPrefix, config.GinkgoConfig.ParallelNode))))
   105  		}
   106  	}
   107  	klog.Infof("Starting e2e run %q on Ginkgo node %d", framework.RunID, config.GinkgoConfig.ParallelNode)
   108  	ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "QDR Operator e2e suite", r)
   109  }