k8s.io/kubernetes@v1.29.3/test/e2e/upgrades/upgrade_suite.go (about) 1 /* 2 Copyright 2021 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 upgrades 18 19 import ( 20 "context" 21 "encoding/xml" 22 "fmt" 23 "os" 24 "path/filepath" 25 "regexp" 26 "strings" 27 "sync" 28 "time" 29 30 "k8s.io/kubernetes/test/e2e/chaosmonkey" 31 "k8s.io/kubernetes/test/e2e/framework" 32 "k8s.io/kubernetes/test/utils/junit" 33 admissionapi "k8s.io/pod-security-admission/api" 34 35 "github.com/onsi/ginkgo/v2" 36 ) 37 38 type chaosMonkeyAdapter struct { 39 test Test 40 framework *framework.Framework 41 upgradeType UpgradeType 42 upgCtx UpgradeContext 43 } 44 45 func (cma *chaosMonkeyAdapter) Test(ctx context.Context, sem *chaosmonkey.Semaphore) { 46 var once sync.Once 47 ready := func() { 48 once.Do(func() { 49 sem.Ready() 50 }) 51 } 52 defer ready() 53 if skippable, ok := cma.test.(Skippable); ok && skippable.Skip(cma.upgCtx) { 54 ginkgo.By("skipping test " + cma.test.Name()) 55 return 56 } 57 58 ginkgo.DeferCleanup(cma.test.Teardown, cma.framework) 59 cma.test.Setup(ctx, cma.framework) 60 ready() 61 cma.test.Test(ctx, cma.framework, sem.StopCh, cma.upgradeType) 62 } 63 64 func CreateUpgradeFrameworks(tests []Test) map[string]*framework.Framework { 65 nsFilter := regexp.MustCompile("[^[:word:]-]+") // match anything that's not a word character or hyphen 66 testFrameworks := map[string]*framework.Framework{} 67 for _, t := range tests { 68 ns := nsFilter.ReplaceAllString(t.Name(), "-") // and replace with a single hyphen 69 ns = strings.Trim(ns, "-") 70 f := framework.NewDefaultFramework(ns) 71 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 72 testFrameworks[t.Name()] = f 73 } 74 return testFrameworks 75 } 76 77 // RunUpgradeSuite runs the actual upgrade tests. 78 func RunUpgradeSuite( 79 ctx context.Context, 80 upgCtx *UpgradeContext, 81 tests []Test, 82 testFrameworks map[string]*framework.Framework, 83 testSuite *junit.TestSuite, 84 upgradeType UpgradeType, 85 upgradeFunc func(ctx context.Context), 86 ) { 87 cm := chaosmonkey.New(upgradeFunc) 88 for _, t := range tests { 89 testCase := &junit.TestCase{ 90 Name: t.Name(), 91 Classname: "upgrade_tests", 92 } 93 testSuite.TestCases = append(testSuite.TestCases, testCase) 94 cma := chaosMonkeyAdapter{ 95 test: t, 96 framework: testFrameworks[t.Name()], 97 upgradeType: upgradeType, 98 upgCtx: *upgCtx, 99 } 100 cm.Register(cma.Test) 101 } 102 103 start := time.Now() 104 defer func() { 105 testSuite.Update() 106 testSuite.Time = time.Since(start).Seconds() 107 if framework.TestContext.ReportDir != "" { 108 fname := filepath.Join(framework.TestContext.ReportDir, fmt.Sprintf("junit_%supgrades.xml", framework.TestContext.ReportPrefix)) 109 f, err := os.Create(fname) 110 if err != nil { 111 return 112 } 113 defer f.Close() 114 xml.NewEncoder(f).Encode(testSuite) 115 } 116 }() 117 cm.Do(ctx) 118 }