Skip to content

Commit 3f0ebc2

Browse files
committed
small additions, improved naming, fixed typos
1 parent 55109b4 commit 3f0ebc2

8 files changed

Lines changed: 77 additions & 35 deletions

File tree

testsupport/assertions/assertions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func AppendGeneric[SuperType any, Type any](assertionList Assertions[Type], asse
2727
return assertionList
2828
}
2929

30-
// AppendLifted is a convenience function to first lift all the assertions to the "To" type and then append them to the provided list.
31-
func AppendLifted[From any, To any](conversion func(To) (From, bool), assertionList Assertions[To], assertions ...Assertion[From]) Assertions[To] {
32-
return Append(assertionList, LiftAll(conversion, assertions...)...)
30+
// AppendConverted is a convenience function to first lift all the assertions to the "To" type and then append them to the provided list.
31+
func AppendConverted[From any, To any](conversion func(To) (From, bool), assertionList Assertions[To], assertions ...Assertion[From]) Assertions[To] {
32+
return Append(assertionList, ConvertAll(conversion, assertions...)...)
3333
}
3434

3535
// AppendFunc is a convenience function that is able to take in the assertions as simple functions.

testsupport/assertions/conditions/conditions.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/codeready-toolchain/toolchain-common/pkg/condition"
66
"github.com/codeready-toolchain/toolchain-e2e/testsupport/assertions"
77
"github.com/stretchr/testify/assert"
8+
corev1 "k8s.io/api/core/v1"
89
)
910

1011
type ConditionAssertions struct {
@@ -23,3 +24,24 @@ func (cas *ConditionAssertions) Type(typ toolchainv1alpha1.ConditionType) *Condi
2324
})
2425
return cas
2526
}
27+
28+
func (cas *ConditionAssertions) Status(typ toolchainv1alpha1.ConditionType, status corev1.ConditionStatus) *ConditionAssertions {
29+
cas.Assertions = assertions.AppendFunc(cas.Assertions, func(t assertions.AssertT, conds []toolchainv1alpha1.Condition) {
30+
t.Helper()
31+
cond, found := condition.FindConditionByType(conds, typ)
32+
assert.True(t, found, "didn't find a condition with the type '%v'", typ)
33+
assert.Equal(t, status, cond.Status, "condition of type '%v' doesn't have the expected status", typ)
34+
})
35+
return cas
36+
}
37+
38+
func (cas *ConditionAssertions) StatusAndReason(typ toolchainv1alpha1.ConditionType, status corev1.ConditionStatus, reason string) *ConditionAssertions {
39+
cas.Assertions = assertions.AppendFunc(cas.Assertions, func(t assertions.AssertT, conds []toolchainv1alpha1.Condition) {
40+
t.Helper()
41+
cond, found := condition.FindConditionByType(conds, typ)
42+
assert.True(t, found, "didn't find a condition with the type '%v'", typ)
43+
assert.Equal(t, status, cond.Status, "condition of type '%v' doesn't have the expected status", typ)
44+
assert.Equal(t, reason, cond.Reason, "condition of type '%v' doesn't have the expected reason", typ)
45+
})
46+
return cas
47+
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ func CastAssertion[SuperType any, Type any](a Assertion[SuperType]) Assertion[Ty
99
return cast[SuperType](o)
1010
}
1111

12-
return Lift(conversion, a)
12+
return Convert(conversion, a)
1313
}
1414

