k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/test/simple_context.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  	"io/fs"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"k8s.io/perf-tests/clusterloader2/api"
    25  	"k8s.io/perf-tests/clusterloader2/pkg/chaos"
    26  	"k8s.io/perf-tests/clusterloader2/pkg/config"
    27  	"k8s.io/perf-tests/clusterloader2/pkg/framework"
    28  	"k8s.io/perf-tests/clusterloader2/pkg/measurement"
    29  	"k8s.io/perf-tests/clusterloader2/pkg/state"
    30  	"k8s.io/perf-tests/clusterloader2/pkg/tuningset"
    31  	"k8s.io/perf-tests/clusterloader2/pkg/util"
    32  )
    33  
    34  type simpleContext struct {
    35  	clusterLoaderConfig *config.ClusterLoaderConfig
    36  	clusterFramework    *framework.Framework
    37  	prometheusFramework *framework.Framework
    38  	testReporter        Reporter
    39  	state               *state.State
    40  	templateMapping     map[string]interface{}
    41  	templateProvider    *config.TemplateProvider
    42  	tuningSetFactory    tuningset.Factory
    43  	measurementManager  measurement.Manager
    44  	chaosMonkey         *chaos.Monkey
    45  	testScenario        *api.TestScenario
    46  	testConfig          *api.Config
    47  }
    48  
    49  func createSimpleContext(c *config.ClusterLoaderConfig, f, p *framework.Framework, s *state.State, testReporter Reporter, templateMapping map[string]interface{}, testScenario *api.TestScenario) Context {
    50  	templateProvider := config.NewTemplateProvider(basepathFS(filepath.Dir(testScenario.ConfigPath)))
    51  	return &simpleContext{
    52  		clusterLoaderConfig: c,
    53  		clusterFramework:    f,
    54  		prometheusFramework: p,
    55  		testReporter:        testReporter,
    56  		state:               s,
    57  		templateMapping:     util.CloneMap(templateMapping),
    58  		templateProvider:    templateProvider,
    59  		tuningSetFactory:    tuningset.NewFactory(),
    60  		measurementManager:  measurement.CreateManager(f, p, templateProvider, c),
    61  		chaosMonkey:         chaos.NewMonkey(f.GetClientSets().GetClient(), c.ClusterConfig.Provider),
    62  		testScenario:        testScenario,
    63  	}
    64  }
    65  
    66  // GetClusterConfig return cluster config.
    67  func (sc *simpleContext) GetClusterLoaderConfig() *config.ClusterLoaderConfig {
    68  	return sc.clusterLoaderConfig
    69  }
    70  
    71  // GetFramework returns cluster framework.
    72  func (sc *simpleContext) GetClusterFramework() *framework.Framework {
    73  	return sc.clusterFramework
    74  }
    75  
    76  // GetFramework returns prometheus framework.
    77  func (sc *simpleContext) GetPrometheusFramework() *framework.Framework {
    78  	return sc.prometheusFramework
    79  }
    80  
    81  // GetTestReporter returns test items reporter.
    82  func (sc *simpleContext) GetTestReporter() Reporter {
    83  	return sc.testReporter
    84  }
    85  
    86  // GetState returns current test state.
    87  func (sc *simpleContext) GetState() *state.State {
    88  	return sc.state
    89  }
    90  
    91  // GetTemplateProvider returns template provider.
    92  func (sc *simpleContext) GetTemplateProvider() *config.TemplateProvider {
    93  	return sc.templateProvider
    94  }
    95  
    96  // GetTemplateMappingCopy returns a copy of template mapping.
    97  func (sc *simpleContext) GetTemplateMappingCopy() map[string]interface{} {
    98  	return util.CloneMap(sc.templateMapping)
    99  }
   100  
   101  // GetTickerFactory returns tuning set factory.
   102  func (sc *simpleContext) GetFactory() tuningset.Factory {
   103  	return sc.tuningSetFactory
   104  }
   105  
   106  // GetManager returns measurement manager.
   107  func (sc *simpleContext) GetManager() measurement.Manager {
   108  	return sc.measurementManager
   109  }
   110  
   111  // GetChaosMonkey returns chaos monkey.
   112  func (sc *simpleContext) GetChaosMonkey() *chaos.Monkey {
   113  	return sc.chaosMonkey
   114  }
   115  
   116  // GetTestScenario returns test scenario.
   117  func (sc *simpleContext) GetTestScenario() *api.TestScenario {
   118  	return sc.testScenario
   119  }
   120  
   121  // GetTestConfig returns test config.
   122  func (sc *simpleContext) GetTestConfig() *api.Config {
   123  	return sc.testConfig
   124  }
   125  
   126  // SetTestConfig sets test config.
   127  func (sc *simpleContext) SetTestConfig(c *api.Config) {
   128  	sc.testConfig = c
   129  }
   130  
   131  // basepathFS is a os.DirFS-like system, but without validations allowing for wild name patterns used in CL2 tests, such as:
   132  // * absolute paths that are in fact relative ones (e.g. /modules/measurements in load/config.yaml)
   133  // * path traversal (e.g. ../load/modules/dns-performance-metrics.yaml in huge-services/config.yaml)
   134  // TODO(mborsz): Cleanup the paths in tests and switch to os.DirFS.
   135  type basepathFS string
   136  
   137  func (b basepathFS) Open(name string) (fs.File, error) {
   138  	return os.Open(filepath.Join(string(b), name))
   139  }
   140  
   141  var _ fs.FS = basepathFS("")