diff --git a/pkg/datadogunifi/integration_test.go b/pkg/datadogunifi/integration_test.go index 31773dc7..023c4f92 100644 --- a/pkg/datadogunifi/integration_test.go +++ b/pkg/datadogunifi/integration_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/unpoller/unpoller/pkg/datadogunifi" - "github.com/unpoller/unpoller/pkg/testutil" + "github.com/unpoller/unpoller/pkg/unittest" "golift.io/cnfg" "gopkg.in/yaml.v3" ) @@ -199,7 +199,7 @@ func TestDataDogUnifiIntegration(t *testing.T) { err = yaml.Unmarshal(yamlFile, &testExpectationsData) require.NoError(t, err) - testRig := testutil.NewTestSetup(t) + testRig := unittest.NewTestSetup(t) defer testRig.Close() mockCapture := &mockStatsd{ @@ -216,7 +216,7 @@ func TestDataDogUnifiIntegration(t *testing.T) { u := datadogunifi.DatadogUnifi{ Datadog: &datadogunifi.Datadog{ Config: &datadogunifi.Config{ - Enable: testutil.PBool(true), + Enable: unittest.PBool(true), Interval: cnfg.Duration{Duration: time.Hour}, }, }, @@ -233,8 +233,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // gauges assert.Equal(t, len(testExpectationsData.Gauges), len(mockCapture.gauges)) - expectedKeys := testutil.NewSetFromSlice[string](testExpectationsData.Gauges) - foundKeys := testutil.NewSetFromMap[string](mockCapture.gauges) + expectedKeys := unittest.NewSetFromSlice[string](testExpectationsData.Gauges) + foundKeys := unittest.NewSetFromMap[string](mockCapture.gauges) additions, deletions := expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) @@ -242,8 +242,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // counts assert.Len(t, mockCapture.counts, 12) - expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Counts) - foundKeys = testutil.NewSetFromMap[string](mockCapture.counts) + expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Counts) + foundKeys = unittest.NewSetFromMap[string](mockCapture.counts) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) @@ -251,8 +251,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // timings assert.Len(t, mockCapture.timings, 2) - expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Timings) - foundKeys = testutil.NewSetFromMap[string](mockCapture.timings) + expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Timings) + foundKeys = unittest.NewSetFromMap[string](mockCapture.timings) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) @@ -260,8 +260,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // histograms assert.Len(t, mockCapture.histograms, 0) - expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Histograms) - foundKeys = testutil.NewSetFromMap[string](mockCapture.histograms) + expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Histograms) + foundKeys = unittest.NewSetFromMap[string](mockCapture.histograms) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) @@ -269,8 +269,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // distributions assert.Len(t, mockCapture.distributions, 0) - expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Distributions) - foundKeys = testutil.NewSetFromMap[string](mockCapture.distributions) + expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Distributions) + foundKeys = unittest.NewSetFromMap[string](mockCapture.distributions) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) @@ -278,8 +278,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // sets assert.Len(t, mockCapture.sets, 0) - expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Sets) - foundKeys = testutil.NewSetFromMap[string](mockCapture.sets) + expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Sets) + foundKeys = unittest.NewSetFromMap[string](mockCapture.sets) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) @@ -291,8 +291,8 @@ func TestDataDogUnifiIntegration(t *testing.T) { // service checks assert.Len(t, mockCapture.checks, 0) - expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.ServiceChecks) - foundKeys = testutil.NewSetFromSlice[string](mockCapture.checks) + expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.ServiceChecks) + foundKeys = unittest.NewSetFromSlice[string](mockCapture.checks) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) diff --git a/pkg/influxunifi/integration_test.go b/pkg/influxunifi/integration_test.go index 7d3e280b..873528fd 100644 --- a/pkg/influxunifi/integration_test.go +++ b/pkg/influxunifi/integration_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/unpoller/unpoller/pkg/influxunifi" - "github.com/unpoller/unpoller/pkg/testutil" + "github.com/unpoller/unpoller/pkg/unittest" "golift.io/cnfg" "gopkg.in/yaml.v3" ) @@ -164,7 +164,7 @@ func TestInfluxV1Integration(t *testing.T) { err = yaml.Unmarshal(yamlFile, &testExpectationsData) require.NoError(t, err) - testRig := testutil.NewTestSetup(t) + testRig := unittest.NewTestSetup(t) defer testRig.Close() mockCapture := newMockInfluxV1Client() @@ -189,37 +189,37 @@ func TestInfluxV1Integration(t *testing.T) { // databases assert.Len(t, mockCapture.databases, 1) - expectedKeys := testutil.NewSetFromSlice[string](testExpectationsData.Databases) - foundKeys := testutil.NewSetFromMap[string](mockCapture.databases) + expectedKeys := unittest.NewSetFromSlice[string](testExpectationsData.Databases) + foundKeys := unittest.NewSetFromMap[string](mockCapture.databases) additions, deletions := expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) // point names - assert.Len(t, testutil.NewSetFromMap[string](mockCapture.points).Slice(), len(testExpectationsData.Points)) - expectedKeys = testutil.NewSetFromMap[string](testExpectationsData.Points) - foundKeys = testutil.NewSetFromMap[string](mockCapture.points) + assert.Len(t, unittest.NewSetFromMap[string](mockCapture.points).Slice(), len(testExpectationsData.Points)) + expectedKeys = unittest.NewSetFromMap[string](testExpectationsData.Points) + foundKeys = unittest.NewSetFromMap[string](mockCapture.points) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0) assert.Len(t, deletions, 0) // validate tags and fields per point - pointNames := testutil.NewSetFromMap[string](testExpectationsData.Points).Slice() + pointNames := unittest.NewSetFromMap[string](testExpectationsData.Points).Slice() sort.Strings(pointNames) for _, pointName := range pointNames { expectedContent := testExpectationsData.Points[pointName] foundContent := mockCapture.points[pointName] // check tags left intact - expectedKeys = testutil.NewSetFromSlice[string](expectedContent.Tags) - foundKeys = testutil.NewSetFromMap[string](foundContent.Tags) + expectedKeys = unittest.NewSetFromSlice[string](expectedContent.Tags) + foundKeys = unittest.NewSetFromMap[string](foundContent.Tags) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len(t, additions, 0, "point \"%s\" found the following tag keys have a difference!: additions=%+v", pointName, additions) assert.Len(t, deletions, 0, "point \"%s\" found the following tag keys have a difference!: deletions=%+v", pointName, deletions) // check field keys intact - expectedKeys = testutil.NewSetFromMap[string](expectedContent.Fields) - foundKeys = testutil.NewSetFromMap[string](foundContent.Fields) + expectedKeys = unittest.NewSetFromMap[string](expectedContent.Fields) + foundKeys = unittest.NewSetFromMap[string](foundContent.Fields) additions, deletions = expectedKeys.Difference(foundKeys) assert.Len( t, @@ -241,7 +241,7 @@ func TestInfluxV1Integration(t *testing.T) { ) // check field types - fieldNames := testutil.NewSetFromMap[string](expectedContent.Fields).Slice() + fieldNames := unittest.NewSetFromMap[string](expectedContent.Fields).Slice() sort.Strings(fieldNames) for _, fieldName := range fieldNames { diff --git a/pkg/inputunifi/collector.go b/pkg/inputunifi/collector.go index 7468cea5..ef26f312 100644 --- a/pkg/inputunifi/collector.go +++ b/pkg/inputunifi/collector.go @@ -50,7 +50,7 @@ func (u *InputUnifi) dynamicController(filter *poller.Filter) (*poller.Metrics, if err := u.getUnifi(c); err != nil { u.logController(c) - + return nil, fmt.Errorf("authenticating to %s: %w", filter.Path, err) } @@ -62,7 +62,7 @@ func (u *InputUnifi) dynamicController(filter *poller.Filter) (*poller.Metrics, func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { u.LogDebugf("Collecting controller data: %s (%s)", c.URL, c.ID) - + if u.isNill(c) { u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) @@ -87,7 +87,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { u.RLock() defer u.RUnlock() - u.LogDebugf("polling controller: %s (%s)", c.URL, c.ID) + u.LogDebugf("Polling controller: %s (%s)", c.URL, c.ID) // Get the sites we care about. sites, err := u.getFilteredSites(c) diff --git a/pkg/poller/testutil.go b/pkg/poller/testutil.go index ff909036..06439638 100644 --- a/pkg/poller/testutil.go +++ b/pkg/poller/testutil.go @@ -11,8 +11,6 @@ type TestCollector struct { poller *Poller } -var _ Collect = &TestCollector{} - func NewTestCollector(l testLogger) *TestCollector { return &TestCollector{ Logger: NewTestLogger(l), diff --git a/pkg/testutil/dep.go b/pkg/unittest/dep.go similarity index 98% rename from pkg/testutil/dep.go rename to pkg/unittest/dep.go index e8b469ac..ddd2c3b3 100644 --- a/pkg/testutil/dep.go +++ b/pkg/unittest/dep.go @@ -1,4 +1,4 @@ -package testutil +package unittest import ( "testing" diff --git a/pkg/testutil/sets.go b/pkg/unittest/sets.go similarity index 60% rename from pkg/testutil/sets.go rename to pkg/unittest/sets.go index 7083f411..b9a48139 100644 --- a/pkg/testutil/sets.go +++ b/pkg/unittest/sets.go @@ -1,9 +1,12 @@ -package testutil +package unittest +// Set provides a generic way to compare sets of type K. This is only used for unit testing. type Set[K comparable] struct { entities map[K]any } +// NewSetFromMap will create a Set of type K from a map[K]V. V is useless here as we are only comparing the set of keys +// in the map. func NewSetFromMap[K comparable, V any](m map[K]V) *Set[K] { entities := make(map[K]any, 0) @@ -16,6 +19,7 @@ func NewSetFromMap[K comparable, V any](m map[K]V) *Set[K] { } } +// NewSetFromSlice will create a Set of type K from a slice of keys. Duplicates will be dropped as this is a set. func NewSetFromSlice[K comparable](s []K) *Set[K] { entities := make(map[K]any, 0) @@ -28,6 +32,9 @@ func NewSetFromSlice[K comparable](s []K) *Set[K] { } } +// Difference will compare two this Set against another Set of the same type K. This will return entries that +// exist in this set but not the other as `additions` and entries that exist in the other set but not this set +// as `deletions`. func (s *Set[K]) Difference(other *Set[K]) (additions []K, deletions []K) { additions = make([]K, 0) @@ -52,6 +59,7 @@ func (s *Set[K]) Len() int { return len(s.entities) } +// Slice will return the set back as a slice of type K func (s *Set[K]) Slice() []K { ret := make([]K, 0) for k := range s.entities { diff --git a/pkg/testutil/sets_test.go b/pkg/unittest/sets_test.go similarity index 78% rename from pkg/testutil/sets_test.go rename to pkg/unittest/sets_test.go index 4161e56c..5b8e4534 100644 --- a/pkg/testutil/sets_test.go +++ b/pkg/unittest/sets_test.go @@ -1,21 +1,21 @@ -package testutil_test +package unittest_test import ( "testing" "github.com/stretchr/testify/assert" - "github.com/unpoller/unpoller/pkg/testutil" + "github.com/unpoller/unpoller/pkg/unittest" ) func TestSets(t *testing.T) { - s1 := testutil.NewSetFromSlice[string]([]string{"a", "b", "c", "c"}) + s1 := unittest.NewSetFromSlice[string]([]string{"a", "b", "c", "c"}) assert.Len(t, s1.Slice(), 3) assert.Contains(t, s1.Slice(), "a") assert.Contains(t, s1.Slice(), "b") assert.Contains(t, s1.Slice(), "c") - s2 := testutil.NewSetFromMap[string](map[string]bool{ + s2 := unittest.NewSetFromMap[string](map[string]bool{ "c": true, "d": false, "e": true,