github.com/verrazzano/verrazzano@v1.7.0/pkg/controller/errors/errors_test.go (about) 1 // Copyright (c) 2021, 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package spi 4 5 import ( 6 "errors" 7 "fmt" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 controllerruntime "sigs.k8s.io/controller-runtime" 13 ) 14 15 // TestRetryableError Tests RetryableError 16 // GIVEN a RetryableError 17 // THEN the error is properly created and the Error() message returns the proper values 18 func TestRetryableError(t *testing.T) { 19 tests := []struct { 20 name string 21 description string 22 err RetryableError 23 }{ 24 { 25 name: "EmptyError", 26 description: "Retryable error with nothing provided", 27 err: RetryableError{}, 28 }, 29 { 30 name: "SourceOnly", 31 description: "Retryable error with only a source", 32 err: RetryableError{ 33 Source: "mySource", 34 }, 35 }, 36 { 37 name: "SourceAndOp", 38 description: "Retryable error with a source and an operation", 39 err: RetryableError{ 40 Source: "mySource", 41 Operation: "myOp", 42 }, 43 }, 44 { 45 name: "SourceAndOpAndCause", 46 description: "Retryable error with a source, an op, and a cause", 47 err: RetryableError{ 48 Source: "mySource", 49 Operation: "myOp", 50 Cause: fmt.Errorf("Custom error"), 51 }, 52 }, 53 { 54 name: "SourceAndOpAndCauseAndResult", 55 description: "Retryable error with a source, an op, a cause, and a Result", 56 err: RetryableError{ 57 Source: "mySource", 58 Operation: "myOp", 59 Cause: fmt.Errorf("Custom error"), 60 Result: controllerruntime.Result{ 61 Requeue: true, 62 RequeueAfter: time.Second * 30, 63 }, 64 }, 65 }, 66 } 67 for _, test := range tests { 68 assert := assert.New(t) 69 70 t.Log(test.description) 71 72 err := test.err 73 t.Logf("Error message: %s", err) 74 75 if len(err.Operation) > 0 { 76 assert.Contains(err.Error(), err.Source, "Source not found in message") 77 } 78 if len(err.Operation) > 0 { 79 assert.Contains(err.Error(), err.Operation, "Operation not found in message") 80 } 81 if err.Cause != nil { 82 assert.True(err.HasCause(), "HasCause should return true") 83 assert.Contains(err.Error(), err.Cause.Error()) 84 } else { 85 assert.False(err.HasCause(), "HasCause should return false") 86 } 87 } 88 } 89 90 func TestRetryableErrorFmt(t *testing.T) { 91 var tests = []struct { 92 name string 93 err RetryableError 94 msg string 95 }{ 96 { 97 "empty error", 98 RetryableError{}, 99 "Retryable error", 100 }, 101 { 102 "error with operation", 103 RetryableError{ 104 Operation: "foobar", 105 }, 106 "Retryable error, operation: foobar", 107 }, 108 { 109 "error with source and cause", 110 RetryableError{ 111 Source: "foo", 112 Cause: errors.New("bar"), 113 }, 114 "Retryable error, source: foo, cause: bar", 115 }, 116 { 117 "error with source, operation and cause", 118 RetryableError{ 119 Source: "src", 120 Operation: "oper", 121 Cause: errors.New("c"), 122 }, 123 "Retryable error, source: src, operation: oper, cause: c", 124 }, 125 } 126 127 for _, tt := range tests { 128 t.Run(tt.name, func(t *testing.T) { 129 assert.Equal(t, tt.msg, tt.err.Error()) 130 }) 131 } 132 } 133 134 // TestShouldLogKubenetesAPIError tests ShouldLogKubernetesAPIError 135 // Given an error 136 // Check whether it should be logged ot not 137 func TestShouldLogKubenetesAPIError(t *testing.T) { 138 asserts := assert.New(t) 139 err := fmt.Errorf("some kubernetes API error") 140 141 asserts.True(ShouldLogKubernetesAPIError(err)) 142 143 err = fmt.Errorf(`operation cannot be fulfilled on configmaps "test": the object has been modified; please apply your changes to the latest version and try again`) 144 asserts.False(ShouldLogKubernetesAPIError(err)) 145 }