github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/stack/swarm/deploy_composefile_test.go (about)

     1  package swarm
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/internal/test/network"
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/pkg/errors"
    10  	"gotest.tools/v3/assert"
    11  )
    12  
    13  type notFound struct {
    14  	error
    15  }
    16  
    17  func (n notFound) NotFound() bool {
    18  	return true
    19  }
    20  
    21  func TestValidateExternalNetworks(t *testing.T) {
    22  	var testcases = []struct {
    23  		inspectResponse types.NetworkResource
    24  		inspectError    error
    25  		expectedMsg     string
    26  		network         string
    27  	}{
    28  		{
    29  			inspectError: notFound{},
    30  			expectedMsg:  "could not be found. You need to create a swarm-scoped network",
    31  		},
    32  		{
    33  			inspectError: errors.New("Unexpected"),
    34  			expectedMsg:  "Unexpected",
    35  		},
    36  		// FIXME(vdemeester) that doesn't work under windows, the check needs to be smarter
    37  		/*
    38  			{
    39  				inspectError: errors.New("host net does not exist on swarm classic"),
    40  				network:      "host",
    41  			},
    42  		*/
    43  		{
    44  			network:     "user",
    45  			expectedMsg: "is not in the right scope",
    46  		},
    47  		{
    48  			network:         "user",
    49  			inspectResponse: types.NetworkResource{Scope: "swarm"},
    50  		},
    51  	}
    52  
    53  	for _, testcase := range testcases {
    54  		fakeClient := &network.FakeClient{
    55  			NetworkInspectFunc: func(_ context.Context, _ string, _ types.NetworkInspectOptions) (types.NetworkResource, error) {
    56  				return testcase.inspectResponse, testcase.inspectError
    57  			},
    58  		}
    59  		networks := []string{testcase.network}
    60  		err := validateExternalNetworks(context.Background(), fakeClient, networks)
    61  		if testcase.expectedMsg == "" {
    62  			assert.NilError(t, err)
    63  		} else {
    64  			assert.ErrorContains(t, err, testcase.expectedMsg)
    65  		}
    66  	}
    67  }