github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v1/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", 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.Fprintf(w, `
    21    {
    22      "volumes": [
    23        {
    24          "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
    25          "display_name": "vol-001"
    26        },
    27        {
    28          "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
    29          "display_name": "vol-002"
    30        }
    31      ]
    32    }
    33    `)
    34  	})
    35  }
    36  
    37  func MockGetResponse(t *testing.T) {
    38  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    39  		th.TestMethod(t, r, "GET")
    40  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    41  
    42  		w.Header().Add("Content-Type", "application/json")
    43  		w.WriteHeader(http.StatusOK)
    44  		fmt.Fprintf(w, `
    45  			{
    46  			    "volume": {
    47  			        "id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
    48  			        "display_name": "vol-001",
    49  			        "display_description": "Another volume.",
    50  			        "status": "active",
    51  			        "size": 30,
    52  			        "volume_type": "289da7f8-6440-407c-9fb4-7db01ec49164",
    53  			        "metadata": {
    54  			            "contents": "junk"
    55  			        },
    56  			        "availability_zone": "us-east1",
    57  			        "bootable": "false",
    58  			        "snapshot_id": null,
    59  			        "attachments": [
    60  			            {
    61  			                "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
    62  			                "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
    63  			                "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe",
    64  			                "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
    65  			                "host_name": "mitaka",
    66  			                "device": "/"
    67  			            }
    68  			        ],
    69  			        "created_at": "2012-02-14T20:53:07"
    70  			    }
    71  			}
    72        `)
    73  	})
    74  }
    75  
    76  func MockCreateResponse(t *testing.T) {
    77  	th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
    78  		th.TestMethod(t, r, "POST")
    79  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    80  		th.TestHeader(t, r, "Content-Type", "application/json")
    81  		th.TestHeader(t, r, "Accept", "application/json")
    82  		th.TestJSONRequest(t, r, `
    83  {
    84      "volume": {
    85          "size": 75,
    86          "availability_zone": "us-east1"
    87      }
    88  }
    89        `)
    90  
    91  		w.Header().Add("Content-Type", "application/json")
    92  		w.WriteHeader(http.StatusCreated)
    93  
    94  		fmt.Fprintf(w, `
    95  {
    96      "volume": {
    97          "size": 4,
    98          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
    99      }
   100  }
   101      `)
   102  	})
   103  }
   104  
   105  func MockDeleteResponse(t *testing.T) {
   106  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   107  		th.TestMethod(t, r, "DELETE")
   108  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   109  		w.WriteHeader(http.StatusNoContent)
   110  	})
   111  }
   112  
   113  func MockUpdateResponse(t *testing.T) {
   114  	th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   115  		th.TestMethod(t, r, "PUT")
   116  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   117  		w.WriteHeader(http.StatusOK)
   118  		fmt.Fprintf(w, `
   119      {
   120        "volume": {
   121          "display_name": "vol-002",
   122          "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
   123          }
   124      }
   125      `)
   126  	})
   127  }