github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/bms/v2/nics/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk/openstack/bms/v2/nics" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 fake "github.com/huaweicloud/golangsdk/testhelper/client" 11 ) 12 13 func TestListNIC(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc("/servers/2bff7a8a-3934-4f79-b1d6-53dc5540f00e/os-interface", func(w http.ResponseWriter, r *http.Request) { 18 th.TestMethod(t, r, "GET") 19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 20 21 w.Header().Add("Content-Type", "application/json") 22 w.WriteHeader(http.StatusOK) 23 24 fmt.Fprintf(w, ` 25 { 26 "interfaceAttachments": [ 27 { 28 "port_state": "ACTIVE", 29 "fixed_ips": [ 30 { 31 "subnet_id": "518e34f2-16d4-4242-9378-b7eea505ab9c", 32 "ip_address": "192.168.0.80" 33 } 34 ], 35 "port_id": "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f", 36 "net_id": "31e04eb7-7ccc-4116-9f43-a51eb29fa348", 37 "mac_addr": "fa:16:3e:00:1a:9a" 38 }, 39 { 40 "port_state": "ACTIVE", 41 "fixed_ips": [ 42 { 43 "subnet_id": "f3ef8cb3-9954-4434-a558-70b623b3c69b", 44 "ip_address": "192.168.1.206" 45 } 46 ], 47 "port_id": "a5fff9e7-65e1-4e46-95da-263d66ff4a7a", 48 "net_id": "68cf7e29-b770-4951-be66-9b3f16297732", 49 "mac_addr": "fa:16:3e:83:dc:08" 50 } 51 ] 52 } 53 `) 54 }) 55 56 //count := 0 57 58 actual, err := nics.List(fake.ServiceClient(), "2bff7a8a-3934-4f79-b1d6-53dc5540f00e", nics.ListOpts{}) 59 if err != nil { 60 t.Errorf("Failed to extract nics: %v", err) 61 } 62 63 expected := []nics.Nic{ 64 { 65 ID: "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f", 66 Status: "ACTIVE", 67 NetworkID: "31e04eb7-7ccc-4116-9f43-a51eb29fa348", 68 MACAddress: "fa:16:3e:00:1a:9a", 69 FixedIP: []nics.FixedIP{{SubnetID: "518e34f2-16d4-4242-9378-b7eea505ab9c", IPAddress: "192.168.0.80"}}, 70 }, 71 { 72 ID: "a5fff9e7-65e1-4e46-95da-263d66ff4a7a", 73 Status: "ACTIVE", 74 NetworkID: "68cf7e29-b770-4951-be66-9b3f16297732", 75 MACAddress: "fa:16:3e:83:dc:08", 76 FixedIP: []nics.FixedIP{{SubnetID: "f3ef8cb3-9954-4434-a558-70b623b3c69b", IPAddress: "192.168.1.206"}}, 77 }, 78 } 79 80 th.AssertDeepEquals(t, expected, actual) 81 } 82 83 func TestGetNIC(t *testing.T) { 84 th.SetupHTTP() 85 defer th.TeardownHTTP() 86 87 th.Mux.HandleFunc("/servers/2bff7a8a-3934-4f79-b1d6-53dc5540f00e/os-interface/1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f", func(w http.ResponseWriter, r *http.Request) { 88 th.TestMethod(t, r, "GET") 89 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 90 91 w.Header().Add("Content-Type", "application/json") 92 w.WriteHeader(http.StatusOK) 93 94 fmt.Fprintf(w, ` 95 { 96 "interfaceAttachment": { 97 "port_state": "ACTIVE", 98 "fixed_ips": [ 99 { 100 "subnet_id": "518e34f2-16d4-4242-9378-b7eea505ab9c", 101 "ip_address": "192.168.0.80" 102 } 103 ], 104 "port_id": "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f", 105 "net_id": "31e04eb7-7ccc-4116-9f43-a51eb29fa348", 106 "mac_addr": "fa:16:3e:00:1a:9a" 107 } 108 } 109 `) 110 }) 111 112 n, err := nics.Get(fake.ServiceClient(), "2bff7a8a-3934-4f79-b1d6-53dc5540f00e", "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f").Extract() 113 th.AssertNoErr(t, err) 114 th.AssertEquals(t, "1d3bf3ae-bc4a-4890-86f8-8c31a6eb764f", n.ID) 115 th.AssertEquals(t, "ACTIVE", n.Status) 116 th.AssertEquals(t, "fa:16:3e:00:1a:9a", n.MACAddress) 117 th.AssertEquals(t, "31e04eb7-7ccc-4116-9f43-a51eb29fa348", n.NetworkID) 118 th.AssertDeepEquals(t, []nics.FixedIP{{SubnetID: "518e34f2-16d4-4242-9378-b7eea505ab9c", IPAddress: "192.168.0.80"}}, n.FixedIP) 119 120 }