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

     1  // Copyright 2023 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 allocator
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"strconv"
    21  	"testing"
    22  	"time"
    23  
    24  	helper "agones.dev/agones/test/e2e/allochelper"
    25  	e2eframework "agones.dev/agones/test/e2e/framework"
    26  	log "github.com/sirupsen/logrus"
    27  )
    28  
    29  var framework *e2eframework.Framework
    30  
    31  func TestMain(m *testing.M) {
    32  	log.SetFormatter(&log.TextFormatter{
    33  		EnvironmentOverrideColors: true,
    34  		FullTimestamp:             true,
    35  		TimestampFormat:           "2006-01-02 15:04:05.000",
    36  	})
    37  
    38  	var (
    39  		err      error
    40  		exitCode int
    41  	)
    42  
    43  	if err = e2eframework.ParseTestFlags(); err != nil {
    44  		log.WithError(err).Error("failed to parse go test flags")
    45  		os.Exit(1)
    46  	}
    47  
    48  	if framework, err = e2eframework.NewFromFlags(); err != nil {
    49  		log.WithError(err).Error("failed to setup framework")
    50  		os.Exit(1)
    51  	}
    52  
    53  	if err = helper.CleanupNamespaces(context.Background(), framework); err != nil {
    54  		log.WithError(err).Error("failed to cleanup e2e namespaces")
    55  		os.Exit(1)
    56  	}
    57  
    58  	if framework.Namespace == "" {
    59  		// use a custom namespace - Unix timestamp
    60  		framework.Namespace = strconv.Itoa(int(time.Now().Unix()))
    61  		log.Infof("Custom namespace is set: %s", framework.Namespace)
    62  
    63  		if err := framework.CreateNamespace(framework.Namespace); err != nil {
    64  			log.WithError(err).Error("failed to create a custom namespace")
    65  			os.Exit(1)
    66  		}
    67  
    68  		defer func() {
    69  			if derr := framework.DeleteNamespace(framework.Namespace); derr != nil {
    70  				log.Error(derr)
    71  			}
    72  			os.Exit(exitCode)
    73  		}()
    74  	} else {
    75  		// use an already existing namespace
    76  		// run cleanup before tests to ensure no resources from previous runs exist
    77  		err = framework.CleanUp(framework.Namespace)
    78  		if err != nil {
    79  			log.WithError(err).Error("failed to cleanup resources")
    80  		}
    81  
    82  		defer func() {
    83  			err = framework.CleanUp(framework.Namespace)
    84  			if err != nil {
    85  				log.WithError(err).Error("failed to cleanup resources")
    86  			}
    87  			os.Exit(exitCode)
    88  		}()
    89  	}
    90  
    91  	exitCode = m.Run()
    92  }