Add acceptance test for CreateImage and DeleteImageById
diff --git a/acceptance/17-create-delete-image.go b/acceptance/17-create-delete-image.go
new file mode 100644
index 0000000..0ad9b14
--- /dev/null
+++ b/acceptance/17-create-delete-image.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "github.com/rackspace/gophercloud"
+)
+
+var quiet = flag.Bool("quiet", false, "Quiet mode for acceptance testing. $? non-zero on error though.")
+var rgn = flag.String("r", "DFW", "Datacenter region to interrogate.")
+
+func main() {
+ flag.Parse()
+
+ withIdentity(false, func(auth gophercloud.AccessProvider) {
+ withServerApi(auth, func(servers gophercloud.CloudServersProvider) {
+ log("Creating server")
+ serverId, err := createServer(servers, "", "", "", "")
+ if err != nil {
+ panic(err)
+ }
+ waitForServerState(servers, serverId, "ACTIVE")
+
+ log("Creating image")
+ name := randomString("ACPTTEST", 16)
+ createImage := gophercloud.CreateImage{
+ Name: name,
+ }
+ imageId, err := servers.CreateImage(serverId, createImage)
+ if err != nil {
+ panic(err)
+ }
+ waitForImageState(servers, imageId, "ACTIVE")
+
+ log("Deleting server")
+ servers.DeleteServerById(serverId)
+
+ log("Deleting image")
+ servers.DeleteImageById(imageId)
+
+ log("Done")
+ })
+ })
+}
+
+func log(s string) {
+ if !*quiet {
+ fmt.Println(s)
+ }
+}
diff --git a/acceptance/libargs.go b/acceptance/libargs.go
index f9b56ea..b1e79ff 100644
--- a/acceptance/libargs.go
+++ b/acceptance/libargs.go
@@ -186,3 +186,20 @@
}
panic("Impossible")
}
+
+// waitForImageState polls, every 10 seconds, for a given image to appear in the indicated state.
+// This call will block forever if it never appears in the desired state, so if a timeout is required,
+// make sure to call this function in a goroutine.
+func waitForImageState(api gophercloud.CloudServersProvider, id, state string) error {
+ for {
+ s, err := api.ImageById(id)
+ if err != nil {
+ return err
+ }
+ if s.Status == state {
+ return nil
+ }
+ time.Sleep(10 * time.Second)
+ }
+ panic("Impossible")
+}