Skip to content

Commit

Permalink
HTTP fallback regression test (#117)
Browse files Browse the repository at this point in the history
* Example of mocking out the NodeInfoFunc

* Merge mock into body of config_test

* Fix import of errors package

* Don't export NodeInfoFunc; fix error
  • Loading branch information
philipnrmn committed Sep 27, 2017
1 parent a9fa069 commit 74c87c6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
25 changes: 19 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand Down
76 changes: 76 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 74c87c6

Please sign in to comment.