15-
// Lift converts from one assertion type to another by converting the tested value.
15+
// Convert converts from one assertion type to another by converting the tested value.
1616
// It respectes the ObjectNameAssertion and ObjectNamespaceAssertion so that assertions
1717
// can still be used to identify the object after lifting.
1818
// The provided accessor can be fallible, returning false on the failure to convert the object.
19-
func Lift[From any, To any](accessor func(To) (From, bool), assertion Assertion[From]) Assertion[To] {
19+
func Convert[From any, To any](accessor func(To) (From, bool), assertion Assertion[From]) Assertion[To] {
2020
if _, ok := assertion.(ObjectNameAssertion); ok {
21-
return &liftedObjectName[From, To]{liftedAssertion: liftedAssertion[From, To]{accessor: accessor, assertion: assertion}}
21+
return &convertedObjectName[From, To]{convertedAssertion: convertedAssertion[From, To]{accessor: accessor, assertion: assertion}}
2222
} else if _, ok := assertion.(ObjectNamespaceAssertion); ok {
23-
return &liftedObjectNamespace[From, To]{liftedAssertion: liftedAssertion[From, To]{accessor: accessor, assertion: assertion}}
23+
return &convertedObjectNamespace[From, To]{convertedAssertion: convertedAssertion[From, To]{accessor: accessor, assertion: assertion}}
2424
} else {
25-
return &liftedAssertion[From, To]{accessor: accessor, assertion: assertion}
25+
return &convertedAssertion[From, To]{accessor: accessor, assertion: assertion}
2626
}
2727
}
2828

29-
// LiftAll performs Lift on all the provided assertions.
30-
func LiftAll[From any, To any](accessor func(To) (From, bool), assertions ...Assertion[From]) Assertions[To] {
29+
// ConvertAll performs Lift on all the provided assertions.
30+
func ConvertAll[From any, To any](accessor func(To) (From, bool), assertions ...Assertion[From]) Assertions[To] {
3131
tos := make(Assertions[To], len(assertions))
3232
for i, a := range assertions {
33-
tos[i] = Lift(accessor, a)
33+
tos[i] = Convert(accessor, a)
3434
}
3535
return tos
3636
}
@@ -44,12 +44,12 @@ func cast[T any](obj any) (T, bool) {
4444
return ret, ok
4545
}
4646

47-
type liftedAssertion[From any, To any] struct {
47+
type convertedAssertion[From any, To any] struct {
4848
assertion Assertion[From]
4949
accessor func(To) (From, bool)
5050
}
5151

52-
func (lon *liftedAssertion[From, To]) Test(t AssertT, obj To) {
52+
func (lon *convertedAssertion[From, To]) Test(t AssertT, obj To) {
5353
t.Helper()
5454
o, ok := lon.accessor(obj)
5555
if !ok {
@@ -59,18 +59,18 @@ func (lon *liftedAssertion[From, To]) Test(t AssertT, obj To) {
5959
lon.assertion.Test(t, o)
6060
}
6161

62-
type liftedObjectName[From any, To any] struct {
63-
liftedAssertion[From, To]
62+
type convertedObjectName[From any, To any] struct {
63+
convertedAssertion[From, To]
6464
}
6565

66-
func (lon *liftedObjectName[From, To]) Name() string {
66+
func (lon *convertedObjectName[From, To]) Name() string {
6767
return lon.assertion.(ObjectNameAssertion).Name()
6868
}
6969

70-
type liftedObjectNamespace[From any, To any] struct {
71-
liftedAssertion[From, To]
70+
type convertedObjectNamespace[From any, To any] struct {
71+
convertedAssertion[From, To]
7272
}
7373

74-
func (lon *liftedObjectNamespace[From, To]) Namespace() string {
74+
func (lon *convertedObjectNamespace[From, To]) Namespace() string {
7575
return lon.assertion.(ObjectNamespaceAssertion).Namespace()
7676
}

testsupport/assertions/metadata/metadata.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func (ma *MetadataAssertions) Label(name string) *MetadataAssertions {
5757
return ma
5858
}
5959

60+
func (ma *MetadataAssertions) NoLabel(name string) *MetadataAssertions {
61+
ma.Assertions = assertions.AppendFunc(ma.Assertions, func(t assertions.AssertT, obj client.Object) {
62+
t.Helper()
63+
assert.NotContains(t, obj.GetLabels(), name, "a label called '%s' found on the object but none expected", name)
64+
})
65+
return ma
66+
}
67+
6068
func (a *objectName) Test(t assertions.AssertT, obj client.Object) {
6169
t.Helper()
6270
assert.Equal(t, a.name, obj.GetName(), "object name doesn't match")

testsupport/assertions/object.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/codeready-toolchain/toolchain-e2e/testsupport/wait"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
1413
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -18,6 +17,11 @@ import (
1817
"sigs.k8s.io/controller-runtime/pkg/client"
1918
)
2019

20+
const (
21+
DefaultRetryInterval = time.Millisecond * 100 // make it short because a "retry interval" is waited before the first test
22+
DefaultTimeout = time.Second * 120
23+
)
24+
2125
type AddressableObjectAssertions[T client.Object] struct {
2226
Assertions[T]
2327
}
@@ -50,8 +54,8 @@ type errorCollectingT struct {
5054
func (oa *AddressableObjectAssertions[T]) Await(cl client.Client) *Await[T] {
5155
return &Await[T]{
5256
cl: cl,
53-
timeout: wait.DefaultTimeout,
54-
tick: wait.DefaultRetryInterval,
57+
timeout: DefaultTimeout,
58+
tick: DefaultRetryInterval,
5559
assertions: oa.Assertions,
5660
}
5761
}

testsupport/assertions/object/object.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import (
66
"sigs.k8s.io/controller-runtime/pkg/client"
77
)
88

9-
// ObjectAssertions is a common base for assertions on client.Object subtypes.
9+
// ObjectAssertions is a common base for assertions on client.Object subtypes
10+
// and is meant to be embedded in other structs.
11+
//
1012
// It provides assertions on the object metadata.
13+
//
14+
// It is necessary to initialize this using the SetFluentSelf method so that
15+
// the methods defined on this struct can participate in the fluent call-chains
16+
// defined on the struct that embeds it.
1117
type ObjectAssertions[Self any, T client.Object] struct {
1218
assertions.AddressableObjectAssertions[T]
1319
self *Self
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package spaceprovisionerconfig
22

33
import (
4-
toolchainv1aplha1 "github.com/codeready-toolchain/api/api/v1alpha1"
4+
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
55
"github.com/codeready-toolchain/toolchain-e2e/testsupport/assertions"
66
"github.com/codeready-toolchain/toolchain-e2e/testsupport/assertions/conditions"
77
"github.com/codeready-toolchain/toolchain-e2e/testsupport/assertions/object"
88
"github.com/stretchr/testify/assert"
99
)
1010

1111
type SpaceProvisionerConfigAssertions struct {
12-
object.ObjectAssertions[SpaceProvisionerConfigAssertions, *toolchainv1aplha1.SpaceProvisionerConfig]
12+
object.ObjectAssertions[SpaceProvisionerConfigAssertions, *toolchainv1alpha1.SpaceProvisionerConfig]
1313
}
1414

1515
func Asserting() *SpaceProvisionerConfigAssertions {
@@ -19,18 +19,18 @@ func Asserting() *SpaceProvisionerConfigAssertions {
1919
}
2020

2121
func (spc *SpaceProvisionerConfigAssertions) Conditions(cas *conditions.ConditionAssertions) *SpaceProvisionerConfigAssertions {
22-
spc.Assertions = assertions.AppendLifted(getConditions, spc.Assertions, cas.Assertions...)
22+
spc.Assertions = assertions.AppendConverted(getConditions, spc.Assertions, cas.Assertions...)
2323
return spc
2424
}
2525

2626
func (spc *SpaceProvisionerConfigAssertions) ToolchainClusterName(name string) *SpaceProvisionerConfigAssertions {
27-
spc.Assertions = assertions.AppendFunc(spc.Assertions, func(t assertions.AssertT, obj *toolchainv1aplha1.SpaceProvisionerConfig) {
27+
spc.Assertions = assertions.AppendFunc(spc.Assertions, func(t assertions.AssertT, obj *toolchainv1alpha1.SpaceProvisionerConfig) {
2828
t.Helper()
2929
assert.Equal(t, name, obj.Spec.ToolchainCluster, "unexpected toolchainCluster")
3030
})
3131
return spc
3232
}
3333

34-
func getConditions(spc *toolchainv1aplha1.SpaceProvisionerConfig) ([]toolchainv1aplha1.Condition, bool) {
34+
func getConditions(spc *toolchainv1alpha1.SpaceProvisionerConfig) ([]toolchainv1alpha1.Condition, bool) {
3535
return spc.Status.Conditions, true
3636
}

testsupport/assertions_use/assertions_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package assertions_use
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
toolchainv1aplha1 "github.com/codeready-toolchain/api/api/v1alpha1"
89
"github.com/codeready-toolchain/toolchain-e2e/testsupport/assertions/conditions"
@@ -17,7 +18,7 @@ import (
1718
func Test(t *testing.T) {
1819
spcUnderTest := &toolchainv1aplha1.SpaceProvisionerConfig{
1920
ObjectMeta: metav1.ObjectMeta{
20-
Name: "kachny",
21+
Name: "myobj",
2122
Namespace: "default",
2223
},
2324
}
@@ -26,17 +27,18 @@ func Test(t *testing.T) {
2627
require.NoError(t, toolchainv1aplha1.AddToScheme(scheme))
2728

2829
// use the assertions in a simple immediate call
29-
spaceprovisionerconfig.Asserting().
30-
ObjectMeta(metadata.With().Name("asdf").Label("kachny")).
31-
Conditions(conditions.With().Type(toolchainv1aplha1.ConditionReady)).
32-
Test(t, spcUnderTest)
30+
// spaceprovisionerconfig.Asserting().
31+
// ObjectMeta(metadata.With().Name("asdf").Label("kachny")).
32+
// Conditions(conditions.With().Type(toolchainv1aplha1.ConditionReady)).
33+
// Test(t, spcUnderTest)
3334

3435
cl := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(spcUnderTest).Build()
3536

3637
// this is the new WaitFor
3738
spaceprovisionerconfig.Asserting().
38-
ObjectMeta(metadata.With().Name("asdf").Namespace("default").Label("kachny")).
39+
ObjectMeta(metadata.With().Name("myobj").Namespace("default").Label("requiredLabel")).
3940
Conditions(conditions.With().Type(toolchainv1aplha1.ConditionReady)).
4041
Await(cl).
42+
WithTimeout(1*time.Second).
4143
Matching(context.TODO(), t)
4244
}

0 commit comments

Comments
 (0)