Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 1 | // +build acceptance compute servergroups |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud" |
| 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/schedulerhints" |
| 11 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" |
| 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 13 | ) |
| 14 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 15 | func TestServergroupsList(t *testing.T) { |
| 16 | client, err := newClient() |
| 17 | if err != nil { |
| 18 | t.Fatalf("Unable to create a compute client: %v", err) |
| 19 | } |
| 20 | |
| 21 | allPages, err := servergroups.List(client).AllPages() |
| 22 | if err != nil { |
| 23 | t.Fatalf("Unable to list server groups: %v", err) |
| 24 | } |
| 25 | |
| 26 | allServerGroups, err := servergroups.ExtractServerGroups(allPages) |
| 27 | if err != nil { |
| 28 | t.Fatalf("Unable to extract server groups: %v", err) |
| 29 | } |
| 30 | |
| 31 | for _, serverGroup := range allServerGroups { |
| 32 | printServerGroup(t, &serverGroup) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestServergroupsCreate(t *testing.T) { |
| 37 | client, err := newClient() |
| 38 | if err != nil { |
| 39 | t.Fatalf("Unable to create a compute client: %v", err) |
| 40 | } |
| 41 | |
| 42 | serverGroup, err := createServerGroup(t, client, "anti-affinity") |
| 43 | if err != nil { |
| 44 | t.Fatalf("Unable to create server group: %v", err) |
| 45 | } |
| 46 | defer deleteServerGroup(t, client, serverGroup) |
| 47 | |
| 48 | serverGroup, err = servergroups.Get(client, serverGroup.ID).Extract() |
| 49 | if err != nil { |
| 50 | t.Fatalf("Unable to get server group: %v", err) |
| 51 | } |
| 52 | |
| 53 | printServerGroup(t, serverGroup) |
| 54 | } |
| 55 | |
| 56 | func TestServergroupsAffinityPolicy(t *testing.T) { |
| 57 | client, err := newClient() |
| 58 | if err != nil { |
| 59 | t.Fatalf("Unable to create a compute client: %v", err) |
| 60 | } |
| 61 | |
| 62 | choices, err := ComputeChoicesFromEnv() |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | serverGroup, err := createServerGroup(t, client, "affinity") |
| 68 | if err != nil { |
| 69 | t.Fatalf("Unable to create server group: %v", err) |
| 70 | } |
| 71 | defer deleteServerGroup(t, client, serverGroup) |
| 72 | |
| 73 | firstServer, err := createServerInServerGroup(t, client, choices, serverGroup) |
| 74 | if err != nil { |
| 75 | t.Fatalf("Unable to create server: %v", err) |
| 76 | } |
| 77 | |
| 78 | if err = waitForStatus(client, firstServer, "ACTIVE"); err != nil { |
| 79 | t.Fatalf("Unable to wait for server: %v", err) |
| 80 | } |
| 81 | defer deleteServer(t, client, firstServer) |
| 82 | |
| 83 | firstServer, err = servers.Get(client, firstServer.ID).Extract() |
| 84 | |
| 85 | secondServer, err := createServerInServerGroup(t, client, choices, serverGroup) |
| 86 | if err != nil { |
| 87 | t.Fatalf("Unable to create server: %v", err) |
| 88 | } |
| 89 | |
| 90 | if err = waitForStatus(client, secondServer, "ACTIVE"); err != nil { |
| 91 | t.Fatalf("Unable to wait for server: %v", err) |
| 92 | } |
| 93 | defer deleteServer(t, client, secondServer) |
| 94 | |
| 95 | secondServer, err = servers.Get(client, secondServer.ID).Extract() |
| 96 | |
| 97 | if firstServer.HostID != secondServer.HostID { |
| 98 | t.Fatalf("%s and %s were not scheduled on the same host.", firstServer.ID, secondServer.ID) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func createServerGroup(t *testing.T, client *gophercloud.ServiceClient, policy string) (*servergroups.ServerGroup, error) { |
| 103 | sg, err := servergroups.Create(client, &servergroups.CreateOpts{ |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 104 | Name: "test", |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 105 | Policies: []string{policy}, |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 106 | }).Extract() |
| 107 | |
| 108 | if err != nil { |
| 109 | t.Fatalf("Unable to create server group: %v", err) |
| 110 | } |
| 111 | |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 112 | return sg, nil |
| 113 | } |
| 114 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 115 | func createServerInServerGroup(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) { |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 116 | if testing.Short() { |
| 117 | t.Skip("Skipping test that requires server creation in short mode.") |
| 118 | } |
| 119 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 120 | networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName) |
| 121 | if err != nil { |
| 122 | t.Fatalf("Failed to obtain network ID: %v", err) |
| 123 | } |
| 124 | |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 125 | name := tools.RandomString("ACPTTEST", 16) |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 126 | t.Logf("Attempting to create server: %s", name) |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 127 | |
| 128 | pwd := tools.MakeNewPassword("") |
| 129 | |
| 130 | serverCreateOpts := servers.CreateOpts{ |
| 131 | Name: name, |
| 132 | FlavorRef: choices.FlavorID, |
| 133 | ImageRef: choices.ImageID, |
| 134 | AdminPass: pwd, |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 135 | Networks: []servers.Network{ |
| 136 | servers.Network{UUID: networkID}, |
| 137 | }, |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 138 | } |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 139 | |
| 140 | schedulerHintsOpts := schedulerhints.CreateOptsExt{ |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 141 | serverCreateOpts, |
| 142 | schedulerhints.SchedulerHints{ |
| 143 | Group: serverGroup.ID, |
| 144 | }, |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 145 | } |
| 146 | server, err := servers.Create(client, schedulerHintsOpts).Extract() |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 147 | if err != nil { |
| 148 | t.Fatalf("Unable to create server: %v", err) |
| 149 | } |
| 150 | |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 151 | return server, err |
| 152 | } |
| 153 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 154 | func deleteServerGroup(t *testing.T, client *gophercloud.ServiceClient, serverGroup *servergroups.ServerGroup) { |
| 155 | err := servergroups.Delete(client, serverGroup.ID).ExtractErr() |
| 156 | if err != nil { |
| 157 | t.Fatalf("Unable to delete server group %s: %v", serverGroup.ID, err) |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 160 | t.Logf("Deleted server group %s", serverGroup.ID) |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Joe Topjian | 6aa8ba0 | 2016-07-24 16:12:30 +0000 | [diff] [blame] | 163 | func printServerGroup(t *testing.T, serverGroup *servergroups.ServerGroup) { |
| 164 | t.Logf("ID: %s", serverGroup.ID) |
| 165 | t.Logf("Name: %s", serverGroup.Name) |
| 166 | t.Logf("Policies: %#v", serverGroup.Policies) |
| 167 | t.Logf("Members: %#v", serverGroup.Members) |
| 168 | t.Logf("Metadata: %#v", serverGroup.Metadata) |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 169 | } |