github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/namespace/namespace_test.go (about)

     1  // Copyright 2020-2021 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  	"fmt"
     9  	"os"
    10  	"path"
    11  	"reflect"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/gojuno/minimock/v3"
    16  )
    17  
    18  type arg struct {
    19  	call syzcall
    20  	flag mountflag
    21  	args []string
    22  }
    23  
    24  func checkArgs(t minimock.Tester, args arg, mod Modifier) error {
    25  	call := mod.(*cmd)
    26  	if args.call != call.syscall {
    27  		return fmt.Errorf("call number are not equal")
    28  	}
    29  	if !reflect.DeepEqual(args.args, call.args) {
    30  		return fmt.Errorf("args are not equal")
    31  	}
    32  	if args.flag != call.flag {
    33  		t.Error("flags are not equal")
    34  		t.Errorf(`args.flag = %v, want %v`, args.flag, call.flag)
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func TestOPS_NewNS(t *testing.T) {
    41  	type args struct {
    42  		ns Namespace
    43  		c  chan Modifier
    44  	}
    45  	tests := []struct {
    46  		name    string
    47  		init    func(t minimock.Tester) File
    48  		inspect func(r File, t *testing.T) // inspects OPS after execution of NewNS
    49  
    50  		args func(t minimock.Tester) args
    51  
    52  		wantErr    bool
    53  		inspectErr func(err error, t *testing.T) // use for more precise error evaluation
    54  	}{
    55  		{
    56  			name: "simple",
    57  			init: func(t minimock.Tester) File {
    58  				f, _ := os.Open("testdata/namespace.simple")
    59  				ops, _ := Parse(f)
    60  				return ops
    61  			},
    62  		},
    63  		{
    64  			name: "ftp",
    65  			init: func(t minimock.Tester) File {
    66  				f, _ := os.Open("testdata/namespace.ftp")
    67  				ops, _ := Parse(f)
    68  				return ops
    69  			},
    70  		},
    71  	}
    72  
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			mc := minimock.NewController(t)
    76  			defer mc.Wait(time.Second)
    77  			wd, _ := os.Getwd()
    78  			receiver := tt.init(mc)
    79  			calls := make(chan Modifier)
    80  
    81  			mock := mockNS{t, calls}
    82  			go func(ops File) {
    83  				for _, call := range ops {
    84  					calls <- call
    85  				}
    86  			}(receiver)
    87  			b := &Builder{
    88  				dir:  path.Join(wd, "testdata"),
    89  				open: open1,
    90  			}
    91  			err := b.buildNS(&mock)
    92  
    93  			mc.Finish()
    94  			if tt.wantErr {
    95  				if err != nil && tt.inspectErr != nil {
    96  					tt.inspectErr(err, t)
    97  				}
    98  			} else if err != nil {
    99  				t.Errorf(`b.buildNS(%v) = %v, want nil`, &mock, err)
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  type mockNS struct {
   106  	t     *testing.T
   107  	calls chan Modifier
   108  }
   109  
   110  func (m *mockNS) Bind(new string, old string, option mountflag) error {
   111  	return checkArgs(m.t, arg{
   112  		args: []string{new, old},
   113  		flag: option,
   114  		call: BIND,
   115  	}, <-m.calls)
   116  }
   117  
   118  func (m *mockNS) Mount(servername, old, spec string, option mountflag) error {
   119  	return checkArgs(m.t, arg{
   120  		args: []string{servername, old},
   121  		flag: option,
   122  		call: MOUNT,
   123  	}, <-m.calls)
   124  }
   125  
   126  func (m *mockNS) Unmount(new string, old string) error {
   127  	return checkArgs(m.t, arg{
   128  		args: []string{new, old},
   129  		flag: 0,
   130  		call: UNMOUNT,
   131  	}, <-m.calls)
   132  }
   133  
   134  func (m *mockNS) Import(host string, remotepath string, mountpoint string, options mountflag) error {
   135  	return checkArgs(m.t, arg{
   136  		args: []string{host, remotepath, mountpoint},
   137  		flag: options,
   138  		call: IMPORT,
   139  	}, <-m.calls)
   140  }
   141  
   142  func (m *mockNS) Clear() error {
   143  	panic("not implemented") // TODO: Implement
   144  }
   145  
   146  func (m *mockNS) Chdir(path string) error {
   147  	return checkArgs(m.t, arg{
   148  		args: []string{path},
   149  		flag: 0,
   150  		call: CHDIR,
   151  	}, <-m.calls)
   152  }