github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/specgenutil/specgenutil_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package specgenutil
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/containers/common/pkg/machine"
    10  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    11  	"github.com/hanks177/podman/v4/pkg/specgen"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestWinPath(t *testing.T) {
    16  	const (
    17  		fail = false
    18  		pass = true
    19  	)
    20  	tests := []struct {
    21  		vol     string
    22  		source  string
    23  		dest    string
    24  		isN     bool
    25  		outcome bool
    26  		mach    string
    27  	}{
    28  		{`C:\Foo:/blah`, "/mnt/c/Foo", "/blah", false, pass, "wsl"},
    29  		{`C:\Foo:/blah`, "/mnt/c/Foo", "/blah", false, fail, ""},
    30  		{`\\?\C:\Foo:/blah`, "/mnt/c/Foo", "/blah", false, pass, "wsl"},
    31  		{`/c/bar:/blah`, "/mnt/c/bar", "/blah", false, pass, "wsl"},
    32  		{`/c/bar:/blah`, "/c/bar", "/blah", false, pass, ""},
    33  		{`/test/this:/blah`, "/test/this", "/blah", false, pass, "wsl"},
    34  		{`c:/bar/something:/other`, "/mnt/c/bar/something", "/other", false, pass, "wsl"},
    35  		{`c:/foo:ro`, "c", "/foo", true, pass, ""},
    36  		{`\\computer\loc:/dest`, "", "", false, fail, "wsl"},
    37  		{`\\.\drive\loc:/target`, "/mnt/wsl/drive/loc", "/target", false, pass, "wsl"},
    38  	}
    39  
    40  	f := func(vol string, mach string) (*specgen.SpecGenerator, error) {
    41  		machine := machine.GetMachineMarker()
    42  		oldEnable, oldType := machine.Enabled, machine.Type
    43  		machine.Enabled, machine.Type = len(mach) > 0, mach
    44  		sg := specgen.NewSpecGenerator("nothing", false)
    45  		err := FillOutSpecGen(sg, &entities.ContainerCreateOptions{
    46  			ImageVolume: "ignore",
    47  			Volume:      []string{vol}}, []string{},
    48  		)
    49  		machine.Enabled, machine.Type = oldEnable, oldType
    50  		return sg, err
    51  	}
    52  
    53  	for _, test := range tests {
    54  		msg := "Checking: " + test.vol
    55  		sg, err := f(test.vol, test.mach)
    56  		if test.outcome == fail {
    57  			assert.NotNil(t, err, msg)
    58  			continue
    59  		}
    60  		if !assert.Nil(t, err, msg) {
    61  			continue
    62  		}
    63  		if test.isN {
    64  			if !assert.Equal(t, 1, len(sg.Volumes), msg) {
    65  				continue
    66  			}
    67  			assert.Equal(t, test.source, sg.Volumes[0].Name, msg)
    68  			assert.Equal(t, test.dest, sg.Volumes[0].Dest, msg)
    69  		} else {
    70  			if !assert.Equal(t, 1, len(sg.Mounts), msg) {
    71  				continue
    72  			}
    73  			assert.Equal(t, test.source, sg.Mounts[0].Source, msg)
    74  			assert.Equal(t, test.dest, sg.Mounts[0].Destination, msg)
    75  		}
    76  	}
    77  }