github.com/fafucoder/cilium@v1.6.11/common/utils_test.go (about) 1 // Copyright 2016-2019 Authors of Cilium 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 // +build !privileged_tests 16 17 package common 18 19 import ( 20 "testing" 21 22 "github.com/cilium/cilium/pkg/checker" 23 24 "gopkg.in/check.v1" 25 "io/ioutil" 26 "os" 27 "path/filepath" 28 ) 29 30 // Hook up gocheck into the "go test" runner. 31 func Test(t *testing.T) { 32 check.TestingT(t) 33 } 34 35 type CommonSuite struct{} 36 37 var _ = check.Suite(&CommonSuite{}) 38 39 func (s *CommonSuite) TestC2GoArray(c *check.C) { 40 c.Assert(C2GoArray("0x0, 0x1, 0x2, 0x3"), checker.DeepEquals, []byte{0, 0x01, 0x02, 0x03}) 41 c.Assert(C2GoArray("0x0, 0xff, 0xff, 0xff"), checker.DeepEquals, []byte{0, 0xFF, 0xFF, 0xFF}) 42 c.Assert(C2GoArray("0xa, 0xbc, 0xde, 0xf1"), checker.DeepEquals, []byte{0xa, 0xbc, 0xde, 0xf1}) 43 c.Assert(C2GoArray("0x0"), checker.DeepEquals, []byte{0}) 44 c.Assert(C2GoArray(""), checker.DeepEquals, []byte{}) 45 } 46 47 func (s *CommonSuite) TestMoveNewFilesTo(c *check.C) { 48 oldDir := c.MkDir() 49 newDir := c.MkDir() 50 f1, err := ioutil.TempFile(oldDir, "") 51 c.Assert(err, check.IsNil) 52 f2, err := ioutil.TempFile(oldDir, "") 53 c.Assert(err, check.IsNil) 54 f3, err := ioutil.TempFile(newDir, "") 55 c.Assert(err, check.IsNil) 56 57 // Copy the same f4 file in both directories to make sure the same files 58 // are not moved from the old directory into the new directory. 59 err = ioutil.WriteFile(filepath.Join(oldDir, "foo"), []byte(""), os.FileMode(0644)) 60 c.Assert(err, check.IsNil) 61 err = ioutil.WriteFile(filepath.Join(newDir, "foo"), []byte(""), os.FileMode(0644)) 62 c.Assert(err, check.IsNil) 63 64 compareDir := func(dir string, wantedFiles []string) { 65 files, err := ioutil.ReadDir(dir) 66 c.Assert(err, check.IsNil) 67 filesNames := make([]string, 0, len(wantedFiles)) 68 for _, file := range files { 69 filesNames = append(filesNames, file.Name()) 70 } 71 c.Assert(wantedFiles, checker.DeepEquals, filesNames) 72 } 73 74 type args struct { 75 oldDir string 76 newDir string 77 } 78 tests := []struct { 79 name string 80 args args 81 wantErr bool 82 wantOldDir []string 83 wantNewDir []string 84 }{ 85 { 86 name: "copying from one directory to the other", 87 args: args{ 88 oldDir: oldDir, 89 newDir: newDir, 90 }, 91 wantErr: false, 92 wantOldDir: []string{ 93 "foo", 94 }, 95 wantNewDir: []string{ 96 f1.Name(), 97 f2.Name(), 98 f3.Name(), 99 "foo", 100 }, 101 }, 102 } 103 for _, tt := range tests { 104 if err := MoveNewFilesTo(tt.args.oldDir, tt.args.newDir); (err != nil) != tt.wantErr { 105 c.Assert(err != nil, check.Equals, tt.wantErr) 106 compareDir(tt.args.oldDir, tt.wantOldDir) 107 compareDir(tt.args.newDir, tt.wantNewDir) 108 } 109 } 110 }