...

Source file src/github.com/Microsoft/hcsshim/internal/cmd/io_binary_test.go

Documentation: github.com/Microsoft/hcsshim/internal/cmd

     1  //go:build windows
     2  
     3  package cmd
     4  
     5  import (
     6  	"context"
     7  	"net/url"
     8  	"testing"
     9  )
    10  
    11  func Test_newBinaryCmd_Key_Value_Pair(t *testing.T) {
    12  	ctx, cancel := context.WithCancel(context.Background())
    13  	defer cancel()
    14  
    15  	type config struct {
    16  		name      string
    17  		urlString string
    18  		expected  string
    19  	}
    20  
    21  	tests := []*config{
    22  		{
    23  			name:      "Path_With_Fwd_Slashes",
    24  			urlString: "binary:///executable?-key=value",
    25  			expected:  `\executable -key value`,
    26  		},
    27  		{
    28  			name:      "Path_With_Back_Slashes",
    29  			urlString: `binary:///\executable?-key=value`,
    30  			expected:  `\executable -key value`,
    31  		},
    32  		{
    33  			name:      "Clean_Path_With_Dots_And_Multiple_Fwd_Slashes",
    34  			urlString: "binary:///../path/to///to/../executable",
    35  			expected:  `\path\to\executable`,
    36  		},
    37  		{
    38  			name:      "Clean_Path_With_Dots_And_Multiple_Back_Slashes",
    39  			urlString: `binary:///..\path\to\\\from\..\executable`,
    40  			expected:  `\path\to\executable`,
    41  		},
    42  		{
    43  			name:      "Stripped_Path_With_Forward_Slashes",
    44  			urlString: "binary:///D:/path/to/executable",
    45  			expected:  `D:\path\to\executable`,
    46  		},
    47  		{
    48  			name:      "Stripped_Path_With_Back_Slashes",
    49  			urlString: `binary:///D:\path\to\executable`,
    50  			expected:  `D:\path\to\executable`,
    51  		},
    52  	}
    53  
    54  	for _, cfg := range tests {
    55  		t.Run(cfg.name, func(t *testing.T) {
    56  			u, err := url.Parse(cfg.urlString)
    57  			if err != nil {
    58  				t.Fatalf("failed to parse url: %s", cfg.urlString)
    59  			}
    60  
    61  			cmd, err := newBinaryCmd(ctx, u, nil)
    62  			if err != nil {
    63  				t.Fatalf("error while creating cmd: %s", err)
    64  			}
    65  
    66  			if cmd.String() != cfg.expected {
    67  				t.Fatalf("failed to create cmd. expected: '%s', actual '%s'", cfg.expected, cmd.String())
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func Test_newBinaryCmd_Empty_Path(t *testing.T) {
    74  	ctx, cancel := context.WithCancel(context.Background())
    75  	defer cancel()
    76  
    77  	u, _ := url.Parse("scheme://")
    78  
    79  	cmd, err := newBinaryCmd(ctx, u, nil)
    80  
    81  	if cmd != nil {
    82  		t.Fatalf("cmd is not nil: %s", cmd)
    83  	}
    84  
    85  	if err == nil {
    86  		t.Fatalf("err is not expected to be nil")
    87  	}
    88  }
    89  
    90  func Test_newBinaryCmd_flags(t *testing.T) {
    91  	ctx, cancel := context.WithCancel(context.Background())
    92  	defer cancel()
    93  
    94  	urlString := "schema:///path/to/binary?foo&bar&baz"
    95  	uri, _ := url.Parse(urlString)
    96  
    97  	expectedPath := `\path\to\binary`
    98  	expectedFlags := map[string]bool{"foo": true, "bar": true, "baz": true}
    99  
   100  	cmd, err := newBinaryCmd(ctx, uri, nil)
   101  
   102  	if err != nil {
   103  		t.Fatalf("error creating binary cmd: %s", err)
   104  	}
   105  
   106  	if cmd.Path != expectedPath {
   107  		t.Fatalf("invalid cmd path: %s", cmd.Path)
   108  	}
   109  
   110  	for _, f := range cmd.Args[1:] {
   111  		if _, ok := expectedFlags[f]; !ok {
   112  			t.Fatalf("flag missing: '%s' in cmd: '%s'", f, cmd.String())
   113  		}
   114  	}
   115  }
   116  

View as plain text