// createServer creates a new server in a manner compatible with acceptance testing. // In particular, it ensures that the name of the server always starts with "ACPTTEST--", // which the delete servers acceptance test relies on to identify servers to delete. // Passing in empty image and flavor references will force the use of reasonable defaults. // An empty name string will result in a dynamically created name prefixed with "ACPTTEST--". // A blank admin password will cause a password to be automatically generated; however, // at present no means of recovering this password exists, as no acceptance tests yet require // this data. func createServer(servers gophercloud.CloudServersProvider, imageRef, flavorRef, name, adminPass string) (string, error) { if imageRef == "" { imageRef = aSuitableImage(servers) } if flavorRef == "" { flavorRef = aSuitableFlavor(servers) } if len(name) < 1 { name = randomString("ACPTTEST", 16) } if (len(name) < 8) || (name[0:8] != "ACPTTEST") { name = fmt.Sprintf("ACPTTEST--%s", name) } newServer, err := servers.CreateServer(gophercloud.NewServer{ Name: name, ImageRef: imageRef, FlavorRef: flavorRef, AdminPass: adminPass, }) if err != nil { return "", err } return newServer.Id, nil }
func tryLinksOnly(api gophercloud.CloudServersProvider) { servers, err := api.ListServersLinksOnly() if err != nil { panic(err) } if !*quiet { fmt.Println("Id,Name") for _, s := range servers { if s.AccessIPv4 != "" { panic("IPv4 not expected") } if s.Status != "" { panic("Status not expected") } if s.Progress != 0 { panic("Progress not expected") } fmt.Printf("%s,\"%s\"\n", s.Id, s.Name) } } }
func withServer(api gophercloud.CloudServersProvider, f func(string)) { id, err := createServer(api, "", "", "", "") if err != nil { panic(err) } for { s, err := api.ServerById(id) if err != nil { panic(err) } if s.Status == "ACTIVE" { break } time.Sleep(10 * time.Second) } f(id) // I've learned that resizing an instance can fail if a delete request // comes in prior to its completion. This ends up leaving the server // in an error state, and neither the resize NOR the delete complete. // This is a bug in OpenStack, as far as I'm concerned, but thankfully, // there's an easy work-around -- just wait for your server to return to // active state first! waitForServerState(api, id, "ACTIVE") err = api.DeleteServerById(id) if err != nil { panic(err) } }
// 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") }
func tryFullDetails(api gophercloud.CloudServersProvider) { servers, err := api.ListServers() if err != nil { panic(err) } if !*quiet { fmt.Println("Id,Name,AccessIPv4,Status,Progress") for _, s := range servers { fmt.Printf("%s,\"%s\",%s,%s,%d\n", s.Id, s.Name, s.AccessIPv4, s.Status, s.Progress) } } }
func tryAddressesByNetwork(networkLabel string, id string, api gophercloud.CloudServersProvider) { log("Getting list of addresses on", networkLabel, "network...") network, err := api.ListAddressesByNetwork(id, networkLabel) if (err != nil) && (err != gophercloud.WarnUnauthoritative) { panic(err) } if err == gophercloud.WarnUnauthoritative { log("Uh oh -- got a response back, but it's not authoritative for some reason.") } for _, addr := range network[networkLabel] { log("Address:", addr.Addr, " IPv", addr.Version) } }
func tryAllAddresses(id string, api gophercloud.CloudServersProvider) { log("Getting list of all addresses...") addresses, err := api.ListAddresses(id) if (err != nil) && (err != gophercloud.WarnUnauthoritative) { panic(err) } if err == gophercloud.WarnUnauthoritative { log("Uh oh -- got a response back, but it's not authoritative for some reason.") } if !*quiet { fmt.Println("Addresses:") fmt.Printf("%+v\n", addresses) } }
// Perform the resize test, but accept the resize request. func resizeAcceptTest(api gophercloud.CloudServersProvider, done chan bool) { withServer(api, func(id string) { newFlavorId := findAlternativeFlavor() err := api.ResizeServer(id, randomString("ACPTTEST", 24), newFlavorId, "") if err != nil { panic(err) } waitForServerState(api, id, "VERIFY_RESIZE") err = api.ConfirmResize(id) if err != nil { panic(err) } }) done <- true }
// aSuitableFlavor finds the minimum flavor capable of running the test image // chosen by aSuitableImage. If none can be found, this function will panic. 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.") }
// aSuitableImage finds a minimal image for use in dynamically creating servers. // If none can be found, this function will panic. func aSuitableImage(api gophercloud.CloudServersProvider) string { images, err := api.ListImages() if err != nil { panic(err) } // 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 strings.Contains(images[i].Name, "Ubuntu 12.04 LTS") { return images[i].Id } } panic("Image for Ubuntu 12.04 LTS not found.") }
// locateAServer queries the set of servers owned by the user. If at least one // exists, the first found is picked, and its ID is returned. Otherwise, a new // server will be created, and its ID returned. // // deleteAfter will be true if the caller should schedule a call to DeleteServerById() // to clean up. func locateAServer(servers gophercloud.CloudServersProvider) (deleteAfter bool, id string, err error) { ss, err := servers.ListServers() if err != nil { return false, "", err } if len(ss) > 0 { // We could just cheat and dump the server details from ss[0]. // But, that tests ListServers(), and not ServerById(). So, we // elect not to cheat. return false, ss[0].Id, nil } serverId, err := createServer(servers, "", "", "", "") if err != nil { return false, "", err } err = waitForServerState(servers, serverId, "ACTIVE") return true, serverId, err }
func tryAllAddresses(id string, api gophercloud.CloudServersProvider) { log("Getting the server instance") s, err := api.ServerById(id) if err != nil { panic(err) } log("Getting the complete set of pools") ps, err := s.AllAddressPools() if err != nil { panic(err) } log("Listing IPs for each pool") for k, v := range ps { log(fmt.Sprintf(" Pool %s", k)) for _, a := range v { log(fmt.Sprintf(" IP: %s, Version: %d", a.Addr, a.Version)) } } }