...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main/testmain.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main

     1  // Copyright 2022 Google LLC
     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 testmain
    16  
    17  import (
    18  	"io/ioutil"
    19  	"log"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/logging"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    26  	testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
    27  	testenvironment "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/environment"
    28  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/webhook"
    29  	cnrmwebhook "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook"
    30  
    31  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    32  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    33  	"sigs.k8s.io/controller-runtime/pkg/manager"
    34  )
    35  
    36  func TestMainForIntegrationTests(m *testing.M, mgr *manager.Manager) {
    37  	// Since Terraform logging defers to the Go standard logger,
    38  	// here we discard everything logged onto the Go standard logger to
    39  	// disable logging from Terraform Google provider in integration tests.
    40  	log.SetOutput(ioutil.Discard)
    41  	TestMain(m, test.IntegrationTestType, nil, mgr)
    42  }
    43  
    44  func TestMainForUnitTests(m *testing.M, mgr *manager.Manager) {
    45  	TestMain(m, test.UnitTestType, nil, mgr)
    46  }
    47  
    48  func TestMainForUnitTestsWithCRDs(m *testing.M, crds []*apiextensions.CustomResourceDefinition, mgr *manager.Manager) {
    49  	TestMain(m, test.UnitTestType, crds, mgr)
    50  }
    51  
    52  // TestMain starts a local K8S API server to run tests against. These tests do
    53  // not require an external API server to execute.
    54  func TestMain(m *testing.M, testType test.TestType, crds []*apiextensions.CustomResourceDefinition, mgr *manager.Manager) {
    55  	TestMainSetupMultipleEnvironments(m, testType, crds, []*manager.Manager{mgr})
    56  }
    57  
    58  // TestMainSetupMultipleEnvironments starts n API servers to run tests against. The value for 'n' is determined by
    59  // the length of the 'mgrPtrs' argument. This is useful when testing multi-cluster scenarios.
    60  func TestMainSetupMultipleEnvironments(m *testing.M, testType test.TestType, crds []*apiextensions.CustomResourceDefinition, mgrPtrs []*manager.Manager) {
    61  	logging.SetupLogger()
    62  	var err error
    63  
    64  	envs := make([]*envtest.Environment, 0, len(mgrPtrs))
    65  	stops := make([]func(), 0, len(mgrPtrs))
    66  	for _, mp := range mgrPtrs {
    67  		var whCfgs []cnrmwebhook.WebhookConfig
    68  		if testType == test.IntegrationTestType {
    69  			whCfgs, err = webhook.GetTestCommonWebhookConfigs()
    70  			if err != nil {
    71  				log.Fatalf("error getting common wehbook configs: %v", err)
    72  			}
    73  		}
    74  		env := testenvironment.StartTestEnvironmentOrLogFatal(testType, crds, whCfgs)
    75  		envs = append(envs, env)
    76  
    77  		mgr, stop := testcontroller.StartTestManagerInstance(env, testType, whCfgs)
    78  		stops = append(stops, stop)
    79  
    80  		testcontroller.EnsureNamespaceExists(mgr.GetClient(), k8s.SystemNamespace)
    81  		*mp = mgr
    82  	}
    83  
    84  	code := m.Run()
    85  
    86  	for _, stop := range stops {
    87  		stop()
    88  	}
    89  	for _, env := range envs {
    90  		if err := env.Stop(); err != nil {
    91  			log.Printf("unable to stop at least one test environment: %v", err)
    92  		}
    93  	}
    94  	os.Exit(code)
    95  }
    96  

View as plain text