github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/volumes/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	th "github.com/gophercloud/gophercloud/testhelper"
     9  	fake "github.com/gophercloud/gophercloud/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": "host-001",
    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      "availability_zone": "nova",
   127      "attachments": [{
   128        "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   129        "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a",
   130        "attached_at": "2016-08-06T14:48:20.000000",
   131        "host_name": "foobar",
   132        "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   133        "device": "/dev/vdc",
   134        "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75"
   135      }],
   136      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   137      "size": 75,
   138      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   139      "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
   140      "os-vol-mig-status-attr:migstat": null,
   141      "metadata": {},
   142      "status": "available",
   143      "volume_image_metadata": {
   144        "container_format": "bare",
   145        "image_name": "centos"
   146      },
   147      "description": null
   148    }
   149  }
   150        `)
   151  	})
   152  }
   153  
   154  func MockCreateResponse(t *testing.T) {
   155  	th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
   156  		th.TestMethod(t, r, "POST")
   157  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   158  		th.TestHeader(t, r, "Content-Type", "application/json")
   159  		th.TestHeader(t, r, "Accept", "application/json")
   160  		th.TestJSONRequest(t, r, `
   161  {
   162      "volume": {
   163      	"name": "vol-001",
   164          "size": 75
   165      }
   166  }
   167        `)
   168  
   169  		w.Header().Add("Content-Type", "application/json")
   170  		w.WriteHeader(http.StatusAccepted)
   171  
   172  		fmt.Fprintf(w, `
   173  {
   174    "volume": {
   175      "size": 75,
   176      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   177      "metadata": {},
   178      "created_at": "2015-09-17T03:32:29.044216",
   179      "encrypted": false,
   180      "bootable": "false",
   181      "availability_zone": "nova",
   182      "attachments": [],
   183      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   184      "status": "creating",
   185      "description": null,
   186      "volume_type": "lvmdriver-1",
   187      "name": "vol-001",
   188      "replication_status": "disabled",
   189      "consistencygroup_id": null,
   190      "source_volid": null,
   191      "snapshot_id": null,
   192      "multiattach": false
   193    }
   194  }
   195      `)
   196  	})
   197  }
   198  
   199  func MockDeleteResponse(t *testing.T) {
   200  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   201  		th.TestMethod(t, r, "DELETE")
   202  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   203  		w.WriteHeader(http.StatusAccepted)
   204  	})
   205  }
   206  
   207  func MockUpdateResponse(t *testing.T) {
   208  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   209  		th.TestMethod(t, r, "PUT")
   210  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   211  		w.WriteHeader(http.StatusOK)
   212  		fmt.Fprintf(w, `
   213  {
   214    "volume": {
   215      "name": "vol-002"
   216    }
   217  }
   218          `)
   219  	})
   220  }
   221  
   222  func MockCreateVolumeFromBackupResponse(t *testing.T) {
   223  	th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
   224  		th.TestMethod(t, r, "POST")
   225  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   226  		th.TestHeader(t, r, "Content-Type", "application/json")
   227  		th.TestHeader(t, r, "Accept", "application/json")
   228  		th.TestJSONRequest(t, r, `
   229  {
   230      "volume": {
   231          "name": "vol-001",
   232          "backup_id": "20c792f0-bb03-434f-b653-06ef238e337e"
   233      }
   234  }
   235  `)
   236  
   237  		w.Header().Add("Content-Type", "application/json")
   238  		w.WriteHeader(http.StatusAccepted)
   239  
   240  		fmt.Fprintf(w, `
   241  {
   242    "volume": {
   243      "size": 30,
   244      "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
   245      "metadata": {},
   246      "created_at": "2015-09-17T03:32:29.044216",
   247      "encrypted": false,
   248      "bootable": "false",
   249      "availability_zone": "nova",
   250      "attachments": [],
   251      "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
   252      "status": "creating",
   253      "description": null,
   254      "volume_type": "lvmdriver-1",
   255      "name": "vol-001",
   256      "replication_status": "disabled",
   257      "consistencygroup_id": null,
   258      "source_volid": null,
   259      "snapshot_id": null,
   260      "backup_id": "20c792f0-bb03-434f-b653-06ef238e337e",
   261      "multiattach": false
   262    }
   263  }`)
   264  	})
   265  }