knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/upgrade/background_verification_test.go (about) 1 /* 2 Copyright 2021 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 "testing" 22 23 "knative.dev/pkg/test/upgrade" 24 ) 25 26 func TestSkipAtBackgroundVerification(t *testing.T) { 27 config, buf := newConfig(t) 28 skipMsg := "It is expected to be skipped" 29 expectedTexts := []string{ 30 upgradeTestRunning, 31 upgradeTestSuccess, 32 "INFO\tSetup 1", 33 "WARN\t" + skipMsg, 34 } 35 s := upgrade.Suite{ 36 Tests: upgrade.Tests{ 37 Continual: []upgrade.BackgroundOperation{ 38 upgrade.NewBackgroundVerification("ShouldBeSkipped", 39 // Setup 40 func(c upgrade.Context) { 41 c.Log.Info("Setup 1") 42 }, 43 // Verify 44 func(c upgrade.Context) { 45 c.Log.Warn(skipMsg) 46 c.T.Skip(skipMsg) 47 c.Log.Info("Verify 1") 48 }, 49 ), 50 }, 51 }, 52 } 53 s.Execute(config) 54 55 assert := assertions{tb: t} 56 57 out := buf.String() 58 assert.textNotContains(out, texts{elms: []string{ 59 "INFO\tVerify 1", 60 }}) 61 assert.textContains(out, texts{elms: expectedTexts}) 62 } 63 64 func TestFailAtBackgroundVerification(t *testing.T) { 65 const failingVerification = "FailAtVerification" 66 expectedTexts := []string{ 67 upgradeTestRunning, 68 upgradeTestFailure, 69 "INFO\tSetup 1", 70 "INFO\tVerify 1", 71 } 72 s := upgrade.Suite{ 73 Tests: upgrade.Tests{ 74 Continual: []upgrade.BackgroundOperation{ 75 upgrade.NewBackgroundVerification(failingVerification, 76 // Setup 77 func(c upgrade.Context) { 78 c.Log.Info("Setup 1") 79 }, 80 // Verify 81 func(c upgrade.Context) { 82 c.Log.Info("Verify 1") 83 c.T.Fatal(failureTestingMessage) 84 c.Log.Info("Verify 2") 85 }, 86 ), 87 }, 88 }, 89 } 90 var ( 91 buf fmt.Stringer 92 c upgrade.Configuration 93 ok bool 94 ) 95 it := []testing.InternalTest{{ 96 Name: t.Name(), 97 F: func(t *testing.T) { 98 c, buf = newConfig(t) 99 s.Execute(c) 100 }, 101 }} 102 testOutput := captureStdOutput(func() { 103 ok = testing.RunTests(allTestsFilter, it) 104 }) 105 if ok { 106 t.Fatal("Didn't fail, but should") 107 } 108 out := buf.String() 109 assert := assertions{tb: t} 110 assert.textNotContains(out, texts{elms: []string{ 111 "INFO\tVerify 2", 112 }}) 113 assert.textContains(out, texts{elms: expectedTexts}) 114 assert.textContains(testOutput, texts{ 115 elms: []string{ 116 fmt.Sprintf("--- FAIL: %s/Run/%s", t.Name(), failingVerification), 117 }, 118 }) 119 }