Skip to content

Commit 318b7c7

Browse files
author
CodingWizKid
committed
add more test cases apply changes
1 parent 19ead08 commit 318b7c7

File tree

1 file changed

+76
-13
lines changed

1 file changed

+76
-13
lines changed

internal/stackitprovider/apply_changes_test.go

+76-13
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ func TestApplyChanges(t *testing.T) {
4040
func testApplyChanges(t *testing.T, changeType ChangeType) {
4141
t.Helper()
4242
ctx := context.Background()
43-
validRespJson := getValidResponseZoneALlBytes(t)
43+
validZoneResponse := getValidResponseZoneALlBytes(t)
4444
validRRSetResponse := getValidResponseRRSetAllBytes(t)
45-
invalidRespJson := []byte(`{"invalid: "json"`)
45+
invalidZoneResponse := []byte(`{"invalid: "json"`)
4646

4747
// Test cases
48-
tests := getApplyChangesBasicTestCases(validRespJson, validRRSetResponse, invalidRespJson)
48+
tests := getApplyChangesBasicTestCases(validZoneResponse, validRRSetResponse, invalidZoneResponse)
4949

5050
for _, tt := range tests {
5151
tt := tt
@@ -79,6 +79,69 @@ func testApplyChanges(t *testing.T, changeType ChangeType) {
7979
}
8080
}
8181

82+
func TestNoMatchingZoneFound(t *testing.T) {
83+
t.Parallel()
84+
85+
ctx := context.Background()
86+
validZoneResponse := getValidResponseZoneALlBytes(t)
87+
88+
mux := http.NewServeMux()
89+
server := httptest.NewServer(mux)
90+
defer server.Close()
91+
92+
// Set up common endpoint for all types of changes
93+
setUpCommonEndpoints(mux, validZoneResponse, http.StatusOK)
94+
95+
stackitDnsProvider, err := getDefaultTestProvider(server)
96+
assert.NoError(t, err)
97+
98+
changes := &plan.Changes{
99+
Create: []*endpoint.Endpoint{
100+
{DNSName: "notfound.com", Targets: endpoint.Targets{"test.notfound.com"}},
101+
},
102+
UpdateNew: []*endpoint.Endpoint{},
103+
Delete: []*endpoint.Endpoint{},
104+
}
105+
106+
err = stackitDnsProvider.ApplyChanges(ctx, changes)
107+
assert.Error(t, err)
108+
}
109+
110+
func TestNoRRSetFound(t *testing.T) {
111+
t.Parallel()
112+
113+
ctx := context.Background()
114+
validZoneResponse := getValidResponseZoneALlBytes(t)
115+
rrSets := getValidResponseRRSetAll()
116+
rrSets.RrSets[0].Name = "notfound.test.com"
117+
validRRSetResponse, err := json.Marshal(rrSets)
118+
assert.NoError(t, err)
119+
120+
mux := http.NewServeMux()
121+
server := httptest.NewServer(mux)
122+
defer server.Close()
123+
124+
// Set up common endpoint for all types of changes
125+
setUpCommonEndpoints(mux, validZoneResponse, http.StatusOK)
126+
127+
mux.HandleFunc(
128+
"/v1/projects/1234/zones/1234/rrsets",
129+
responseHandler(validRRSetResponse, http.StatusOK),
130+
)
131+
132+
stackitDnsProvider, err := getDefaultTestProvider(server)
133+
assert.NoError(t, err)
134+
135+
changes := &plan.Changes{
136+
UpdateNew: []*endpoint.Endpoint{
137+
{DNSName: "test.com", Targets: endpoint.Targets{"notfound.test.com"}},
138+
},
139+
}
140+
141+
err = stackitDnsProvider.ApplyChanges(ctx, changes)
142+
assert.Error(t, err)
143+
}
144+
82145
// setUpCommonEndpoints for all change types.
83146
func setUpCommonEndpoints(mux *http.ServeMux, responseZone []byte, responseZoneCode int) {
84147
mux.HandleFunc("/v1/projects/1234/zones", func(w http.ResponseWriter, r *http.Request) {
@@ -153,9 +216,9 @@ func getChangeTypeChanges(changeType ChangeType) *plan.Changes {
153216
}
154217

155218
func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
156-
validRespJson []byte,
219+
validZoneResponse []byte,
157220
validRRSetResponse []byte,
158-
invalidRespJson []byte,
221+
invalidZoneResponse []byte,
159222
) []struct {
160223
name string
161224
responseZone []byte
@@ -176,7 +239,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
176239
}{
177240
{
178241
"Valid response",
179-
validRespJson,
242+
validZoneResponse,
180243
http.StatusOK,
181244
validRRSetResponse,
182245
http.StatusAccepted,
@@ -203,7 +266,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
203266
},
204267
{
205268
"Zone response Invalid JSON",
206-
invalidRespJson,
269+
invalidZoneResponse,
207270
http.StatusOK,
208271
validRRSetResponse,
209272
http.StatusAccepted,
@@ -212,7 +275,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
212275
},
213276
{
214277
"Zone response, Rrset response 403",
215-
validRespJson,
278+
validZoneResponse,
216279
http.StatusOK,
217280
nil,
218281
http.StatusForbidden,
@@ -221,7 +284,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
221284
},
222285
{
223286
"Zone response, Rrset response 500",
224-
validRespJson,
287+
validZoneResponse,
225288
http.StatusOK,
226289
nil,
227290
http.StatusInternalServerError,
@@ -231,9 +294,9 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
231294
// swagger client does not return an error when the response is invalid json
232295
{
233296
"Zone response, Rrset response Invalid JSON",
234-
validRespJson,
297+
validZoneResponse,
235298
http.StatusOK,
236-
invalidRespJson,
299+
invalidZoneResponse,
237300
http.StatusAccepted,
238301
false,
239302
http.MethodPost,
@@ -257,10 +320,10 @@ func getValidResponseZoneALlBytes(t *testing.T) []byte {
257320
t.Helper()
258321

259322
zones := getValidZoneResponseAll()
260-
validRespJson, err := json.Marshal(zones)
323+
validZoneResponse, err := json.Marshal(zones)
261324
assert.NoError(t, err)
262325

263-
return validRespJson
326+
return validZoneResponse
264327
}
265328

266329
func getValidZoneResponseAll() stackitdnsclient.ZoneResponseZoneAll {

0 commit comments

Comments
 (0)