github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/exec_test.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	flag "github.com/docker/docker/pkg/mflag"
     9  	"github.com/docker/engine-api/types"
    10  )
    11  
    12  type arguments struct {
    13  	args []string
    14  }
    15  
    16  func TestParseExec(t *testing.T) {
    17  	invalids := map[*arguments]error{
    18  		&arguments{[]string{"-unknown"}}: fmt.Errorf("flag provided but not defined: -unknown"),
    19  		&arguments{[]string{"-u"}}:       fmt.Errorf("flag needs an argument: -u"),
    20  		&arguments{[]string{"--user"}}:   fmt.Errorf("flag needs an argument: --user"),
    21  	}
    22  	valids := map[*arguments]*types.ExecConfig{
    23  		&arguments{
    24  			[]string{"container", "command"},
    25  		}: {
    26  			Container:    "container",
    27  			Cmd:          []string{"command"},
    28  			AttachStdout: true,
    29  			AttachStderr: true,
    30  		},
    31  		&arguments{
    32  			[]string{"container", "command1", "command2"},
    33  		}: {
    34  			Container:    "container",
    35  			Cmd:          []string{"command1", "command2"},
    36  			AttachStdout: true,
    37  			AttachStderr: true,
    38  		},
    39  		&arguments{
    40  			[]string{"-i", "-t", "-u", "uid", "container", "command"},
    41  		}: {
    42  			User:         "uid",
    43  			AttachStdin:  true,
    44  			AttachStdout: true,
    45  			AttachStderr: true,
    46  			Tty:          true,
    47  			Container:    "container",
    48  			Cmd:          []string{"command"},
    49  		},
    50  		&arguments{
    51  			[]string{"-d", "container", "command"},
    52  		}: {
    53  			AttachStdin:  false,
    54  			AttachStdout: false,
    55  			AttachStderr: false,
    56  			Detach:       true,
    57  			Container:    "container",
    58  			Cmd:          []string{"command"},
    59  		},
    60  		&arguments{
    61  			[]string{"-t", "-i", "-d", "container", "command"},
    62  		}: {
    63  			AttachStdin:  false,
    64  			AttachStdout: false,
    65  			AttachStderr: false,
    66  			Detach:       true,
    67  			Tty:          true,
    68  			Container:    "container",
    69  			Cmd:          []string{"command"},
    70  		},
    71  	}
    72  	for invalid, expectedError := range invalids {
    73  		cmd := flag.NewFlagSet("exec", flag.ContinueOnError)
    74  		cmd.ShortUsage = func() {}
    75  		cmd.SetOutput(ioutil.Discard)
    76  		_, err := ParseExec(cmd, invalid.args)
    77  		if err == nil || err.Error() != expectedError.Error() {
    78  			t.Fatalf("Expected an error [%v] for %v, got %v", expectedError, invalid, err)
    79  		}
    80  
    81  	}
    82  	for valid, expectedExecConfig := range valids {
    83  		cmd := flag.NewFlagSet("exec", flag.ContinueOnError)
    84  		cmd.ShortUsage = func() {}
    85  		cmd.SetOutput(ioutil.Discard)
    86  		execConfig, err := ParseExec(cmd, valid.args)
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  		if !compareExecConfig(expectedExecConfig, execConfig) {
    91  			t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig)
    92  		}
    93  	}
    94  }
    95  
    96  func compareExecConfig(config1 *types.ExecConfig, config2 *types.ExecConfig) bool {
    97  	if config1.AttachStderr != config2.AttachStderr {
    98  		return false
    99  	}
   100  	if config1.AttachStdin != config2.AttachStdin {
   101  		return false
   102  	}
   103  	if config1.AttachStdout != config2.AttachStdout {
   104  		return false
   105  	}
   106  	if config1.Container != config2.Container {
   107  		return false
   108  	}
   109  	if config1.Detach != config2.Detach {
   110  		return false
   111  	}
   112  	if config1.Privileged != config2.Privileged {
   113  		return false
   114  	}
   115  	if config1.Tty != config2.Tty {
   116  		return false
   117  	}
   118  	if config1.User != config2.User {
   119  		return false
   120  	}
   121  	if len(config1.Cmd) != len(config2.Cmd) {
   122  		return false
   123  	}
   124  	for index, value := range config1.Cmd {
   125  		if value != config2.Cmd[index] {
   126  			return false
   127  		}
   128  	}
   129  	return true
   130  }