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