-
Notifications
You must be signed in to change notification settings - Fork 13
/
aliases_test.go
223 lines (206 loc) · 8.33 KB
/
aliases_test.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package opslevel_test
import (
"testing"
ol "github.com/opslevel/opslevel-go/v2024"
"github.com/rocktavious/autopilot/v2023"
)
type extractAliasesTestCase struct {
aliasesWanted []string
existingAliases []string
expectedAliasesToCreate []string
expectedAliasesToDelete []string
}
func TestExtractAliases(t *testing.T) {
var noAliases []string
aliases := []string{"foo", "alpha", "beta", "gamma"}
// Arrange
testCases := map[string]extractAliasesTestCase{
"create all delete none": {
aliasesWanted: aliases,
existingAliases: noAliases,
expectedAliasesToCreate: aliases,
expectedAliasesToDelete: noAliases,
},
"create none delete all": {
aliasesWanted: noAliases,
existingAliases: aliases,
expectedAliasesToCreate: noAliases,
expectedAliasesToDelete: aliases,
},
"create some delete some": {
aliasesWanted: []string{"foo", "alpha", "beta"},
existingAliases: []string{"alpha", "beta", "gamma"},
expectedAliasesToCreate: []string{"foo"},
expectedAliasesToDelete: []string{"gamma"},
},
"no change": {
aliasesWanted: aliases,
existingAliases: aliases,
expectedAliasesToCreate: noAliases,
expectedAliasesToDelete: noAliases,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
// Act
aliasesToCreate, aliasesToDelete := ol.TestExtractAliases(tc.existingAliases, tc.aliasesWanted)
// Assert
autopilot.Equals(t, aliasesToCreate, tc.expectedAliasesToCreate)
autopilot.Equals(t, aliasesToDelete, tc.expectedAliasesToDelete)
})
}
}
func TestCreateAliases(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation AliasCreate($input:AliasCreateInput!){aliasCreate(input: $input){aliases,ownerId,errors{message,path}}}`,
`{"input": { "alias": "MyAwesomeAlias", "ownerId": "Z2lkOi8vb3BzbGV2ZWwvU2VydmljZS80MTc2" }}`,
`{"data": { "aliasCreate": { "aliases": [ "MyCoolService", "MyAwesomeAlias" ], "ownerId": "Z2lkOi8vb3BzbGV2ZWwvU2VydmljZS80MTc2", "errors": [] }}}`,
)
client := BestTestClient(t, "aliases/create", testRequest)
// Act
result, err := client.CreateAliases("Z2lkOi8vb3BzbGV2ZWwvU2VydmljZS80MTc2", []string{"MyAwesomeAlias"})
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, 2, len(result))
autopilot.Equals(t, "MyAwesomeAlias", result[1])
}
func TestCreateAliasesOwnerNotFound(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation AliasCreate($input:AliasCreateInput!){aliasCreate(input: $input){aliases,ownerId,errors{message,path}}}`,
`{"input": { "alias": "MyAwesomeAlias", "ownerId": "Z2lkOi8vb3BzbGV2ZWwvU2VydmljZS80MTc2" }}`,
`{ "data": { "aliasCreate": { "aliases": null, "ownerId": null, "errors": [ { "message": "'Z2lkOi8vb3BzbGV2ZWwvU2VydmljZS80MTc2' does not identify any record on this account", "path": [ "ownerId" ] } ] } } }`,
)
client := BestTestClient(t, "aliases/create_owner_not_found", testRequest)
// Act
result, err := client.CreateAliases("Z2lkOi8vb3BzbGV2ZWwvU2VydmljZS80MTc2", []string{"MyAwesomeAlias"})
// Assert
if err == nil {
t.Error("expected number of errors to be > 1")
}
autopilot.Equals(t, 0, len(result))
}
func TestDeleteServiceAlias(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation AliasDelete($input:AliasDeleteInput!){aliasDelete(input: $input){deletedAlias,errors{message,path}}}`,
`{"input": { "alias": "MyAwesomeAlias", "ownerType": "service" }}`,
`{"data": { "aliasDelete": { "deletedAlias": "MyAwesomeAlias", "errors": [] }}}`,
)
client := BestTestClient(t, "aliases/delete_service", testRequest)
// Act
err := client.DeleteServiceAlias("MyAwesomeAlias")
// Assert
autopilot.Ok(t, err)
}
func TestDeleteTeamAlias(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation AliasDelete($input:AliasDeleteInput!){aliasDelete(input: $input){deletedAlias,errors{message,path}}}`,
`{"input": { "alias": "MyAwesomeAlias", "ownerType": "team" }}`,
`{"data": { "aliasDelete": { "deletedAlias": "MyAwesomeAlias", "errors": [] }}}`,
)
client := BestTestClient(t, "aliases/delete_team", testRequest)
// Act
err := client.DeleteTeamAlias("MyAwesomeAlias")
// Assert
autopilot.Ok(t, err)
}
func TestDeleteAliasNotFound(t *testing.T) {
// Arrange
testRequest := autopilot.NewTestRequest(
`mutation AliasDelete($input:AliasDeleteInput!){aliasDelete(input: $input){deletedAlias,errors{message,path}}}`,
`{"input": { "alias": "MyAwesomeAlias", "ownerType": "team" }}`,
`{ "data": { "aliasDelete": { "deletedAlias": null, "errors": [ { "message": "'MyAwesomeAlias' does not identify a team on this account", "path": [ "alias" ] } ] } } }`,
)
client := BestTestClient(t, "aliases/delete_alias_not_found", testRequest)
// Act
err := client.DeleteTeamAlias("MyAwesomeAlias")
// Assert
if err == nil {
t.Error("expected number of errors to be > 1")
}
}
func TestGetAliasableResource(t *testing.T) {
service := autopilot.Register[ol.ServiceId]("example_service",
ol.ServiceId{
Id: id1,
Aliases: []string{alias1, alias2, alias3},
},
)
team := autopilot.Register[ol.Team]("example_team",
ol.Team{
TeamId: ol.TeamId{
Id: id1,
},
Aliases: []string{alias1, alias2, alias4},
},
)
system := autopilot.Register[ol.SystemId]("example_system",
ol.SystemId{
Id: id1,
Aliases: []string{alias3},
})
requests := []autopilot.TestRequest{
autopilot.NewTestRequest(
`query ServiceGet($service:ID!){account{service(id: $service){{ template "service_get" }}}}}`,
`{"service": "{{ template "id1_string" }}"}`,
`{ "data": { "account": {"service": {{ template "example_service" }}}}}`,
),
autopilot.NewTestRequest(
`query ServiceGet($service:String!){account{service(alias: $service){{ template "service_get" }}}}}`,
`{"service": "{{ template "alias1" }}"}`,
`{ "data": { "account": {"service": {{ template "example_service" }}}}}`,
),
autopilot.NewTestRequest(
`query TeamGet($id:ID!){account{team(id: $id){{ template "team_get" }}}}`,
`{"id": "{{ template "id1_string" }}"}`,
`{ "data": { "account": {"team": {{ template "example_team" }}}}}`,
),
autopilot.NewTestRequest(
`query TeamGet($alias:String!){account{team(alias: $alias){{ template "team_get" }}}}`,
`{"alias": "{{ template "alias1" }}"}`,
`{ "data": { "account": {"team": {{ template "example_team" }}}}}`,
),
autopilot.NewTestRequest(
`query SystemGet($input:IdentifierInput!){account{system(input: $input){{ template "system_get" }}}}`,
`{"input": {"alias": "{{ template "alias1" }}"}}`,
`{ "data": { "account": {"system": {{ template "example_system"}}}}}`,
),
autopilot.NewTestRequest(
`{{ template "scorecard_get_request" }}`,
`{{ template "scorecard_get_request_vars" }}`,
`{{ template "scorecard_get_response" }}`,
),
autopilot.NewTestRequest(
`query InfrastructureResourceGet($all:Boolean!$input:IdentifierInput!){account{infrastructureResource(input: $input){{ template "infra_get" }}}}`,
`{"all": true, "input":{ {{ template "id1" }} }}`,
`{"data": { "account": { "infrastructureResource": {{ template "infra_1" }} }}}`,
),
}
client := BestTestClient(t, "tags/get_aliasable_resource", requests...)
// Act
service1, err1 := client.GetAliasableResource(ol.AliasOwnerTypeEnumService, string(id1))
service2, err2 := client.GetAliasableResource(ol.AliasOwnerTypeEnumService, alias1)
team1, err3 := client.GetAliasableResource(ol.AliasOwnerTypeEnumTeam, string(id1))
team2, err4 := client.GetAliasableResource(ol.AliasOwnerTypeEnumTeam, alias1)
system1, err5 := client.GetAliasableResource(ol.AliasOwnerTypeEnumSystem, alias1)
scorecard1, err6 := client.GetAliasableResource(ol.AliasOwnerTypeEnumScorecard, scorecardId)
infra1, err7 := client.GetAliasableResource(ol.AliasOwnerTypeEnumInfrastructureResource, string(id1))
// Assert
autopilot.Ok(t, err1)
autopilot.Equals(t, service.Aliases, service1.GetAliases())
autopilot.Ok(t, err2)
autopilot.Equals(t, service.Aliases, service2.GetAliases())
autopilot.Ok(t, err3)
autopilot.Equals(t, team.Aliases, team1.GetAliases())
autopilot.Ok(t, err4)
autopilot.Equals(t, team.Aliases, team2.GetAliases())
autopilot.Ok(t, err5)
autopilot.Equals(t, system.Aliases, system1.GetAliases())
autopilot.Ok(t, err6)
autopilot.Equals(t, []string{"existing_scorecard"}, scorecard1.GetAliases())
autopilot.Ok(t, err7)
autopilot.Equals(t, []string{}, infra1.GetAliases())
}