github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v1/ports/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	client "github.com/chnsz/golangsdk/openstack/networking/v1/common"
     9  	"github.com/chnsz/golangsdk/openstack/networking/v1/ports"
    10  	th "github.com/chnsz/golangsdk/testhelper"
    11  )
    12  
    13  var (
    14  	expectedCreateResponse = fmt.Sprintf(`
    15  {
    16  	"port": %s
    17  }`, buildPortItemResponse("192.168.0.162"))
    18  
    19  	expectedUpdateResponse = fmt.Sprintf(`
    20  {
    21  	"port": %s
    22  }`, buildPortItemResponse("192.168.0.25"))
    23  
    24  	expectedListResponse = fmt.Sprintf(`
    25  {
    26  	"ports": [
    27  		%s
    28  	]
    29  }`, buildPortItemResponse("192.168.0.162"))
    30  
    31  	emptyListResponse = `
    32  {
    33  	"ports": []
    34  }`
    35  
    36  	expectedCreateResponseData = buildPortResponseData("192.168.0.162")
    37  	expectedUpdateResponseData = buildPortResponseData("192.168.0.25")
    38  	expectedListResponseData   = []ports.Port{buildPortResponseData("192.168.0.162")}
    39  )
    40  
    41  func buildPortItemResponse(address string) string {
    42  	return fmt.Sprintf(`
    43  	{
    44  		"admin_state_up": false,
    45  		"allowed_address_pairs": [
    46  		  {
    47  			"ip_address": "192.168.0.25",
    48  			"mac_address": "fa:16:3e:71:db:e5"
    49  		  }
    50  		],
    51  		"binding:vnic_type": "normal",
    52  		"created_at": "2022-03-14T06:18:48",
    53  		"device_owner": "neutron:VIP_PORT",
    54  		"fixed_ips": [
    55  			{
    56  				"ip_address": "%s",
    57  				"subnet_id": "885cb8c3-0cbe-406d-83d6-fc98856fcf26"
    58  			}
    59  		],
    60  		"id": "05547c10-e318-4067-9db2-01f5dc30be38",
    61  		"mac_address": "fa:16:3e:71:db:e5",
    62  		"network_id": "e4cb3b49-78a0-479b-b37d-bd99b3ec0d8a",
    63  		"status": "DOWN"
    64  	}`, address)
    65  }
    66  
    67  func buildPortResponseData(address string) ports.Port {
    68  	return ports.Port{
    69  		AdminStateUp: false,
    70  		AllowedAddressPairs: []ports.AddressPair{
    71  			{
    72  				IpAddress:  "192.168.0.25",
    73  				MacAddress: "fa:16:3e:71:db:e5",
    74  			},
    75  		},
    76  		VnicType:    "normal",
    77  		CreatedAt:   "2022-03-14T06:18:48",
    78  		DeviceOwner: "neutron:VIP_PORT",
    79  		FixedIps: []ports.FixedIp{
    80  			{
    81  				IpAddress: address,
    82  				SubnetId:  "885cb8c3-0cbe-406d-83d6-fc98856fcf26",
    83  			},
    84  		},
    85  		ID:         "05547c10-e318-4067-9db2-01f5dc30be38",
    86  		NetworkId:  "e4cb3b49-78a0-479b-b37d-bd99b3ec0d8a",
    87  		MacAddress: "fa:16:3e:71:db:e5",
    88  		Status:     "DOWN",
    89  	}
    90  }
    91  
    92  func handleV1NetworkPortCreate(t *testing.T) {
    93  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/ports", func(w http.ResponseWriter, r *http.Request) {
    94  		th.TestMethod(t, r, "POST")
    95  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    96  		w.Header().Add("Content-Type", "application/json")
    97  		w.WriteHeader(http.StatusAccepted)
    98  		fmt.Fprint(w, expectedCreateResponse)
    99  	})
   100  }
   101  
   102  func handleV1NetworkPortGet(t *testing.T) {
   103  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/ports/05547c10-e318-4067-9db2-01f5dc30be38", func(w http.ResponseWriter, r *http.Request) {
   104  		th.TestMethod(t, r, "GET")
   105  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   106  		w.Header().Add("Content-Type", "application/json")
   107  		w.WriteHeader(http.StatusOK)
   108  		fmt.Fprint(w, expectedCreateResponse)
   109  	})
   110  }
   111  
   112  func handleV1NetworkPortUpdate(t *testing.T) {
   113  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/ports/05547c10-e318-4067-9db2-01f5dc30be38", func(w http.ResponseWriter, r *http.Request) {
   114  		th.TestMethod(t, r, "PUT")
   115  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   116  		w.Header().Add("Content-Type", "application/json")
   117  		w.WriteHeader(http.StatusOK)
   118  		fmt.Fprint(w, expectedUpdateResponse)
   119  	})
   120  }
   121  
   122  func handleV1NetworkPortDelete(t *testing.T) {
   123  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/ports/05547c10-e318-4067-9db2-01f5dc30be38", func(w http.ResponseWriter, r *http.Request) {
   124  		th.TestMethod(t, r, "DELETE")
   125  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   126  		w.Header().Add("Content-Type", "application/json")
   127  		w.WriteHeader(http.StatusNoContent)
   128  	})
   129  }
   130  
   131  func handleV1NetworkPortList(t *testing.T) {
   132  	th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/ports", func(w http.ResponseWriter, r *http.Request) {
   133  		th.TestMethod(t, r, "GET")
   134  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   135  		w.Header().Add("Content-Type", "application/json")
   136  		w.WriteHeader(http.StatusOK)
   137  
   138  		r.ParseForm()
   139  		marker := r.Form.Get("marker")
   140  		if marker == "" {
   141  			fmt.Fprint(w, expectedListResponse)
   142  		} else {
   143  			fmt.Fprint(w, emptyListResponse)
   144  		}
   145  	})
   146  }