|
| 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