Skip to content

Commit

Permalink
implement compute tests for StopInstances
Browse files Browse the repository at this point in the history
  • Loading branch information
helen-fornazier committed May 10, 2018
1 parent c8fba0d commit 0fc987f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions daisy/compute/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ func TestCreates(t *testing.T) {
}
}

func TestStops(t *testing.T) {
var stopURL, opGetURL string
svr, c, err := NewTestClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.String() == stopURL {
fmt.Fprint(w, `{}`)
} else if r.Method == "GET" && r.URL.String() == opGetURL {
fmt.Fprint(w, `{"Status":"DONE"}`)
} else {
w.WriteHeader(500)
fmt.Fprintln(w, "URL and Method not recognized:", r.Method, r.URL)
}
}))
if err != nil {
t.Fatal(err)
}
defer svr.Close()

stopURL = fmt.Sprintf("/%s/zones/%s/instances/%s/stop?alt=json", testProject, testZone, testInstance)
opGetURL = fmt.Sprintf("/%s/zones/%s/operations/?alt=json", testProject, testZone)
if err := c.StopInstance(testProject, testZone, testInstance); err != nil {
t.Errorf("error running Stop: %v", err)
}
}

func TestDeletes(t *testing.T) {
var deleteURL, opGetURL *string
svr, c, err := NewTestClient(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
9 changes: 9 additions & 0 deletions daisy/compute/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type TestClient struct {
CreateImageFn func(project string, i *compute.Image) error
CreateInstanceFn func(project, zone string, i *compute.Instance) error
CreateNetworkFn func(project string, i *compute.Network) error
StopInstanceFn func(project, zone, name string) error
DeleteDiskFn func(project, zone, name string) error
DeleteImageFn func(project, name string) error
DeleteInstanceFn func(project, zone, name string) error
Expand Down Expand Up @@ -127,6 +128,14 @@ func (c *TestClient) CreateNetwork(project string, n *compute.Network) error {
return c.client.CreateNetwork(project, n)
}

// StopInstance uses the override method StopInstanceFn or the real implementation.
func (c *TestClient) StopInstance(project, zone, name string) error {
if c.StopInstanceFn != nil {
return c.StopInstanceFn(project, zone, name)
}
return c.client.StopInstance(project, zone, name)
}

// DeleteDisk uses the override method DeleteDiskFn or the real implementation.
func (c *TestClient) DeleteDisk(project, zone, name string) error {
if c.DeleteDiskFn != nil {
Expand Down
2 changes: 2 additions & 0 deletions daisy/compute/test_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestTestClient(t *testing.T) {
{"create image", func() { c.CreateImage("a", &compute.Image{}) }, "/a/global/images?alt=json"},
{"create instance", func() { c.CreateInstance("a", "b", &compute.Instance{}) }, "/a/zones/b/instances?alt=json"},
{"create network", func() { c.CreateNetwork("a", &compute.Network{}) }, "/a/global/networks?alt=json"},
{"instances stop", func() { c.StopInstance("a", "b", "c") }, "/a/zones/b/instances/c/stop?alt=json"},
{"delete disk", func() { c.DeleteDisk("a", "b", "c") }, "/a/zones/b/disks/c?alt=json"},
{"delete image", func() { c.DeleteImage("a", "b") }, "/a/global/images/b?alt=json"},
{"delete instance", func() { c.DeleteInstance("a", "b", "c") }, "/a/zones/b/instances/c?alt=json"},
Expand Down Expand Up @@ -106,6 +107,7 @@ func TestTestClient(t *testing.T) {
c.CreateImageFn = func(_ string, _ *compute.Image) error { fakeCalled = true; return nil }
c.CreateInstanceFn = func(_, _ string, _ *compute.Instance) error { fakeCalled = true; return nil }
c.CreateNetworkFn = func(_ string, _ *compute.Network) error { fakeCalled = true; return nil }
c.StopInstanceFn = func(_, _, _ string) error { fakeCalled = true; return nil }
c.DeleteDiskFn = func(_, _, _ string) error { fakeCalled = true; return nil }
c.DeleteImageFn = func(_, _ string) error { fakeCalled = true; return nil }
c.DeleteInstanceFn = func(_, _, _ string) error { fakeCalled = true; return nil }
Expand Down

0 comments on commit 0fc987f

Please sign in to comment.