rename package per per comment, add comments, and drop unnecessary compile time check

This commit is contained in:
Cody Lee 2023-08-03 09:27:09 -05:00
parent 11142c0209
commit 7e87e7f4fb
No known key found for this signature in database
7 changed files with 47 additions and 41 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/unpoller/unpoller/pkg/datadogunifi" "github.com/unpoller/unpoller/pkg/datadogunifi"
"github.com/unpoller/unpoller/pkg/testutil" "github.com/unpoller/unpoller/pkg/unittest"
"golift.io/cnfg" "golift.io/cnfg"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -199,7 +199,7 @@ func TestDataDogUnifiIntegration(t *testing.T) {
err = yaml.Unmarshal(yamlFile, &testExpectationsData) err = yaml.Unmarshal(yamlFile, &testExpectationsData)
require.NoError(t, err) require.NoError(t, err)
testRig := testutil.NewTestSetup(t) testRig := unittest.NewTestSetup(t)
defer testRig.Close() defer testRig.Close()
mockCapture := &mockStatsd{ mockCapture := &mockStatsd{
@ -216,7 +216,7 @@ func TestDataDogUnifiIntegration(t *testing.T) {
u := datadogunifi.DatadogUnifi{ u := datadogunifi.DatadogUnifi{
Datadog: &datadogunifi.Datadog{ Datadog: &datadogunifi.Datadog{
Config: &datadogunifi.Config{ Config: &datadogunifi.Config{
Enable: testutil.PBool(true), Enable: unittest.PBool(true),
Interval: cnfg.Duration{Duration: time.Hour}, Interval: cnfg.Duration{Duration: time.Hour},
}, },
}, },
@ -233,8 +233,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// gauges // gauges
assert.Equal(t, len(testExpectationsData.Gauges), len(mockCapture.gauges)) assert.Equal(t, len(testExpectationsData.Gauges), len(mockCapture.gauges))
expectedKeys := testutil.NewSetFromSlice[string](testExpectationsData.Gauges) expectedKeys := unittest.NewSetFromSlice[string](testExpectationsData.Gauges)
foundKeys := testutil.NewSetFromMap[string](mockCapture.gauges) foundKeys := unittest.NewSetFromMap[string](mockCapture.gauges)
additions, deletions := expectedKeys.Difference(foundKeys) additions, deletions := expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
@ -242,8 +242,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// counts // counts
assert.Len(t, mockCapture.counts, 12) assert.Len(t, mockCapture.counts, 12)
expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Counts) expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Counts)
foundKeys = testutil.NewSetFromMap[string](mockCapture.counts) foundKeys = unittest.NewSetFromMap[string](mockCapture.counts)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
@ -251,8 +251,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// timings // timings
assert.Len(t, mockCapture.timings, 2) assert.Len(t, mockCapture.timings, 2)
expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Timings) expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Timings)
foundKeys = testutil.NewSetFromMap[string](mockCapture.timings) foundKeys = unittest.NewSetFromMap[string](mockCapture.timings)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
@ -260,8 +260,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// histograms // histograms
assert.Len(t, mockCapture.histograms, 0) assert.Len(t, mockCapture.histograms, 0)
expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Histograms) expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Histograms)
foundKeys = testutil.NewSetFromMap[string](mockCapture.histograms) foundKeys = unittest.NewSetFromMap[string](mockCapture.histograms)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
@ -269,8 +269,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// distributions // distributions
assert.Len(t, mockCapture.distributions, 0) assert.Len(t, mockCapture.distributions, 0)
expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Distributions) expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Distributions)
foundKeys = testutil.NewSetFromMap[string](mockCapture.distributions) foundKeys = unittest.NewSetFromMap[string](mockCapture.distributions)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
@ -278,8 +278,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// sets // sets
assert.Len(t, mockCapture.sets, 0) assert.Len(t, mockCapture.sets, 0)
expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.Sets) expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.Sets)
foundKeys = testutil.NewSetFromMap[string](mockCapture.sets) foundKeys = unittest.NewSetFromMap[string](mockCapture.sets)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
@ -291,8 +291,8 @@ func TestDataDogUnifiIntegration(t *testing.T) {
// service checks // service checks
assert.Len(t, mockCapture.checks, 0) assert.Len(t, mockCapture.checks, 0)
expectedKeys = testutil.NewSetFromSlice[string](testExpectationsData.ServiceChecks) expectedKeys = unittest.NewSetFromSlice[string](testExpectationsData.ServiceChecks)
foundKeys = testutil.NewSetFromSlice[string](mockCapture.checks) foundKeys = unittest.NewSetFromSlice[string](mockCapture.checks)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)

View File

