github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v2/volumes/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
     9  	fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    10  )
    11  
    12  func MockListResponse(t *testing.T) {
    13  	th.Mux.HandleFunc("/volumes/detail", func(w http.ResponseWriter, r *http.Request) {
    14  		th.TestMethod(t, r, "GET")
    15  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    16  
    17  		w.Header().Add("Content-Type", "application/json")
    18  		w.WriteHeader(http.StatusOK)
    19  
    20  		fmt.Fprint(w, `
    21    {
    22    "volumes": [
    23      {
    24        "volume_type": "lvmdriver-1",
    25        "created_at": "2015-09-17T03:35:03.000000",
    26        "bootable": "false",
    27        "name": "vol-001",
    28        "os-vol-mig-status-attr:name_id": null,
    29        "consistencygroup_id": null,
    30        "source_volid": null,
    31        "os-volume-replication:driver_data": null,
    32        "multiattach": false,
    33        "snapshot_id": null,
    34        "replication_status": "disabled",
    35        "os-volume-replication:extended_status": null,
    36        "encrypted": false,
    37        "os-vol-host-attr:host": null,
    38        "availability_zone": "nova",
    39        "attachments": [{
    40          "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a",
    41          "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a",
    42          "attached_at": "2016-08-06T14:48:20.000000",
    43          "host_name": "foobar",
    44          "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
    45          "device": "/dev/vdc",
    46          "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75"
    47        }],
    48        "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
    49        "size": 75,
    50        "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
    51        "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
    52        "os-vol-mig-status-attr:migstat": null,
    53        "metadata": {"foo": "bar"},
    54        "status": "available",
    55        "description": null
    56      },
    57      {
    58        "volume_type": "lvmdriver-1",
    59        "created_at": "2015-09-17T03:32:29.000000",
    60        "bootable": "false",
    61        "name": "vol-002",
    62        "os-vol-mig-status-attr:name_id": null,
    63        "consistencygroup_id": null,
    64        "source_volid": null,
    65        "os-volume-replication:driver_data": null,
    66        "multiattach": false,
    67        "snapshot_id": null,
    68        "replication_status": "disabled",
    69        "os-volume-replication:extended_status": null,
    70        "encrypted": false,
    71        "os-vol-host-attr:host": null,
    72        "availability_zone": "nova",
    73        "attachments": [],
    74        "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
    75        "size": 75,
    76        "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
    77        "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
    78        "os-vol-mig-status-attr:migstat": null,
    79        "metadata": {},
    80        "status": "available",
    81        "description": null
    82      }
    83    ]
    84  }
    85    `)
    86  	})
    87  }
    88  
    89  func MockGetResponse(t *testing.T) {
    90  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    91  		th.TestMethod(t, r, "GET")
    92  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    93  
    94  		w.Header().Add("Content-Type", "application/json")
    95  		w.WriteHeader(http.StatusOK)
    96  		fmt.Fprint(w, `
    97  {
    98    "volume": {
    99      "volume_type": "lvmdriver-1",
   100      "created_at": "2015-09-17T03:32:29.000000",
   101      "bootable": "false",
   102      "name": "vol-001",
   103      "os-vol-mig-status-attr:name_id": null,
   104      "consistencygroup_id": null,
   105      "source_volid": null,
   106      "os-volume-replication:driver_data": null,
   107      "multiattach": false,
   108      "snapshot_id": null,
   109      "replication_status": "disabled",
   110      "os-volume-replication:extended_status": null,
   111      "encrypted": false,
   112      "os-vol-host-attr:host": null,
   113      "availability_zone": "nova",
   114      "attachments": [{
   115        "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   116        "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a",
   117        "attached_at": "2016-08-06T14:48:20.000000",
   118        "host_name": "foobar",
   119        "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   120        "device": "/dev/vdc",
   121        "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75"
   122      }],
   123      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   124      "size": 75,
   125      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   126      "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
   127      "os-vol-mig-status-attr:migstat": null,
   128      "metadata": {},
   129      "status": "available",
   130      "description": null
   131    }
   132  }
   133        `)
   134  	})
   135  }
   136  
   137  func MockCreateResponse(t *testing.T) {
   138  	th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
   139  		th.TestMethod(t, r, "POST")
   140  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   141  		th.TestHeader(t, r, "Content-Type", "application/json")
   142  		th.TestHeader(t, r, "Accept", "application/json")
   143  		th.TestJSONRequest(t, r, `
   144  {
   145      "volume": {
   146      	"name": "vol-001",
   147          "size": 75
   148      }
   149  }
   150        `)
   151  
   152  		w.Header().Add("Content-Type", "application/json")
   153  		w.WriteHeader(http.StatusAccepted)
   154  
   155  		fmt.Fprint(w, `
   156  {
   157    "volume": {
   158      "size": 75,
   159      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   160      "metadata": {},
   161      "created_at": "2015-09-17T03:32:29.044216",
   162      "encrypted": false,
   163      "bootable": "false",
   164      "availability_zone": "nova",
   165      "attachments": [],
   166      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   167      "status": "creating",
   168      "description": null,
   169      "volume_type": "lvmdriver-1",
   170      "name": "vol-001",
   171      "replication_status": "disabled",
   172      "consistencygroup_id": null,
   173      "source_volid": null,
   174      "snapshot_id": null,
   175      "multiattach": false
   176    }
   177  }
   178      `)
   179  	})
   180  }
   181  
   182  func MockDeleteResponse(t *testing.T) {
   183  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   184  		th.TestMethod(t, r, "DELETE")
   185  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   186  		w.WriteHeader(http.StatusAccepted)
   187  	})
   188  }
   189  
   190  func MockUpdateResponse(t *testing.T) {
   191  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   192  		th.TestMethod(t, r, "PUT")
   193  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   194  		w.WriteHeader(http.StatusOK)
   195  		fmt.Fprint(w, `
   196  {
   197    "volume": {
   198      "name": "vol-002"
   199    }
   200  }
   201          `)
   202  	})
   203  }
   204  
   205  func MockAttachResponse(t *testing.T) {
   206  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   207  		func(w http.ResponseWriter, r *http.Request) {
   208  			th.TestMethod(t, r, "POST")
   209  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   210  			th.TestHeader(t, r, "Content-Type", "application/json")
   211  			th.TestHeader(t, r, "Accept", "application/json")
   212  			th.TestJSONRequest(t, r, `
   213  {
   214      "os-attach":
   215      {
   216          "mountpoint": "/mnt",
   217          "mode": "rw",
   218          "instance_uuid": "50902f4f-a974-46a0-85e9-7efc5e22dfdd"
   219      }
   220  }
   221            `)
   222  
   223  			w.Header().Add("Content-Type", "application/json")
   224  			w.WriteHeader(http.StatusAccepted)
   225  
   226  			fmt.Fprint(w, `{}`)
   227  		})
   228  }
   229  
   230  func MockBeginDetachingResponse(t *testing.T) {
   231  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   232  		func(w http.ResponseWriter, r *http.Request) {
   233  			th.TestMethod(t, r, "POST")
   234  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   235  			th.TestHeader(t, r, "Content-Type", "application/json")
   236  			th.TestHeader(t, r, "Accept", "application/json")
   237  			th.TestJSONRequest(t, r, `
   238  {
   239      "os-begin_detaching": {}
   240  }
   241            `)
   242  
   243  			w.Header().Add("Content-Type", "application/json")
   244  			w.WriteHeader(http.StatusAccepted)
   245  
   246  			fmt.Fprint(w, `{}`)
   247  		})
   248  }
   249  
   250  func MockDetachResponse(t *testing.T) {
   251  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   252  		func(w http.ResponseWriter, r *http.Request) {
   253  			th.TestMethod(t, r, "POST")
   254  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   255  			th.TestHeader(t, r, "Content-Type", "application/json")
   256  			th.TestHeader(t, r, "Accept", "application/json")
   257  			th.TestJSONRequest(t, r, `
   258  {
   259      "os-detach": {}
   260  }
   261            `)
   262  
   263  			w.Header().Add("Content-Type", "application/json")
   264  			w.WriteHeader(http.StatusAccepted)
   265  
   266  			fmt.Fprint(w, `{}`)
   267  		})
   268  }
   269  
   270  func MockUploadImageResponse(t *testing.T) {
   271  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   272  		func(w http.ResponseWriter, r *http.Request) {
   273  			th.TestMethod(t, r, "POST")
   274  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   275  			th.TestHeader(t, r, "Content-Type", "application/json")
   276  			th.TestHeader(t, r, "Accept", "application/json")
   277  			th.TestJSONRequest(t, r, `
   278  {
   279      "os-volume_upload_image": {
   280          "container_format": "bare",
   281          "force": true,
   282          "image_name": "test",
   283          "disk_format": "raw"
   284      }
   285  }
   286            `)
   287  
   288  			w.Header().Add("Content-Type", "application/json")
   289  			w.WriteHeader(http.StatusAccepted)
   290  
   291  			fmt.Fprint(w, `
   292  {
   293      "os-volume_upload_image": {
   294          "container_format": "bare",
   295          "display_description": null,
   296          "id": "cd281d77-8217-4830-be95-9528227c105c",
   297          "image_id": "ecb92d98-de08-45db-8235-bbafe317269c",
   298          "image_name": "test",
   299          "disk_format": "raw",
   300          "size": 5,
   301          "status": "uploading",
   302          "updated_at": "2017-07-17T09:29:22.000000",
   303          "volume_type": {
   304              "created_at": "2016-05-04T08:54:14.000000",
   305              "deleted": false,
   306              "deleted_at": null,
   307              "description": null,
   308              "extra_specs": {
   309                  "volume_backend_name": "basic.ru-2a"
   310              },
   311              "id": "b7133444-62f6-4433-8da3-70ac332229b7",
   312              "is_public": true,
   313              "name": "basic.ru-2a",
   314              "updated_at": "2016-05-04T09:15:33.000000"
   315          }
   316      }
   317  }
   318            `)
   319  		})
   320  }
   321  
   322  func MockReserveResponse(t *testing.T) {
   323  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   324  		func(w http.ResponseWriter, r *http.Request) {
   325  			th.TestMethod(t, r, "POST")
   326  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   327  			th.TestHeader(t, r, "Content-Type", "application/json")
   328  			th.TestHeader(t, r, "Accept", "application/json")
   329  			th.TestJSONRequest(t, r, `
   330  {
   331      "os-reserve": {}
   332  }
   333            `)
   334  
   335  			w.Header().Add("Content-Type", "application/json")
   336  			w.WriteHeader(http.StatusAccepted)
   337  
   338  			fmt.Fprint(w, `{}`)
   339  		})
   340  }
   341  
   342  func MockUnreserveResponse(t *testing.T) {
   343  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   344  		func(w http.ResponseWriter, r *http.Request) {
   345  			th.TestMethod(t, r, "POST")
   346  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   347  			th.TestHeader(t, r, "Content-Type", "application/json")
   348  			th.TestHeader(t, r, "Accept", "application/json")
   349  			th.TestJSONRequest(t, r, `
   350  {
   351      "os-unreserve": {}
   352  }
   353            `)
   354  
   355  			w.Header().Add("Content-Type", "application/json")
   356  			w.WriteHeader(http.StatusAccepted)
   357  
   358  			fmt.Fprint(w, `{}`)
   359  		})
   360  }
   361  
   362  func MockInitializeConnectionResponse(t *testing.T) {
   363  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   364  		func(w http.ResponseWriter, r *http.Request) {
   365  			th.TestMethod(t, r, "POST")
   366  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   367  			th.TestHeader(t, r, "Content-Type", "application/json")
   368  			th.TestHeader(t, r, "Accept", "application/json")
   369  			th.TestJSONRequest(t, r, `
   370  {
   371      "os-initialize_connection":
   372      {
   373          "connector":
   374          {
   375          "ip":"127.0.0.1",
   376          "host":"stack",
   377          "initiator":"iqn.1994-05.com.redhat:17cf566367d2",
   378          "multipath": false,
   379          "platform": "x86_64",
   380          "os_type": "linux2"
   381          }
   382      }
   383  }
   384            `)
   385  
   386  			w.Header().Add("Content-Type", "application/json")
   387  			w.WriteHeader(http.StatusAccepted)
   388  
   389  			fmt.Fprint(w, `{
   390  "connection_info": {
   391      "data": {
   392        "target_portals": [
   393          "172.31.17.48:3260"
   394        ],
   395        "auth_method": "CHAP",
   396        "auth_username": "5MLtcsTEmNN5jFVcT6ui",
   397        "access_mode": "rw",
   398        "target_lun": 0,
   399        "volume_id": "cd281d77-8217-4830-be95-9528227c105c",
   400        "target_luns": [
   401          0
   402        ],
   403        "target_iqns": [
   404          "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c"
   405        ],
   406        "auth_password": "x854ZY5Re3aCkdNL",
   407        "target_discovered": false,
   408        "encrypted": false,
   409        "qos_specs": null,
   410        "target_iqn": "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c",
   411        "target_portal": "172.31.17.48:3260"
   412      },
   413      "driver_volume_type": "iscsi"
   414    }
   415              }`)
   416  		})
   417  }
   418  
   419  func MockTerminateConnectionResponse(t *testing.T) {
   420  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   421  		func(w http.ResponseWriter, r *http.Request) {
   422  			th.TestMethod(t, r, "POST")
   423  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   424  			th.TestHeader(t, r, "Content-Type", "application/json")
   425  			th.TestHeader(t, r, "Accept", "application/json")
   426  			th.TestJSONRequest(t, r, `
   427  {
   428      "os-terminate_connection":
   429      {
   430          "connector":
   431          {
   432          "ip":"127.0.0.1",
   433          "host":"stack",
   434          "initiator":"iqn.1994-05.com.redhat:17cf566367d2",
   435          "multipath": true,
   436          "platform": "x86_64",
   437          "os_type": "linux2"
   438          }
   439      }
   440  }
   441            `)
   442  
   443  			w.Header().Add("Content-Type", "application/json")
   444  			w.WriteHeader(http.StatusAccepted)
   445  
   446  			fmt.Fprint(w, `{}`)
   447  		})
   448  }
   449  
   450  func MockExtendSizeResponse(t *testing.T) {
   451  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   452  		func(w http.ResponseWriter, r *http.Request) {
   453  			th.TestMethod(t, r, "POST")
   454  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   455  			th.TestHeader(t, r, "Content-Type", "application/json")
   456  			th.TestHeader(t, r, "Accept", "application/json")
   457  			th.TestJSONRequest(t, r, `
   458  {
   459      "os-extend":
   460      {
   461          "new_size": 3
   462      }
   463  }
   464            `)
   465  
   466  			w.Header().Add("Content-Type", "application/json")
   467  			w.WriteHeader(http.StatusAccepted)
   468  
   469  			fmt.Fprint(w, `{}`)
   470  		})
   471  }
   472  
   473  func MockForceDeleteResponse(t *testing.T) {
   474  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) {
   475  		th.TestMethod(t, r, "POST")
   476  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   477  		th.TestBody(t, r, `{"os-force_delete":""}`)
   478  		w.WriteHeader(http.StatusAccepted)
   479  	})
   480  }
   481  
   482  func MockSetImageMetadataResponse(t *testing.T) {
   483  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) {
   484  		th.TestMethod(t, r, "POST")
   485  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   486  		th.TestHeader(t, r, "Content-Type", "application/json")
   487  		th.TestHeader(t, r, "Accept", "application/json")
   488  		th.TestJSONRequest(t, r, `
   489  {
   490  	"os-set_image_metadata": {
   491  		"metadata": {
   492  			"label": "test"
   493  		}
   494  	}
   495  }
   496  		`)
   497  		w.Header().Add("Content-Type", "application/json")
   498  		w.WriteHeader(http.StatusOK)
   499  
   500  		fmt.Fprint(w, `{}`)
   501  	})
   502  }
   503  
   504  func MockSetBootableResponse(t *testing.T) {
   505  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) {
   506  		th.TestMethod(t, r, "POST")
   507  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   508  		th.TestHeader(t, r, "Content-Type", "application/json")
   509  		th.TestHeader(t, r, "Accept", "application/json")
   510  		th.TestJSONRequest(t, r, `
   511  {
   512  	"os-set_bootable": {
   513  		"bootable": true
   514  	}
   515  }
   516  		`)
   517  		w.Header().Add("Content-Type", "application/json")
   518  		w.Header().Add("Content-Length", "0")
   519  		w.WriteHeader(http.StatusOK)
   520  	})
   521  }
   522  
   523  func MockReImageResponse(t *testing.T) {
   524  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) {
   525  		th.TestMethod(t, r, "POST")
   526  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   527  		th.TestHeader(t, r, "Content-Type", "application/json")
   528  		th.TestHeader(t, r, "Accept", "application/json")
   529  		th.TestJSONRequest(t, r, `
   530  {
   531  	"os-reimage": {
   532  		"image_id": "71543ced-a8af-45b6-a5c4-a46282108a90",
   533  		"reimage_reserved": false
   534  	}
   535  }
   536  		`)
   537  		w.Header().Add("Content-Type", "application/json")
   538  		w.Header().Add("Content-Length", "0")
   539  		w.WriteHeader(http.StatusAccepted)
   540  	})
   541  }
   542  
   543  func MockChangeTypeResponse(t *testing.T) {
   544  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   545  		func(w http.ResponseWriter, r *http.Request) {
   546  			th.TestMethod(t, r, "POST")
   547  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   548  			th.TestHeader(t, r, "Content-Type", "application/json")
   549  			th.TestHeader(t, r, "Accept", "application/json")
   550  			th.TestJSONRequest(t, r, `
   551  {
   552      "os-retype":
   553      {
   554  		"new_type": "ssd",
   555  		"migration_policy": "on-demand"
   556      }
   557  }
   558            `)
   559  
   560  			w.Header().Add("Content-Type", "application/json")
   561  			w.WriteHeader(http.StatusAccepted)
   562  
   563  			fmt.Fprint(w, `{}`)
   564  		})
   565  }
   566  
   567  func MockResetStatusResponse(t *testing.T) {
   568  	th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
   569  		func(w http.ResponseWriter, r *http.Request) {
   570  			th.TestMethod(t, r, "POST")
   571  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   572  			th.TestHeader(t, r, "Content-Type", "application/json")
   573  			th.TestJSONRequest(t, r, `
   574  {
   575      "os-reset_status":
   576      {
   577  		"status": "error",
   578  		"attach_status": "detached",
   579  		"migration_status": "migrating"
   580      }
   581  }
   582            `)
   583  
   584  			w.WriteHeader(http.StatusAccepted)
   585  		})
   586  }