Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame^] | 7 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 13 | var instanceID = "d4603f69-ec7e-4e9b-803f-600b9205576f" |
| 14 | |
| 15 | var expectedInstance = &Instance{ |
| 16 | Created: "2014-02-13T21:47:13", |
| 17 | Updated: "2014-02-13T21:47:13", |
| 18 | Flavor: Flavor{ |
| 19 | ID: "1", |
| 20 | Links: []gophercloud.Link{ |
| 21 | gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"}, |
| 22 | gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"}, |
| 23 | }, |
| 24 | }, |
| 25 | Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com", |
| 26 | ID: instanceID, |
| 27 | Links: []gophercloud.Link{ |
| 28 | gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"}, |
| 29 | }, |
| 30 | Name: "json_rack_instance", |
| 31 | Status: "BUILD", |
| 32 | Volume: Volume{Size: 2}, |
| 33 | } |
| 34 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 35 | func TestCreate(t *testing.T) { |
| 36 | th.SetupHTTP() |
| 37 | defer th.TeardownHTTP() |
| 38 | |
| 39 | HandleCreateInstanceSuccessfully(t) |
| 40 | |
| 41 | opts := CreateOpts{ |
| 42 | Name: "json_rack_instance", |
| 43 | FlavorRef: "1", |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame^] | 44 | Databases: []db.CreateOpts{ |
| 45 | db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"}, |
| 46 | db.CreateOpts{Name: "nextround"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 47 | }, |
| 48 | Users: UsersOpts{ |
| 49 | UserOpts{ |
| 50 | Name: "demouser", |
| 51 | Password: "demopassword", |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame^] | 52 | Databases: []db.CreateOpts{ |
| 53 | db.CreateOpts{Name: "sampledb"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 54 | }, |
| 55 | }, |
| 56 | }, |
| 57 | Size: 2, |
| 58 | } |
| 59 | |
| 60 | instance, err := Create(fake.ServiceClient(), opts).Extract() |
| 61 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 62 | th.AssertNoErr(t, err) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 63 | th.AssertDeepEquals(t, expectedInstance, instance) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 64 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 65 | |
| 66 | func TestInstanceList(t *testing.T) { |
| 67 | th.SetupHTTP() |
| 68 | defer th.TeardownHTTP() |
| 69 | |
| 70 | HandleListInstanceSuccessfully(t) |
| 71 | |
| 72 | expectedInstance := Instance{ |
| 73 | Flavor: Flavor{ |
| 74 | ID: "1", |
| 75 | Links: []gophercloud.Link{ |
| 76 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, |
| 77 | gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"}, |
| 78 | }, |
| 79 | }, |
| 80 | ID: "8fb081af-f237-44f5-80cc-b46be1840ca9", |
| 81 | Links: []gophercloud.Link{ |
| 82 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", Rel: "self"}, |
| 83 | }, |
| 84 | Name: "xml_rack_instance", |
| 85 | Status: "ACTIVE", |
| 86 | Volume: Volume{Size: 2}, |
| 87 | } |
| 88 | |
| 89 | pages := 0 |
| 90 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 91 | pages++ |
| 92 | |
| 93 | actual, err := ExtractInstances(page) |
| 94 | if err != nil { |
| 95 | return false, err |
| 96 | } |
| 97 | |
| 98 | if len(actual) != 1 { |
| 99 | t.Fatalf("Expected 1 DB instance, got %d", len(actual)) |
| 100 | } |
| 101 | th.CheckDeepEquals(t, expectedInstance, actual[0]) |
| 102 | |
| 103 | return true, nil |
| 104 | }) |
| 105 | |
| 106 | th.AssertNoErr(t, err) |
| 107 | |
| 108 | if pages != 1 { |
| 109 | t.Errorf("Expected 1 page, saw %d", pages) |
| 110 | } |
| 111 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 112 | |
| 113 | func TestGetInstance(t *testing.T) { |
| 114 | th.SetupHTTP() |
| 115 | defer th.TeardownHTTP() |
| 116 | |
| 117 | HandleGetInstanceSuccessfully(t, instanceID) |
| 118 | |
| 119 | instance, err := Get(fake.ServiceClient(), instanceID).Extract() |
| 120 | |
| 121 | th.AssertNoErr(t, err) |
| 122 | th.AssertDeepEquals(t, instance, expectedInstance) |
| 123 | } |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 124 | |
| 125 | func TestDeleteInstance(t *testing.T) { |
| 126 | th.SetupHTTP() |
| 127 | defer th.TeardownHTTP() |
| 128 | |
| 129 | HandleDeleteInstanceSuccessfully(t, instanceID) |
| 130 | |
| 131 | res := Delete(fake.ServiceClient(), instanceID) |
| 132 | th.AssertNoErr(t, res.Err) |
| 133 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 134 | |
| 135 | func TestEnableRootUser(t *testing.T) { |
| 136 | th.SetupHTTP() |
| 137 | defer th.TeardownHTTP() |
| 138 | |
| 139 | HandleEnableRootUserSuccessfully(t, instanceID) |
| 140 | |
| 141 | expected := &User{Name: "root", Password: "secretsecret"} |
| 142 | |
| 143 | user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract() |
| 144 | th.AssertNoErr(t, err) |
| 145 | th.AssertDeepEquals(t, expected, user) |
| 146 | } |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 147 | |
| 148 | func TestIsRootEnabled(t *testing.T) { |
| 149 | th.SetupHTTP() |
| 150 | defer th.TeardownHTTP() |
| 151 | |
| 152 | HandleIsRootEnabledSuccessfully(t, instanceID) |
| 153 | |
| 154 | isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID) |
| 155 | |
| 156 | th.AssertNoErr(t, err) |
| 157 | th.AssertEquals(t, true, isEnabled) |
| 158 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 159 | |
| 160 | func TestRestartService(t *testing.T) { |
| 161 | th.SetupHTTP() |
| 162 | defer th.TeardownHTTP() |
| 163 | |
| 164 | HandleRestartSuccessfully(t, instanceID) |
| 165 | |
| 166 | res := RestartService(fake.ServiceClient(), instanceID) |
| 167 | |
| 168 | th.AssertNoErr(t, res.Err) |
| 169 | } |
| 170 | |
| 171 | func TestResizeInstance(t *testing.T) { |
| 172 | th.SetupHTTP() |
| 173 | defer th.TeardownHTTP() |
| 174 | |
| 175 | HandleResizeInstanceSuccessfully(t, instanceID) |
| 176 | |
| 177 | res := ResizeInstance(fake.ServiceClient(), instanceID, "2") |
| 178 | |
| 179 | th.AssertNoErr(t, res.Err) |
| 180 | } |
| 181 | |
| 182 | func TestResizeVolume(t *testing.T) { |
| 183 | th.SetupHTTP() |
| 184 | defer th.TeardownHTTP() |
| 185 | |
| 186 | HandleResizeVolSuccessfully(t, instanceID) |
| 187 | |
| 188 | res := ResizeVolume(fake.ServiceClient(), instanceID, 4) |
| 189 | |
| 190 | th.AssertNoErr(t, res.Err) |
| 191 | } |