Skip to content

Commit 6faa0e2

Browse files
committed
Refactor removed_api_tests to tests against sorted strings to avoid flakiness (#245)
* Revert "fix removed_apis_test" This reverts commit beb9613e0dc43e74ddcc583dfca3e59dd19c2660. * refactor removed_apis_test to test on sorted error strings to remove flakiness Signed-off-by: perdasilva <[email protected]> Upstream-repository: api Upstream-commit: cb9dc0ee2c6a5c31e04ce1ab0c2eee72d9125ffb
1 parent a9f1bbe commit 6faa0e2

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

staging/api/pkg/validation/internal/removed_apis_test.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package internal
22

33
import (
4-
"github.com/operator-framework/api/pkg/manifests"
5-
"github.com/stretchr/testify/require"
64
"reflect"
5+
"sort"
6+
"strings"
77
"testing"
8+
9+
"github.com/operator-framework/api/pkg/manifests"
10+
"github.com/stretchr/testify/require"
811
)
912

1013
func Test_GetRemovedAPIsOn1_22From(t *testing.T) {
@@ -280,20 +283,40 @@ func TestValidateDeprecatedAPIS(t *testing.T) {
280283
require.Equal(t, tt.wantWarning, len(warnsResult) > 0)
281284
if tt.wantWarning {
282285
require.Equal(t, len(tt.warnStrings), len(warnsResult))
286+
// testing against sorted strings to address flakiness on the order
287+
// of APIs listed
288+
sortedWarnStrings := sortStringSlice(tt.warnStrings)
283289
for _, w := range warnsResult {
284290
wString := w.Error()
285-
require.Contains(t, tt.warnStrings, wString)
291+
require.Contains(t, sortedWarnStrings, sortString(wString))
286292
}
287293
}
288294

289295
require.Equal(t, tt.wantError, len(errsResult) > 0)
290296
if tt.wantError {
291297
require.Equal(t, len(tt.errStrings), len(errsResult))
298+
// testing against sorted strings to address flakiness on the order
299+
// of APIs listed
300+
sortedErrStrings := sortStringSlice(tt.errStrings)
292301
for _, err := range errsResult {
293-
errString := err.Error()
294-
require.Contains(t, tt.errStrings, errString)
302+
errString := sortString(err.Error())
303+
require.Contains(t, sortedErrStrings, errString)
295304
}
296305
}
297306
})
298307
}
299308
}
309+
310+
func sortString(str string) string {
311+
split := strings.Split(str, "")
312+
sort.Strings(split)
313+
return strings.Join(split, "")
314+
}
315+
316+
func sortStringSlice(slice []string) []string {
317+
var newSlice []string
318+
for _, str := range slice {
319+
newSlice = append(newSlice, sortString(str))
320+
}
321+
return newSlice
322+
}

0 commit comments

Comments
 (0)