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

Commit

Permalink
Altered plugin test files and helper.go
Browse files Browse the repository at this point in the history
    - main_test.go in plugin dirs no longer needlessly rebuilds plugins.
    - Rewrote helper.go and added CheckPluginBuilt func to make sure the binaries are there
  • Loading branch information
kjang96 committed Jul 7, 2016
1 parent cc4eda7 commit c51379f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 50 deletions.
17 changes: 8 additions & 9 deletions plugin/collector/snap-collector-mock1/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ limitations under the License.
package main

import (
"fmt"
"os"
"path"
"testing"
Expand All @@ -44,19 +43,19 @@ func TestMockPluginLoad(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 SnapPath != "" {
// Helper plugin trigger build if possible for this plugin
helper.BuildPlugin(PluginType, PluginName)
//
Convey("ensure plugin loads and responds", t, func() {

Convey("make sure plugin has been built", t, func() {
err := helper.CheckPluginBuilt(SnapPath, PluginName)
So(err, ShouldBeNil)

Convey("ensure plugin loads and responds", func() {
c := control.New(control.GetDefaultConfig())
c.Start()
rp, _ := core.NewRequestedPlugin(PluginPath)
_, err := c.Load(rp)

So(err, ShouldBeNil)
})
} else {
fmt.Printf("SNAP_PATH not set. Cannot test %s plugin.\n", PluginName)
}

})
}
16 changes: 7 additions & 9 deletions plugin/collector/snap-collector-mock2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ limitations under the License.
package main

import (
"fmt"
"os"
"path"
"testing"
Expand All @@ -44,19 +43,18 @@ func TestMockPluginLoad(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 SnapPath != "" {
// Helper plugin trigger build if possible for this plugin
helper.BuildPlugin(PluginType, PluginName)
//
Convey("ensure plugin loads and responds", t, func() {
Convey("make sure plugin has been built", t, func() {
err := helper.CheckPluginBuilt(SnapPath, PluginName)
So(err, ShouldBeNil)

Convey("ensure plugin loads and responds", func() {
c := control.New(control.GetDefaultConfig())
c.Start()
rp, _ := core.NewRequestedPlugin(PluginPath)
_, err := c.Load(rp)

So(err, ShouldBeNil)
})
} else {
fmt.Printf("SNAP_PATH not set. Cannot test %s plugin.\n", PluginName)
}

})
}
34 changes: 12 additions & 22 deletions plugin/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,21 @@ limitations under the License.
package helper

import (
// "fmt"
"fmt"
"os"
"os/exec"
"path"
"strings"
)

var (
buildScript = "/scripts/build.sh"
)
//SNAP_PATH should be set to Snap's build directory

// BuildPlugin attempts to make the plugins before each test.
func BuildPlugin(pluginType, pluginName string) error {
wd, err := os.Getwd()
if err != nil {
return err
//CheckPluginBuilt checks if PluginName has been built.
func CheckPluginBuilt(SnapPath string, PluginName string) error {
if SnapPath == "" {
return fmt.Errorf("SNAP_PATH not set. Cannot test %s plugin.\n", PluginName)
}

bPath := strings.Replace(wd, path.Join("/", "plugin", pluginType, pluginName), buildScript, 1)
sPath := strings.Replace(wd, path.Join("/", "plugin", pluginType, pluginName), "", 1)

// fmt.Println(bPath, sPath, pluginType, pluginName)
c := exec.Command(bPath, sPath, pluginType, pluginName)

_, e := c.Output()
// fmt.Println(string(o))
return e
bPath := fmt.Sprintf("%s/plugin/%s", SnapPath, PluginName)
if _, err := os.Stat(bPath); os.IsNotExist(err) {
//the binary has not been built yet
return fmt.Errorf("Error: $SNAP_PATH/plugin/%s does not exist. Run make to build it.", PluginName)
}
return nil
}
17 changes: 7 additions & 10 deletions plugin/publisher/snap-publisher-file/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ limitations under the License.
package main

import (
"fmt"
"os"
"path"
"testing"
Expand All @@ -44,20 +43,18 @@ func TestFilePublisherLoad(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 SnapPath != "" {
// Helper plugin trigger build if possible for this plugin
helper.BuildPlugin(PluginType, PluginName)
//
//TODO cannot test this locally. We need AMQP and integration tests.
SkipConvey("ensure plugin loads and responds", t, func() {

Convey("make sure plugin has been built", t, func() {
err := helper.CheckPluginBuilt(SnapPath, PluginName)
So(err, ShouldBeNil)

Convey("ensure plugin loads and responds", func() {
c := control.New(control.GetDefaultConfig())
c.Start()
rp, _ := core.NewRequestedPlugin(PluginPath)
_, err := c.Load(rp)

So(err, ShouldBeNil)
})
} else {
fmt.Printf("SNAP_PATH not set. Cannot test %s plugin.\n", PluginName)
}
})
}

0 comments on commit c51379f

Please sign in to comment.