Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fixes #1002: Generate a test config with an available port.
Browse files Browse the repository at this point in the history
  • Loading branch information
geauxvirtual committed Jul 6, 2016
1 parent cc4eda7 commit c21517c
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions control/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"path"
"strings"
"testing"
Expand Down Expand Up @@ -108,10 +109,33 @@ func load(c *pluginControl, paths ...string) (core.CatalogedPlugin, serror.SnapE
return p, nil
}

func TestPluginControlGenerateArgs(t *testing.T) {
// Generates a config to use for testing by taking the a default config
// and setting the ListenPort to an available port on the system running
// the tests.
func getTestConfig() *Config {
config := GetDefaultConfig()
config.ListenPort = 0
c := New(config)
config.ListenPort = getPort()
return config
}

// Returns an available port on the test system. If no port is available
// after 1000 tries, then the test will panic.
func getPort() int {
count := 0
for count < 1000 {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err == nil {
p := ln.Addr().(*net.TCPAddr).Port
ln.Close()
return p
}
count++
}
panic("Could not find an available port")
}

func TestPluginControlGenerateArgs(t *testing.T) {
c := New(getTestConfig())
err := c.Start()
Convey("pluginControl starts successfully", t, func() {
So(c.Started, ShouldBeTrue)
Expand All @@ -127,7 +151,7 @@ func TestPluginControlGenerateArgs(t *testing.T) {

func TestSwapPlugin(t *testing.T) {
if fixtures.SnapPath != "" {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.Start()
time.Sleep(100 * time.Millisecond)
lpe := newListenToPluginEvent()
Expand Down Expand Up @@ -325,7 +349,7 @@ func TestLoad(t *testing.T) {
// It is the responsibility of the testing framework to
// build the plugins first into the build dir.
if fixtures.SnapPath != "" {
c := New(GetDefaultConfig())
c := New(getTestConfig())

// Testing trying to load before starting pluginControl
Convey("pluginControl before being started", t, func() {
Expand Down Expand Up @@ -379,7 +403,7 @@ func TestLoad(t *testing.T) {
func TestLoadWithSignedPlugins(t *testing.T) {
if fixtures.SnapPath != "" {
Convey("pluginControl.Load should successufully load a signed plugin with trust enabled", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.pluginTrust = PluginTrustEnabled
c.signingManager = &mocksigningManager{signed: true}
lpe := newListenToPluginEvent()
Expand All @@ -395,7 +419,7 @@ func TestLoadWithSignedPlugins(t *testing.T) {
time.Sleep(100 * time.Millisecond)
})
Convey("pluginControl.Load should successfully load unsigned plugin when trust level set to warning", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.pluginTrust = PluginTrustWarn
c.signingManager = &mocksigningManager{signed: false}
lpe := newListenToPluginEvent()
Expand All @@ -410,7 +434,7 @@ func TestLoadWithSignedPlugins(t *testing.T) {
time.Sleep(100 * time.Millisecond)
})
Convey("pluginControl.Load returns error with trust enabled and signing not validated", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.pluginTrust = PluginTrustEnabled
c.signingManager = &mocksigningManager{signed: false}
c.Start()
Expand All @@ -431,7 +455,7 @@ func TestUnload(t *testing.T) {
// It is the responsibility of the testing framework to
// build the plugins first into the build dir.
if fixtures.SnapPath != "" {
c := New(GetDefaultConfig())
c := New(getTestConfig())
lpe := newListenToPluginEvent()
c.eventManager.RegisterHandler("TestUnload", lpe)
c.Start()
Expand Down Expand Up @@ -494,7 +518,7 @@ func TestUnload(t *testing.T) {

func TestStop(t *testing.T) {
Convey("pluginControl.Stop", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
lps := newLoadedPlugins()
err := lps.add(&loadedPlugin{
Type: plugin.CollectorPluginType,
Expand All @@ -518,7 +542,7 @@ func TestStop(t *testing.T) {
func TestPluginCatalog(t *testing.T) {
ts := time.Now()

c := New(GetDefaultConfig())
c := New(getTestConfig())

// We need our own plugin manager to drop mock
// loaded plugins into. Arbitrarily adding
Expand Down Expand Up @@ -725,7 +749,7 @@ func (m *mockCDProc) HasRules() bool {

func TestExportedMetricCatalog(t *testing.T) {
Convey(".MetricCatalog()", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
lp := &loadedPlugin{}
mt := newMetricType(core.NewNamespace("foo", "bar"), time.Now(), lp)
c.metricCatalog.Add(mt)
Expand All @@ -746,15 +770,15 @@ func TestExportedMetricCatalog(t *testing.T) {

func TestMetricExists(t *testing.T) {
Convey("MetricExists()", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.metricCatalog = &mc{}
So(c.MetricExists(core.NewNamespace("hi"), -1), ShouldEqual, false)
})
}

func TestMetricConfig(t *testing.T) {
Convey("required config provided by task", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.Start()
lpe := newListenToPluginEvent()
c.eventManager.RegisterHandler("Control.PluginLoaded", lpe)
Expand Down Expand Up @@ -790,7 +814,7 @@ func TestMetricConfig(t *testing.T) {
})

Convey("config provided by defaults", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.Config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "pwd"})
c.Start()
lpe := newListenToPluginEvent()
Expand All @@ -816,7 +840,7 @@ func TestMetricConfig(t *testing.T) {
})

Convey("nil config provided by task", t, func() {
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
c := New(config)
c.Start()
Expand All @@ -839,7 +863,7 @@ func TestMetricConfig(t *testing.T) {
time.Sleep(100 * time.Millisecond)
})
Convey("required config provided by global plugin config", t, func() {
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
c := New(config)
c.Start()
Expand All @@ -864,7 +888,7 @@ func TestMetricConfig(t *testing.T) {

func TestRoutingCachingStrategy(t *testing.T) {
Convey("Given loaded plugins that use sticky routing", t, func() {
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
c := New(config)
c.Start()
Expand Down Expand Up @@ -923,7 +947,7 @@ func TestRoutingCachingStrategy(t *testing.T) {
})

Convey("Given loaded plugins that use least-recently-used routing", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.Start()
c.Config.Plugins.Collector.Plugins["mock"] = newPluginConfigItem(
optAddPluginConfigItem("test", ctypes.ConfigValueBool{Value: true}),
Expand Down Expand Up @@ -992,7 +1016,7 @@ func TestRoutingCachingStrategy(t *testing.T) {

func TestCollectDynamicMetrics(t *testing.T) {
Convey("given a plugin using the native client", t, func() {
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
config.CacheExpiration = jsonutil.Duration{time.Second * 1}
c := New(config)
Expand Down Expand Up @@ -1133,7 +1157,7 @@ func TestCollectDynamicMetrics(t *testing.T) {
func TestFailedPlugin(t *testing.T) {
Convey("given a loaded plugin", t, func() {
// Create controller
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.Start()
lpe := newListenToPluginEvent()
c.eventManager.RegisterHandler("TEST", lpe)
Expand Down Expand Up @@ -1206,7 +1230,7 @@ func TestCollectMetrics(t *testing.T) {
plugin.PingTimeoutDurationDefault = time.Second * 1

// Create controller
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
c := New(config)
c.pluginRunner.(*runner).monitor.duration = time.Millisecond * 100
Expand Down Expand Up @@ -1287,7 +1311,7 @@ func TestCollectMetrics(t *testing.T) {
plugin.PingTimeoutLimit = 1
plugin.PingTimeoutDurationDefault = time.Second * 1
// Create controller
c := New(GetDefaultConfig())
c := New(getTestConfig())
c.pluginRunner.(*runner).monitor.duration = time.Millisecond * 100
c.Start()
load(c, fixtures.PluginPath)
Expand All @@ -1305,7 +1329,7 @@ func TestExpandWildcards(t *testing.T) {
plugin.PingTimeoutDurationDefault = time.Second * 1

// Create controller
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
c := New(config)
c.pluginRunner.(*runner).monitor.duration = time.Millisecond * 100
Expand Down Expand Up @@ -1379,7 +1403,7 @@ func TestGatherCollectors(t *testing.T) {
plugin.PingTimeoutDurationDefault = time.Second * 1

// Create controller
config := GetDefaultConfig()
config := getTestConfig()
config.Plugins.All.AddItem("password", ctypes.ConfigValueStr{Value: "testval"})
c := New(config)
c.pluginRunner.(*runner).monitor.duration = time.Millisecond * 100
Expand Down Expand Up @@ -1447,7 +1471,7 @@ func TestPublishMetrics(t *testing.T) {
plugin.PingTimeoutDurationDefault = time.Second * 1

// Create controller
c := New(GetDefaultConfig())
c := New(getTestConfig())
lpe := newListenToPluginEvent()
c.eventManager.RegisterHandler("TestPublishMetrics", lpe)
c.pluginRunner.(*runner).monitor.duration = time.Millisecond * 100
Expand Down Expand Up @@ -1500,7 +1524,7 @@ func TestProcessMetrics(t *testing.T) {
plugin.PingTimeoutDurationDefault = time.Second * 1

// Create controller
c := New(GetDefaultConfig())
c := New(getTestConfig())
lpe := newListenToPluginEvent()
c.eventManager.RegisterHandler("TestProcessMetrics", lpe)
c.pluginRunner.(*runner).monitor.duration = time.Millisecond * 100
Expand Down Expand Up @@ -1584,7 +1608,7 @@ func (l *listenToPluginSubscriptionEvent) HandleGomitEvent(e gomit.Event) {
}
func TestMetricSubscriptionToNewVersion(t *testing.T) {
Convey("Given a metric that is being collected at v1", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
lpe := newListenToPluginSubscriptionEvent()
c.eventManager.RegisterHandler("TestMetricSubscriptionToNewVersion", lpe)
c.Start()
Expand Down Expand Up @@ -1647,7 +1671,7 @@ func TestMetricSubscriptionToNewVersion(t *testing.T) {

func TestMetricSubscriptionToOlderVersion(t *testing.T) {
Convey("Given a metric that is being collected at v2", t, func() {
c := New(GetDefaultConfig())
c := New(getTestConfig())
lpe := newListenToPluginSubscriptionEvent()
c.eventManager.RegisterHandler("TestMetricSubscriptionToOlderVersion", lpe)
c.Start()
Expand Down

0 comments on commit c21517c

Please sign in to comment.