...

Source file src/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils/net-attach-def_test.go

Documentation: github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils

     1  // Copyright (c) 2019 Kubernetes Network Plumbing Working Group
     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 utils
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  
    21  	cnitypes "github.com/containernetworking/cni/pkg/types"
    22  	cnicurrent "github.com/containernetworking/cni/pkg/types/current"
    23  
    24  	v1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
    25  
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/client-go/kubernetes/fake"
    29  
    30  	. "github.com/onsi/ginkgo"
    31  	. "github.com/onsi/gomega"
    32  )
    33  
    34  // EnsureCIDR parses/verify CIDR ip string and convert to net.IPNet
    35  func EnsureCIDR(cidr string) *net.IPNet {
    36      ip, net, err := net.ParseCIDR(cidr)
    37      Expect(err).NotTo(HaveOccurred())
    38      net.IP = ip
    39      return net
    40  }
    41  
    42  func TestNetworkAttachmentDefinition(t *testing.T) {
    43  	RegisterFailHandler(Fail)
    44  	RunSpecs(t, "Network Attachment Definition utils")
    45  }
    46  
    47  var _ = Describe("Netwok Attachment Definition manipulations", func() {
    48  
    49  	It("test convertDNS", func() {
    50  		cniDNS := cnitypes.DNS{
    51  			Nameservers: []string{ "aaa", "bbb" },
    52  			Domain: "testDomain",
    53  			Search: []string{ "1.example.com", "2.example.com" },
    54  			Options: []string{ "debug", "inet6" },
    55  		}
    56  
    57  		v1DNS := convertDNS(cniDNS)
    58  		Expect(v1DNS.Nameservers).To(Equal(cniDNS.Nameservers))
    59  		Expect(v1DNS.Domain).To(Equal(cniDNS.Domain))
    60  		Expect(v1DNS.Search).To(Equal(cniDNS.Search))
    61  		Expect(v1DNS.Options).To(Equal(cniDNS.Options))
    62  	})
    63  
    64  	It("set network status into pod", func() {
    65  		fakePod := &corev1.Pod{
    66  			ObjectMeta: metav1.ObjectMeta{
    67  				Name: "fakePod1",
    68  				Namespace: "fakeNamespace1",
    69  			},
    70  			Spec: corev1.PodSpec{
    71  				Containers: []corev1.Container{
    72  					{
    73  						Name: "fakeContainer",
    74  						Image: "fakeImage",
    75  					},
    76  				},
    77  			},
    78  		}
    79  		fakeStatus := []v1.NetworkStatus{
    80  			v1.NetworkStatus{
    81  				Name: "cbr0",
    82  				Interface: "eth0",
    83  				IPs: []string{ "10.244.1.2" },
    84  				Mac: "92:79:27:01:7c:ce",
    85  			},
    86  			v1.NetworkStatus{
    87  				Name: "test-net-attach-def-1",
    88  				Interface: "net1",
    89  				IPs: []string{ "1.1.1.1" },
    90  				Mac: "ea:0e:fa:63:95:f9",
    91  			},
    92  		}
    93  
    94  		clientSet := fake.NewSimpleClientset(fakePod)
    95  		pod, err:= clientSet.CoreV1().Pods("fakeNamespace1").Get("fakePod1", metav1.GetOptions{})
    96  		Expect(err).NotTo(HaveOccurred())
    97  
    98  		err = SetNetworkStatus(clientSet, pod, fakeStatus)
    99  		Expect(err).NotTo(HaveOccurred())
   100  
   101  		getStatuses, err := GetNetworkStatus(pod)
   102  		Expect(err).NotTo(HaveOccurred())
   103  		Expect(fakeStatus).To(Equal(getStatuses))
   104  	})
   105  
   106  	It("create network status from cni result", func() {
   107  		cniResult := &cnicurrent.Result{
   108  			CNIVersion: "0.3.1",
   109  			Interfaces: []*cnicurrent.Interface{
   110  				&cnicurrent.Interface{
   111  					Name: "net1",
   112  					Mac: "92:79:27:01:7c:cf",
   113  					Sandbox: "/proc/1123/ns/net",
   114  				},
   115  			},
   116  			IPs: []*cnicurrent.IPConfig{
   117  				{
   118  					Version: "4",
   119  					Address: *EnsureCIDR("1.1.1.3/24"),
   120  				},
   121  				{
   122  					Version: "6",
   123  					Address: *EnsureCIDR("2001::1/64"),
   124  				},
   125  			},
   126  		}
   127  		status, err := CreateNetworkStatus(cniResult, "test-net-attach-def", false)
   128  		Expect(err).NotTo(HaveOccurred())
   129  		Expect(status.Name).To(Equal("test-net-attach-def"))
   130  		Expect(status.Interface).To(Equal("net1"))
   131  		Expect(status.Mac).To(Equal("92:79:27:01:7c:cf"))
   132  		Expect(status.IPs).To(Equal([]string{ "1.1.1.3", "2001::1" }))
   133  	})
   134  
   135  	It("parse network selection element in pod", func() {
   136  		selectionElement := `
   137  		[{
   138  			"name": "test-net-attach-def",
   139  			"interface": "test1"
   140  		}]`
   141  		expectedElement := []*v1.NetworkSelectionElement{
   142  			&v1.NetworkSelectionElement{
   143  				Name: "test-net-attach-def",
   144  				InterfaceRequest: "test1",
   145  				Namespace: "fakeNamespace1",
   146  			},
   147  		}
   148  
   149  		fakePod := &corev1.Pod{
   150  			ObjectMeta: metav1.ObjectMeta{
   151  				Name: "fakePod1",
   152  				Namespace: "fakeNamespace1",
   153  				Annotations: map[string]string{
   154  					"k8s.v1.cni.cncf.io/networks": selectionElement,
   155  				},
   156  			},
   157  			Spec: corev1.PodSpec{
   158  				Containers: []corev1.Container{
   159  					{
   160  						Name: "fakeContainer",
   161  						Image: "fakeImage",
   162  					},
   163  				},
   164  			},
   165  		}
   166  		elem, err := ParsePodNetworkAnnotation(fakePod)
   167  		Expect(err).NotTo(HaveOccurred())
   168  		Expect(elem).To(Equal(expectedElement))
   169  	})
   170  })
   171  

View as plain text