agones.dev/agones@v1.54.0/test/e2e/main_test.go (about)

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package e2e
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"strconv"
    21  	"testing"
    22  	"time"
    23  
    24  	e2eframework "agones.dev/agones/test/e2e/framework"
    25  	"github.com/pkg/errors"
    26  	log "github.com/sirupsen/logrus"
    27  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/labels"
    30  )
    31  
    32  var framework *e2eframework.Framework
    33  
    34  func TestMain(m *testing.M) {
    35  	log.SetFormatter(&log.TextFormatter{
    36  		EnvironmentOverrideColors: true,
    37  		FullTimestamp:             true,
    38  		TimestampFormat:           "2006-01-02 15:04:05.000",
    39  	})
    40  
    41  	var (
    42  		err      error
    43  		exitCode int
    44  	)
    45  
    46  	if err = e2eframework.ParseTestFlags(); err != nil {
    47  		log.WithError(err).Error("failed to parse go test flags")
    48  		os.Exit(1)
    49  	}
    50  
    51  	if framework, err = e2eframework.NewFromFlags(); err != nil {
    52  		log.WithError(err).Error("failed to setup framework")
    53  		os.Exit(1)
    54  	}
    55  
    56  	if err = cleanupNamespaces(context.Background(), framework); err != nil {
    57  		log.WithError(err).Error("failed to cleanup e2e namespaces")
    58  		os.Exit(1)
    59  	}
    60  
    61  	if framework.Namespace == "" {
    62  		// use a custom namespace - Unix timestamp
    63  		framework.Namespace = strconv.Itoa(int(time.Now().Unix()))
    64  		log.Infof("Custom namespace is set: %s", framework.Namespace)
    65  
    66  		if err := framework.CreateNamespace(framework.Namespace); err != nil {
    67  			log.WithError(err).Error("failed to create a custom namespace")
    68  			os.Exit(1)
    69  		}
    70  
    71  		defer func() {
    72  			if derr := framework.DeleteNamespace(framework.Namespace); derr != nil {
    73  				log.Error(derr)
    74  			}
    75  			os.Exit(exitCode)
    76  		}()
    77  	} else {
    78  		// use an already existing namespace
    79  		// run cleanup before tests to ensure no resources from previous runs exist
    80  		err = framework.CleanUp(framework.Namespace)
    81  		if err != nil {
    82  			log.WithError(err).Error("failed to cleanup resources")
    83  		}
    84  
    85  		defer func() {
    86  			err = framework.CleanUp(framework.Namespace)
    87  			if err != nil {
    88  				log.WithError(err).Error("failed to cleanup resources")
    89  			}
    90  			os.Exit(exitCode)
    91  		}()
    92  	}
    93  
    94  	exitCode = m.Run()
    95  }
    96  
    97  func cleanupNamespaces(ctx context.Context, framework *e2eframework.Framework) error {
    98  	// list all e2e namespaces
    99  	opts := metav1.ListOptions{LabelSelector: labels.Set(e2eframework.NamespaceLabel).String()}
   100  	list, err := framework.KubeClient.CoreV1().Namespaces().List(ctx, opts)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	// loop through them, and delete them
   106  	for _, ns := range list.Items {
   107  		if err := framework.DeleteNamespace(ns.ObjectMeta.Name); err != nil {
   108  			cause := errors.Cause(err)
   109  			if k8serrors.IsConflict(cause) {
   110  				log.WithError(cause).Warn("namespace already being deleted")
   111  				continue
   112  			}
   113  			// here just in case we need to catch other errors
   114  			log.WithField("reason", k8serrors.ReasonForError(cause)).Info("cause for namespace deletion error")
   115  			return cause
   116  		}
   117  	}
   118  
   119  	return nil
   120  }