...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture/test_runner.go

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

     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 resourcefixture
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    22  	testconstants "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/constants"
    23  )
    24  
    25  type ShouldRunFunc func(fixture ResourceFixture) bool
    26  type TestCaseFunc func(t *testing.T, fixture ResourceFixture)
    27  
    28  func RunTests(t *testing.T, shouldRun ShouldRunFunc, testCaseFunc TestCaseFunc) {
    29  	testCases := Load(t)
    30  	for _, tc := range testCases {
    31  		if !shouldRun(tc) {
    32  			continue
    33  		}
    34  		runTestCase(t, tc, testCaseFunc)
    35  	}
    36  }
    37  
    38  func RunSpecificTests(t *testing.T, fixtures []ResourceFixture, testCaseFunc TestCaseFunc) {
    39  	for _, f := range fixtures {
    40  		runTestCase(t, f, testCaseFunc)
    41  	}
    42  }
    43  
    44  func runTestCase(t *testing.T, fixture ResourceFixture, testCaseFunc TestCaseFunc) {
    45  	testName := FormatTestName(fixture)
    46  	if test.StringMatchesRegexList(t, testconstants.TestNameRegexesToSkip, testName) {
    47  		return
    48  	}
    49  	t.Run(FormatTestName(fixture), func(t *testing.T) {
    50  		t.Parallel()
    51  		testCaseFunc(t, fixture)
    52  		// note, this function, runTestCase(...) almost always returns before testCaseFunc(...) returns
    53  	})
    54  }
    55  
    56  func FormatTestName(tc ResourceFixture) string {
    57  	return fmt.Sprintf("%v-%v", string(tc.Type), tc.Name)
    58  }
    59  

View as plain text