zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/test/inject/inject_test.go (about) 1 //go:build dev 2 // +build dev 3 4 // This file should be linked only in **development** mode. 5 6 package inject_test 7 8 import ( 9 "errors" 10 "testing" 11 12 . "github.com/smartystreets/goconvey/convey" 13 14 "zotregistry.io/zot/pkg/test/inject" 15 ) 16 17 var ( 18 errKey1 = errors.New("key1 not found") 19 errKey2 = errors.New("key2 not found") 20 errNotZero = errors.New("not zero") 21 errCall1 = errors.New("call1 error") 22 errCall2 = errors.New("call2 error") 23 ) 24 25 func foo() error { 26 fmap := map[string]string{"key1": "val1", "key2": "val2"} 27 28 _, ok := fmap["key1"] // should never fail 29 if !inject.Ok(ok) { 30 return errKey1 31 } 32 33 _, ok = fmap["key2"] // should never fail 34 if !inject.Ok(ok) { 35 return errKey2 36 } 37 38 return nil 39 } 40 41 func errgen(i int) error { 42 if i != 0 { 43 return errNotZero 44 } 45 46 return nil 47 } 48 49 func bar() error { 50 err := errgen(0) // should never fail 51 if inject.Error(err) != nil { 52 return errCall1 53 } 54 55 err = errgen(0) // should never fail 56 if inject.Error(err) != nil { 57 return errCall2 58 } 59 60 return nil 61 } 62 63 func baz() error { 64 if inject.ErrStatusCode(0) != 0 { 65 return errCall1 66 } 67 68 if inject.ErrStatusCode(0) != 0 { 69 return errCall2 70 } 71 72 return nil 73 } 74 75 func alwaysErr() error { 76 return errNotZero 77 } 78 79 func alwaysNotOk() bool { 80 return false 81 } 82 83 func TestInject(t *testing.T) { 84 Convey("Injected failure", t, func(c C) { 85 // should be success without injection 86 err := foo() 87 So(err, ShouldBeNil) 88 89 Convey("Check Ok", func() { 90 Convey("Without skipping", func() { 91 inject.InjectFailure(0) // inject a failure 92 err := foo() // should be a failure 93 So(err, ShouldNotBeNil) // should be a failure 94 So(errors.Is(err, errKey1), ShouldBeTrue) 95 }) 96 97 Convey("With skipping", func() { 98 inject.InjectFailure(1) // inject a failure but skip first one 99 err := foo() // should be a failure 100 So(errors.Is(err, errKey1), ShouldBeFalse) 101 So(errors.Is(err, errKey2), ShouldBeTrue) 102 }) 103 }) 104 105 // should be success without injection 106 err = bar() 107 So(err, ShouldBeNil) 108 109 Convey("Check Err", func() { 110 Convey("Without skipping", func() { 111 inject.InjectFailure(0) // inject a failure 112 err := bar() // should be a failure 113 So(err, ShouldNotBeNil) // should be a failure 114 So(errors.Is(err, errCall1), ShouldBeTrue) 115 }) 116 117 Convey("With skipping", func() { 118 inject.InjectFailure(1) // inject a failure but skip first one 119 err := bar() // should be a failure 120 So(errors.Is(err, errCall1), ShouldBeFalse) 121 So(errors.Is(err, errCall2), ShouldBeTrue) 122 }) 123 }) 124 125 Convey("Check ErrStatusCode", func() { 126 Convey("Without skipping", func() { 127 inject.InjectFailure(0) // inject a failure 128 err := baz() // should be a failure 129 So(err, ShouldNotBeNil) // should be a failure 130 So(errors.Is(err, errCall1), ShouldBeTrue) 131 }) 132 133 Convey("With skipping", func() { 134 inject.InjectFailure(1) // inject a failure but skip first one 135 err := baz() // should be a failure 136 So(errors.Is(err, errCall1), ShouldBeFalse) 137 So(errors.Is(err, errCall2), ShouldBeTrue) 138 }) 139 }) 140 }) 141 142 Convey("Without injected failure", t, func(c C) { 143 err := alwaysErr() 144 So(inject.Error(err), ShouldNotBeNil) 145 146 ok := alwaysNotOk() 147 So(inject.Ok(ok), ShouldBeFalse) 148 }) 149 150 Convey("Incomplete injected failure", t, func(c C) { 151 inject.InjectFailure(0) // inject a failure 152 So(func() { inject.InjectFailure(0) }, ShouldPanic) 153 }) 154 }