knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/upgrade/execute_failures_test.go (about) 1 /* 2 Copyright 2020 The Knative 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 upgrade_test 18 19 import ( 20 "fmt" 21 "io" 22 "os" 23 "testing" 24 25 "knative.dev/pkg/test/upgrade" 26 ) 27 28 func TestSuiteExecuteWithFailures(t *testing.T) { 29 suite := completeSuite() 30 for i, st := range createSteps(suite) { 31 for j := range st.ops.ops { 32 fp := failurePoint{ 33 step: i + 1, 34 element: j + 1, 35 } 36 testSuiteExecuteWithFailingStep(suite, fp, t) 37 } 38 } 39 } 40 41 var allTestsFilter = func(_, _ string) (bool, error) { return true, nil } 42 43 func testSuiteExecuteWithFailingStep(suite upgrade.Suite, fp failurePoint, t *testing.T) { 44 testName := fmt.Sprintf("FailAt-%d-%d", fp.step, fp.element) 45 t.Run(testName, func(t *testing.T) { 46 assert := assertions{tb: t} 47 var ( 48 output string 49 c upgrade.Configuration 50 buf fmt.Stringer 51 ) 52 suiteWithFailures := enrichSuiteWithFailures(suite, fp) 53 txt := expectedTexts(suiteWithFailures, fp) 54 txt.append(upgradeTestRunning, upgradeTestFailure) 55 56 it := []testing.InternalTest{{ 57 Name: testName, 58 F: func(t *testing.T) { 59 c, buf = newConfig(t) 60 suiteWithFailures.Execute(c) 61 }, 62 }} 63 var ok bool 64 testOutput := captureStdOutput(func() { 65 ok = testing.RunTests(allTestsFilter, it) 66 }) 67 output = buf.String() 68 69 if ok { 70 t.Fatal("Didn't fail, but should") 71 } 72 73 assert.textContains(output, txt) 74 assert.textContains(testOutput, texts{ 75 elms: []string{ 76 fmt.Sprintf("--- FAIL: FailAt-%d-%d", fp.step, fp.element), 77 }, 78 }) 79 }) 80 } 81 82 func captureStdOutput(call func()) string { 83 rescueStdout := os.Stdout 84 r, w, _ := os.Pipe() 85 os.Stdout = w 86 defer func() { 87 os.Stdout = rescueStdout 88 }() 89 90 call() 91 92 _ = w.Close() 93 out, _ := io.ReadAll(r) 94 return string(out) 95 }