Debian Codegraph @

network/network_test.go

raw symbols SCIP
1 package network_test
2
3 import (
4 "encoding/json"
5 "net/url"
6 "strings"
7 "testing"
8
9 "github.com/CenturyLinkCloud/clc-sdk/api"
10 "github.com/CenturyLinkCloud/clc-sdk/network"
11 //"github.com/CenturyLinkCloud/clc-sdk/status"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/mock"
14 )
15
16 const respClaim = `
17 {
18 "requestType": "blueprintOperation",
19 "status": "succeeded",
20 "summary": {
21 "blueprintId": 92121,
22 "locationId": "DC",
23 "links": [
24 {
25 "rel": "network",
26 "href": "/v2-experimental/networks/ALIAS/DC/123456",
27 "id": "123456"
28 }
29 ]
30 }
31 }
32 `
33
34 const respNetwork = `
35 {
36 "id": "ec6ff75a0ffd4504840dab343fe41077",
37 "cidr": "11.22.33.0/24",
38 "description": "vlan_9999_11.22.33",
39 "gateway": "11.22.33.1",
40 "name": "vlan_9999_11.22.33",
41 "netmask": "255.255.255.0",
42 "type": "private",
43 "vlan": 9999,
44 "ipAddresses": [
45 {
46 "address": "11.22.33.12",
47 "claimed": true,
48 "server": "WA1ALIASAPI01",
49 "type": "private"
50 }
51 ],
52 "links": []
53 }
54 `
55
56 func TestGetNetwork(t *testing.T) {
57 assert := assert.New(t)
58
59 client := NewMockClient()
60 client.On("Get", "http://localhost/v2-experimental/networks/ALIAS/DC/ec6ff75a0ffd4504840dab343fe41077", mock.Anything).Return(nil)
61 service := network.New(client)
62
63 id := "ec6ff75a0ffd4504840dab343fe41077"
64 resp, err := service.Get("DC", id)
65
66 assert.Nil(err)
67 assert.Equal(id, resp.ID)
68 assert.Equal("11.22.33.1", resp.Gateway)
69 assert.Equal("11.22.33.12", resp.IPAddresses[0].Address)
70 client.AssertExpectations(t)
71 }
72
73 func NewMockClient() *MockClient {
74 return &MockClient{}
75 }
76
77 type MockClient struct {
78 mock.Mock
79 }
80
81 func (m *MockClient) Get(url string, resp interface{}) error {
82 if strings.HasSuffix(url, "ec6ff75a0ffd4504840dab343fe41077") {
83 json.Unmarshal([]byte(respNetwork), resp)
84 }
85 args := m.Called(url, resp)
86 return args.Error(0)
87 }
88
89 func (m *MockClient) Post(url string, body, resp interface{}) error {
90 if strings.HasSuffix(url, "claim") {
91 json.Unmarshal([]byte(respClaim), resp)
92 }
93 if strings.HasSuffix(url, "release") {
94 // 204 on release
95 }
96 args := m.Called(url, resp)
97 return args.Error(0)
98 }
99
100 func (m *MockClient) Put(url string, body, resp interface{}) error {
101 return nil
102 }
103
104 func (m *MockClient) Patch(url string, body, resp interface{}) error {
105 return nil
106 }
107
108 func (m *MockClient) Delete(url string, resp interface{}) error {
109 return nil
110 }
111
112 func (m *MockClient) Config() *api.Config {
113 u, _ := url.Parse("http://localhost/v2-experimental")
114 return &api.Config{
115 User: api.User{
116 Username: "test.user",
117 Password: "s0s3cur3",
118 },
119 Alias: "ALIAS",
120 BaseURL: u,
121 }
122 }