github.com/john-lin/cni@v0.6.0-rc1.0.20170712150331-b69e640cc0e2/pkg/types/020/types_test.go (about)

     1  // Copyright 2016 CNI authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package types020_test
    16  
    17  import (
    18  	"io/ioutil"
    19  	"net"
    20  	"os"
    21  
    22  	"github.com/containernetworking/cni/pkg/types"
    23  	"github.com/containernetworking/cni/pkg/types/020"
    24  
    25  	. "github.com/onsi/ginkgo"
    26  	. "github.com/onsi/gomega"
    27  )
    28  
    29  var _ = Describe("Ensures compatibility with the 0.1.0/0.2.0 spec", func() {
    30  	It("correctly encodes a 0.1.0/0.2.0 Result", func() {
    31  		ipv4, err := types.ParseCIDR("1.2.3.30/24")
    32  		Expect(err).NotTo(HaveOccurred())
    33  		Expect(ipv4).NotTo(BeNil())
    34  
    35  		routegwv4, routev4, err := net.ParseCIDR("15.5.6.8/24")
    36  		Expect(err).NotTo(HaveOccurred())
    37  		Expect(routev4).NotTo(BeNil())
    38  		Expect(routegwv4).NotTo(BeNil())
    39  
    40  		ipv6, err := types.ParseCIDR("abcd:1234:ffff::cdde/64")
    41  		Expect(err).NotTo(HaveOccurred())
    42  		Expect(ipv6).NotTo(BeNil())
    43  
    44  		routegwv6, routev6, err := net.ParseCIDR("1111:dddd::aaaa/80")
    45  		Expect(err).NotTo(HaveOccurred())
    46  		Expect(routev6).NotTo(BeNil())
    47  		Expect(routegwv6).NotTo(BeNil())
    48  
    49  		// Set every field of the struct to ensure source compatibility
    50  		res := types020.Result{
    51  			CNIVersion: types020.ImplementedSpecVersion,
    52  			IP4: &types020.IPConfig{
    53  				IP:      *ipv4,
    54  				Gateway: net.ParseIP("1.2.3.1"),
    55  				Routes: []types.Route{
    56  					{Dst: *routev4, GW: routegwv4},
    57  				},
    58  			},
    59  			IP6: &types020.IPConfig{
    60  				IP:      *ipv6,
    61  				Gateway: net.ParseIP("abcd:1234:ffff::1"),
    62  				Routes: []types.Route{
    63  					{Dst: *routev6, GW: routegwv6},
    64  				},
    65  			},
    66  			DNS: types.DNS{
    67  				Nameservers: []string{"1.2.3.4", "1::cafe"},
    68  				Domain:      "acompany.com",
    69  				Search:      []string{"somedomain.com", "otherdomain.net"},
    70  				Options:     []string{"foo", "bar"},
    71  			},
    72  		}
    73  
    74  		Expect(res.String()).To(Equal("IP4:{IP:{IP:1.2.3.30 Mask:ffffff00} Gateway:1.2.3.1 Routes:[{Dst:{IP:15.5.6.0 Mask:ffffff00} GW:15.5.6.8}]}, IP6:{IP:{IP:abcd:1234:ffff::cdde Mask:ffffffffffffffff0000000000000000} Gateway:abcd:1234:ffff::1 Routes:[{Dst:{IP:1111:dddd:: Mask:ffffffffffffffffffff000000000000} GW:1111:dddd::aaaa}]}, DNS:{Nameservers:[1.2.3.4 1::cafe] Domain:acompany.com Search:[somedomain.com otherdomain.net] Options:[foo bar]}"))
    75  
    76  		// Redirect stdout to capture JSON result
    77  		oldStdout := os.Stdout
    78  		r, w, err := os.Pipe()
    79  		Expect(err).NotTo(HaveOccurred())
    80  
    81  		os.Stdout = w
    82  		err = res.Print()
    83  		w.Close()
    84  		Expect(err).NotTo(HaveOccurred())
    85  
    86  		// parse the result
    87  		out, err := ioutil.ReadAll(r)
    88  		os.Stdout = oldStdout
    89  		Expect(err).NotTo(HaveOccurred())
    90  
    91  		Expect(string(out)).To(Equal(`{
    92      "cniVersion": "0.2.0",
    93      "ip4": {
    94          "ip": "1.2.3.30/24",
    95          "gateway": "1.2.3.1",
    96          "routes": [
    97              {
    98                  "dst": "15.5.6.0/24",
    99                  "gw": "15.5.6.8"
   100              }
   101          ]
   102      },
   103      "ip6": {
   104          "ip": "abcd:1234:ffff::cdde/64",
   105          "gateway": "abcd:1234:ffff::1",
   106          "routes": [
   107              {
   108                  "dst": "1111:dddd::/80",
   109                  "gw": "1111:dddd::aaaa"
   110              }
   111          ]
   112      },
   113      "dns": {
   114          "nameservers": [
   115              "1.2.3.4",
   116              "1::cafe"
   117          ],
   118          "domain": "acompany.com",
   119          "search": [
   120              "somedomain.com",
   121              "otherdomain.net"
   122          ],
   123          "options": [
   124              "foo",
   125              "bar"
   126          ]
   127      }
   128  }`))
   129  	})
   130  })