Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 6 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/openstack/db/v1/users" |
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" |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 11 | "github.com/rackspace/gophercloud/testhelper/fixture" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 14 | var ( |
| 15 | instanceID = "{instanceID}" |
| 16 | rootURL = "/instances" |
| 17 | resURL = rootURL + "/" + instanceID |
| 18 | uRootURL = resURL + "/root" |
| 19 | aURL = resURL + "/action" |
| 20 | ) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 21 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 22 | func TestCreate(t *testing.T) { |
| 23 | th.SetupHTTP() |
| 24 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 25 | fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 26 | |
| 27 | opts := CreateOpts{ |
| 28 | Name: "json_rack_instance", |
| 29 | FlavorRef: "1", |
Jamie Hannaford | 85f1033 | 2015-02-12 11:51:37 +0100 | [diff] [blame] | 30 | Databases: db.BatchCreateOpts{ |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 31 | db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"}, |
| 32 | db.CreateOpts{Name: "nextround"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 33 | }, |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 34 | Users: users.BatchCreateOpts{ |
| 35 | users.CreateOpts{ |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 36 | Name: "demouser", |
| 37 | Password: "demopassword", |
Jamie Hannaford | 85f1033 | 2015-02-12 11:51:37 +0100 | [diff] [blame] | 38 | Databases: db.BatchCreateOpts{ |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 39 | db.CreateOpts{Name: "sampledb"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 40 | }, |
| 41 | }, |
| 42 | }, |
| 43 | Size: 2, |
| 44 | } |
| 45 | |
| 46 | instance, err := Create(fake.ServiceClient(), opts).Extract() |
| 47 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 48 | th.AssertNoErr(t, err) |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 49 | th.AssertDeepEquals(t, &expectedInstance, instance) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 50 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 51 | |
| 52 | func TestInstanceList(t *testing.T) { |
| 53 | th.SetupHTTP() |
| 54 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 55 | fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 56 | |
| 57 | pages := 0 |
| 58 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 59 | pages++ |
| 60 | |
| 61 | actual, err := ExtractInstances(page) |
| 62 | if err != nil { |
| 63 | return false, err |
| 64 | } |
| 65 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 66 | th.CheckDeepEquals(t, []Instance{expectedInstance}, actual) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 67 | |
| 68 | return true, nil |
| 69 | }) |
| 70 | |
| 71 | th.AssertNoErr(t, err) |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 72 | th.AssertEquals(t, 1, pages) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 73 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 74 | |
| 75 | func TestGetInstance(t *testing.T) { |
| 76 | th.SetupHTTP() |
| 77 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 78 | fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 79 | |
| 80 | instance, err := Get(fake.ServiceClient(), instanceID).Extract() |
| 81 | |
| 82 | th.AssertNoErr(t, err) |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 83 | th.AssertDeepEquals(t, &expectedInstance, instance) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 84 | } |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 85 | |
| 86 | func TestDeleteInstance(t *testing.T) { |
| 87 | th.SetupHTTP() |
| 88 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 89 | fixture.SetupHandler(t, resURL, "DELETE", "", "", 202) |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 90 | |
| 91 | res := Delete(fake.ServiceClient(), instanceID) |
| 92 | th.AssertNoErr(t, res.Err) |
| 93 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 94 | |
| 95 | func TestEnableRootUser(t *testing.T) { |
| 96 | th.SetupHTTP() |
| 97 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 98 | fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200) |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 99 | |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 100 | expected := &users.User{Name: "root", Password: "secretsecret"} |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 101 | user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 102 | |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 103 | th.AssertNoErr(t, err) |
| 104 | th.AssertDeepEquals(t, expected, user) |
| 105 | } |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 106 | |
| 107 | func TestIsRootEnabled(t *testing.T) { |
| 108 | th.SetupHTTP() |
| 109 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 110 | fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200) |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 111 | |
| 112 | isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID) |
| 113 | |
| 114 | th.AssertNoErr(t, err) |
| 115 | th.AssertEquals(t, true, isEnabled) |
| 116 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 117 | |
| 118 | func TestRestartService(t *testing.T) { |
| 119 | th.SetupHTTP() |
| 120 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 121 | fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 122 | |
| 123 | res := RestartService(fake.ServiceClient(), instanceID) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 124 | th.AssertNoErr(t, res.Err) |
| 125 | } |
| 126 | |
| 127 | func TestResizeInstance(t *testing.T) { |
| 128 | th.SetupHTTP() |
| 129 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 130 | fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 131 | |
| 132 | res := ResizeInstance(fake.ServiceClient(), instanceID, "2") |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 133 | th.AssertNoErr(t, res.Err) |
| 134 | } |
| 135 | |
| 136 | func TestResizeVolume(t *testing.T) { |
| 137 | th.SetupHTTP() |
| 138 | defer th.TeardownHTTP() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame^] | 139 | fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 140 | |
| 141 | res := ResizeVolume(fake.ServiceClient(), instanceID, 4) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 142 | th.AssertNoErr(t, res.Err) |
| 143 | } |