test: Fix tests by hooking up go test properly

The internal golang tests broke when we turned everything into packages.
This resurrects them with the hopes that we'll add more!
This commit is contained in:
James Shubin
2017-02-20 15:51:39 -05:00
parent 421bacd7dc
commit 69b0913315
4 changed files with 96 additions and 58 deletions

View File

@@ -24,13 +24,18 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/purpleidea/mgmt/resources"
"github.com/purpleidea/mgmt/util"
) )
// NV is a helper function to make testing easier. It creates a new noop vertex. // NV is a helper function to make testing easier. It creates a new noop vertex.
func NV(s string) *Vertex { func NV(s string) *Vertex {
obj, err := NewNoopRes(s) obj := &resources.NoopRes{
if err != nil { BaseRes: resources.BaseRes{
panic(err) // unlikely test failure! Name: s,
},
Comment: "Testing!",
} }
return NewVertex(obj) return NewVertex(obj)
} }
@@ -623,10 +628,10 @@ func TestPgraphT11(t *testing.T) {
} }
type NoopResTest struct { type NoopResTest struct {
NoopRes resources.NoopRes
} }
func (obj *NoopResTest) GroupCmp(r Res) bool { func (obj *NoopResTest) GroupCmp(r resources.Res) bool {
res, ok := r.(*NoopResTest) res, ok := r.(*NoopResTest)
if !ok { if !ok {
return false return false
@@ -643,10 +648,10 @@ func (obj *NoopResTest) GroupCmp(r Res) bool {
func NewNoopResTest(name string) *NoopResTest { func NewNoopResTest(name string) *NoopResTest {
obj := &NoopResTest{ obj := &NoopResTest{
NoopRes: NoopRes{ NoopRes: resources.NoopRes{
BaseRes: BaseRes{ BaseRes: resources.BaseRes{
Name: name, Name: name,
MetaParams: MetaParams{ MetaParams: resources.MetaParams{
AutoGroup: true, // always autogroup AutoGroup: true, // always autogroup
}, },
}, },
@@ -695,7 +700,7 @@ Loop:
for _, x1 := range v1.GetGroup() { for _, x1 := range v1.GetGroup() {
l1 = append(l1, x1.GetName()) // add my contents l1 = append(l1, x1.GetName()) // add my contents
} }
l1 = StrRemoveDuplicatesInList(l1) // remove duplicates l1 = util.StrRemoveDuplicatesInList(l1) // remove duplicates
sort.Strings(l1) sort.Strings(l1)
// inner loop // inner loop
@@ -705,7 +710,7 @@ Loop:
for _, x2 := range v2.GetGroup() { for _, x2 := range v2.GetGroup() {
l2 = append(l2, x2.GetName()) l2 = append(l2, x2.GetName())
} }
l2 = StrRemoveDuplicatesInList(l2) // remove duplicates l2 = util.StrRemoveDuplicatesInList(l2) // remove duplicates
sort.Strings(l2) sort.Strings(l2)
// does l1 match l2 ? // does l1 match l2 ?
@@ -738,14 +743,14 @@ Loop:
for _, x1 := range vv1.GetGroup() { for _, x1 := range vv1.GetGroup() {
l1 = append(l1, x1.GetName()) // add my contents l1 = append(l1, x1.GetName()) // add my contents
} }
l1 = StrRemoveDuplicatesInList(l1) // remove duplicates l1 = util.StrRemoveDuplicatesInList(l1) // remove duplicates
sort.Strings(l1) sort.Strings(l1)
l2 := strings.Split(vv2.GetName(), ",") l2 := strings.Split(vv2.GetName(), ",")
for _, x2 := range vv2.GetGroup() { for _, x2 := range vv2.GetGroup() {
l2 = append(l2, x2.GetName()) l2 = append(l2, x2.GetName())
} }
l2 = StrRemoveDuplicatesInList(l2) // remove duplicates l2 = util.StrRemoveDuplicatesInList(l2) // remove duplicates
sort.Strings(l2) sort.Strings(l2)
// does l1 match l2 ? // does l1 match l2 ?
@@ -782,7 +787,7 @@ func (ag *testGrouper) vertexMerge(v1, v2 *Vertex) (v *Vertex, err error) {
for _, n := range obj.GetGroup() { for _, n := range obj.GetGroup() {
names = append(names, n.GetName()) // add my contents names = append(names, n.GetName()) // add my contents
} }
names = StrRemoveDuplicatesInList(names) // remove duplicates names = util.StrRemoveDuplicatesInList(names) // remove duplicates
sort.Strings(names) sort.Strings(names)
obj.SetName(strings.Join(names, ",")) obj.SetName(strings.Join(names, ","))
return // success or fail, and no need to merge the actual vertices! return // success or fail, and no need to merge the actual vertices!
@@ -793,7 +798,7 @@ func (ag *testGrouper) edgeMerge(e1, e2 *Edge) *Edge {
n1 := strings.Split(e1.Name, ",") // load n1 := strings.Split(e1.Name, ",") // load
n2 := strings.Split(e2.Name, ",") // load n2 := strings.Split(e2.Name, ",") // load
names := append(n1, n2...) names := append(n1, n2...)
names = StrRemoveDuplicatesInList(names) // remove duplicates names = util.StrRemoveDuplicatesInList(names) // remove duplicates
sort.Strings(names) sort.Strings(names)
return NewEdge(strings.Join(names, ",")) return NewEdge(strings.Join(names, ","))
} }

View File

@@ -22,6 +22,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/gob" "encoding/gob"
"testing" "testing"
//"github.com/purpleidea/mgmt/event"
) )
func TestMiscEncodeDecode1(t *testing.T) { func TestMiscEncodeDecode1(t *testing.T) {
@@ -119,55 +120,55 @@ func TestIFF(t *testing.T) {
} }
func TestReadEvent(t *testing.T) { func TestReadEvent(t *testing.T) {
res := FileRes{} //res := FileRes{}
shouldExit := map[eventName]bool{ //shouldExit := map[event.EventName]bool{
eventStart: false, // event.EventStart: false,
eventPoke: false, // event.EventPoke: false,
eventBackPoke: false, // event.EventBackPoke: false,
eventExit: true, // event.EventExit: true,
} //}
shouldPoke := map[eventName]bool{ //shouldPoke := map[event.EventName]bool{
eventStart: true, // event.EventStart: true,
eventPoke: true, // event.EventPoke: true,
eventBackPoke: true, // event.EventBackPoke: true,
eventExit: false, // event.EventExit: false,
} //}
for event := range shouldExit { //for ev := range shouldExit {
exit, poke := res.ReadEvent(&Event{Name: event}) // exit, poke := res.ReadEvent(&event.Event{Name: ev})
if exit != shouldExit[event] { // if exit != shouldExit[ev] {
t.Errorf("resource.ReadEvent returned wrong exit flag for a %v event (%v, should be %v)", // t.Errorf("resource.ReadEvent returned wrong exit flag for a %v event (%v, should be %v)",
event, exit, shouldExit[event]) // ev, exit, shouldExit[ev])
} // }
if poke != shouldPoke[event] { // if poke != shouldPoke[ev] {
t.Errorf("resource.ReadEvent returned wrong poke flag for a %v event (%v, should be %v)", // t.Errorf("resource.ReadEvent returned wrong poke flag for a %v event (%v, should be %v)",
event, poke, shouldPoke[event]) // ev, poke, shouldPoke[ev])
} // }
} //}
res.Init() //res.Init()
res.SetWatching(true) //res.SetWatching(true)
// test result when a pause event is followed by start // test result when a pause event is followed by start
go res.SendEvent(eventStart, false, false) //go res.SendEvent(event.EventStart, nil)
exit, poke := res.ReadEvent(&Event{Name: eventPause}) //exit, poke := res.ReadEvent(&event.Event{Name: event.EventPause})
if exit { //if exit {
t.Error("resource.ReadEvent returned wrong exit flag for a pause+start event (true, should be false)") // t.Error("resource.ReadEvent returned wrong exit flag for a pause+start event (true, should be false)")
} //}
if poke { //if poke {
t.Error("resource.ReadEvent returned wrong poke flag for a pause+start event (true, should be false)") // t.Error("resource.ReadEvent returned wrong poke flag for a pause+start event (true, should be false)")
} //}
// test result when a pause event is followed by exit // test result when a pause event is followed by exit
go res.SendEvent(eventExit, false, false) //go res.SendEvent(event.EventExit, nil)
exit, poke = res.ReadEvent(&Event{Name: eventPause}) //exit, poke = res.ReadEvent(&event.Event{Name: event.EventPause})
if !exit { //if !exit {
t.Error("resource.ReadEvent returned wrong exit flag for a pause+start event (false, should be true)") // t.Error("resource.ReadEvent returned wrong exit flag for a pause+start event (false, should be true)")
} //}
if poke { //if poke {
t.Error("resource.ReadEvent returned wrong poke flag for a pause+start event (true, should be false)") // t.Error("resource.ReadEvent returned wrong poke flag for a pause+start event (true, should be false)")
} //}
// TODO: create a wrapper API around log, so that Fatals can be mocked and tested // TODO: create a wrapper API around log, so that Fatals can be mocked and tested
} }

View File

@@ -22,12 +22,12 @@ run-test ./test/test-yamlfmt.sh
run-test ./test/test-bashfmt.sh run-test ./test/test-bashfmt.sh
run-test ./test/test-headerfmt.sh run-test ./test/test-headerfmt.sh
run-test ./test/test-commit-message.sh run-test ./test/test-commit-message.sh
run-test go test
run-test ./test/test-govet.sh run-test ./test/test-govet.sh
run-test ./test/test-gotest.sh
# do these longer tests only when running on ci # do these longer tests only when running on ci
if env | grep -q -e '^TRAVIS=true$' -e '^JENKINS_URL=' -e '^BUILD_TAG=jenkins'; then if env | grep -q -e '^TRAVIS=true$' -e '^JENKINS_URL=' -e '^BUILD_TAG=jenkins'; then
run-test go test -race run-test ./test/test-gotest.sh --race
run-test ./test/test-shell.sh run-test ./test/test-shell.sh
fi fi

32
test/test-gotest.sh Executable file
View File

@@ -0,0 +1,32 @@
#!/bin/bash
. test/util.sh
echo "running test-gotest.sh $1"
failures=''
function run-test()
{
$@ || failures=$( [ -n "$failures" ] && echo "$failures\\n$@" || echo "$@" )
}
ROOT=$(dirname "${BASH_SOURCE}")/..
cd "${ROOT}"
for pkg in `go list ./... | grep -v 'vendor/' | grep -v 'examples/' | grep -v 'old/' | grep -v 'tmp/'`; do
echo "Testing: $pkg"
# FIXME: can we capture and output the stderr from these tests too?
run-test go test "$pkg"
if [ "$1" = "--race" ]; then
run-test go test -race "$pkg"
fi
done
if [[ -n "$failures" ]]; then
echo 'FAIL'
echo 'The following `go test` runs have failed:'
echo -e "$failures"
exit 1
fi
echo 'PASS'