Remove extra test flags from all commands

Currently all commands are being build with extra flags. The extra
flags appear because of a direct import of the testing package from
the fake_etcd_client.go source file.

Remove the direct import of the testing package. Add a tools.T
interface to support existing behavior. Also clean up two TODO items
by remove using of the expectError and expectNoError functions.

Fixes #579
This commit is contained in:
Kelsey Hightower 2014-07-27 06:30:32 -07:00
parent 8a5cc87df8
commit 87fa19cdfe
5 changed files with 68 additions and 38 deletions

View File

@ -24,20 +24,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
) )
// TODO: remove this
func expectError(t *testing.T, err error) {
if err == nil {
t.Errorf("Expected error, Got %v", err)
}
}
// TODO: remove this
func expectNoError(t *testing.T, err error) {
if err != nil {
t.Errorf("Expected no error, Got %v", err)
}
}
func expectEmptyChannel(t *testing.T, ch <-chan interface{}) { func expectEmptyChannel(t *testing.T, ch <-chan interface{}) {
select { select {
case update := <-ch: case update := <-ch:

View File

@ -64,7 +64,9 @@ func TestGetEtcdNoData(t *testing.T) {
} }
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
_, err := c.fetchNextState(0) _, err := c.fetchNextState(0)
expectError(t, err) if err == nil {
t.Errorf("Expected error")
}
expectEmptyChannel(t, ch) expectEmptyChannel(t, ch)
} }
@ -82,7 +84,9 @@ func TestGetEtcd(t *testing.T) {
} }
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
lastIndex, err := c.fetchNextState(0) lastIndex, err := c.fetchNextState(0)
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if lastIndex != 2 { if lastIndex != 2 {
t.Errorf("Expected %#v, Got %#v", 2, lastIndex) t.Errorf("Expected %#v, Got %#v", 2, lastIndex)
} }
@ -107,7 +111,9 @@ func TestWatchEtcd(t *testing.T) {
} }
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
lastIndex, err := c.fetchNextState(1) lastIndex, err := c.fetchNextState(1)
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if lastIndex != 3 { if lastIndex != 3 {
t.Errorf("Expected %d, Got %d", 3, lastIndex) t.Errorf("Expected %d, Got %d", 3, lastIndex)
} }
@ -127,7 +133,9 @@ func TestGetEtcdNotFound(t *testing.T) {
} }
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
_, err := c.fetchNextState(0) _, err := c.fetchNextState(0)
expectError(t, err) if err == nil {
t.Errorf("Expected error")
}
expectEmptyChannel(t, ch) expectEmptyChannel(t, ch)
} }
@ -142,6 +150,8 @@ func TestGetEtcdError(t *testing.T) {
} }
c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
_, err := c.fetchNextState(0) _, err := c.fetchNextState(0)
expectError(t, err) if err == nil {
t.Errorf("Expected error")
}
expectEmptyChannel(t, ch) expectEmptyChannel(t, ch)
} }

View File

