From 1646f69cd9f53482da1157d9a843e32d3a921fbf Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Fri, 5 Apr 2024 11:07:47 -0400 Subject: [PATCH] Make it easier to run a single test (#2938) --- conformance/conformance.go | 2 +- conformance/utils/suite/suite.go | 44 ++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/conformance/conformance.go b/conformance/conformance.go index 6ebc932b6f..b6f58518f6 100644 --- a/conformance/conformance.go +++ b/conformance/conformance.go @@ -116,7 +116,7 @@ func RunConformanceWithOptions(t *testing.T, opts suite.ConformanceOptions) { cSuite, err := suite.NewConformanceTestSuite(opts) require.NoError(t, err, "error initializing conformance suite") - cSuite.Setup(t) + cSuite.Setup(t, tests.ConformanceTests) err = cSuite.Run(t, tests.ConformanceTests) require.NoError(t, err) diff --git a/conformance/utils/suite/suite.go b/conformance/utils/suite/suite.go index 505b16b97f..690315d884 100644 --- a/conformance/utils/suite/suite.go +++ b/conformance/utils/suite/suite.go @@ -21,12 +21,14 @@ import ( "errors" "fmt" "io/fs" + "slices" "sort" "strings" "sync" "testing" "time" + "github.com/stretchr/testify/require" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" @@ -167,8 +169,16 @@ const ( // NewConformanceTestSuite is a helper to use for creating a new ConformanceTestSuite. func NewConformanceTestSuite(options ConformanceOptions) (*ConformanceTestSuite, error) { - if len(options.ConformanceProfiles) == 0 { - return nil, errors.New("conformance profiles need to be given") + // test suite callers are required to provide either: + // - one conformance profile via the flag '-conformance-profiles' + // - a list of supported features via the flag '-supported-features' + // - an explicit test to run via the flag '-run-test' + // - all features are being tested via the flag '-all-features' + if options.SupportedFeatures.Len() == 0 && + options.ConformanceProfiles.Len() == 0 && + !options.EnableAllSupportedFeatures && + options.RunTest == "" { + return nil, fmt.Errorf("no conformance profile, supported features, explicit tests were provided so no tests could be selected") } config.SetupTimeoutConfig(&options.TimeoutConfig) @@ -200,12 +210,6 @@ func NewConformanceTestSuite(options ConformanceOptions) (*ConformanceTestSuite, mode = options.Mode } - // test suite callers are required to provide a conformance profile OR at - // minimum a list of features which they support. - if options.SupportedFeatures == nil && options.ConformanceProfiles.Len() == 0 && !options.EnableAllSupportedFeatures { - return nil, fmt.Errorf("no conformance profile was selected for test run, and no supported features were provided so no tests could be selected") - } - // test suite callers can potentially just run all tests by saying they // cover all features, if they don't they'll need to have provided a // conformance profile or at least some specific features they support. @@ -298,12 +302,29 @@ func NewConformanceTestSuite(options ConformanceOptions) (*ConformanceTestSuite, // Setup ensures the base resources required for conformance tests are installed // in the cluster. It also ensures that all relevant resources are ready. -func (suite *ConformanceTestSuite) Setup(t *testing.T) { +func (suite *ConformanceTestSuite) Setup(t *testing.T, tests []ConformanceTest) { suite.Applier.ManifestFS = suite.ManifestFS suite.Applier.UsableNetworkAddresses = suite.UsableNetworkAddresses suite.Applier.UnusableNetworkAddresses = suite.UnusableNetworkAddresses - if suite.SupportedFeatures.Has(SupportGateway) { + supportsGateway := suite.SupportedFeatures.Has(SupportGateway) + supportsMesh := suite.SupportedFeatures.Has(SupportMesh) + + if suite.RunTest != "" { + idx := slices.IndexFunc(tests, func(t ConformanceTest) bool { + return t.ShortName == suite.RunTest + }) + + if idx == -1 { + require.FailNow(t, fmt.Sprintf("Test %q does not exist", suite.RunTest)) + } + + test := tests[idx] + supportsGateway = supportsGateway || slices.Contains(test.Features, SupportGateway) + supportsMesh = supportsMesh || slices.Contains(test.Features, SupportMesh) + } + + if supportsGateway { tlog.Logf(t, "Test Setup: Ensuring GatewayClass has been accepted") suite.ControllerName = kubernetes.GWCMustHaveAcceptedConditionTrue(t, suite.Client, suite.TimeoutConfig, suite.GatewayClassName) @@ -331,7 +352,8 @@ func (suite *ConformanceTestSuite) Setup(t *testing.T) { } kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces) } - if suite.SupportedFeatures.Has(SupportMesh) { + + if supportsMesh { tlog.Logf(t, "Test Setup: Applying base manifests") suite.Applier.MustApplyWithCleanup(t, suite.Client, suite.TimeoutConfig, suite.MeshManifests, suite.Cleanup) tlog.Logf(t, "Test Setup: Ensuring Gateways and Pods from mesh manifests are ready")