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

Commit

Permalink
-- Added testing for standalone plugin
Browse files Browse the repository at this point in the history
-- Added PluginUri* to fixtures, updated dependencies
-- Added cmd ops to start/kill standalone plugin during tests
-- Updated returnPluginDetails for standalone plugin to capture additional details
-- Fixed import listing order
-- Updated plugin-lib-go to latest commit version (to include --stand-alone flags)
-- Updated fixtures/tests to use mock-grpc plugin for stand-alone testing
-- Fixed golang formatting
  • Loading branch information
aarontay committed May 31, 2017
1 parent 2e5d9c8 commit cce62b7
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
10 changes: 4 additions & 6 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,6 @@ func (p *pluginControl) verifySignature(rp *core.RequestedPlugin) (bool, serror.
}

func (p *pluginControl) returnPluginDetails(rp *core.RequestedPlugin) (*pluginDetails, serror.SnapError) {
if rp.Uri() != nil {
return &pluginDetails{
Uri: rp.Uri(),
}, nil
}
details := &pluginDetails{}
var serr serror.SnapError
//Check plugin signing
Expand All @@ -609,8 +604,11 @@ func (p *pluginControl) returnPluginDetails(rp *core.RequestedPlugin) (*pluginDe
details.KeyPath = rp.KeyPath()
details.CACertPaths = rp.CACertPaths()
details.TLSEnabled = rp.TLSEnabled()
details.Uri = rp.Uri()

if filepath.Ext(rp.Path()) == ".aci" {
if rp.Uri() != nil {
// Is a standalone plugin
} else if filepath.Ext(rp.Path()) == ".aci" {
f, err := os.Open(rp.Path())
if err != nil {
return nil, serror.New(err)
Expand Down
73 changes: 73 additions & 0 deletions control/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"math/rand"
"net"
"os/exec"
"path"
"strings"
"testing"
Expand Down Expand Up @@ -438,6 +439,78 @@ func TestLoad(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}

// Uses a Uri to simulate loading a standalone plugin
func TestLoadWithStandalone(t *testing.T) {
// These tests only work if SNAP_PATH is known.
// It is the responsibility of the testing framework to
// build the plugins first into the build dir.
if fixtures.SnapPath == "" {
t.Fatal("SNAP_PATH not set. Cannot test loading plugins.")
}
c := New(getTestConfig())
// Testing trying to load before starting pluginControl
Convey("pluginControl before being started", t, func() {
_, err := load(c, fixtures.PluginUriMock2Grpc)
Convey("should return an error when loading a plugin", func() {
So(err, ShouldNotBeNil)
})
Convey("and there should be no plugin loaded", func() {
So(len(c.pluginManager.all()), ShouldEqual, 0)
})
})
// Start pluginControl and load our standalone plugin
c.Start()
lpe := newListenToPluginEvent()
c.eventManager.RegisterHandler("Control.PluginLoaded", lpe)

_, err := load(c, fixtures.PluginUriMock2Grpc)
Convey("Loading uri without starting standalone plugin", t, func() {
Convey("Should return an error", func() {
So(err, ShouldNotBeNil)
})
})

cmd := exec.Command(fixtures.PluginPathMock2Grpc, "--stand-alone", "--stand-alone-port", "8183")
errcmd := cmd.Start()
if errcmd != nil {
t.Fatal(serror.New(errcmd))
}
time.Sleep(100 * time.Millisecond)

_, err = load(c, fixtures.PluginUriMock2Grpc)
Convey("Loading uri with starting standalone plugin", t, func() {
Convey("Should not return an error", func() {
So(err, ShouldBeNil)
})
})

if err != nil {
t.Fatal(err)
}
<-lpe.done

Convey("pluginControl.Load on successful load plugin standalone mock-grpc", t, func() {
Convey("should emit a plugin event message", func() {
Convey("with loaded plugin name is mock-grpc", func() {
So(lpe.plugin.LoadedPluginName, ShouldEqual, "mock-grpc")
})
Convey("with loaded plugin version as 1", func() {
So(lpe.plugin.LoadedPluginVersion, ShouldEqual, 1)
})
Convey("with loaded plugin type as collector", func() {
So(lpe.plugin.PluginType, ShouldEqual, int(plugin.CollectorPluginType))
})
})
})

// Stop our controller so the plugins are unloaded and cleaned up from the system
c.Stop()
time.Sleep(100 * time.Millisecond)

// Kill the standalone plugin
cmd.Process.Kill()
}

func TestLoadWithSetPluginTrustLevel(t *testing.T) {
if fixtures.SnapPath == "" {
t.Fatal("SNAP_PATH not set. Cannot test loading plugins with set trust level.")
Expand Down
4 changes: 4 additions & 0 deletions control/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var (

PluginNameStreamRand1 = "snap-plugin-stream-collector-rand1"
PluginPathStreamRand1 = helper.PluginFilePath(PluginNameStreamRand1)

PluginNameMock2Grpc = "snap-plugin-collector-mock2-grpc"
PluginPathMock2Grpc = helper.PluginFilePath(PluginNameMock2Grpc)
PluginUriMock2Grpc = "http://127.0.0.1:8183"
)

// mocks a metric type
Expand Down
42 changes: 42 additions & 0 deletions control/plugin_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ package control
import (
"bufio"
"errors"
"net/url"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -117,6 +119,28 @@ func loadPlugin(p *pluginManager, fileName string, retries ...int) (*loadedPlugi
return lp, e
}

func loadStandalonePlugin(p *pluginManager, uriName string) (*loadedPlugin, serror.SnapError) {
// This is a Travis optimized loading of plugins. From time to time, tests will error in Travis
// due to a timeout when waiting for a response from a plugin. We are going to attempt loading a plugin
// 3 times before letting the error through. Hopefully this cuts down on the number of Travis failures
var e serror.SnapError
var lp *loadedPlugin

uri, err := url.ParseRequestURI(uriName)
if err != nil {
return nil, serror.New(err)
}
details := &pluginDetails{
Uri: uri,
}

lp, e = p.LoadPlugin(details, nil)
if e != nil {
return nil, e
}
return lp, nil
}

// Uses the mock collector plugin to simulate loading
func TestLoadPlugin(t *testing.T) {
// These tests only work if SNAP_PATH is known
Expand Down Expand Up @@ -205,7 +229,25 @@ func TestLoadPlugin(t *testing.T) {
So(lp.Meta.CacheTTL, ShouldNotBeNil)
So(lp.Meta.CacheTTL, ShouldResemble, time.Duration(time.Millisecond*1100))
})
Convey("loads standalone plugin successfully", func() {
cmd := exec.Command(fixtures.PluginPathMock2Grpc, "--stand-alone", "--stand-alone-port", "8183")
err := cmd.Start()
So(err, ShouldBeNil)
time.Sleep(100 * time.Millisecond)

p := newPluginManager()
p.SetMetricCatalog(newMetricCatalog())
lp, err := loadStandalonePlugin(p, fixtures.PluginUriMock2Grpc)

So(lp, ShouldHaveSameTypeAs, new(loadedPlugin))
So(p.all(), ShouldNotBeEmpty)
So(err, ShouldBeNil)
So(len(p.all()), ShouldBeGreaterThan, 0)
So(lp.Details.Uri.String(), ShouldContainSubstring, fixtures.PluginUriMock2Grpc)
So(len(lp.Details.Uri.String()), ShouldEqual, len(fixtures.PluginUriMock2Grpc))

cmd.Process.Kill()
})
})

}
Expand Down
2 changes: 1 addition & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion mgmt/rest/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (s *apiV1) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
var certPath string
var keyPath string
var caCertPaths string
os.Stdout.WriteString("TEST 2\n")

var signature []byte
var checkSum [sha256.Size]byte
Expand Down

0 comments on commit cce62b7

Please sign in to comment.