@ -34,7 +34,9 @@ func TestExtractFromNonExistentFile(t *testing.T) {
ch := make(chan interface{}, 1) ch := make(chan interface{}, 1)
c := SourceFile{"/some/fake/file", ch} c := SourceFile{"/some/fake/file", ch}
err := c.extractFromPath() err := c.extractFromPath()
expectError(t, err) if err == nil {
t.Errorf("Expected error")
}
} }
func TestUpdateOnNonExistentFile(t *testing.T) { func TestUpdateOnNonExistentFile(t *testing.T) {
@ -94,7 +96,9 @@ func TestExtractFromBadDataFile(t *testing.T) {
ch := make(chan interface{}, 1) ch := make(chan interface{}, 1)
c := SourceFile{file.Name(), ch} c := SourceFile{file.Name(), ch}
err := c.extractFromPath() err := c.extractFromPath()
expectError(t, err) if err == nil {
t.Errorf("Expected error")
}
expectEmptyChannel(t, ch) expectEmptyChannel(t, ch)
} }
@ -102,15 +106,18 @@ func TestExtractFromValidDataFile(t *testing.T) {
manifest := api.ContainerManifest{ID: ""} manifest := api.ContainerManifest{ID: ""}
text, err := json.Marshal(manifest) text, err := json.Marshal(manifest)
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
file := writeTestFile(t, os.TempDir(), "test_pod_config", string(text)) file := writeTestFile(t, os.TempDir(), "test_pod_config", string(text))
defer os.Remove(file.Name()) defer os.Remove(file.Name())
ch := make(chan interface{}, 1) ch := make(chan interface{}, 1)
c := SourceFile{file.Name(), ch} c := SourceFile{file.Name(), ch}
err = c.extractFromPath() err = c.extractFromPath()
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
update := (<-ch).(kubelet.PodUpdate) update := (<-ch).(kubelet.PodUpdate)
expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: simpleSubdomainSafeHash(file.Name()), Manifest: manifest}) expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: simpleSubdomainSafeHash(file.Name()), Manifest: manifest})
if !reflect.DeepEqual(expected, update) { if !reflect.DeepEqual(expected, update) {
@ -120,13 +127,17 @@ func TestExtractFromValidDataFile(t *testing.T) {
func TestExtractFromEmptyDir(t *testing.T) { func TestExtractFromEmptyDir(t *testing.T) {
dirName, err := ioutil.TempDir("", "foo") dirName, err := ioutil.TempDir("", "foo")
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer os.RemoveAll(dirName) defer os.RemoveAll(dirName)
ch := make(chan interface{}, 1) ch := make(chan interface{}, 1)
c := SourceFile{dirName, ch} c := SourceFile{dirName, ch}
err = c.extractFromPath() err = c.extractFromPath()
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
update := (<-ch).(kubelet.PodUpdate) update := (<-ch).(kubelet.PodUpdate)
expected := CreatePodUpdate(kubelet.SET) expected := CreatePodUpdate(kubelet.SET)
@ -143,15 +154,23 @@ func TestExtractFromDir(t *testing.T) {
files := make([]*os.File, len(manifests)) files := make([]*os.File, len(manifests))
dirName, err := ioutil.TempDir("", "foo") dirName, err := ioutil.TempDir("", "foo")
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
for i, manifest := range manifests { for i, manifest := range manifests {
data, err := json.Marshal(manifest) data, err := json.Marshal(manifest)
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
file, err := ioutil.TempFile(dirName, manifest.ID) file, err := ioutil.TempFile(dirName, manifest.ID)
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
name := file.Name() name := file.Name()
expectNoError(t, file.Close()) if err := file.Close(); err != nil {
t.Errorf("Unexpected error: %v", err)
}
ioutil.WriteFile(name, data, 0755) ioutil.WriteFile(name, data, 0755)
files[i] = file files[i] = file
} }
@ -159,7 +178,9 @@ func TestExtractFromDir(t *testing.T) {
ch := make(chan interface{}, 1) ch := make(chan interface{}, 1)
c := SourceFile{dirName, ch} c := SourceFile{dirName, ch}
err = c.extractFromPath() err = c.extractFromPath()
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
update := (<-ch).(kubelet.PodUpdate) update := (<-ch).(kubelet.PodUpdate)
expected := CreatePodUpdate( expected := CreatePodUpdate(

View File

@ -42,7 +42,9 @@ func TestExtractFromHttpBadness(t *testing.T) {
ch := make(chan interface{}, 1) ch := make(chan interface{}, 1)
c := SourceURL{"http://localhost:49575/_not_found_", ch} c := SourceURL{"http://localhost:49575/_not_found_", ch}
err := c.extractFromURL() err := c.extractFromURL()
expectError(t, err) if err == nil {
t.Errorf("Expected error")
}
expectEmptyChannel(t, ch) expectEmptyChannel(t, ch)
} }
@ -63,7 +65,9 @@ func TestExtractFromHttpSingle(t *testing.T) {
c := SourceURL{testServer.URL, ch} c := SourceURL{testServer.URL, ch}
err = c.extractFromURL() err = c.extractFromURL()
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
update := (<-ch).(kubelet.PodUpdate) update := (<-ch).(kubelet.PodUpdate)
expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "foo", Manifest: manifests[0]}) expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "foo", Manifest: manifests[0]})
if !reflect.DeepEqual(expected, update) { if !reflect.DeepEqual(expected, update) {
@ -90,7 +94,9 @@ func TestExtractFromHttpMultiple(t *testing.T) {
c := SourceURL{testServer.URL, ch} c := SourceURL{testServer.URL, ch}
err = c.extractFromURL() err = c.extractFromURL()
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
update := (<-ch).(kubelet.PodUpdate) update := (<-ch).(kubelet.PodUpdate)
expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "1", Manifest: manifests[0]}, kubelet.Pod{Name: "bar", Manifest: manifests[1]}) expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "1", Manifest: manifests[0]}, kubelet.Pod{Name: "bar", Manifest: manifests[1]})
@ -115,7 +121,9 @@ func TestExtractFromHttpEmptyArray(t *testing.T) {
c := SourceURL{testServer.URL, ch} c := SourceURL{testServer.URL, ch}
err = c.extractFromURL() err = c.extractFromURL()
expectNoError(t, err) if err != nil {
t.Errorf("Unexpected error: %v", err)
}
update := (<-ch).(kubelet.PodUpdate) update := (<-ch).(kubelet.PodUpdate)
expected := CreatePodUpdate(kubelet.SET) expected := CreatePodUpdate(kubelet.SET)
if !reflect.DeepEqual(expected, update) { if !reflect.DeepEqual(expected, update) {

View File

@ -18,7 +18,6 @@ package tools
import ( import (
"fmt" "fmt"
"testing"
"github.com/coreos/go-etcd/etcd" "github.com/coreos/go-etcd/etcd"
) )
@ -28,13 +27,19 @@ type EtcdResponseWithError struct {
E error E error
} }
// TestLogger is a type passed to Test functions to support formatted test logs.
type TestLogger interface {
Errorf(format string, args ...interface{})
Logf(format string, args ...interface{})
}
type FakeEtcdClient struct { type FakeEtcdClient struct {
watchCompletedChan chan bool watchCompletedChan chan bool
Data map[string]EtcdResponseWithError Data map[string]EtcdResponseWithError
DeletedKeys []string DeletedKeys []string
Err error Err error
t *testing.T t TestLogger
Ix int Ix int
// Will become valid after Watch is called; tester may write to it. Tester may // Will become valid after Watch is called; tester may write to it. Tester may
@ -45,7 +50,7 @@ type FakeEtcdClient struct {
WatchStop chan<- bool WatchStop chan<- bool
} }
func MakeFakeEtcdClient(t *testing.T) *FakeEtcdClient { func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient {
ret := &FakeEtcdClient{ ret := &FakeEtcdClient{
t: t, t: t,
Data: map[string]EtcdResponseWithError{}, Data: map[string]EtcdResponseWithError{},