k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/test/test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "fmt" 21 "path/filepath" 22 23 "k8s.io/perf-tests/clusterloader2/api" 24 "k8s.io/perf-tests/clusterloader2/pkg/config" 25 "k8s.io/perf-tests/clusterloader2/pkg/errors" 26 "k8s.io/perf-tests/clusterloader2/pkg/framework" 27 "k8s.io/perf-tests/clusterloader2/pkg/modifier" 28 "k8s.io/perf-tests/clusterloader2/pkg/state" 29 ) 30 31 var ( 32 // CreateContext global function for creating context. 33 // This function should be set by Context implementation. 34 CreateContext = createSimpleContext 35 36 // Test is a singleton for test execution object. 37 // This object should be set by Executor implementation. 38 Test = createSimpleExecutor() 39 ) 40 41 // CreateTestContext creates the test context. 42 func CreateTestContext( 43 clusterFramework *framework.Framework, 44 prometheusFramework *framework.Framework, 45 clusterLoaderConfig *config.ClusterLoaderConfig, 46 testReporter Reporter, 47 testScenario *api.TestScenario, 48 ) (Context, *errors.ErrorList) { 49 if clusterFramework == nil { 50 return nil, errors.NewErrorList(fmt.Errorf("framework must be provided")) 51 } 52 if clusterLoaderConfig == nil { 53 return nil, errors.NewErrorList(fmt.Errorf("cluster loader config must be provided")) 54 } 55 if CreateContext == nil { 56 return nil, errors.NewErrorList(fmt.Errorf("no CreateContext function installed")) 57 } 58 59 mapping, errList := config.GetMapping(clusterLoaderConfig, testScenario.OverridePaths) 60 if errList != nil { 61 return nil, errList 62 } 63 64 return CreateContext(clusterLoaderConfig, clusterFramework, prometheusFramework, state.NewState(), testReporter, mapping, testScenario), errors.NewErrorList() 65 } 66 67 // CompileTestConfig loads the test configuration and nested modules. 68 func CompileTestConfig(ctx Context) (*api.Config, *errors.ErrorList) { 69 if Test == nil { 70 return nil, errors.NewErrorList(fmt.Errorf("no Test installed")) 71 } 72 73 clusterLoaderConfig := ctx.GetClusterLoaderConfig() 74 testScenario := ctx.GetTestScenario() 75 testConfigFilename := filepath.Base(testScenario.ConfigPath) 76 testConfig, err := ctx.GetTemplateProvider().TemplateToConfig(testConfigFilename, ctx.GetTemplateMappingCopy()) 77 if err != nil { 78 return nil, errors.NewErrorList(fmt.Errorf("config reading error: %v", err)) 79 } 80 81 if err := modifier.NewModifier(&clusterLoaderConfig.ModifierConfig).ChangeTest(testConfig); err != nil { 82 return nil, errors.NewErrorList(fmt.Errorf("config mutation error: %v", err)) 83 } 84 85 steps, err := flattenModuleSteps(ctx, testConfig.Steps) 86 if err != nil { 87 return nil, errors.NewErrorList( 88 fmt.Errorf("error flattening module steps: %w", err)) 89 } 90 testConfig.Steps = steps 91 92 // TODO: remove them after the deprecated command options are removed. 93 clusterFramework := ctx.GetClusterFramework() 94 if testConfig.Namespace.DeleteStaleNamespaces == nil { 95 testConfig.Namespace.DeleteStaleNamespaces = &clusterFramework.GetClusterConfig().DeleteStaleNamespaces 96 } 97 if testConfig.Namespace.DeleteAutomanagedNamespaces == nil { 98 testConfig.Namespace.DeleteAutomanagedNamespaces = &clusterFramework.GetClusterConfig().DeleteAutomanagedNamespaces 99 } 100 101 testConfig.SetDefaults() 102 basePath := filepath.Dir(testScenario.ConfigPath) 103 configValidator := api.NewConfigValidator(basePath, testConfig) 104 if err := configValidator.Validate(); err != nil { 105 // Returning test config which failed validation for debugging. 106 return testConfig, err 107 } 108 109 return testConfig, errors.NewErrorList() 110 } 111 112 // RunTest runs test based on provided test configuration. 113 func RunTest(ctx Context) *errors.ErrorList { 114 clusterLoaderConfig := ctx.GetClusterLoaderConfig() 115 testConfig := ctx.GetTestConfig() 116 117 testName := ctx.GetTestScenario().Identifier 118 if testName == "" { 119 testName = testConfig.Name 120 } 121 ctx.GetTestReporter().SetTestName(testName) 122 123 if err := modifier.NewModifier(&clusterLoaderConfig.ModifierConfig).ChangeTest(testConfig); err != nil { 124 return errors.NewErrorList(fmt.Errorf("config mutation error: %v", err)) 125 } 126 127 return Test.ExecuteTest(ctx, testConfig) 128 }