Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ type Plan struct {
// Changes holds lists of actions to be executed by dns providers
type Changes struct {
// Records that need to be created
Create []*endpoint.Endpoint
Create []*endpoint.Endpoint `json:"create,omitempty"`
// Records that need to be updated (current data)
UpdateOld []*endpoint.Endpoint
UpdateOld []*endpoint.Endpoint `json:"updateOld,omitempty"`
// Records that need to be updated (desired data)
UpdateNew []*endpoint.Endpoint
UpdateNew []*endpoint.Endpoint `json:"updateNew,omitempty"`
// Records that need to be deleted
Delete []*endpoint.Endpoint
Delete []*endpoint.Endpoint `json:"delete,omitempty"`
}

// planKey is a key for a row in `planTable`.
Expand Down
45 changes: 45 additions & 0 deletions plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package plan

import (
"bytes"
"encoding/json"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -245,6 +248,48 @@ func (suite *PlanTestSuite) SetupTest() {
}
}

func TestPlan_ChangesJson_DecodeEncode(t *testing.T) {
ch := &Changes{
Create: []*endpoint.Endpoint{
{
DNSName: "foo",
},
},
UpdateOld: []*endpoint.Endpoint{
{
DNSName: "bar",
},
},
UpdateNew: []*endpoint.Endpoint{
{
DNSName: "baz",
},
},
Delete: []*endpoint.Endpoint{
{
DNSName: "qux",
},
},
}
jsonBytes, err := json.Marshal(ch)
assert.NoError(t, err)
assert.Equal(t,
`{"create":[{"dnsName":"foo"}],"updateOld":[{"dnsName":"bar"}],"updateNew":[{"dnsName":"baz"}],"delete":[{"dnsName":"qux"}]}`,
string(jsonBytes))
var changes Changes
err = json.NewDecoder(bytes.NewBuffer(jsonBytes)).Decode(&changes)
assert.NoError(t, err)
assert.Equal(t, ch, &changes)
}

func TestPlan_ChangesJson_DecodeMixedCase(t *testing.T) {
input := `{"Create":[{"dnsName":"foo"}],"UpdateOld":[{"dnsName":"bar"}],"updateNew":[{"dnsName":"baz"}],"Delete":[{"dnsName":"qux"}]}`
var changes Changes
err := json.NewDecoder(strings.NewReader(input)).Decode(&changes)
assert.NoError(t, err)
assert.Len(t, changes.Create, 1)
}

func (suite *PlanTestSuite) TestSyncFirstRound() {
current := []*endpoint.Endpoint{}
desired := []*endpoint.Endpoint{suite.fooV1Cname, suite.fooV2Cname, suite.bar127A}
Expand Down
4 changes: 2 additions & 2 deletions provider/webhook/api/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.
return
}

pve := []*endpoint.Endpoint{}
var pve []*endpoint.Endpoint
if err := json.NewDecoder(req.Body).Decode(&pve); err != nil {
log.Errorf("Failed to decode in adjustEndpointsHandler: %v", err)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -104,7 +104,7 @@ func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.
}
}

func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, req *http.Request) {
func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
}
Expand Down
47 changes: 44 additions & 3 deletions provider/webhook/api/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
Expand All @@ -35,8 +37,9 @@ import (
var records []*endpoint.Endpoint

type FakeWebhookProvider struct {
err error
domainFilter endpoint.DomainFilter
err error
domainFilter endpoint.DomainFilter
assertChanges func(*plan.Changes)
}

func (p FakeWebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
Expand All @@ -51,6 +54,9 @@ func (p FakeWebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Cha
return p.err
}
records = append(records, changes.Create...)
if p.assertChanges != nil {
p.assertChanges(changes)
}
return nil
}

Expand Down Expand Up @@ -179,7 +185,7 @@ func TestRecordsHandlerApplyChangesWithErrors(t *testing.T) {
}

func TestRecordsHandlerWithWrongHTTPMethod(t *testing.T) {
req := httptest.NewRequest(http.MethodPut, "/records", nil)
req := httptest.NewRequest(http.MethodPut, UrlRecords, nil)
w := httptest.NewRecorder()

providerAPIServer := &WebhookServer{
Expand All @@ -190,6 +196,41 @@ func TestRecordsHandlerWithWrongHTTPMethod(t *testing.T) {
require.Equal(t, http.StatusBadRequest, res.StatusCode)
}

func TestRecordsHandlerWithMixedCase(t *testing.T) {
input := `{"Create":[{"dnsName":"foo"}],"updateOld":[{"dnsName":"bar"}],"updateNew":[{"dnsName":"baz"}],"Delete":[{"dnsName":"qux"}]}`
req := httptest.NewRequest(http.MethodPost, UrlRecords, strings.NewReader(input))
w := httptest.NewRecorder()

records = []*endpoint.Endpoint{}

providerAPIServer := &WebhookServer{
Provider: &FakeWebhookProvider{
assertChanges: func(changes *plan.Changes) {
t.Helper()
require.Equal(t, []*endpoint.Endpoint{
{
DNSName: "foo",
},
}, changes.Create)
require.Equal(t, []*endpoint.Endpoint{
{
DNSName: "bar",
},
}, changes.UpdateOld)
require.Equal(t, []*endpoint.Endpoint{
{
DNSName: "qux",
},
}, changes.Delete)
},
},
}
providerAPIServer.RecordsHandler(w, req)
res := w.Result()
require.Equal(t, http.StatusNoContent, res.StatusCode)
assert.Equal(t, 1, len(records))
}

func TestAdjustEndpointsHandlerWithInvalidRequest(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, UrlAdjustEndpoints, nil)
w := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion provider/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (p WebhookProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
}

// ApplyChanges will make a POST to remoteServerURL/records with the changes
func (p WebhookProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
func (p WebhookProvider) ApplyChanges(_ context.Context, changes *plan.Changes) error {
applyChangesRequestsGauge.Gauge.Inc()
u := p.remoteServerURL.JoinPath(webhookapi.UrlRecords).String()

Expand Down
Loading