github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/db/v1/instances/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/huaweicloud/golangsdk" 9 "github.com/huaweicloud/golangsdk/openstack/db/v1/datastores" 10 "github.com/huaweicloud/golangsdk/openstack/db/v1/instances" 11 "github.com/huaweicloud/golangsdk/testhelper/fixture" 12 ) 13 14 var ( 15 timestamp = "2015-11-12T14:22:42" 16 timeVal, _ = time.Parse(golangsdk.RFC3339NoZ, timestamp) 17 ) 18 19 var instance = ` 20 { 21 "created": "` + timestamp + `", 22 "datastore": { 23 "type": "mysql", 24 "version": "5.6" 25 }, 26 "flavor": { 27 "id": "1", 28 "links": [ 29 { 30 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 31 "rel": "self" 32 }, 33 { 34 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 35 "rel": "bookmark" 36 } 37 ] 38 }, 39 "links": [ 40 { 41 "href": "https://openstack.example.com/v1.0/1234/instances/1", 42 "rel": "self" 43 } 44 ], 45 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 46 "id": "{instanceID}", 47 "name": "json_rack_instance", 48 "status": "BUILD", 49 "updated": "` + timestamp + `", 50 "volume": { 51 "size": 2 52 } 53 } 54 ` 55 56 var createReq = ` 57 { 58 "instance": { 59 "databases": [ 60 { 61 "character_set": "utf8", 62 "collate": "utf8_general_ci", 63 "name": "sampledb" 64 }, 65 { 66 "name": "nextround" 67 } 68 ], 69 "flavorRef": "1", 70 "name": "json_rack_instance", 71 "users": [ 72 { 73 "databases": [ 74 { 75 "name": "sampledb" 76 } 77 ], 78 "name": "demouser", 79 "password": "demopassword" 80 } 81 ], 82 "volume": { 83 "size": 2 84 } 85 } 86 } 87 ` 88 89 var instanceWithFault = ` 90 { 91 "created": "` + timestamp + `", 92 "datastore": { 93 "type": "mysql", 94 "version": "5.6" 95 }, 96 "flavor": { 97 "id": "1", 98 "links": [ 99 { 100 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 101 "rel": "self" 102 }, 103 { 104 "href": "https://openstack.example.com/v1.0/1234/flavors/1", 105 "rel": "bookmark" 106 } 107 ] 108 }, 109 "links": [ 110 { 111 "href": "https://openstack.example.com/v1.0/1234/instances/1", 112 "rel": "self" 113 } 114 ], 115 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 116 "id": "{instanceID}", 117 "name": "json_rack_instance", 118 "status": "BUILD", 119 "updated": "` + timestamp + `", 120 "volume": { 121 "size": 2 122 }, 123 "fault": { 124 "message": "some error message", 125 "created": "` + timestamp + `", 126 "details": "some details about the error" 127 } 128 } 129 ` 130 131 var ( 132 instanceID = "{instanceID}" 133 configGroupID = "00000000-0000-0000-0000-000000000000" 134 rootURL = "/instances" 135 resURL = rootURL + "/" + instanceID 136 uRootURL = resURL + "/root" 137 aURL = resURL + "/action" 138 ) 139 140 var ( 141 restartReq = `{"restart": {}}` 142 resizeReq = `{"resize": {"flavorRef": "2"}}` 143 resizeVolReq = `{"resize": {"volume": {"size": 4}}}` 144 attachConfigurationGroupReq = `{"instance": {"configuration": "00000000-0000-0000-0000-000000000000"}}` 145 detachConfigurationGroupReq = `{"instance": {}}` 146 ) 147 148 var ( 149 createResp = fmt.Sprintf(`{"instance": %s}`, instance) 150 createWithFaultResp = fmt.Sprintf(`{"instance": %s}`, instanceWithFault) 151 listInstancesResp = fmt.Sprintf(`{"instances":[%s]}`, instance) 152 getInstanceResp = createResp 153 enableUserResp = `{"user":{"name":"root","password":"secretsecret"}}` 154 isUserEnabledResp = `{"rootEnabled":true}` 155 ) 156 157 var expectedInstance = instances.Instance{ 158 Created: timeVal, 159 Updated: timeVal, 160 Flavor: instances.Flavor{ 161 ID: "1", 162 Links: []golangsdk.Link{ 163 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, 164 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"}, 165 }, 166 }, 167 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 168 ID: instanceID, 169 Links: []golangsdk.Link{ 170 {Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"}, 171 }, 172 Name: "json_rack_instance", 173 Status: "BUILD", 174 Volume: instances.Volume{Size: 2}, 175 Datastore: datastores.DatastorePartial{ 176 Type: "mysql", 177 Version: "5.6", 178 }, 179 } 180 181 var expectedInstanceWithFault = instances.Instance{ 182 Created: timeVal, 183 Updated: timeVal, 184 Flavor: instances.Flavor{ 185 ID: "1", 186 Links: []golangsdk.Link{ 187 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, 188 {Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"}, 189 }, 190 }, 191 Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com", 192 ID: instanceID, 193 Links: []golangsdk.Link{ 194 {Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"}, 195 }, 196 Name: "json_rack_instance", 197 Status: "BUILD", 198 Volume: instances.Volume{Size: 2}, 199 Datastore: datastores.DatastorePartial{ 200 Type: "mysql", 201 Version: "5.6", 202 }, 203 Fault: &instances.Fault{ 204 Created: timeVal, 205 Message: "some error message", 206 Details: "some details about the error", 207 }, 208 } 209 210 func HandleCreate(t *testing.T) { 211 fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200) 212 } 213 214 func HandleCreateWithFault(t *testing.T) { 215 fixture.SetupHandler(t, rootURL, "POST", createReq, createWithFaultResp, 200) 216 } 217 218 func HandleList(t *testing.T) { 219 fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200) 220 } 221 222 func HandleGet(t *testing.T) { 223 fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200) 224 } 225 226 func HandleDelete(t *testing.T) { 227 fixture.SetupHandler(t, resURL, "DELETE", "", "", 202) 228 } 229 230 func HandleEnableRoot(t *testing.T) { 231 fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200) 232 } 233 234 func HandleIsRootEnabled(t *testing.T) { 235 fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200) 236 } 237 238 func HandleRestart(t *testing.T) { 239 fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202) 240 } 241 242 func HandleResize(t *testing.T) { 243 fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202) 244 } 245 246 func HandleResizeVol(t *testing.T) { 247 fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202) 248 } 249 250 func HandleAttachConfigurationGroup(t *testing.T) { 251 fixture.SetupHandler(t, resURL, "PUT", attachConfigurationGroupReq, "", 202) 252 } 253 254 func HandleDetachConfigurationGroup(t *testing.T) { 255 fixture.SetupHandler(t, resURL, "PUT", detachConfigurationGroupReq, "", 202) 256 }