...

Source file src/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils/cniconfig_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  	v1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
    19  	"io/ioutil"
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	. "github.com/onsi/ginkgo"
    26  	. "github.com/onsi/gomega"
    27  )
    28  
    29  func TestCNIConfig(t *testing.T) {
    30  	RegisterFailHandler(Fail)
    31  	RunSpecs(t, "CNI Config")
    32  }
    33  
    34  var _ = Describe("CNI config manipulations", func() {
    35  	var tmpDir string
    36  
    37  	BeforeEach(func() {
    38  		var err error
    39  		tmpDir, err = ioutil.TempDir("", "multus-tmp")
    40  		Expect(err).NotTo(HaveOccurred())
    41  	})
    42  
    43  	AfterEach(func() {
    44  		err := os.RemoveAll(tmpDir)
    45  		Expect(err).NotTo(HaveOccurred())
    46  	})
    47  
    48  	Context("Valid case", func() {
    49  		It("test net-attach-def with valid config", func() {
    50  			cniConfig := `
    51  		{
    52  			"type": "test",
    53  			"name": "testname",
    54  			"version": "0.3.1"
    55  		}
    56  		`
    57  			netattachdef := v1.NetworkAttachmentDefinition{
    58  				ObjectMeta: metav1.ObjectMeta{
    59  					Name:      "test",
    60  					Namespace: "testnamespace",
    61  				},
    62  				Spec: v1.NetworkAttachmentDefinitionSpec{
    63  					Config: cniConfig,
    64  				},
    65  			}
    66  			config, err := GetCNIConfig(&netattachdef, "")
    67  			Expect(err).NotTo(HaveOccurred())
    68  			Expect(config).To(Equal([]byte(cniConfig)))
    69  		})
    70  
    71  		It("test net-attach-def with conf file", func() {
    72  			tmpConfFilePath := filepath.Join(tmpDir, "testCNI.conf")
    73  			cniConfig := `{
    74  			"type": "test",
    75  			"name": "test-net-attach-def",
    76  			"version": "0.3.1"
    77  		}`
    78  			ioutil.WriteFile(tmpConfFilePath, []byte(cniConfig), 0644)
    79  
    80  			netattachdef := v1.NetworkAttachmentDefinition{
    81  				ObjectMeta: metav1.ObjectMeta{
    82  					Name:      "test-net-attach-def",
    83  					Namespace: "testnamespace",
    84  				},
    85  			}
    86  			config, err := GetCNIConfig(&netattachdef, tmpDir)
    87  			Expect(err).NotTo(HaveOccurred())
    88  			Expect(config).To(Equal([]byte(cniConfig)))
    89  		})
    90  
    91  		It("test net-attach-def with conflist file", func() {
    92  			tmpConfFilePath := filepath.Join(tmpDir, "testCNI.conflist")
    93  			cniConfig := `{
    94  			"name": "test-net-attach-def",
    95  			"version": "0.3.1",
    96  			"plugins": [
    97  			{
    98  				"type": "test"
    99  			}]
   100  		}`
   101  			ioutil.WriteFile(tmpConfFilePath, []byte(cniConfig), 0644)
   102  
   103  			netattachdef := v1.NetworkAttachmentDefinition{
   104  				ObjectMeta: metav1.ObjectMeta{
   105  					Name:      "test-net-attach-def",
   106  					Namespace: "testnamespace",
   107  				},
   108  			}
   109  			config, err := GetCNIConfig(&netattachdef, tmpDir)
   110  			Expect(err).NotTo(HaveOccurred())
   111  			Expect(config).To(Equal([]byte(cniConfig)))
   112  		})
   113  	})
   114  
   115  	Context("Invalid case", func() {
   116  		It("test net-attach-def with invalid config", func() {
   117  			cniConfig := `***invalid json file***`
   118  			netattachdef := v1.NetworkAttachmentDefinition{
   119  				ObjectMeta: metav1.ObjectMeta{
   120  					Name:      "test-net-attach-def",
   121  					Namespace: "testnamespace",
   122  				},
   123  				Spec: v1.NetworkAttachmentDefinitionSpec{
   124  					Config: cniConfig,
   125  				},
   126  			}
   127  			_, err := GetCNIConfig(&netattachdef, "")
   128  			Expect(err).To(HaveOccurred())
   129  		})
   130  
   131  		It("test net-attach-def with invalid conf file", func() {
   132  			tmpConfFilePath := filepath.Join(tmpDir, "testCNI.conf")
   133  			cniConfig := `***invalid json file***`
   134  			ioutil.WriteFile(tmpConfFilePath, []byte(cniConfig), 0644)
   135  
   136  			netattachdef := v1.NetworkAttachmentDefinition{
   137  				ObjectMeta: metav1.ObjectMeta{
   138  					Name:      "test-net-attach-def",
   139  					Namespace: "testnamespace",
   140  				},
   141  			}
   142  			_, err := GetCNIConfig(&netattachdef, tmpDir)
   143  			Expect(err).To(HaveOccurred())
   144  		})
   145  
   146  		It("test net-attach-def with invalid conflist file", func() {
   147  			tmpConfFilePath := filepath.Join(tmpDir, "testCNI.conflist")
   148  			cniConfig := `***invalid json file***`
   149  			ioutil.WriteFile(tmpConfFilePath, []byte(cniConfig), 0644)
   150  
   151  			netattachdef := v1.NetworkAttachmentDefinition{
   152  				ObjectMeta: metav1.ObjectMeta{
   153  					Name:      "test-net-attach-def",
   154  					Namespace: "testnamespace",
   155  				},
   156  			}
   157  			_, err := GetCNIConfig(&netattachdef, tmpDir)
   158  			Expect(err).To(HaveOccurred())
   159  		})
   160  	})
   161  
   162  })
   163  

View as plain text