github.com/datadog/cilium@v1.6.12/pkg/revert/revert_test.go (about) 1 // Copyright 2018 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package revert 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/cilium/cilium/pkg/checker" 24 25 . "gopkg.in/check.v1" 26 ) 27 28 // Hook up gocheck into the "go test" runner. 29 func Test(t *testing.T) { 30 TestingT(t) 31 } 32 33 type RevertTestSuite struct{} 34 35 var _ = Suite(&RevertTestSuite{}) 36 37 func (s *RevertTestSuite) TestRevertStack(c *C) { 38 expectedContent := []string{"lmao", "ayy", "bar", "foo"} 39 content := make([]string, 0, 4) 40 rStack := RevertStack{} 41 42 rStack.Push(func() error { 43 content = append(content, "foo") 44 return nil 45 }) 46 rStack.Push(func() error { 47 content = append(content, "bar") 48 return nil 49 }) 50 rStack.Push(func() error { 51 content = append(content, "ayy") 52 return nil 53 }) 54 rStack.Push(func() error { 55 content = append(content, "lmao") 56 return nil 57 }) 58 59 err := rStack.Revert() 60 c.Assert(err, IsNil) 61 62 c.Assert(content, checker.DeepEquals, expectedContent) 63 } 64 65 func (s *RevertTestSuite) TestRevertStackError(c *C) { 66 var firstFuncCalled, secondFuncCalled, thirdFuncCalled bool 67 rStack := RevertStack{} 68 69 rStack.Push(func() error { 70 firstFuncCalled = true 71 return nil 72 }) 73 rStack.Push(func() error { 74 secondFuncCalled = true 75 return errors.New("2nd function failed") 76 }) 77 rStack.Push(func() error { 78 thirdFuncCalled = true 79 return nil 80 }) 81 82 err := rStack.Revert() 83 c.Assert(err, ErrorMatches, "failed to execute revert function; skipping 1 revert functions: 2nd function failed") 84 85 c.Assert(firstFuncCalled, Equals, false) 86 c.Assert(secondFuncCalled, Equals, true) 87 c.Assert(thirdFuncCalled, Equals, true) 88 }