Skip to content

Commit ff90b5d

Browse files
committed
Add a parse config parser
This makes setup more testable.
1 parent a5655e1 commit ff90b5d

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require (
77
github.com/coredns/coredns v1.7.0
88
github.com/miekg/dns v1.1.29
99
github.com/prometheus/client_golang v1.7.1
10+
github.com/stretchr/testify v1.5.1
1011
gopkg.in/h2non/gock.v1 v1.0.15
1112
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ github.com/cpu/goacmedns v0.0.1/go.mod h1:sesf/pNnCYwUevQEQfEwY0Y3DydlQWSGZbaMEl
103103
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
104104
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
105105
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
106+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
106107
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
107108
github.com/decker502/dnspod-go v0.2.0/go.mod h1:qsurYu1FgxcDwfSwXJdLt4kRsBLZeosEb9uq4Sy+08g=
108109
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
@@ -323,6 +324,7 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
323324
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
324325
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
325326
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
327+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
326328
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
327329
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
328330
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
@@ -376,6 +378,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
376378
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
377379
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
378380
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
381+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
379382
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
380383
github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY=
381384
github.com/tinylib/msgp v1.1.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
@@ -672,6 +675,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
672675
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
673676
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
674677
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
678+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
675679
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
676680
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
677681
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

netbox.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net"
2121
"os"
2222
"strings"
23+
"time"
2324

2425
"github.com/coredns/coredns/plugin"
2526
"github.com/coredns/coredns/plugin/metrics"
@@ -33,9 +34,10 @@ import (
3334
var log = clog.NewWithPlugin("netbox")
3435

3536
type Netbox struct {
36-
Url string
37-
Token string
38-
Next plugin.Handler
37+
Url string
38+
Token string
39+
CacheDuration time.Duration
40+
Next plugin.Handler
3941
}
4042

4143
func (n Netbox) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {

netbox_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/miekg/dns"
2222
"gopkg.in/h2non/gock.v1"
2323
"testing"
24+
"time"
2425
)
2526

2627
func TestNetbox(t *testing.T) {
@@ -29,7 +30,7 @@ func TestNetbox(t *testing.T) {
2930
map[string]string{"dns_name": "my_host"}).Reply(
3031
200).BodyString(
3132
`{"count":1, "results":[{"address": "10.0.0.2/25", "dns_name": "my_host"}]}`)
32-
nb := Netbox{Url: "https://example.org/api/ipam/ip-addresses", Token: "s3kr3tt0ken"}
33+
nb := Netbox{Url: "https://example.org/api/ipam/ip-addresses", Token: "s3kr3tt0ken", CacheDuration: time.Second * 10}
3334

3435
if nb.Name() != "netbox" {
3536
t.Errorf("expected plugin name: %s, got %s", "netbox", nb.Name())

setup.go

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/coredns/coredns/core/dnsserver"
2020
"github.com/coredns/coredns/plugin"
2121
"github.com/coredns/coredns/plugin/metrics"
22+
"time"
2223

2324
"github.com/caddyserver/caddy"
2425
)
@@ -30,8 +31,36 @@ func init() { plugin.Register("netbox", setup) }
3031
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
3132
func setup(c *caddy.Controller) error {
3233

34+
netboxPlugin, err := newNetBox(c)
35+
if err != nil {
36+
return plugin.Error("netbox", err)
37+
}
38+
39+
// Add a startup function that will -- after all plugins have been loaded -- check if the
40+
// prometheus plugin has been used - if so we will export metrics. We can only register
41+
// this metric once, hence the "once.Do".
42+
c.OnStartup(func() error {
43+
once.Do(func() { metrics.MustRegister(c, requestCount) })
44+
return nil
45+
})
46+
47+
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
48+
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
49+
return netboxPlugin
50+
})
51+
52+
// All OK, return a nil error.
53+
return nil
54+
}
55+
56+
func newNetBox(c *caddy.Controller) (Netbox, error) {
57+
3358
url := ""
3459
token := ""
60+
localCacheDuration := ""
61+
duration := time.Second
62+
var err error
63+
3564
for c.Next() {
3665
if c.NextBlock() {
3766
for {
@@ -47,6 +76,16 @@ func setup(c *caddy.Controller) error {
4776
c.ArgErr()
4877
}
4978
token = c.Val()
79+
80+
case "localCacheDuration":
81+
if !c.NextArg() {
82+
c.ArgErr()
83+
}
84+
localCacheDuration = c.Val()
85+
duration, err = time.ParseDuration(localCacheDuration)
86+
if err != nil {
87+
localCacheDuration = ""
88+
}
5089
}
5190

5291
if !c.Next() {
@@ -56,22 +95,11 @@ func setup(c *caddy.Controller) error {
5695
}
5796

5897
}
59-
// Add a startup function that will -- after all plugins have been loaded -- check if the
60-
// prometheus plugin has been used - if so we will export metrics. We can only register
61-
// this metric once, hence the "once.Do".
62-
c.OnStartup(func() error {
63-
once.Do(func() { metrics.MustRegister(c, requestCount) })
64-
return nil
65-
})
66-
67-
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
68-
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
69-
return Netbox{Url: url, Token: token, Next: next}
70-
})
7198

72-
if url == "" || token == "" {
73-
return errors.New("Could not find url or token in netbox config")
99+
if url == "" || token == "" || localCacheDuration == "" {
100+
return Netbox{}, errors.New("Could not parse netbox config")
74101
}
75-
// All OK, return a nil error.
76-
return nil
102+
103+
return Netbox{Url: url, Token: token, CacheDuration: duration}, nil
104+
77105
}

setup_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
package netbox
1616

1717
import (
18+
//"fmt"
1819
"testing"
20+
"time"
1921

2022
"github.com/caddyserver/caddy"
23+
"github.com/stretchr/testify/assert"
2124
)
2225

2326
// TestSetup tests the various things that should be parsed by setup.
2427
// Make sure you also test for parse errors.
2528
func TestSetup(t *testing.T) {
26-
c := caddy.NewTestController("dns", `netbox { url example.org\n token foobar}`)
29+
c := caddy.NewTestController("dns", `netbox { url example.org\n token foobar\n localCacheDuration 10s }`)
2730
if err := setup(c); err != nil {
2831
t.Fatalf("Expected no errors, but got: %v", err)
2932
}
@@ -33,3 +36,15 @@ func TestSetup(t *testing.T) {
3336
t.Fatalf("Expected errors, but got: %v", err)
3437
}
3538
}
39+
40+
func TestSetupWithWrongDuration(t *testing.T) {
41+
c := caddy.NewTestController("dns", `netbox { url example.org\n token foobar\n localCacheDuration Wrong }`)
42+
_, err := newNetBox(c)
43+
assert.Error(t, err, "Expected error")
44+
}
45+
46+
func TestSetupWithDuration(t *testing.T) {
47+
c := caddy.NewTestController("dns", `netbox { url example.org\n token foobar\n localCacheDuration 10s }`)
48+
nb, _ := newNetBox(c)
49+
assert.Equal(t, nb.CacheDuration, time.Second*10, "Duration not set properly")
50+
}

0 commit comments

Comments
 (0)