github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/container/attach_test.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/internal/test"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/api/types/container"
    12  	"github.com/pkg/errors"
    13  	"gotest.tools/assert"
    14  )
    15  
    16  func TestNewAttachCommandErrors(t *testing.T) {
    17  	testCases := []struct {
    18  		name                 string
    19  		args                 []string
    20  		expectedError        string
    21  		containerInspectFunc func(img string) (types.ContainerJSON, error)
    22  	}{
    23  		{
    24  			name:          "client-error",
    25  			args:          []string{"5cb5bb5e4a3b"},
    26  			expectedError: "something went wrong",
    27  			containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
    28  				return types.ContainerJSON{}, errors.Errorf("something went wrong")
    29  			},
    30  		},
    31  		{
    32  			name:          "client-stopped",
    33  			args:          []string{"5cb5bb5e4a3b"},
    34  			expectedError: "You cannot attach to a stopped container",
    35  			containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
    36  				c := types.ContainerJSON{}
    37  				c.ContainerJSONBase = &types.ContainerJSONBase{}
    38  				c.ContainerJSONBase.State = &types.ContainerState{Running: false}
    39  				return c, nil
    40  			},
    41  		},
    42  		{
    43  			name:          "client-paused",
    44  			args:          []string{"5cb5bb5e4a3b"},
    45  			expectedError: "You cannot attach to a paused container",
    46  			containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
    47  				c := types.ContainerJSON{}
    48  				c.ContainerJSONBase = &types.ContainerJSONBase{}
    49  				c.ContainerJSONBase.State = &types.ContainerState{
    50  					Running: true,
    51  					Paused:  true,
    52  				}
    53  				return c, nil
    54  			},
    55  		},
    56  		{
    57  			name:          "client-restarting",
    58  			args:          []string{"5cb5bb5e4a3b"},
    59  			expectedError: "You cannot attach to a restarting container",
    60  			containerInspectFunc: func(containerID string) (types.ContainerJSON, error) {
    61  				c := types.ContainerJSON{}
    62  				c.ContainerJSONBase = &types.ContainerJSONBase{}
    63  				c.ContainerJSONBase.State = &types.ContainerState{
    64  					Running:    true,
    65  					Paused:     false,
    66  					Restarting: true,
    67  				}
    68  				return c, nil
    69  			},
    70  		},
    71  	}
    72  	for _, tc := range testCases {
    73  		cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{inspectFunc: tc.containerInspectFunc}))
    74  		cmd.SetOutput(ioutil.Discard)
    75  		cmd.SetArgs(tc.args)
    76  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    77  	}
    78  }
    79  
    80  func TestGetExitStatus(t *testing.T) {
    81  	var (
    82  		expectedErr = fmt.Errorf("unexpected error")
    83  		errC        = make(chan error, 1)
    84  		resultC     = make(chan container.ContainerWaitOKBody, 1)
    85  	)
    86  
    87  	testcases := []struct {
    88  		result        *container.ContainerWaitOKBody
    89  		err           error
    90  		expectedError error
    91  	}{
    92  		{
    93  			result: &container.ContainerWaitOKBody{
    94  				StatusCode: 0,
    95  			},
    96  		},
    97  		{
    98  			err:           expectedErr,
    99  			expectedError: expectedErr,
   100  		},
   101  		{
   102  			result: &container.ContainerWaitOKBody{
   103  				Error: &container.ContainerWaitOKBodyError{Message: expectedErr.Error()},
   104  			},
   105  			expectedError: expectedErr,
   106  		},
   107  		{
   108  			result: &container.ContainerWaitOKBody{
   109  				StatusCode: 15,
   110  			},
   111  			expectedError: cli.StatusError{StatusCode: 15},
   112  		},
   113  	}
   114  
   115  	for _, testcase := range testcases {
   116  		if testcase.err != nil {
   117  			errC <- testcase.err
   118  		}
   119  		if testcase.result != nil {
   120  			resultC <- *testcase.result
   121  		}
   122  		err := getExitStatus(errC, resultC)
   123  		if testcase.expectedError == nil {
   124  			assert.NilError(t, err)
   125  		} else {
   126  			assert.Error(t, err, testcase.expectedError.Error())
   127  		}
   128  	}
   129  }