-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only update PiHole entries when they have actually changed #3297
Changes from 2 commits
83d8804
27eb7c9
e264fde
735b13b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better unit tests would explicitly specify the expected requests, verifying the dnsname, recordtype, and target. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the tests to compare the actual and expected requests instead of just the counts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new version of the tests is asserting that the three new records are created in order, which isn't actually a requirement. I would have written this to put the expected calls in the tracker. The client would then find the request in the tracker and remove it, asserting that the request was found in the tracker. At the end, the test would assert that there were no remaining expected calls in the tracker. |
||
} | ||
|
||
func (r *requestTracker) clear() { | ||
r.createRequests = 0 | ||
r.deleteRequests = 0 | ||
} | ||
|
||
func TestNewPiholeProvider(t *testing.T) { | ||
// Test invalid configuration | ||
_, err := NewPiholeProvider(PiholeConfig{}) | ||
|
@@ -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()) | ||
|
@@ -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 { | ||
|
@@ -123,6 +143,8 @@ func TestProvider(t *testing.T) { | |
} | ||
} | ||
|
||
requests.clear() | ||
|
||
// Test delete a record | ||
|
||
records = []*endpoint.Endpoint{ | ||
|
@@ -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()) | ||
|
@@ -167,6 +195,8 @@ func TestProvider(t *testing.T) { | |
} | ||
} | ||
|
||
requests.clear() | ||
|
||
// Test update a record | ||
|
||
records = []*endpoint.Endpoint{ | ||
|
@@ -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"}, | ||
|
@@ -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 { | ||
|
@@ -217,4 +263,6 @@ func TestProvider(t *testing.T) { | |
t.Error("Targets malformed on retrieval, got:", newRecords[idx].Targets, "expected:", record.Targets) | ||
} | ||
} | ||
|
||
requests.clear() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
changes.UpdateNew
has multiple endpoints with the sameDNSName
? Shouldn't you put theRecordType
in the map key?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! PiHole doesn't allow two records of the same RecordType with the same DNS name, but it does allow two records with the same DNS name if the record types are different. Fixed