Skip to content

Commit 5d0b9bb

Browse files
committed
add unit tests
Signed-off-by: Miguel Duarte Barroso <[email protected]>
1 parent 9041116 commit 5d0b9bb

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

pkg/multuscni/client.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Client interface {
2525

2626
type HttpClient struct {
2727
httpClient *http.Client
28+
serverURL string
2829
}
2930

3031
func NewClient(socketPath string) *HttpClient {
@@ -36,11 +37,12 @@ func NewClient(socketPath string) *HttpClient {
3637
},
3738
},
3839
},
40+
serverURL: MultusDelegateURL(),
3941
}
4042
}
4143

4244
func (c *HttpClient) InvokeDelegate(req *multusapi.Request) (*multusapi.Response, error) {
43-
httpResp, err := c.DoCNI(MultusDelegateURL(), req)
45+
httpResp, err := c.DoCNI(req)
4446
if err != nil {
4547
return nil, err
4648
}
@@ -53,13 +55,13 @@ func (c *HttpClient) InvokeDelegate(req *multusapi.Request) (*multusapi.Response
5355
return response, nil
5456
}
5557

56-
func (c *HttpClient) DoCNI(url string, req interface{}) ([]byte, error) {
58+
func (c *HttpClient) DoCNI(req interface{}) ([]byte, error) {
5759
data, err := json.Marshal(req)
5860
if err != nil {
5961
return nil, fmt.Errorf("failed to marshal CNI request %v: %v", req, err)
6062
}
6163

62-
resp, err := c.httpClient.Post(url, "application/json", bytes.NewReader(data))
64+
resp, err := c.httpClient.Post(c.serverURL, "application/json", bytes.NewReader(data))
6365
if err != nil {
6466
return nil, fmt.Errorf("failed to send CNI request: %v", err)
6567
}

pkg/multuscni/client_test.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package multuscni
2+
3+
import (
4+
"encoding/json"
5+
"github.com/containernetworking/cni/pkg/types"
6+
cni100 "github.com/containernetworking/cni/pkg/types/100"
7+
"net"
8+
"net/http"
9+
"net/http/httptest"
10+
"testing"
11+
12+
. "github.com/onsi/ginkgo/v2"
13+
. "github.com/onsi/gomega"
14+
15+
multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
16+
)
17+
18+
func TestController(t *testing.T) {
19+
RegisterFailHandler(Fail)
20+
RunSpecs(t, "Dynamic network attachment controller suite")
21+
}
22+
23+
var _ = Describe("multuscni REST client", func() {
24+
const socketPath = "/over/there"
25+
26+
BeforeEach(func() {
27+
28+
})
29+
30+
It("", func() {
31+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32+
w.WriteHeader(http.StatusBadRequest)
33+
_, _ = w.Write([]byte(`kablewit`))
34+
}))
35+
36+
defer server.Close()
37+
_, err := newDummyClient(server.Client(), server.URL).InvokeDelegate(multusRequest())
38+
Expect(err).To(MatchError("CNI request failed with status 400: 'kablewit'"))
39+
})
40+
41+
It("", func() {
42+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43+
w.WriteHeader(http.StatusOK)
44+
_, _ = w.Write([]byte(`{asd:123}`))
45+
}))
46+
47+
defer server.Close()
48+
_, err := newDummyClient(server.Client(), server.URL).InvokeDelegate(multusRequest())
49+
Expect(err).To(MatchError(ContainSubstring("failed to unmarshal response '{asd:123}':")))
50+
})
51+
52+
It("", func() {
53+
response := multusapi.Response{Result: &cni100.Result{
54+
CNIVersion: "",
55+
Interfaces: []*cni100.Interface{{
56+
Name: "net1",
57+
Mac: "02:03:04:05:06:07",
58+
Sandbox: "/over-there",
59+
}},
60+
}}
61+
62+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63+
w.WriteHeader(http.StatusOK)
64+
serializedResponse, _ := json.Marshal(response)
65+
_, _ = w.Write(serializedResponse)
66+
}))
67+
68+
defer server.Close()
69+
Expect(newDummyClient(server.Client(), server.URL).InvokeDelegate(multusRequest())).To(Equal(&response))
70+
})
71+
72+
It("", func() {
73+
ip, ipNet, _ := net.ParseCIDR("192.168.14.14/24")
74+
ipNet.IP = ip
75+
response := multusapi.Response{Result: &cni100.Result{
76+
CNIVersion: "",
77+
Interfaces: []*cni100.Interface{{
78+
Name: "net1",
79+
Mac: "02:03:04:05:06:07",
80+
Sandbox: "/over-there",
81+
}},
82+
IPs: []*cni100.IPConfig{{
83+
Address: *ipNet,
84+
}},
85+
Routes: nil,
86+
DNS: types.DNS{},
87+
}}
88+
89+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
90+
w.WriteHeader(http.StatusOK)
91+
serializedResponse, _ := json.Marshal(response)
92+
_, _ = w.Write(serializedResponse)
93+
}))
94+
95+
defer server.Close()
96+
Expect(newDummyClient(server.Client(), server.URL).InvokeDelegate(multusRequest())).To(Equal(&response))
97+
})
98+
})
99+
100+
func multusRequest() *multusapi.Request {
101+
return &multusapi.Request{
102+
Env: map[string]string{},
103+
Config: nil,
104+
}
105+
}
106+
107+
func newDummyClient(httpClient *http.Client, serverURL string) *HttpClient {
108+
return &HttpClient{httpClient: httpClient, serverURL: serverURL}
109+
}

0 commit comments

Comments
 (0)