github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/volumeattach/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  	"github.com/huaweicloud/golangsdk/testhelper/client"
    10  )
    11  
    12  // ListOutput is a sample response to a List call.
    13  const ListOutput = `
    14  {
    15    "volumeAttachments": [
    16      {
    17        "device": "/dev/vdd",
    18        "id": "a26887c6-c47b-4654-abb5-dfadf7d3f803",
    19        "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    20        "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f803"
    21      },
    22      {
    23        "device": "/dev/vdc",
    24        "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
    25        "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    26        "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
    27      }
    28    ]
    29  }
    30  `
    31  
    32  // GetOutput is a sample response to a Get call.
    33  const GetOutput = `
    34  {
    35    "volumeAttachment": {
    36      "device": "/dev/vdc",
    37      "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
    38      "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    39      "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
    40    }
    41  }
    42  `
    43  
    44  // CreateOutput is a sample response to a Create call.
    45  const CreateOutput = `
    46  {
    47    "volumeAttachment": {
    48      "device": "/dev/vdc",
    49      "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
    50      "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    51      "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
    52    }
    53  }
    54  `
    55  
    56  // HandleListSuccessfully configures the test server to respond to a List request.
    57  func HandleListSuccessfully(t *testing.T) {
    58  	th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) {
    59  		th.TestMethod(t, r, "GET")
    60  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    61  
    62  		w.Header().Add("Content-Type", "application/json")
    63  		fmt.Fprintf(w, ListOutput)
    64  	})
    65  }
    66  
    67  // HandleGetSuccessfully configures the test server to respond to a Get request
    68  // for an existing attachment
    69  func HandleGetSuccessfully(t *testing.T) {
    70  	th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) {
    71  		th.TestMethod(t, r, "GET")
    72  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    73  
    74  		w.Header().Add("Content-Type", "application/json")
    75  		fmt.Fprintf(w, GetOutput)
    76  	})
    77  }
    78  
    79  // HandleCreateSuccessfully configures the test server to respond to a Create request
    80  // for a new attachment
    81  func HandleCreateSuccessfully(t *testing.T) {
    82  	th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) {
    83  		th.TestMethod(t, r, "POST")
    84  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    85  		th.TestJSONRequest(t, r, `
    86  {
    87    "volumeAttachment": {
    88      "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
    89      "device": "/dev/vdc"
    90    }
    91  }
    92  `)
    93  
    94  		w.Header().Add("Content-Type", "application/json")
    95  		fmt.Fprintf(w, CreateOutput)
    96  	})
    97  }
    98  
    99  // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
   100  // an existing attachment
   101  func HandleDeleteSuccessfully(t *testing.T) {
   102  	th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) {
   103  		th.TestMethod(t, r, "DELETE")
   104  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   105  
   106  		w.WriteHeader(http.StatusAccepted)
   107  	})
   108  }