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