Samuel A. Falvo II | b5d93f2 | 2014-02-21 15:00:20 -0800 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 3 | package v2 |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 4 | |
| 5 | import ( |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 6 | "fmt" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 7 | "os" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 8 | "strings" |
Jamie Hannaford | c8fc6ea | 2014-09-10 13:59:58 +0200 | [diff] [blame] | 9 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/acceptance/tools" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack" |
Jamie Hannaford | c8fc6ea | 2014-09-10 13:59:58 +0200 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/openstack/utils" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 15 | ) |
| 16 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 17 | func newClient() (*gophercloud.ServiceClient, error) { |
| 18 | ao, err := utils.AuthOptions() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 19 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 20 | return nil, err |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 21 | } |
| 22 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 23 | client, err := openstack.AuthenticatedClient(ao) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 27 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 28 | return openstack.NewComputeV2(client, gophercloud.EndpointOpts{ |
| 29 | Region: os.Getenv("OS_REGION_NAME"), |
| 30 | }) |
| 31 | } |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 32 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 33 | func waitForStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error { |
| 34 | return tools.WaitFor(func() (bool, error) { |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 35 | latest, err := servers.Get(client, server.ID).Extract() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 36 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 37 | return false, err |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 40 | if latest.Status == status { |
| 41 | // Success! |
| 42 | return true, nil |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 43 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 44 | |
| 45 | return false, nil |
| 46 | }) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 49 | // ComputeChoices contains image and flavor selections for use by the acceptance tests. |
| 50 | type ComputeChoices struct { |
| 51 | // ImageID contains the ID of a valid image. |
| 52 | ImageID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 53 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 54 | // FlavorID contains the ID of a valid flavor. |
| 55 | FlavorID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 56 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 57 | // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct |
| 58 | // from FlavorID. |
| 59 | FlavorIDResize string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 62 | // ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables. |
| 63 | // If any required state is missing, an `error` will be returned that enumerates the missing properties. |
| 64 | func ComputeChoicesFromEnv() (*ComputeChoices, error) { |
| 65 | imageID := os.Getenv("OS_IMAGE_ID") |
| 66 | flavorID := os.Getenv("OS_FLAVOR_ID") |
| 67 | flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE") |
| 68 | |
| 69 | missing := make([]string, 0, 3) |
| 70 | if imageID == "" { |
| 71 | missing = append(missing, "OS_IMAGE_ID") |
| 72 | } |
| 73 | if flavorID == "" { |
| 74 | missing = append(missing, "OS_FLAVOR_ID") |
| 75 | } |
| 76 | if flavorIDResize == "" { |
| 77 | missing = append(missing, "OS_FLAVOR_ID_RESIZE") |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 80 | notDistinct := "" |
| 81 | if flavorID == flavorIDResize { |
| 82 | notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct." |
| 83 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 84 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 85 | if len(missing) > 0 || notDistinct != "" { |
| 86 | text := "You're missing some important setup:\n" |
| 87 | if len(missing) > 0 { |
| 88 | text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n" |
| 89 | } |
| 90 | if notDistinct != "" { |
| 91 | text += " * " + notDistinct + "\n" |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 94 | return nil, fmt.Errorf(text) |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 97 | return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize}, nil |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 98 | } |