gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/namespace/builder_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  	"bytes"
     9  	"io"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"path"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/gojuno/minimock"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  type args struct {
    22  	ns Namespace
    23  }
    24  
    25  func newTestBuilder(name string) func(t minimock.Tester) *Builder {
    26  	return func(t minimock.Tester) *Builder {
    27  		wd, err := os.Getwd()
    28  		if err != nil {
    29  			t.Error(err)
    30  			return nil
    31  		}
    32  		f, err := os.Open("testdata/" + name)
    33  		if err != nil {
    34  			t.Error(err)
    35  			return nil
    36  		}
    37  		file, err := Parse(f)
    38  		if err != nil {
    39  			t.Error(err)
    40  			return nil
    41  		}
    42  		return &Builder{
    43  			dir:  wd,
    44  			file: file,
    45  			open: func(path string) (io.Reader, error) { return bytes.NewBuffer(nil), nil },
    46  		}
    47  	}
    48  }
    49  func mockNSBuilder(t minimock.Tester) args { return args{&noopNS{}} }
    50  func TestBuilder_buildNS(t *testing.T) {
    51  
    52  	tests := []struct {
    53  		name    string
    54  		init    func(t minimock.Tester) *Builder
    55  		inspect func(r *Builder, t *testing.T) //inspects *Builder after execution of buildNS
    56  
    57  		args func(t minimock.Tester) args
    58  
    59  		wantErr    bool
    60  		inspectErr func(err error, t *testing.T) //use for more precise error evaluation
    61  	}{}
    62  	files, err := ioutil.ReadDir("testdata")
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  	for _, file := range files {
    67  		tests = append(tests, struct {
    68  			name    string
    69  			init    func(t minimock.Tester) *Builder
    70  			inspect func(r *Builder, t *testing.T) //inspects *Builder after execution of buildNS
    71  
    72  			args func(t minimock.Tester) args
    73  
    74  			wantErr    bool
    75  			inspectErr func(err error, t *testing.T) //use for more precise error evaluation
    76  		}{
    77  			name:    file.Name(),
    78  			init:    newTestBuilder(file.Name()),
    79  			args:    mockNSBuilder,
    80  			wantErr: path.Ext(file.Name()) == ".wrong",
    81  		})
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			mc := minimock.NewController(t)
    87  			defer mc.Wait(time.Second)
    88  
    89  			tArgs := tt.args(mc)
    90  			receiver := tt.init(mc)
    91  
    92  			err := receiver.buildNS(tArgs.ns)
    93  
    94  			if tt.inspect != nil {
    95  				tt.inspect(receiver, t)
    96  			}
    97  
    98  			if tt.wantErr {
    99  				if assert.Error(t, err) && tt.inspectErr != nil {
   100  					tt.inspectErr(err, t)
   101  				}
   102  			} else {
   103  				assert.NoError(t, err)
   104  			}
   105  
   106  		})
   107  	}
   108  }
   109  
   110  type noopNS struct{}
   111  
   112  func (m *noopNS) Bind(new string, old string, option mountflag) error        { return nil }
   113  func (m *noopNS) Mount(servername, old, spec string, option mountflag) error { return nil }
   114  func (m *noopNS) Unmount(new string, old string) error                       { return nil }
   115  func (m *noopNS) Import(host string, remotepath string, mountpoint string, options mountflag) error {
   116  	return nil
   117  }
   118  func (m *noopNS) Clear() error            { return nil }
   119  func (m *noopNS) Chdir(path string) error { return nil }