Skip to content

Commit

Permalink
add more test cases apply changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickKoss committed Jul 13, 2023
1 parent 19ead08 commit 318b7c7
Showing 1 changed file with 76 additions and 13 deletions.
89 changes: 76 additions & 13 deletions internal/stackitprovider/apply_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func TestApplyChanges(t *testing.T) {
func testApplyChanges(t *testing.T, changeType ChangeType) {
t.Helper()
ctx := context.Background()
validRespJson := getValidResponseZoneALlBytes(t)
validZoneResponse := getValidResponseZoneALlBytes(t)
validRRSetResponse := getValidResponseRRSetAllBytes(t)
invalidRespJson := []byte(`{"invalid: "json"`)
invalidZoneResponse := []byte(`{"invalid: "json"`)

// Test cases
tests := getApplyChangesBasicTestCases(validRespJson, validRRSetResponse, invalidRespJson)
tests := getApplyChangesBasicTestCases(validZoneResponse, validRRSetResponse, invalidZoneResponse)

for _, tt := range tests {
tt := tt
Expand Down Expand Up @@ -79,6 +79,69 @@ func testApplyChanges(t *testing.T, changeType ChangeType) {
}
}

func TestNoMatchingZoneFound(t *testing.T) {
t.Parallel()

ctx := context.Background()
validZoneResponse := getValidResponseZoneALlBytes(t)

mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()

// Set up common endpoint for all types of changes
setUpCommonEndpoints(mux, validZoneResponse, http.StatusOK)

stackitDnsProvider, err := getDefaultTestProvider(server)
assert.NoError(t, err)

changes := &plan.Changes{
Create: []*endpoint.Endpoint{
{DNSName: "notfound.com", Targets: endpoint.Targets{"test.notfound.com"}},
},
UpdateNew: []*endpoint.Endpoint{},
Delete: []*endpoint.Endpoint{},
}

err = stackitDnsProvider.ApplyChanges(ctx, changes)
assert.Error(t, err)
}

func TestNoRRSetFound(t *testing.T) {
t.Parallel()

ctx := context.Background()
validZoneResponse := getValidResponseZoneALlBytes(t)
rrSets := getValidResponseRRSetAll()
rrSets.RrSets[0].Name = "notfound.test.com"
validRRSetResponse, err := json.Marshal(rrSets)
assert.NoError(t, err)

mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()

// Set up common endpoint for all types of changes
setUpCommonEndpoints(mux, validZoneResponse, http.StatusOK)

mux.HandleFunc(
"/v1/projects/1234/zones/1234/rrsets",
responseHandler(validRRSetResponse, http.StatusOK),
)

stackitDnsProvider, err := getDefaultTestProvider(server)
assert.NoError(t, err)

changes := &plan.Changes{
UpdateNew: []*endpoint.Endpoint{
{DNSName: "test.com", Targets: endpoint.Targets{"notfound.test.com"}},
},
}

err = stackitDnsProvider.ApplyChanges(ctx, changes)
assert.Error(t, err)
}

// setUpCommonEndpoints for all change types.
func setUpCommonEndpoints(mux *http.ServeMux, responseZone []byte, responseZoneCode int) {
mux.HandleFunc("/v1/projects/1234/zones", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -153,9 +216,9 @@ func getChangeTypeChanges(changeType ChangeType) *plan.Changes {
}

func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
validRespJson []byte,
validZoneResponse []byte,
validRRSetResponse []byte,
invalidRespJson []byte,
invalidZoneResponse []byte,
) []struct {
name string
responseZone []byte
Expand All @@ -176,7 +239,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
}{
{
"Valid response",
validRespJson,
validZoneResponse,
http.StatusOK,
validRRSetResponse,
http.StatusAccepted,
Expand All @@ -203,7 +266,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
},
{
"Zone response Invalid JSON",
invalidRespJson,
invalidZoneResponse,
http.StatusOK,
validRRSetResponse,
http.StatusAccepted,
Expand All @@ -212,7 +275,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
},
{
"Zone response, Rrset response 403",
validRespJson,
validZoneResponse,
http.StatusOK,
nil,
http.StatusForbidden,
Expand All @@ -221,7 +284,7 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
},
{
"Zone response, Rrset response 500",
validRespJson,
validZoneResponse,
http.StatusOK,
nil,
http.StatusInternalServerError,
Expand All @@ -231,9 +294,9 @@ func getApplyChangesBasicTestCases( //nolint:funlen // Test cases are long
// swagger client does not return an error when the response is invalid json
{
"Zone response, Rrset response Invalid JSON",
validRespJson,
validZoneResponse,
http.StatusOK,
invalidRespJson,
invalidZoneResponse,
http.StatusAccepted,
false,
http.MethodPost,
Expand All @@ -257,10 +320,10 @@ func getValidResponseZoneALlBytes(t *testing.T) []byte {
t.Helper()

zones := getValidZoneResponseAll()
validRespJson, err := json.Marshal(zones)
validZoneResponse, err := json.Marshal(zones)
assert.NoError(t, err)

return validRespJson
return validZoneResponse
}

func getValidZoneResponseAll() stackitdnsclient.ZoneResponseZoneAll {
Expand Down

0 comments on commit 318b7c7

Please sign in to comment.