github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/stack/swarm/deploy_composefile_test.go (about)

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