github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/v1/ports/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud/openstack/baremetal/v1/ports"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  	"github.com/gophercloud/gophercloud/testhelper/client"
    12  )
    13  
    14  // PortListBody contains the canned body of a ports.List response, without detail.
    15  const PortListBody = `
    16  {
    17     "ports": [
    18         {
    19             "uuid": "3abe3f36-9708-4e9f-b07e-0f898061d3a7",
    20             "links": [
    21                 {
    22                     "href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
    23                     "rel": "self"
    24                 },
    25                 {
    26                     "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
    27                     "rel": "bookmark"
    28                 }
    29             ],
    30             "address": "52:54:00:0a:af:d1"
    31         },
    32         {
    33             "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a",
    34             "links": [
    35                 {
    36                     "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
    37                     "rel": "self"
    38                 },
    39                 {
    40                     "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
    41                     "rel": "bookmark"
    42                 }
    43             ],
    44             "address": "52:54:00:4d:87:e6"
    45         }
    46     ]
    47  }
    48  `
    49  
    50  // PortListDetailBody contains the canned body of a port.ListDetail response.
    51  const PortListDetailBody = `
    52  {
    53     "ports": [
    54         {
    55             "local_link_connection": {},
    56             "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
    57             "uuid": "3abe3f36-9708-4e9f-b07e-0f898061d3a7",
    58             "links": [
    59                 {
    60                     "href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
    61                     "rel": "self"
    62                 },
    63                 {
    64                     "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7",
    65                     "rel": "bookmark"
    66                 }
    67             ],
    68             "extra": {},
    69             "pxe_enabled": true,
    70             "portgroup_uuid": null,
    71             "updated_at": "2019-02-15T09:55:19+00:00",
    72             "physical_network": null,
    73             "address": "52:54:00:0a:af:d1",
    74             "internal_info": {
    75  
    76             },
    77             "created_at": "2019-02-15T09:52:23+00:00"
    78         },
    79         {
    80             "local_link_connection": {},
    81             "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
    82             "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a",
    83             "links": [
    84                 {
    85                     "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
    86                     "rel": "self"
    87                 },
    88                 {
    89                     "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
    90                     "rel": "bookmark"
    91                 }
    92             ],
    93             "extra": {},
    94             "pxe_enabled": true,
    95             "portgroup_uuid": null,
    96             "updated_at": "2019-02-15T09:55:19+00:00",
    97             "physical_network": null,
    98             "address": "52:54:00:4d:87:e6",
    99             "internal_info": {},
   100             "created_at": "2019-02-15T09:52:24+00:00"
   101         }
   102     ]
   103  }
   104  `
   105  
   106  // SinglePortBody is the canned body of a Get request on an existing port.
   107  const SinglePortBody = `
   108  {
   109      "local_link_connection": {
   110  
   111      },
   112      "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
   113      "uuid": "f2845e11-dbd4-4728-a8c0-30d19f48924a",
   114      "links": [
   115          {
   116              "href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
   117              "rel": "self"
   118          },
   119          {
   120              "href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a",
   121              "rel": "bookmark"
   122          }
   123      ],
   124      "extra": {
   125  
   126      },
   127      "pxe_enabled": true,
   128      "portgroup_uuid": null,
   129      "updated_at": "2019-02-15T09:55:19+00:00",
   130      "physical_network": null,
   131      "address": "52:54:00:4d:87:e6",
   132      "internal_info": {
   133  
   134      },
   135      "created_at": "2019-02-15T09:52:24+00:00"
   136  }
   137  `
   138  
   139  var (
   140  	fooCreated, _ = time.Parse(time.RFC3339, "2019-02-15T09:52:24+00:00")
   141  	fooUpdated, _ = time.Parse(time.RFC3339, "2019-02-15T09:55:19+00:00")
   142  	BarCreated, _ = time.Parse(time.RFC3339, "2019-02-15T09:52:23+00:00")
   143  	BarUpdated, _ = time.Parse(time.RFC3339, "2019-02-15T09:55:19+00:00")
   144  	PortFoo       = ports.Port{
   145  		UUID:                "f2845e11-dbd4-4728-a8c0-30d19f48924a",
   146  		NodeUUID:            "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
   147  		Address:             "52:54:00:4d:87:e6",
   148  		PXEEnabled:          true,
   149  		LocalLinkConnection: map[string]interface{}{},
   150  		InternalInfo:        map[string]interface{}{},
   151  		Extra:               map[string]interface{}{},
   152  		CreatedAt:           fooCreated,
   153  		UpdatedAt:           fooUpdated,
   154  		Links:               []interface{}{map[string]interface{}{"href": "http://192.168.0.8/baremetal/v1/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", "rel": "self"}, map[string]interface{}{"href": "http://192.168.0.8/baremetal/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", "rel": "bookmark"}},
   155  	}
   156  
   157  	PortBar = ports.Port{
   158  		UUID:                "3abe3f36-9708-4e9f-b07e-0f898061d3a7",
   159  		NodeUUID:            "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
   160  		Address:             "52:54:00:0a:af:d1",
   161  		PXEEnabled:          true,
   162  		LocalLinkConnection: map[string]interface{}{},
   163  		InternalInfo:        map[string]interface{}{},
   164  		Extra:               map[string]interface{}{},
   165  		CreatedAt:           BarCreated,
   166  		UpdatedAt:           BarUpdated,
   167  		Links:               []interface{}{map[string]interface{}{"href": "http://192.168.0.8/baremetal/v1/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", "rel": "self"}, map[string]interface{}{"rel": "bookmark", "href": "http://192.168.0.8/baremetal/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7"}},
   168  	}
   169  )
   170  
   171  // HandlePortListSuccessfully sets up the test server to respond to a port List request.
   172  func HandlePortListSuccessfully(t *testing.T) {
   173  	th.Mux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) {
   174  		th.TestMethod(t, r, "GET")
   175  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   176  		w.Header().Add("Content-Type", "application/json")
   177  		r.ParseForm()
   178  
   179  		marker := r.Form.Get("marker")
   180  		switch marker {
   181  		case "":
   182  			fmt.Fprintf(w, PortListBody)
   183  
   184  		case "f2845e11-dbd4-4728-a8c0-30d19f48924a":
   185  			fmt.Fprintf(w, `{ "ports": [] }`)
   186  		default:
   187  			t.Fatalf("/ports invoked with unexpected marker=[%s]", marker)
   188  		}
   189  	})
   190  }
   191  
   192  // HandlePortListSuccessfully sets up the test server to respond to a port List request.
   193  func HandlePortListDetailSuccessfully(t *testing.T) {
   194  	th.Mux.HandleFunc("/ports/detail", func(w http.ResponseWriter, r *http.Request) {
   195  		th.TestMethod(t, r, "GET")
   196  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   197  		w.Header().Add("Content-Type", "application/json")
   198  		r.ParseForm()
   199  
   200  		fmt.Fprintf(w, PortListDetailBody)
   201  	})
   202  }
   203  
   204  // HandleSPortCreationSuccessfully sets up the test server to respond to a port creation request
   205  // with a given response.
   206  func HandlePortCreationSuccessfully(t *testing.T, response string) {
   207  	th.Mux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) {
   208  		th.TestMethod(t, r, "POST")
   209  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   210  		th.TestJSONRequest(t, r, `{
   211            "node_uuid": "ddd06a60-b91e-4ab4-a6e7-56c0b25b6086",
   212            "address": "52:54:00:4d:87:e6",
   213            "pxe_enabled": true
   214          }`)
   215  
   216  		w.WriteHeader(http.StatusAccepted)
   217  		w.Header().Add("Content-Type", "application/json")
   218  		fmt.Fprintf(w, response)
   219  	})
   220  }
   221  
   222  // HandlePortDeletionSuccessfully sets up the test server to respond to a port deletion request.
   223  func HandlePortDeletionSuccessfully(t *testing.T) {
   224  	th.Mux.HandleFunc("/ports/3abe3f36-9708-4e9f-b07e-0f898061d3a7", func(w http.ResponseWriter, r *http.Request) {
   225  		th.TestMethod(t, r, "DELETE")
   226  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   227  
   228  		w.WriteHeader(http.StatusNoContent)
   229  	})
   230  }
   231  
   232  func HandlePortGetSuccessfully(t *testing.T) {
   233  	th.Mux.HandleFunc("/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", func(w http.ResponseWriter, r *http.Request) {
   234  		th.TestMethod(t, r, "GET")
   235  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   236  		th.TestHeader(t, r, "Accept", "application/json")
   237  
   238  		fmt.Fprintf(w, SinglePortBody)
   239  	})
   240  }
   241  
   242  func HandlePortUpdateSuccessfully(t *testing.T, response string) {
   243  	th.Mux.HandleFunc("/ports/f2845e11-dbd4-4728-a8c0-30d19f48924a", func(w http.ResponseWriter, r *http.Request) {
   244  		th.TestMethod(t, r, "PATCH")
   245  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   246  		th.TestHeader(t, r, "Accept", "application/json")
   247  		th.TestHeader(t, r, "Content-Type", "application/json")
   248  		th.TestJSONRequest(t, r, `[{"op": "replace", "path": "/address", "value": "22:22:22:22:22:22"}]`)
   249  
   250  		fmt.Fprintf(w, response)
   251  	})
   252  }