/
lifecycle_notification.go
170 lines (148 loc) · 7.04 KB
/
lifecycle_notification.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package asserters
import (
"context"
"net/http"
"testing"
"time"
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"
testingx "github.com/kyma-incubator/compass/tests/pkg/testing"
"github.com/machinebox/graphql"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
type LifecycleNotificationsAsserter struct {
operation string
formationName string
tenantID string
parentTenantID string
state string
expectNotifications bool
externalServicesMockMtlsSecuredURL string
certSecuredGraphQLClient *graphql.Client
client *http.Client
timeout time.Duration
tick time.Duration
}
func NewLifecycleNotificationsAsserter(externalServicesMockMtlsSecuredURL string, gqlClient *graphql.Client, client *http.Client) *LifecycleNotificationsAsserter {
return &LifecycleNotificationsAsserter{
expectNotifications: true,
externalServicesMockMtlsSecuredURL: externalServicesMockMtlsSecuredURL,
certSecuredGraphQLClient: gqlClient,
client: client,
timeout: eventuallyTimeout,
tick: eventuallyTick,
}
}
func (a *LifecycleNotificationsAsserter) WithOperation(operation string) *LifecycleNotificationsAsserter {
a.operation = operation
return a
}
func (a *LifecycleNotificationsAsserter) WithFormationName(formationName string) *LifecycleNotificationsAsserter {
a.formationName = formationName
return a
}
func (a *LifecycleNotificationsAsserter) WithTenantID(tenantID string) *LifecycleNotificationsAsserter {
a.tenantID = tenantID
return a
}
func (a *LifecycleNotificationsAsserter) WithParentTenantID(parentTenantID string) *LifecycleNotificationsAsserter {
a.parentTenantID = parentTenantID
return a
}
func (a *LifecycleNotificationsAsserter) WithState(state string) *LifecycleNotificationsAsserter {
a.state = state
return a
}
func (a *LifecycleNotificationsAsserter) WithExpectNotifications(expectNotifications bool) *LifecycleNotificationsAsserter {
a.expectNotifications = expectNotifications
return a
}
func (a *LifecycleNotificationsAsserter) WithTimeout(timeout time.Duration) *LifecycleNotificationsAsserter {
a.timeout = timeout
return a
}
func (a *LifecycleNotificationsAsserter) WithTick(tick time.Duration) *LifecycleNotificationsAsserter {
a.tick = tick
return a
}
func (a *LifecycleNotificationsAsserter) AssertExpectations(t *testing.T, ctx context.Context) {
var formationID string
var formationName string
if a.formationName != "" {
formation := fixtures.GetFormationByName(t, ctx, a.certSecuredGraphQLClient, a.formationName, a.tenantID)
formationID = formation.ID
formationName = a.formationName
} else {
formationID = ctx.Value(context_keys.FormationIDKey).(string)
formationName = ctx.Value(context_keys.FormationNameKey).(string)
}
body := getNotificationsFromExternalSvcMock(t, a.client, a.externalServicesMockMtlsSecuredURL)
a.assertAsyncFormationNotificationFromCreationOrDeletionWithEventually(t, ctx, body, formationID, formationName, a.state, a.operation, a.tenantID, a.parentTenantID, a.expectNotifications, a.timeout, a.tick)
}
func (a *LifecycleNotificationsAsserter) assertAsyncFormationNotificationFromCreationOrDeletionWithEventually(t *testing.T, ctx context.Context, body []byte, formationID, formationName, formationState, formationOperation, tenantID, parentTenantID string, expectNotifications bool, timeout, tick time.Duration) {
var shouldExpectDeleted bool
if formationOperation == createFormationOperation || formationState == "DELETE_ERROR" {
shouldExpectDeleted = false
} else {
shouldExpectDeleted = true
}
a.assertAsyncFormationNotificationFromCreationOrDeletionExpectDeletedWithEventually(t, ctx, body, formationID, formationName, formationState, formationOperation, tenantID, parentTenantID, expectNotifications, shouldExpectDeleted, timeout, tick)
}
func (a *LifecycleNotificationsAsserter) assertAsyncFormationNotificationFromCreationOrDeletionExpectDeletedWithEventually(t *testing.T, ctx context.Context, body []byte, formationID, formationName, formationState, formationOperation, tenantID, parentTenantID string, expectNotifications, shouldExpectDeleted bool, timeout, tick time.Duration) {
t.Logf("Assert asynchronous formation lifecycle notifications are sent for %q operation...", formationOperation)
notificationsForFormation := gjson.GetBytes(body, formationID)
if !expectNotifications {
require.False(t, notificationsForFormation.Exists())
require.Len(t, notificationsForFormation.Array(), 0)
return
}
require.True(t, notificationsForFormation.Exists())
require.Len(t, notificationsForFormation.Array(), 1)
notificationForFormation := notificationsForFormation.Array()[0]
require.Equal(t, formationOperation, notificationForFormation.Get("Operation").String())
require.Equal(t, tenantID, notificationForFormation.Get("RequestBody.globalAccountId").String())
require.Equal(t, parentTenantID, notificationForFormation.Get("RequestBody.crmId").String())
notificationForFormationDetails := notificationForFormation.Get("RequestBody.details")
require.True(t, notificationForFormationDetails.Exists())
require.Equal(t, formationID, notificationForFormationDetails.Get("id").String())
require.Equal(t, formationName, notificationForFormationDetails.Get("name").String())
t.Logf("Asserting formation with eventually...")
tOnce := testingx.NewOnceLogger(t)
require.Eventually(t, func() (isOkay bool) {
tOnce.Log("Assert formation lifecycle notifications are successfully processed...")
formationPage := fixtures.ListFormationsWithinTenant(t, ctx, tenantID, a.certSecuredGraphQLClient)
var foundFormation *gql.Formation
for _, formation := range formationPage.Data {
if formation.Name == a.formationName {
foundFormation = formation
}
}
if shouldExpectDeleted {
if foundFormation != nil {
tOnce.Logf("Formation lifecycle notification is expected to have deleted formation with ID %q, but it is still there", formationID)
return
}
} else {
if foundFormation == nil {
tOnce.Logf("Formation with ID %s was not found", formationID)
return
}
if foundFormation.State != formationState {
tOnce.Logf("Formation state for formation with ID %q is %q, expected: %q", formationID, foundFormation.State, formationState)
return
}
if foundFormation.ID != formationID {
tOnce.Logf("Formation ID is %q, expected: %q", foundFormation.ID, formationID)
return
}
if foundFormation.Name != formationName {
tOnce.Logf("Formation name is %q, expected: %q", foundFormation.Name, formationName)
return
}
}
tOnce.Logf("Asynchronous formation lifecycle notifications are successfully validated for %q operation.", formationOperation)
return true
}, timeout, tick)
}