@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/unpoller/unpoller/pkg/influxunifi" "github.com/unpoller/unpoller/pkg/influxunifi"
"github.com/unpoller/unpoller/pkg/testutil" "github.com/unpoller/unpoller/pkg/unittest"
"golift.io/cnfg" "golift.io/cnfg"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -164,7 +164,7 @@ func TestInfluxV1Integration(t *testing.T) {
err = yaml.Unmarshal(yamlFile, &testExpectationsData) err = yaml.Unmarshal(yamlFile, &testExpectationsData)
require.NoError(t, err) require.NoError(t, err)
testRig := testutil.NewTestSetup(t) testRig := unittest.NewTestSetup(t)
defer testRig.Close() defer testRig.Close()
mockCapture := newMockInfluxV1Client() mockCapture := newMockInfluxV1Client()
@ -189,37 +189,37 @@ func TestInfluxV1Integration(t *testing.T) {
// databases // databases
assert.Len(t, mockCapture.databases, 1) assert.Len(t, mockCapture.databases, 1)
expectedKeys := testutil.NewSetFromSlice[string](testExpectationsData.Databases) expectedKeys := unittest.NewSetFromSlice[string](testExpectationsData.Databases)
foundKeys := testutil.NewSetFromMap[string](mockCapture.databases) foundKeys := unittest.NewSetFromMap[string](mockCapture.databases)
additions, deletions := expectedKeys.Difference(foundKeys) additions, deletions := expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
// point names // point names
assert.Len(t, testutil.NewSetFromMap[string](mockCapture.points).Slice(), len(testExpectationsData.Points)) assert.Len(t, unittest.NewSetFromMap[string](mockCapture.points).Slice(), len(testExpectationsData.Points))
expectedKeys = testutil.NewSetFromMap[string](testExpectationsData.Points) expectedKeys = unittest.NewSetFromMap[string](testExpectationsData.Points)
foundKeys = testutil.NewSetFromMap[string](mockCapture.points) foundKeys = unittest.NewSetFromMap[string](mockCapture.points)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len(t, additions, 0) assert.Len(t, additions, 0)
assert.Len(t, deletions, 0) assert.Len(t, deletions, 0)
// validate tags and fields per point // validate tags and fields per point
pointNames := testutil.NewSetFromMap[string](testExpectationsData.Points).Slice() pointNames := unittest.NewSetFromMap[string](testExpectationsData.Points).Slice()
sort.Strings(pointNames) sort.Strings(pointNames)
for _, pointName := range pointNames { for _, pointName := range pointNames {
expectedContent := testExpectationsData.Points[pointName] expectedContent := testExpectationsData.Points[pointName]
foundContent := mockCapture.points[pointName] foundContent := mockCapture.points[pointName]
// check tags left intact // check tags left intact
expectedKeys = testutil.NewSetFromSlice[string](expectedContent.Tags) expectedKeys = unittest.NewSetFromSlice[string](expectedContent.Tags)
foundKeys = testutil.NewSetFromMap[string](foundContent.Tags) foundKeys = unittest.NewSetFromMap[string](foundContent.Tags)
additions, deletions = expectedKeys.Difference(foundKeys) 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, 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) assert.Len(t, deletions, 0, "point \"%s\" found the following tag keys have a difference!: deletions=%+v", pointName, deletions)
// check field keys intact // check field keys intact
expectedKeys = testutil.NewSetFromMap[string](expectedContent.Fields) expectedKeys = unittest.NewSetFromMap[string](expectedContent.Fields)
foundKeys = testutil.NewSetFromMap[string](foundContent.Fields) foundKeys = unittest.NewSetFromMap[string](foundContent.Fields)
additions, deletions = expectedKeys.Difference(foundKeys) additions, deletions = expectedKeys.Difference(foundKeys)
assert.Len( assert.Len(
t, t,
@ -241,7 +241,7 @@ func TestInfluxV1Integration(t *testing.T) {
) )
// check field types // check field types
fieldNames := testutil.NewSetFromMap[string](expectedContent.Fields).Slice() fieldNames := unittest.NewSetFromMap[string](expectedContent.Fields).Slice()
sort.Strings(fieldNames) sort.Strings(fieldNames)
for _, fieldName := range fieldNames { for _, fieldName := range fieldNames {

View File

@ -87,7 +87,7 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) {
func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) { func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
u.RLock() u.RLock()
defer u.RUnlock() 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. // Get the sites we care about.
sites, err := u.getFilteredSites(c) sites, err := u.getFilteredSites(c)

View File

@ -11,8 +11,6 @@ type TestCollector struct {
poller *Poller poller *Poller
} }
var _ Collect = &TestCollector{}
func NewTestCollector(l testLogger) *TestCollector { func NewTestCollector(l testLogger) *TestCollector {
return &TestCollector{ return &TestCollector{
Logger: NewTestLogger(l), Logger: NewTestLogger(l),

View File

@ -1,4 +1,4 @@
package testutil package unittest
import ( import (
"testing" "testing"

View File

@ -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 { type Set[K comparable] struct {
entities map[K]any 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] { func NewSetFromMap[K comparable, V any](m map[K]V) *Set[K] {
entities := make(map[K]any, 0) 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] { func NewSetFromSlice[K comparable](s []K) *Set[K] {
entities := make(map[K]any, 0) 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) { func (s *Set[K]) Difference(other *Set[K]) (additions []K, deletions []K) {
additions = make([]K, 0) additions = make([]K, 0)
@ -52,6 +59,7 @@ func (s *Set[K]) Len() int {
return len(s.entities) return len(s.entities)
} }
// Slice will return the set back as a slice of type K
func (s *Set[K]) Slice() []K { func (s *Set[K]) Slice() []K {
ret := make([]K, 0) ret := make([]K, 0)
for k := range s.entities { for k := range s.entities {

View File

@ -1,21 +1,21 @@
package testutil_test package unittest_test
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/unpoller/unpoller/pkg/testutil" "github.com/unpoller/unpoller/pkg/unittest"
) )
func TestSets(t *testing.T) { 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.Len(t, s1.Slice(), 3)
assert.Contains(t, s1.Slice(), "a") assert.Contains(t, s1.Slice(), "a")
assert.Contains(t, s1.Slice(), "b") assert.Contains(t, s1.Slice(), "b")
assert.Contains(t, s1.Slice(), "c") assert.Contains(t, s1.Slice(), "c")
s2 := testutil.NewSetFromMap[string](map[string]bool{ s2 := unittest.NewSetFromMap[string](map[string]bool{
"c": true, "c": true,
"d": false, "d": false,
"e": true, "e": true,