github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/namespace/namespace_test.go (about) 1 // Copyright 2020 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package namespace 6 7 import ( 8 "os" 9 "path" 10 "testing" 11 "time" 12 13 "github.com/gojuno/minimock" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 type arg struct { 18 call syzcall 19 flag mountflag 20 args []string 21 } 22 23 func checkArgs(t minimock.Tester, args arg, mod Modifier) error { 24 call := mod.(*cmd) 25 assert.EqualValues(t, args.call, call.syscall, "call number are not equal") 26 assert.EqualValues(t, args.args, call.args, "args are not equal") 27 assert.Equal(t, args.flag, call.flag, "flags are not equal") 28 return nil 29 } 30 func TestOPS_NewNS(t *testing.T) { 31 type args struct { 32 ns Namespace 33 c chan Modifier 34 } 35 tests := []struct { 36 name string 37 init func(t minimock.Tester) File 38 inspect func(r File, t *testing.T) //inspects OPS after execution of NewNS 39 40 args func(t minimock.Tester) args 41 42 wantErr bool 43 inspectErr func(err error, t *testing.T) //use for more precise error evaluation 44 }{ 45 { 46 name: "simple", 47 init: func(t minimock.Tester) File { 48 f, _ := os.Open("testdata/namespace.simple") 49 ops, _ := Parse(f) 50 return ops 51 }, 52 }, 53 { 54 name: "ftp", 55 init: func(t minimock.Tester) File { 56 f, _ := os.Open("testdata/namespace.ftp") 57 ops, _ := Parse(f) 58 return ops 59 }, 60 }, 61 } 62 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 mc := minimock.NewController(t) 66 defer mc.Wait(time.Second) 67 wd, _ := os.Getwd() 68 receiver := tt.init(mc) 69 calls := make(chan Modifier) 70 71 mock := mockNS{t, calls} 72 go func(ops File) { 73 for _, call := range ops { 74 calls <- call 75 } 76 }(receiver) 77 b := &Builder{ 78 dir: path.Join(wd, "testdata"), 79 open: open1, 80 } 81 err := b.buildNS(&mock) 82 83 mc.Finish() 84 if tt.wantErr { 85 if assert.Error(t, err) && tt.inspectErr != nil { 86 tt.inspectErr(err, t) 87 } 88 } else { 89 assert.NoError(t, err) 90 } 91 92 }) 93 } 94 } 95 96 type mockNS struct { 97 t *testing.T 98 calls chan Modifier 99 } 100 101 func (m *mockNS) Bind(new string, old string, option mountflag) error { 102 return checkArgs(m.t, arg{ 103 args: []string{new, old}, 104 flag: option, 105 call: BIND, 106 }, <-m.calls) 107 } 108 109 func (m *mockNS) Mount(servername, old, spec string, option mountflag) error { 110 return checkArgs(m.t, arg{ 111 args: []string{servername, old}, 112 flag: option, 113 call: MOUNT, 114 }, <-m.calls) 115 } 116 117 func (m *mockNS) Unmount(new string, old string) error { 118 return checkArgs(m.t, arg{ 119 args: []string{new, old}, 120 flag: 0, 121 call: UNMOUNT, 122 }, <-m.calls) 123 } 124 125 func (m *mockNS) Import(host string, remotepath string, mountpoint string, options mountflag) error { 126 return checkArgs(m.t, arg{ 127 args: []string{host, remotepath, mountpoint}, 128 flag: options, 129 call: IMPORT, 130 }, <-m.calls) 131 } 132 133 func (m *mockNS) Clear() error { 134 panic("not implemented") // TODO: Implement 135 } 136 137 func (m *mockNS) Chdir(path string) error { 138 return checkArgs(m.t, arg{ 139 args: []string{path}, 140 flag: 0, 141 call: CHDIR, 142 }, <-m.calls) 143 }