Skip to content

Commit

Permalink
Update unit tests to include request count tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
sfleener committed Jan 8, 2023
1 parent 83d8804 commit 27eb7c9
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion provider/pihole/pihole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

type testPiholeClient struct {
endpoints []*endpoint.Endpoint
requests *requestTracker
}

func (t *testPiholeClient) listRecords(ctx context.Context, rtype string) ([]*endpoint.Endpoint, error) {
Expand All @@ -40,6 +41,7 @@ func (t *testPiholeClient) listRecords(ctx context.Context, rtype string) ([]*en

func (t *testPiholeClient) createRecord(ctx context.Context, ep *endpoint.Endpoint) error {
t.endpoints = append(t.endpoints, ep)
t.requests.createRequests += 1
return nil
}

Expand All @@ -51,9 +53,20 @@ func (t *testPiholeClient) deleteRecord(ctx context.Context, ep *endpoint.Endpoi
}
}
t.endpoints = newEPs
t.requests.deleteRequests += 1
return nil
}

type requestTracker struct {
createRequests int
deleteRequests int
}

func (r *requestTracker) clear() {
r.createRequests = 0
r.deleteRequests = 0
}

func TestNewPiholeProvider(t *testing.T) {
// Test invalid configuration
_, err := NewPiholeProvider(PiholeConfig{})
Expand All @@ -68,8 +81,9 @@ func TestNewPiholeProvider(t *testing.T) {
}

func TestProvider(t *testing.T) {
requests := requestTracker{}
p := &PiholeProvider{
api: &testPiholeClient{},
api: &testPiholeClient{endpoints: make([]*endpoint.Endpoint, 0), requests: &requests},
}

records, err := p.Records(context.Background())
Expand Down Expand Up @@ -113,6 +127,12 @@ func TestProvider(t *testing.T) {
if len(newRecords) != 3 {
t.Fatal("Expected list of 3 records, got:", records)
}
if requests.createRequests != 3 {
t.Fatal("Expected 3 create requests, got:", requests.createRequests)
}
if requests.deleteRequests != 0 {
t.Fatal("Expected no delete requests, got:", requests.deleteRequests)
}

for idx, record := range records {
if newRecords[idx].DNSName != record.DNSName {
Expand All @@ -123,6 +143,8 @@ func TestProvider(t *testing.T) {
}
}

requests.clear()

// Test delete a record

records = []*endpoint.Endpoint{
Expand All @@ -148,6 +170,12 @@ func TestProvider(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
if requests.createRequests != 0 {
t.Fatal("Expected no create requests, got:", requests.createRequests)
}
if requests.deleteRequests != 1 {
t.Fatal("Expected 1 delete request, got:", requests.deleteRequests)
}

// Test records are updated
newRecords, err = p.Records(context.Background())
Expand All @@ -167,6 +195,8 @@ func TestProvider(t *testing.T) {
}
}

requests.clear()

// Test update a record

records = []*endpoint.Endpoint{
Expand All @@ -183,13 +213,23 @@ func TestProvider(t *testing.T) {
}
if err := p.ApplyChanges(context.Background(), &plan.Changes{
UpdateOld: []*endpoint.Endpoint{
{
DNSName: "test1.example.com",
Targets: []string{"192.168.1.1"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "test2.example.com",
Targets: []string{"192.168.1.2"},
RecordType: endpoint.RecordTypeA,
},
},
UpdateNew: []*endpoint.Endpoint{
{
DNSName: "test1.example.com",
Targets: []string{"192.168.1.1"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "test2.example.com",
Targets: []string{"10.0.0.1"},
Expand All @@ -208,6 +248,12 @@ func TestProvider(t *testing.T) {
if len(newRecords) != 2 {
t.Fatal("Expected list of 2 records, got:", records)
}
if requests.createRequests != 1 {
t.Fatal("Expected 1 create request, got:", requests.createRequests)
}
if requests.deleteRequests != 1 {
t.Fatal("Expected 1 delete request, got:", requests.deleteRequests)
}

for idx, record := range records {
if newRecords[idx].DNSName != record.DNSName {
Expand All @@ -217,4 +263,6 @@ func TestProvider(t *testing.T) {
t.Error("Targets malformed on retrieval, got:", newRecords[idx].Targets, "expected:", record.Targets)
}
}

requests.clear()
}

0 comments on commit 27eb7c9

Please sign in to comment.