github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/test/e2e/main_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package test provides e2e tests for Jackal.
     5  package test
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path"
    11  	"testing"
    12  
    13  	"github.com/Racer159/jackal/src/config"
    14  	"github.com/Racer159/jackal/src/pkg/message"
    15  	"github.com/Racer159/jackal/src/test"
    16  )
    17  
    18  var (
    19  	e2e test.JackalE2ETest //nolint:gochecknoglobals
    20  )
    21  
    22  const (
    23  	applianceModeEnvVar     = "APPLIANCE_MODE"
    24  	applianceModeKeepEnvVar = "APPLIANCE_MODE_KEEP"
    25  	skipK8sEnvVar           = "SKIP_K8S"
    26  )
    27  
    28  // TestMain lets us customize the test run. See https://medium.com/goingogo/why-use-testmain-for-testing-in-go-dafb52b406bc.
    29  func TestMain(m *testing.M) {
    30  	// Work from the root directory of the project
    31  	err := os.Chdir("../../../")
    32  	if err != nil {
    33  		fmt.Println(err) //nolint:forbidigo
    34  	}
    35  
    36  	// K3d use the intern package, which requires this to be set in go 1.19
    37  	os.Setenv("ASSUME_NO_MOVING_GC_UNSAFE_RISK_IT_WITH", "go1.19")
    38  
    39  	// Set the log level to trace for when we call Jackal functions internally
    40  	message.SetLogLevel(message.TraceLevel)
    41  
    42  	retCode, err := doAllTheThings(m)
    43  	if err != nil {
    44  		fmt.Println(err) //nolint:forbidigo
    45  	}
    46  
    47  	os.Exit(retCode)
    48  }
    49  
    50  // doAllTheThings just wraps what should go in TestMain. It's in its own function so it can
    51  // [a] Not have a bunch of `os.Exit()` calls in it
    52  // [b] Do defers properly
    53  // [c] Handle errors cleanly
    54  //
    55  // It returns the return code passed from `m.Run()` and any error thrown.
    56  func doAllTheThings(m *testing.M) (int, error) {
    57  	var err error
    58  
    59  	// Set up constants in the global variable that all the tests are able to access
    60  	e2e.Arch = config.GetArch()
    61  	e2e.JackalBinPath = path.Join("build", test.GetCLIName())
    62  	e2e.ApplianceMode = os.Getenv(applianceModeEnvVar) == "true"
    63  	e2e.ApplianceModeKeep = os.Getenv(applianceModeKeepEnvVar) == "true"
    64  	e2e.RunClusterTests = os.Getenv(skipK8sEnvVar) != "true"
    65  
    66  	// Validate that the Jackal binary exists. If it doesn't that means the dev hasn't built it, usually by running
    67  	// `make build-cli`
    68  	_, err = os.Stat(e2e.JackalBinPath)
    69  	if err != nil {
    70  		return 1, fmt.Errorf("jackal binary %s not found", e2e.JackalBinPath)
    71  	}
    72  
    73  	// Run the tests, with the cluster cleanup being deferred to the end of the function call
    74  	returnCode := m.Run()
    75  
    76  	return returnCode, nil
    77  }