Skip to content

Commit

Permalink
Add debugging info printing to etcd fake
Browse files Browse the repository at this point in the history
And make tests pass again.
  • Loading branch information
lavalamp committed Aug 11, 2014
1 parent 5cdce0e commit b7752a8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ type PodStatus string
// These are the valid statuses of pods.
const (
// PodWaiting means that we're waiting for the pod to begin running.
PodWaiting = "Waiting"
PodWaiting PodStatus = "Waiting"
// PodRunning means that the pod is up and running.
PodRunning PodStatus = "Running"
// PodTerminated means that the pod has stopped.
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ type PodStatus string
// These are the valid statuses of pods.
const (
// PodWaiting means that we're waiting for the pod to begin running.
PodWaiting = "Waiting"
PodWaiting PodStatus = "Waiting"
// PodRunning means that the pod is up and running.
PodRunning PodStatus = "Running"
// PodTerminated means that the pod has stopped.
Expand Down
9 changes: 1 addition & 8 deletions pkg/registry/etcdregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,11 @@ func makeContainerKey(machine string) string {

// CreatePod creates a pod based on a specification, schedule it onto a specific machine.
func (registry *EtcdRegistry) CreatePod(machine string, pod api.Pod) error {
// TODO: When our client supports it, switch to atomic creates.
var pod2 api.Pod
err := registry.helper.ExtractObj(makePodKey(pod.ID), &pod2, false)
if err == nil {
return fmt.Errorf("a pod named %s already exists (%#v)", pod.ID, pod2)
}

// Set status to "Waiting".
pod.CurrentState.Status = api.PodWaiting
pod.CurrentState.Host = ""

err = registry.helper.SetObj(makePodKey(pod.ID), &pod)
err := registry.helper.CreateObj(makePodKey(pod.ID), &pod)
if err != nil {
return err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/registry/etcdregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func TestEtcdCreatePodAlreadyExisting(t *testing.T) {

func TestEtcdCreatePodWithContainersError(t *testing.T) {
fakeClient := tools.MakeFakeEtcdClient(t)
fakeClient.TestIndex = true
fakeClient.Data["/registry/pods/foo"] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: nil,
Expand All @@ -160,7 +161,7 @@ func TestEtcdCreatePodWithContainersError(t *testing.T) {
},
})
if err == nil {
t.Error("Unexpected non-error")
t.Fatalf("Unexpected non-error")
}
_, err = fakeClient.Get("/registry/pods/foo", false, false)
if err == nil {
Expand All @@ -173,6 +174,7 @@ func TestEtcdCreatePodWithContainersError(t *testing.T) {

func TestEtcdCreatePodWithContainersNotFound(t *testing.T) {
fakeClient := tools.MakeFakeEtcdClient(t)
fakeClient.TestIndex = true
fakeClient.Data["/registry/pods/foo"] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: nil,
Expand Down Expand Up @@ -202,7 +204,7 @@ func TestEtcdCreatePodWithContainersNotFound(t *testing.T) {
},
})
if err != nil {
t.Errorf("unexpected error: %v", err)
t.Fatalf("unexpected error: %v", err)
}

resp, err := fakeClient.Get("/registry/pods/foo", false, false)
Expand Down Expand Up @@ -588,7 +590,7 @@ func TestEtcdCreateController(t *testing.T) {

func TestEtcdCreateControllerAlreadyExisting(t *testing.T) {
fakeClient := tools.MakeFakeEtcdClient(t)
fakeClient.Set("/registry/controllers/foo", util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/controllers/foo", api.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0)

registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"})
err := registry.CreateController(api.ReplicationController{
Expand Down Expand Up @@ -680,7 +682,7 @@ func TestEtcdCreateService(t *testing.T) {

func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
fakeClient := tools.MakeFakeEtcdClient(t)
fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
fakeClient.Set("/registry/services/specs/foo", api.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"})
err := registry.CreateService(api.Service{
JSONBase: api.JSONBase{ID: "foo"},
Expand Down Expand Up @@ -782,7 +784,7 @@ func TestEtcdUpdateEndpoints(t *testing.T) {
Endpoints: []string{"baz", "bar"},
}

fakeClient.Set("/registry/services/endpoints/foo", util.MakeJSONString(api.Endpoints{}), 0)
fakeClient.Set("/registry/services/endpoints/foo", api.EncodeOrDie(api.Endpoints{}), 0)

err := registry.UpdateEndpoints(endpoints)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/tools/fake_etcd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (f *FakeEtcdClient) generateIndex() uint64 {
}

f.ChangeIndex++
f.t.Logf("generating index %v", f.ChangeIndex)
return f.ChangeIndex
}

Expand Down Expand Up @@ -115,7 +116,7 @@ func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response,

func (f *FakeEtcdClient) nodeExists(key string) bool {
result, ok := f.Data[key]
return ok && result.R != nil && result.R.Node != nil
return ok && result.R != nil && result.R.Node != nil && result.E == nil
}

func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Response, error) {
Expand All @@ -128,6 +129,7 @@ func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Respons
if f.nodeExists(key) {
prevResult := f.Data[key]
createdIndex := prevResult.R.Node.CreatedIndex
f.t.Logf("updating %v, index %v -> %v", key, createdIndex, i)
result := EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Expand All @@ -141,6 +143,7 @@ func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Respons
return result.R, nil
}

f.t.Logf("creating %v, index %v", key, i)
result := EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Expand All @@ -163,6 +166,7 @@ func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, err

func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error) {
if f.Err != nil {
f.t.Logf("c&s: returning err %v", f.Err)
return nil, f.Err
}

Expand All @@ -179,16 +183,19 @@ func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue
defer f.Mutex.Unlock()

if !f.nodeExists(key) {
f.t.Logf("c&s: node doesn't exist")
return nil, EtcdErrorNotFound
}

prevNode := f.Data[key].R.Node

if prevValue != "" && prevValue != prevNode.Value {
f.t.Logf("body didn't match")
return nil, EtcdErrorTestFailed
}

if prevIndex != 0 && prevIndex != prevNode.ModifiedIndex {
f.t.Logf("got index %v but needed %v", prevIndex, prevNode.ModifiedIndex)
return nil, EtcdErrorTestFailed
}

Expand Down

0 comments on commit b7752a8

Please sign in to comment.