Ensure that we can pass in a hostname

This commit is contained in:
Tim Raymond 2016-02-10 15:42:07 -05:00
parent 2f85731a22
commit 1fa3a447d4
2 changed files with 34 additions and 12 deletions

View File

@ -14,11 +14,13 @@ type Config struct {
type Service struct { type Service struct {
hosts []*client.Host hosts []*client.Host
logger *log.Logger logger *log.Logger
hostname string
} }
func NewEnterprise(c Config) *Service { func NewEnterprise(c Config, hostname string) *Service {
return &Service{ return &Service{
hosts: c.Hosts, hosts: c.Hosts,
hostname: hostname,
logger: log.New(os.Stdout, "[enterprise]", log.Ldate|log.Ltime), logger: log.New(os.Stdout, "[enterprise]", log.Ldate|log.Ltime),
} }
} }
@ -35,7 +37,7 @@ func (s *Service) Open() {
func (s *Service) registerProduct(cl *client.Client) { func (s *Service) registerProduct(cl *client.Client) {
p := client.Product{ p := client.Product{
ProductID: "telegraf", ProductID: "telegraf",
Host: "localhost", Host: s.hostname,
} }
_, err := cl.Register(&p) _, err := cl.Register(&p)

View File

@ -1,6 +1,7 @@
package enterprise_test package enterprise_test
import ( import (
"encoding/json"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -10,15 +11,31 @@ import (
"github.com/influxdata/telegraf/services/enterprise" "github.com/influxdata/telegraf/services/enterprise"
) )
func Test_RegistersWithEnterprise(t *testing.T) { func mockEnterprise(srvFunc func(*client.Product, error)) (chan struct{}, *httptest.Server) {
success := make(chan struct{}) success := make(chan struct{})
mockEnterprise := http.NewServeMux() me := http.NewServeMux()
mockEnterprise.HandleFunc("/api/v2/products", func(rw http.ResponseWriter, r *http.Request) { me.HandleFunc("/api/v2/products", func(rw http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
c := &client.Product{}
d := json.NewDecoder(r.Body)
err := d.Decode(c)
srvFunc(c, err)
close(success) close(success)
} }
}) })
srv := httptest.NewServer(mockEnterprise) srv := httptest.NewServer(me)
return success, srv
}
func Test_RegistersWithEnterprise(t *testing.T) {
expected := "www.example.com"
var actualHostname string
success, srv := mockEnterprise(func(c *client.Product, err error) {
if err != nil {
t.Error(err.Error())
}
actualHostname = c.Host
})
defer srv.Close() defer srv.Close()
c := enterprise.Config{ c := enterprise.Config{
@ -26,13 +43,16 @@ func Test_RegistersWithEnterprise(t *testing.T) {
&client.Host{URL: srv.URL}, &client.Host{URL: srv.URL},
}, },
} }
e := enterprise.NewEnterprise(c) e := enterprise.NewEnterprise(c, expected)
e.Open() e.Open()
timeout := time.After(50 * time.Millisecond) timeout := time.After(1 * time.Millisecond)
for { for {
select { select {
case <-success: case <-success:
if actualHostname != expected {
t.Errorf("Expected hostname to be %s but was %s", expected, actualHostname)
}
return return
case <-timeout: case <-timeout:
t.Fatal("Expected to receive call to Enterprise API, but received none") t.Fatal("Expected to receive call to Enterprise API, but received none")