github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v3/volumes/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	th "github.com/huaweicloud/golangsdk/testhelper"
     9  	fake "github.com/huaweicloud/golangsdk/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  		r.ParseForm()
    21  		marker := r.Form.Get("marker")
    22  		switch marker {
    23  		case "":
    24  			fmt.Fprintf(w, `
    25    {
    26    "volumes": [
    27      {
    28        "volume_type": "lvmdriver-1",
    29        "created_at": "2015-09-17T03:35:03.000000",
    30        "bootable": "false",
    31        "name": "vol-001",
    32        "os-vol-mig-status-attr:name_id": null,
    33        "consistencygroup_id": null,
    34        "source_volid": null,
    35        "os-volume-replication:driver_data": null,
    36        "multiattach": false,
    37        "snapshot_id": null,
    38        "replication_status": "disabled",
    39        "os-volume-replication:extended_status": null,
    40        "encrypted": false,
    41        "os-vol-host-attr:host": null,
    42        "availability_zone": "nova",
    43        "attachments": [{
    44          "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a",
    45          "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a",
    46          "attached_at": "2016-08-06T14:48:20.000000",
    47          "host_name": "foobar",
    48          "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
    49          "device": "/dev/vdc",
    50          "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75"
    51        }],
    52        "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
    53        "size": 75,
    54        "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
    55        "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
    56        "os-vol-mig-status-attr:migstat": null,
    57        "metadata": {"foo": "bar"},
    58        "status": "available",
    59        "description": null
    60      },
    61      {
    62        "volume_type": "lvmdriver-1",
    63        "created_at": "2015-09-17T03:32:29.000000",
    64        "bootable": "false",
    65        "name": "vol-002",
    66        "os-vol-mig-status-attr:name_id": null,
    67        "consistencygroup_id": null,
    68        "source_volid": null,
    69        "os-volume-replication:driver_data": null,
    70        "multiattach": false,
    71        "snapshot_id": null,
    72        "replication_status": "disabled",
    73        "os-volume-replication:extended_status": null,
    74        "encrypted": false,
    75        "os-vol-host-attr:host": null,
    76        "availability_zone": "nova",
    77        "attachments": [],
    78        "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
    79        "size": 75,
    80        "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
    81        "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
    82        "os-vol-mig-status-attr:migstat": null,
    83        "metadata": {},
    84        "status": "available",
    85        "description": null
    86      }
    87    ],
    88  	"volumes_links": [
    89  	{
    90  		"href": "%s/volumes/detail?marker=1",
    91  		"rel": "next"
    92  	}]
    93  }
    94    `, th.Server.URL)
    95  		case "1":
    96  			fmt.Fprintf(w, `{"volumes": []}`)
    97  		default:
    98  			t.Fatalf("Unexpected marker: [%s]", marker)
    99  		}
   100  	})
   101  }
   102  
   103  func MockGetResponse(t *testing.T) {
   104  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   105  		th.TestMethod(t, r, "GET")
   106  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   107  
   108  		w.Header().Add("Content-Type", "application/json")
   109  		w.WriteHeader(http.StatusOK)
   110  		fmt.Fprintf(w, `
   111  {
   112    "volume": {
   113      "volume_type": "lvmdriver-1",
   114      "created_at": "2015-09-17T03:32:29.000000",
   115      "bootable": "false",
   116      "name": "vol-001",
   117      "os-vol-mig-status-attr:name_id": null,
   118      "consistencygroup_id": null,
   119      "source_volid": null,
   120      "os-volume-replication:driver_data": null,
   121      "multiattach": false,
   122      "snapshot_id": null,
   123      "replication_status": "disabled",
   124      "os-volume-replication:extended_status": null,
   125      "encrypted": false,
   126      "os-vol-host-attr:host": null,
   127      "availability_zone": "nova",
   128      "attachments": [{
   129        "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   130        "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a",
   131        "attached_at": "2016-08-06T14:48:20.000000",
   132        "host_name": "foobar",
   133        "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   134        "device": "/dev/vdc",
   135        "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75"
   136      }],
   137      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   138      "size": 75,
   139      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   140      "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
   141      "os-vol-mig-status-attr:migstat": null,
   142      "metadata": {},
   143      "status": "available",
   144      "description": null
   145    }
   146  }
   147        `)
   148  	})
   149  }
   150  
   151  func MockCreateResponse(t *testing.T) {
   152  	th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
   153  		th.TestMethod(t, r, "POST")
   154  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   155  		th.TestHeader(t, r, "Content-Type", "application/json")
   156  		th.TestHeader(t, r, "Accept", "application/json")
   157  		th.TestJSONRequest(t, r, `
   158  {
   159      "volume": {
   160      	"name": "vol-001",
   161          "size": 75
   162      }
   163  }
   164        `)
   165  
   166  		w.Header().Add("Content-Type", "application/json")
   167  		w.WriteHeader(http.StatusAccepted)
   168  
   169  		fmt.Fprintf(w, `
   170  {
   171    "volume": {
   172      "size": 75,
   173      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   174      "metadata": {},
   175      "created_at": "2015-09-17T03:32:29.044216",
   176      "encrypted": false,
   177      "bootable": "false",
   178      "availability_zone": "nova",
   179      "attachments": [],
   180      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   181      "status": "creating",
   182      "description": null,
   183      "volume_type": "lvmdriver-1",
   184      "name": "vol-001",
   185      "replication_status": "disabled",
   186      "consistencygroup_id": null,
   187      "source_volid": null,
   188      "snapshot_id": null,
   189      "multiattach": false
   190    }
   191  }
   192      `)
   193  	})
   194  }
   195  
   196  func MockDeleteResponse(t *testing.T) {
   197  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   198  		th.TestMethod(t, r, "DELETE")
   199  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   200  		w.WriteHeader(http.StatusAccepted)
   201  	})
   202  }
   203  
   204  func MockUpdateResponse(t *testing.T) {
   205  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   206  		th.TestMethod(t, r, "PUT")
   207  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   208  		w.WriteHeader(http.StatusOK)
   209  		fmt.Fprintf(w, `
   210  {
   211    "volume": {
   212      "name": "vol-002"
   213    }
   214  }
   215          `)
   216  	})
   217  }