Skip to content

Commit

Permalink
CLOUDP-294932: Fix contract test skips (#2049)
Browse files Browse the repository at this point in the history
* Fix contract test skips

Signed-off-by: jose.vazquez <[email protected]>

* Update test/helper/contract/contract.go

Use simpler non parameterised error constructor

Co-authored-by: Sergiusz Urbaniak <[email protected]>

* fixup

Signed-off-by: jose.vazquez <[email protected]>

---------

Signed-off-by: jose.vazquez <[email protected]>
Co-authored-by: Sergiusz Urbaniak <[email protected]>
  • Loading branch information
josvazg and s-urbaniak authored Jan 16, 2025
1 parent a04982b commit 8b2869c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
24 changes: 14 additions & 10 deletions test/helper/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package contract

import (
"context"
"errors"
"fmt"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -48,16 +50,8 @@ func (ct *contractTest) cleanup(ctx context.Context) error {
}

func RunGoContractTest(ctx context.Context, t *testing.T, name string, contractTest func(ch ContractHelper)) {
enabled := control.Enabled("AKO_CONTRACT_TEST")
focus := os.Getenv("AKO_CONTRACT_TEST_FOCUS")
focused := focus != "" && focus == name
if !enabled || !focused {
if !focused {
t.Skipf("Skipping contract test %q as focus is %q", name, focus)
} else {
t.Skip("Skipping contract test as AKO_CONTRACT_TEST is unset")
}
return
if err := skipCheck(name, os.Getenv("AKO_CONTRACT_TEST_FOCUS"), control.Enabled("AKO_CONTRACT_TEST")); err != nil {
t.Skipf("Skipping contract test: %v", err.Error())
}
ct := newContractTest(ctx)
defer func() {
Expand All @@ -68,6 +62,16 @@ func RunGoContractTest(ctx context.Context, t *testing.T, name string, contractT
})
}

func skipCheck(name, focus string, enabled bool) error {
if !enabled {
return errors.New("AKO_CONTRACT_TEST is unset")
}
if focus != "" && !strings.Contains(name, focus) {
return fmt.Errorf("test %q does not contain focus string %q", name, focus)
}
return nil
}

func newContractTest(ctx context.Context) *contractTest {
return &contractTest{
k8sClient: mustCreateK8sClient(),
Expand Down
67 changes: 67 additions & 0 deletions test/helper/contract/skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package contract

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSkip(t *testing.T) {
for _, tc := range []struct {
title string
name string
focus string
enabled bool
expected error
}{
{
title: "empty name, focus and disabled skips all",
expected: errors.New("AKO_CONTRACT_TEST is unset"),
},
{
title: "empty name, focus and enabled does not skip",
enabled: true,
},
{
title: "disabled skips regardles of focus matching",
name: "target",
focus: "target",
expected: errors.New("AKO_CONTRACT_TEST is unset"),
},
{
title: "enabled with no focus does not skip",
enabled: true,
name: "target",
},
{
title: "enabled with no focus does not skip",
enabled: true,
name: "target",
},
{
title: "enabled with non matching focus skips",
enabled: true,
name: "something else",
focus: "target",
expected: errors.New("test \"something else\" does not contain focus string \"target\""),
},
{
title: "enabled with matching focus does not skip",
enabled: true,
name: "target",
focus: "target",
},
{
title: "enabled matching a sub-target focus does not skip",
enabled: true,
name: "some target phrase",
focus: "target",
},
} {
t.Run(tc.title, func(t *testing.T) {
err := skipCheck(tc.name, tc.focus, tc.enabled)
assert.Equal(t, tc.expected, err)
})
}
}

0 comments on commit 8b2869c

Please sign in to comment.