Skip to content

Commit

Permalink
Fix tests for new constructor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Jan 13, 2020
1 parent 8f4461f commit 9951ab2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
36 changes: 26 additions & 10 deletions pkg/api/cache_test.go
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/peterbourgon/fastly-exporter/pkg/api"
"github.com/peterbourgon/fastly-exporter/pkg/filter"
)

func TestCache(t *testing.T) {
Expand Down Expand Up @@ -43,47 +43,47 @@ func TestCache(t *testing.T) {
},
{
name: "exact name include match",
options: []api.CacheOption{api.WithNameIncluding(regexp.MustCompile(`^` + s1.Name + `$`))},
options: []api.CacheOption{api.WithNameFilter(filterWhitelist(`^` + s1.Name + `$`))},
want: []api.Service{s1},
},
{
name: "partial name include match",
options: []api.CacheOption{api.WithNameIncluding(regexp.MustCompile(`mmy`))},
options: []api.CacheOption{api.WithNameFilter(filterWhitelist(`mmy`))},
want: []api.Service{s2},
},
{
name: "generous name include match",
options: []api.CacheOption{api.WithNameIncluding(regexp.MustCompile(`.*e.*`))},
options: []api.CacheOption{api.WithNameFilter(filterWhitelist(`.*e.*`))},
want: []api.Service{s1, s2},
},
{
name: "no name include match",
options: []api.CacheOption{api.WithNameIncluding(regexp.MustCompile(`not found`))},
options: []api.CacheOption{api.WithNameFilter(filterWhitelist(`not found`))},
want: []api.Service{},
},
{
name: "exact name exclude match",
options: []api.CacheOption{api.WithNameExcluding(regexp.MustCompile(`^` + s1.Name + `$`))},
options: []api.CacheOption{api.WithNameFilter(filterBlacklist(`^` + s1.Name + `$`))},
want: []api.Service{s2},
},
{
name: "partial name exclude match",
options: []api.CacheOption{api.WithNameExcluding(regexp.MustCompile(`mmy`))},
options: []api.CacheOption{api.WithNameFilter(filterBlacklist(`mmy`))},
want: []api.Service{s1},
},
{
name: "generous name exclude match",
options: []api.CacheOption{api.WithNameExcluding(regexp.MustCompile(`.*e.*`))},
options: []api.CacheOption{api.WithNameFilter(filterBlacklist(`.*e.*`))},
want: []api.Service{},
},
{
name: "no name exclude match",
options: []api.CacheOption{api.WithNameExcluding(regexp.MustCompile(`not found`))},
options: []api.CacheOption{api.WithNameFilter(filterBlacklist(`not found`))},
want: []api.Service{s1, s2},
},
{
name: "name exclude and include",
options: []api.CacheOption{api.WithNameIncluding(regexp.MustCompile(`.*e.*`)), api.WithNameExcluding(regexp.MustCompile(`mmy`))},
options: []api.CacheOption{api.WithNameFilter(filterWhitelistBlacklist(`.*e.*`, `mmy`))},
want: []api.Service{s1},
},
{
Expand Down Expand Up @@ -140,6 +140,22 @@ func TestCache(t *testing.T) {
}
}

func filterWhitelist(w string) (f filter.Filter) {
f.Whitelist(w)
return f
}

func filterBlacklist(b string) (f filter.Filter) {
f.Blacklist(b)
return f
}

func filterWhitelistBlacklist(w, b string) (f filter.Filter) {
f.Whitelist(w)
f.Blacklist(b)
return f
}

type fixedResponseClient struct {
code int
response string
Expand Down
14 changes: 8 additions & 6 deletions pkg/prom/metrics_test.go
Expand Up @@ -3,31 +3,33 @@ package prom_test
import (
"testing"

"github.com/peterbourgon/fastly-exporter/pkg/filter"
"github.com/peterbourgon/fastly-exporter/pkg/prom"
"github.com/prometheus/client_golang/prometheus"
)

func TestRegistration(t *testing.T) {
var (
namespace = "namespace"
subsystem = "subsystem"
registry = prometheus.NewRegistry()
namespace = "namespace"
subsystem = "subsystem"
nameFilter = filter.Filter{} // allow all
registry = prometheus.NewRegistry()
)

{
_, err := prom.NewMetrics(namespace, subsystem, registry)
_, err := prom.NewMetrics(namespace, subsystem, nameFilter, registry)
if err != nil {
t.Errorf("unexpected error on first construction: %v", err)
}
}
{
_, err := prom.NewMetrics(namespace, subsystem, registry)
_, err := prom.NewMetrics(namespace, subsystem, nameFilter, registry)
if err == nil {
t.Error("unexpected success on second construction")
}
}
{
_, err := prom.NewMetrics("alt"+namespace, subsystem, registry)
_, err := prom.NewMetrics("alt"+namespace, subsystem, nameFilter, registry)
if err != nil {
t.Errorf("unexpected error on third, alt-namespace construction: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/rt/manager_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-kit/kit/log/level"
"github.com/google/go-cmp/cmp"
"github.com/peterbourgon/fastly-exporter/pkg/api"
"github.com/peterbourgon/fastly-exporter/pkg/filter"
"github.com/peterbourgon/fastly-exporter/pkg/prom"
"github.com/peterbourgon/fastly-exporter/pkg/rt"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -22,7 +23,7 @@ func TestManager(t *testing.T) {
s3 = api.Service{ID: "3a3b3c", Name: "service 3", Version: 3}
client = newMockRealtimeClient(`{}`)
token = "irrelevant-token"
metrics, _ = prom.NewMetrics("namespace", "subsystem", prometheus.NewRegistry())
metrics, _ = prom.NewMetrics("namespace", "subsystem", filter.Filter{}, prometheus.NewRegistry())
logbuf = &bytes.Buffer{}
logger = log.NewLogfmtLogger(logbuf)
options = []rt.SubscriberOption{rt.WithMetadataProvider(cache)}
Expand Down
10 changes: 6 additions & 4 deletions pkg/rt/subscriber_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/peterbourgon/fastly-exporter/pkg/api"
"github.com/peterbourgon/fastly-exporter/pkg/filter"
"github.com/peterbourgon/fastly-exporter/pkg/prom"
"github.com/peterbourgon/fastly-exporter/pkg/rt"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -17,7 +18,8 @@ func TestSubscriberFixture(t *testing.T) {
namespace = "testspace"
subsystem = "testsystem"
registry = prometheus.NewRegistry()
metrics, _ = prom.NewMetrics(namespace, subsystem, registry)
nameFilter = filter.Filter{}
metrics, _ = prom.NewMetrics(namespace, subsystem, nameFilter, registry)
)

var (
Expand Down Expand Up @@ -54,7 +56,7 @@ func TestSubscriberNoData(t *testing.T) {
var (
client = newMockRealtimeClient(`{"Error": "No data available, please retry"}`, `{}`)
registry = prometheus.NewRegistry()
metrics, _ = prom.NewMetrics("ns", "ss", registry)
metrics, _ = prom.NewMetrics("ns", "ss", filter.Filter{}, registry)
processed = make(chan struct{}, 100)
postprocess = func() { processed <- struct{}{} }
options = []rt.SubscriberOption{rt.WithPostprocess(postprocess)}
Expand All @@ -79,7 +81,7 @@ func TestUserAgent(t *testing.T) {
var (
client = newMockRealtimeClient(`{}`)
userAgent = "Some user agent string"
metrics, _ = prom.NewMetrics("ns", "ss", prometheus.NewRegistry())
metrics, _ = prom.NewMetrics("ns", "ss", filter.Filter{}, prometheus.NewRegistry())
processed = make(chan struct{})
postprocess = func() { close(processed) }
options = []rt.SubscriberOption{rt.WithUserAgent(userAgent), rt.WithPostprocess(postprocess)}
Expand All @@ -97,7 +99,7 @@ func TestUserAgent(t *testing.T) {
func TestBadTokenNoSpam(t *testing.T) {
var (
client = &countingRealtimeClient{code: 403, response: `{"Error": "unauthorized"}`}
metrics, _ = prom.NewMetrics("namespace", "subsystem", prometheus.NewRegistry())
metrics, _ = prom.NewMetrics("namespace", "subsystem", filter.Filter{}, prometheus.NewRegistry())
subscriber = rt.NewSubscriber(client, "presumably bad token", "service ID", metrics)
)
go subscriber.Run(context.Background())
Expand Down

0 comments on commit 9951ab2

Please sign in to comment.