-
Notifications
You must be signed in to change notification settings - Fork 84
/
formation_status.go
92 lines (79 loc) · 3.29 KB
/
formation_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package asserters
import (
"context"
"testing"
gql "github.com/kyma-incubator/compass/components/director/pkg/graphql"
"github.com/kyma-incubator/compass/tests/pkg/fixtures"
context_keys "github.com/kyma-incubator/compass/tests/pkg/notifications/context-keys"
"github.com/machinebox/graphql"
"github.com/stretchr/testify/require"
)
type FormationStatusAsserter struct {
tenant string
formationName string
formationState string
certSecuredGraphQLClient *graphql.Client
condition gql.FormationStatusCondition
errors []*gql.FormationStatusError
}
func NewFormationStatusAsserter(tenant string, certSecuredGraphQLClient *graphql.Client) *FormationStatusAsserter {
return &FormationStatusAsserter{
tenant: tenant,
certSecuredGraphQLClient: certSecuredGraphQLClient,
condition: gql.FormationStatusConditionReady,
errors: nil,
}
}
func (a *FormationStatusAsserter) WithCondition(condition gql.FormationStatusCondition) *FormationStatusAsserter {
a.condition = condition
return a
}
func (a *FormationStatusAsserter) WithErrors(errors []*gql.FormationStatusError) *FormationStatusAsserter {
a.errors = errors
return a
}
func (a *FormationStatusAsserter) WithFormationName(formationName string) *FormationStatusAsserter {
a.formationName = formationName
return a
}
func (a *FormationStatusAsserter) WithState(state string) *FormationStatusAsserter {
a.formationState = state
return a
}
func (a *FormationStatusAsserter) AssertExpectations(t *testing.T, ctx context.Context) {
a.assertFormationStatus(t, ctx, a.tenant, a.formationName, gql.FormationStatus{
Condition: a.condition,
Errors: a.errors,
})
}
func (a *FormationStatusAsserter) assertFormationStatus(t *testing.T, ctx context.Context, tenant, formationName string, expectedFormationStatus gql.FormationStatus) {
// Get the formation with its status
var gotFormation *gql.FormationExt
if formationName != "" {
gotFormation = fixtures.GetFormationByName(t, ctx, a.certSecuredGraphQLClient, formationName, tenant)
} else {
gotFormation = fixtures.GetFormationByID(t, ctx, a.certSecuredGraphQLClient, ctx.Value(context_keys.FormationIDKey).(string), tenant)
}
formationID := gotFormation.ID
// Assert the status
require.Equal(t, expectedFormationStatus.Condition, gotFormation.Status.Condition, "Formation with ID %q is with status %q, but %q was expected", formationID, gotFormation.Status.Condition, expectedFormationStatus.Condition)
if a.formationState != "" {
require.Equal(t, a.formationState, gotFormation.State)
}
if expectedFormationStatus.Errors == nil {
require.Nil(t, gotFormation.Status.Errors)
} else { // assert only the Message and ErrorCode
require.Len(t, gotFormation.Status.Errors, len(expectedFormationStatus.Errors))
for _, expectedError := range expectedFormationStatus.Errors {
found := false
for _, gotError := range gotFormation.Status.Errors {
if gotError.ErrorCode == expectedError.ErrorCode && gotError.Message == expectedError.Message {
found = true
break
}
}
require.Truef(t, found, "Error %q with error code %d was not found", expectedError.Message, expectedError.ErrorCode)
}
}
t.Logf("Formation status was successfully asserted for ID: %q", formationID)
}