github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/portsbinding/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
     9  	porttest "github.com/huaweicloud/golangsdk/openstack/networking/v2/ports/testing"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func HandleListSuccessfully(t *testing.T) {
    14  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
    15  		th.TestMethod(t, r, "GET")
    16  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    17  
    18  		w.Header().Add("Content-Type", "application/json")
    19  		w.WriteHeader(http.StatusOK)
    20  
    21  		fmt.Fprintf(w, porttest.ListResponse)
    22  	})
    23  }
    24  
    25  func HandleGet(t *testing.T) {
    26  	th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
    27  		th.TestMethod(t, r, "GET")
    28  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    29  
    30  		w.Header().Add("Content-Type", "application/json")
    31  		w.WriteHeader(http.StatusOK)
    32  
    33  		fmt.Fprintf(w, porttest.GetResponse)
    34  	})
    35  }
    36  
    37  func HandleCreate(t *testing.T) {
    38  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
    39  		th.TestMethod(t, r, "POST")
    40  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    41  		th.TestHeader(t, r, "Content-Type", "application/json")
    42  		th.TestHeader(t, r, "Accept", "application/json")
    43  		th.TestJSONRequest(t, r, `
    44  {
    45      "port": {
    46          "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
    47          "name": "private-port",
    48          "admin_state_up": true,
    49  		    "fixed_ips": [
    50  				    {
    51  						    "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
    52  						    "ip_address": "10.0.0.2"
    53  				    }
    54  		    ],
    55  		    "security_groups": ["foo"],
    56  		    "binding:host_id": "HOST1",
    57          "binding:vnic_type": "normal"
    58      }
    59  }
    60  			`)
    61  
    62  		w.Header().Add("Content-Type", "application/json")
    63  		w.WriteHeader(http.StatusCreated)
    64  
    65  		fmt.Fprintf(w, `
    66  {
    67      "port": {
    68          "status": "DOWN",
    69          "name": "private-port",
    70          "allowed_address_pairs": [],
    71          "admin_state_up": true,
    72          "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
    73          "tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
    74          "device_owner": "",
    75          "mac_address": "fa:16:3e:c9:cb:f0",
    76          "fixed_ips": [
    77              {
    78                  "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
    79                  "ip_address": "10.0.0.2"
    80              }
    81          ],
    82          "binding:host_id": "HOST1",
    83          "binding:vnic_type": "normal",
    84          "id": "65c0ee9f-d634-4522-8954-51021b570b0d",
    85          "security_groups": [
    86              "f0ac4394-7e4a-4409-9701-ba8be283dbc3"
    87          ],
    88          "device_id": ""
    89      }
    90  }
    91  		`)
    92  	})
    93  }
    94  
    95  func HandleUpdate(t *testing.T) {
    96  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
    97  		th.TestMethod(t, r, "PUT")
    98  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    99  		th.TestHeader(t, r, "Content-Type", "application/json")
   100  		th.TestHeader(t, r, "Accept", "application/json")
   101  		th.TestJSONRequest(t, r, `
   102  {
   103  		"port": {
   104  			"name": "new_port_name",
   105  			"fixed_ips": [
   106  				  {
   107  					    "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
   108  					    "ip_address": "10.0.0.3"
   109  				  }
   110  			],
   111  			"security_groups": [
   112          "f0ac4394-7e4a-4409-9701-ba8be283dbc3"
   113        ],
   114        "binding:host_id": "HOST1",
   115        "binding:vnic_type": "normal"
   116  		}
   117  }
   118  			`)
   119  
   120  		w.Header().Add("Content-Type", "application/json")
   121  		w.WriteHeader(http.StatusOK)
   122  
   123  		fmt.Fprintf(w, `
   124  {
   125      "port": {
   126          "status": "DOWN",
   127          "name": "new_port_name",
   128          "admin_state_up": true,
   129          "network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   130          "tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
   131          "device_owner": "",
   132          "mac_address": "fa:16:3e:c9:cb:f0",
   133          "fixed_ips": [
   134              {
   135                  "subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
   136                  "ip_address": "10.0.0.3"
   137              }
   138          ],
   139          "id": "65c0ee9f-d634-4522-8954-51021b570b0d",
   140          "security_groups": [
   141              "f0ac4394-7e4a-4409-9701-ba8be283dbc3"
   142          ],
   143          "device_id": "",
   144          "binding:host_id": "HOST1",
   145          "binding:vnic_type": "normal"
   146      }
   147  }
   148  		`)
   149  	})
   150  }