Finish acceptance test for CreateServer() function
Two problems remain -- server status is coming up blank, and
measurements of disk and RAM are coming up zeros. Not sure why this is.
It used to work in Gorax. More comparative testing between Gorax and
Gophercloud is needed.
However, what is in this commit is sufficient to get the acceptance
tests running again, at least on Rackspace's infrastructure. If you
attempt to run these tests on HP or other infrastructure, they will
fail for you.
diff --git a/acceptance/04-create-server.go b/acceptance/04-create-server.go
index 4c50e5d..bc5ff69 100644
--- a/acceptance/04-create-server.go
+++ b/acceptance/04-create-server.go
@@ -3,31 +3,67 @@
import (
"flag"
"fmt"
- "log"
"github.com/rackspace/gophercloud"
)
+var provider, username, password string
+
var region, serverName, imageRef, flavorRef *string
var adminPass = flag.String("a", "", "Administrator password (auto-assigned if none)")
+var quiet = flag.Bool("quiet", false, "Quiet mode for acceptance tests. $? non-zero if error.")
-func main() {
- provider, username, password := getCredentials()
+func configure() {
+ provider, username, password = getCredentials()
region = flag.String("r", "DFW", "Rackspace region in which to create the server")
serverName = flag.String("n", randomString(16), "Server name (what you see in the control panel)")
- imageRef = flag.String("i", "", "ID of image to deploy onto the server") // TODO(sfalvo): Make this work in -quiet mode.
- flavorRef = flag.String("f", "", "Flavor of server to deploy image upon") // TODO(sfalvo): Make this work in -quiet mode.
+ imageRef = flag.String("i", "", "ID of image to deploy onto the server")
+ flavorRef = flag.String("f", "", "Flavor of server to deploy image upon")
flag.Parse()
+}
- validations := map[string]string{
- "an image reference (-i flag)": *imageRef,
- "a server flavor (-f flag)": *flavorRef,
+func aSuitableImage(api gophercloud.CloudServersProvider) string {
+ images, err := api.ListImages()
+ if err != nil {
+ panic(err)
}
- for flag, value := range validations {
- if value == "" {
- log.Fatal(fmt.Sprintf("You must provide %s", flag))
+
+ // TODO(sfalvo):
+ // Works for Rackspace, might not work for your provider!
+ // Need to figure out why ListImages() provides 0 values for
+ // Ram and Disk fields.
+ //
+ // Until then, just return Ubuntu 12.04 LTS.
+ for i := 0; i < len(images); i++ {
+ if images[i].Id == "6a668bb8-fb5d-407a-9a89-6f957bced767" {
+ return images[i].Id
}
}
+ panic("Image 6a668bb8-fb5d-407a-9a89-6f957bced767 (Ubuntu 12.04 LTS) not found.")
+}
+
+func aSuitableFlavor(api gophercloud.CloudServersProvider) string {
+ flavors, err := api.ListFlavors()
+ if err != nil {
+ panic(err)
+ }
+
+ // TODO(sfalvo):
+ // Works for Rackspace, might not work for your provider!
+ // Need to figure out why ListFlavors() provides 0 values for
+ // Ram and Disk fields.
+ //
+ // Until then, just return Ubuntu 12.04 LTS.
+ for i := 0; i < len(flavors); i++ {
+ if flavors[i].Id == "2" {
+ return flavors[i].Id
+ }
+ }
+ panic("Flavor 2 (512MB 1-core 20GB machine) not found.")
+}
+
+func main() {
+ configure()
auth, err := gophercloud.Authenticate(
provider,
@@ -50,6 +86,14 @@
panic(err)
}
+ if *imageRef == "" {
+ *imageRef = aSuitableImage(servers)
+ }
+
+ if *flavorRef == "" {
+ *flavorRef = aSuitableFlavor(servers)
+ }
+
_, err = servers.CreateServer(gophercloud.NewServer{
Name: *serverName,
ImageRef: *imageRef,
@@ -65,8 +109,10 @@
panic(err)
}
- fmt.Printf("ID,Name,Status,Progress\n")
- for _, i := range allServers {
- fmt.Printf("%s,\"%s\",%s,%d\n", i.Id, i.Name, i.Status, i.Progress)
+ if !*quiet {
+ fmt.Printf("ID,Name,Status,Progress\n")
+ for _, i := range allServers {
+ fmt.Printf("%s,\"%s\",%s,%d\n", i.Id, i.Name, i.Status, i.Progress)
+ }
}
}