github.com/olljanat/moby@v1.13.1/cli/command/container/exec_test.go (about)

     1  package container
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/api/types"
     7  )
     8  
     9  type arguments struct {
    10  	options execOptions
    11  	execCmd []string
    12  }
    13  
    14  func TestParseExec(t *testing.T) {
    15  	valids := map[*arguments]*types.ExecConfig{
    16  		&arguments{
    17  			execCmd: []string{"command"},
    18  		}: {
    19  			Cmd:          []string{"command"},
    20  			AttachStdout: true,
    21  			AttachStderr: true,
    22  		},
    23  		&arguments{
    24  			execCmd: []string{"command1", "command2"},
    25  		}: {
    26  			Cmd:          []string{"command1", "command2"},
    27  			AttachStdout: true,
    28  			AttachStderr: true,
    29  		},
    30  		&arguments{
    31  			options: execOptions{
    32  				interactive: true,
    33  				tty:         true,
    34  				user:        "uid",
    35  			},
    36  			execCmd: []string{"command"},
    37  		}: {
    38  			User:         "uid",
    39  			AttachStdin:  true,
    40  			AttachStdout: true,
    41  			AttachStderr: true,
    42  			Tty:          true,
    43  			Cmd:          []string{"command"},
    44  		},
    45  		&arguments{
    46  			options: execOptions{
    47  				detach: true,
    48  			},
    49  			execCmd: []string{"command"},
    50  		}: {
    51  			AttachStdin:  false,
    52  			AttachStdout: false,
    53  			AttachStderr: false,
    54  			Detach:       true,
    55  			Cmd:          []string{"command"},
    56  		},
    57  		&arguments{
    58  			options: execOptions{
    59  				tty:         true,
    60  				interactive: true,
    61  				detach:      true,
    62  			},
    63  			execCmd: []string{"command"},
    64  		}: {
    65  			AttachStdin:  false,
    66  			AttachStdout: false,
    67  			AttachStderr: false,
    68  			Detach:       true,
    69  			Tty:          true,
    70  			Cmd:          []string{"command"},
    71  		},
    72  	}
    73  
    74  	for valid, expectedExecConfig := range valids {
    75  		execConfig, err := parseExec(&valid.options, valid.execCmd)
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  		if !compareExecConfig(expectedExecConfig, execConfig) {
    80  			t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig)
    81  		}
    82  	}
    83  }
    84  
    85  func compareExecConfig(config1 *types.ExecConfig, config2 *types.ExecConfig) bool {
    86  	if config1.AttachStderr != config2.AttachStderr {
    87  		return false
    88  	}
    89  	if config1.AttachStdin != config2.AttachStdin {
    90  		return false
    91  	}
    92  	if config1.AttachStdout != config2.AttachStdout {
    93  		return false
    94  	}
    95  	if config1.Detach != config2.Detach {
    96  		return false
    97  	}
    98  	if config1.Privileged != config2.Privileged {
    99  		return false
   100  	}
   101  	if config1.Tty != config2.Tty {
   102  		return false
   103  	}
   104  	if config1.User != config2.User {
   105  		return false
   106  	}
   107  	if len(config1.Cmd) != len(config2.Cmd) {
   108  		return false
   109  	}
   110  	for index, value := range config1.Cmd {
   111  		if value != config2.Cmd[index] {
   112  			return false
   113  		}
   114  	}
   115  	return true
   116  }