Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 1 | // +build acceptance compute servers |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 6 | "fmt" |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 7 | "testing" |
| 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 9 | "github.com/gophercloud/gophercloud" |
| 10 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 11 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/schedulerhints" |
| 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups" |
| 13 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
| 14 | th "github.com/gophercloud/gophercloud/testhelper" |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | func createServerGroup(t *testing.T, computeClient *gophercloud.ServiceClient) (*servergroups.ServerGroup, error) { |
| 18 | sg, err := servergroups.Create(computeClient, &servergroups.CreateOpts{ |
| 19 | Name: "test", |
| 20 | Policies: []string{"affinity"}, |
| 21 | }).Extract() |
| 22 | |
| 23 | if err != nil { |
| 24 | t.Fatalf("Unable to create server group: %v", err) |
| 25 | } |
| 26 | |
| 27 | t.Logf("Created server group: %v", sg.ID) |
| 28 | t.Logf("It has policies: %v", sg.Policies) |
| 29 | |
| 30 | return sg, nil |
| 31 | } |
| 32 | |
| 33 | func getServerGroup(t *testing.T, computeClient *gophercloud.ServiceClient, sgID string) error { |
| 34 | sg, err := servergroups.Get(computeClient, sgID).Extract() |
| 35 | if err != nil { |
| 36 | t.Fatalf("Unable to get server group: %v", err) |
| 37 | } |
| 38 | |
| 39 | t.Logf("Got server group: %v", sg.Name) |
| 40 | |
| 41 | return nil |
| 42 | } |
| 43 | |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 44 | func createServerInGroup(t *testing.T, computeClient *gophercloud.ServiceClient, choices *ComputeChoices, serverGroup *servergroups.ServerGroup) (*servers.Server, error) { |
| 45 | if testing.Short() { |
| 46 | t.Skip("Skipping test that requires server creation in short mode.") |
| 47 | } |
| 48 | |
| 49 | name := tools.RandomString("ACPTTEST", 16) |
| 50 | t.Logf("Attempting to create server: %s\n", name) |
| 51 | |
| 52 | pwd := tools.MakeNewPassword("") |
| 53 | |
| 54 | serverCreateOpts := servers.CreateOpts{ |
| 55 | Name: name, |
| 56 | FlavorRef: choices.FlavorID, |
| 57 | ImageRef: choices.ImageID, |
| 58 | AdminPass: pwd, |
| 59 | } |
| 60 | server, err := servers.Create(computeClient, schedulerhints.CreateOptsExt{ |
| 61 | serverCreateOpts, |
| 62 | schedulerhints.SchedulerHints{ |
| 63 | Group: serverGroup.ID, |
| 64 | }, |
| 65 | }).Extract() |
| 66 | if err != nil { |
| 67 | t.Fatalf("Unable to create server: %v", err) |
| 68 | } |
| 69 | |
| 70 | th.AssertEquals(t, pwd, server.AdminPass) |
| 71 | |
| 72 | return server, err |
| 73 | } |
| 74 | |
| 75 | func verifySchedulerWorked(t *testing.T, firstServer, secondServer *servers.Server) error { |
| 76 | t.Logf("First server hostID: %v", firstServer.HostID) |
| 77 | t.Logf("Second server hostID: %v", secondServer.HostID) |
| 78 | if firstServer.HostID == secondServer.HostID { |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | return fmt.Errorf("%s and %s were not scheduled on the same host.", firstServer.ID, secondServer.ID) |
| 83 | } |
| 84 | |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 85 | func TestServerGroups(t *testing.T) { |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 86 | choices, err := ComputeChoicesFromEnv() |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 91 | computeClient, err := newClient() |
| 92 | if err != nil { |
| 93 | t.Fatalf("Unable to create a compute client: %v", err) |
| 94 | } |
| 95 | |
| 96 | sg, err := createServerGroup(t, computeClient) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Unable to create server group: %v", err) |
| 99 | } |
| 100 | defer func() { |
| 101 | servergroups.Delete(computeClient, sg.ID) |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 102 | t.Logf("Server Group deleted.") |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 103 | }() |
| 104 | |
| 105 | err = getServerGroup(t, computeClient, sg.ID) |
| 106 | if err != nil { |
| 107 | t.Fatalf("Unable to get server group: %v", err) |
| 108 | } |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 109 | |
| 110 | firstServer, err := createServerInGroup(t, computeClient, choices, sg) |
| 111 | if err != nil { |
| 112 | t.Fatalf("Unable to create server: %v", err) |
| 113 | } |
| 114 | defer func() { |
| 115 | servers.Delete(computeClient, firstServer.ID) |
| 116 | t.Logf("Server deleted.") |
| 117 | }() |
| 118 | |
| 119 | if err = waitForStatus(computeClient, firstServer, "ACTIVE"); err != nil { |
| 120 | t.Fatalf("Unable to wait for server: %v", err) |
| 121 | } |
| 122 | |
| 123 | firstServer, err = servers.Get(computeClient, firstServer.ID).Extract() |
| 124 | |
| 125 | secondServer, err := createServerInGroup(t, computeClient, choices, sg) |
| 126 | if err != nil { |
| 127 | t.Fatalf("Unable to create server: %v", err) |
| 128 | } |
| 129 | defer func() { |
| 130 | servers.Delete(computeClient, secondServer.ID) |
| 131 | t.Logf("Server deleted.") |
| 132 | }() |
| 133 | |
| 134 | if err = waitForStatus(computeClient, secondServer, "ACTIVE"); err != nil { |
| 135 | t.Fatalf("Unable to wait for server: %v", err) |
| 136 | } |
| 137 | |
| 138 | secondServer, err = servers.Get(computeClient, secondServer.ID).Extract() |
| 139 | |
| 140 | if err = verifySchedulerWorked(t, firstServer, secondServer); err != nil { |
| 141 | t.Fatalf("Scheduling did not work: %v", err) |
| 142 | } |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 143 | } |