From 74c87c68631573f90ab921b2d1fc3f0cb216f019 Mon Sep 17 00:00:00 2001 From: Philip Norman Date: Wed, 27 Sep 2017 10:34:32 -0700 Subject: [PATCH] HTTP fallback regression test (#117) * Example of mocking out the NodeInfoFunc * Merge mock into body of config_test * Fix import of errors package * Don't export NodeInfoFunc; fix error --- config.go | 25 +++++++++++++---- config_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index f618c052..2bb003b2 100644 --- a/config.go +++ b/config.go @@ -63,6 +63,9 @@ type Config struct { ConfigPath string LogLevel string VersionFlag bool + + // nodeInfoFunc fetches node info from a URL + nodeInfoFunc func(url.URL) (nodeutil.NodeInfo, error) } // CollectorConfig contains configuration options relevant to the "collector" @@ -108,13 +111,24 @@ func (c *Config) loadConfig() error { return nil } -func (c *Config) getNodeInfo(attemptSSL bool) error { - log.Debug("Getting node info") +func (c *Config) getNodeInfoFromURL(url url.URL) (nodeutil.NodeInfo, error) { + if c.nodeInfoFunc != nil { + return c.nodeInfoFunc(url) + } client, err := httpHelpers.NewMetricsClient(c.CACertificatePath, c.IAMConfigPath) if err != nil { - return err + return nil, err } + // Create a new DC/OS nodeutil instance + info, err := nodeutil.NewNodeInfo(client, c.DCOSRole, nodeutil.OptionMesosStateURL(url.String())) + if err != nil { + return nil, fmt.Errorf("error: could not get nodeInfo: %s", err) + } + return info, nil +} +func (c *Config) getNodeInfo(attemptSSL bool) error { + log.Debug("Getting node info") // If there is no available certificate, immediately drop back to HTTP useSSL := attemptSSL && len(c.IAMConfigPath) > 0 stateURL := url.URL{ @@ -126,10 +140,9 @@ func (c *Config) getNodeInfo(attemptSSL bool) error { stateURL.Scheme = "https" } - // Create a new DC/OS nodeutil instance - node, err := nodeutil.NewNodeInfo(client, c.DCOSRole, nodeutil.OptionMesosStateURL(stateURL.String())) + node, err := c.getNodeInfoFromURL(stateURL) if err != nil { - return fmt.Errorf("error: could not get nodeInfo: %s", err) + return err } // Get node IP address diff --git a/config_test.go b/config_test.go index 76ddf869..122baa21 100644 --- a/config_test.go +++ b/config_test.go @@ -17,16 +17,92 @@ package main import ( + "context" + "errors" "flag" "io/ioutil" + "net" + "net/url" "os" "testing" "time" + "strings" + "github.com/Sirupsen/logrus" + "github.com/dcos/dcos-go/dcos/nodeutil" . "github.com/smartystreets/goconvey/convey" ) +type fakeInfo struct { + ip net.IP + ipErr error + leader bool + leaderErr error + mesosID string + mesosIDErr error + clusterID string + clusterIDErr error +} + +var _ nodeutil.NodeInfo = &fakeInfo{} + +func (f *fakeInfo) DetectIP() (net.IP, error) { + return f.ip, f.ipErr +} + +func (f *fakeInfo) IsLeader() (bool, error) { + return f.leader, f.leaderErr +} + +func (f *fakeInfo) MesosID(ctx context.Context) (string, error) { + return f.mesosID, f.mesosIDErr +} + +func (f *fakeInfo) ClusterID() (string, error) { + return f.clusterID, f.clusterIDErr +} + +func TestGetNodeInfo(t *testing.T) { + Convey("When getting node info", t, func() { + + var fetchedURLs []string + testConfig, _ := getNewConfig([]string{"-role", "agent"}) + + testConfig.nodeInfoFunc = func(url url.URL) (nodeutil.NodeInfo, error) { + fetchedURLs = append(fetchedURLs, url.String()) + info := &fakeInfo{ + ip: net.ParseIP("127.0.0.1"), + clusterID: "my-cluster-ID", + } + if strings.HasPrefix(url.String(), "https://") { + info.mesosIDErr = errors.New("Can't get mesos ID over https") + } else { + info.mesosID = "my mesos id" + } + return info, nil + } + + Convey("When an IAM Config is set", func() { + testConfig.IAMConfigPath = "some config path" + testConfig.getNodeInfo(true) + + So(len(fetchedURLs), ShouldEqual, 2) + So(fetchedURLs[0], ShouldStartWith, "https://") + So(fetchedURLs[1], ShouldStartWith, "http://") + }) + + Convey("When an IAM Config is not set", func() { + // don't set an IAM config path here + testConfig.getNodeInfo(true) + + So(len(fetchedURLs), ShouldEqual, 1) + So(fetchedURLs[0], ShouldStartWith, "http") + }) + + }) +} + func TestNewConfig(t *testing.T) { Convey("Ensure default configuration is set properly", t, func() { testConfig := newConfig()