github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/client/network_inspect_test.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"errors"
     8  	"io"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/api/types/network"
    15  	"github.com/docker/docker/errdefs"
    16  	"gotest.tools/v3/assert"
    17  )
    18  
    19  func TestNetworkInspect(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    22  			if req.Method != http.MethodGet {
    23  				return nil, errors.New("expected GET method, got " + req.Method)
    24  			}
    25  			if req.URL.Path == "/networks/" {
    26  				return errorMock(http.StatusInternalServerError, "client should not make a request for empty IDs")(req)
    27  			}
    28  			if strings.HasPrefix(req.URL.Path, "/networks/unknown") {
    29  				return errorMock(http.StatusNotFound, "Error: No such network: unknown")(req)
    30  			}
    31  			if strings.HasPrefix(req.URL.Path, "/networks/test-500-response") {
    32  				return errorMock(http.StatusInternalServerError, "Server error")(req)
    33  			}
    34  			// other test-cases all use "network_id"
    35  			if !strings.HasPrefix(req.URL.Path, "/networks/network_id") {
    36  				return nil, errors.New("expected URL '/networks/network_id', got " + req.URL.Path)
    37  			}
    38  			if strings.Contains(req.URL.RawQuery, "scope=global") {
    39  				return errorMock(http.StatusNotFound, "Error: No such network: network_id")(req)
    40  			}
    41  			var (
    42  				content []byte
    43  				err     error
    44  			)
    45  			if strings.Contains(req.URL.RawQuery, "verbose=true") {
    46  				s := map[string]network.ServiceInfo{
    47  					"web": {},
    48  				}
    49  				content, err = json.Marshal(types.NetworkResource{
    50  					Name:     "mynetwork",
    51  					Services: s,
    52  				})
    53  			} else {
    54  				content, err = json.Marshal(types.NetworkResource{
    55  					Name: "mynetwork",
    56  				})
    57  			}
    58  			if err != nil {
    59  				return nil, err
    60  			}
    61  			return &http.Response{
    62  				Header:     http.Header{"Content-Type": []string{"application/json"}},
    63  				StatusCode: http.StatusOK,
    64  				Body:       io.NopCloser(bytes.NewReader(content)),
    65  			}, nil
    66  		}),
    67  	}
    68  
    69  	t.Run("empty ID", func(t *testing.T) {
    70  		// verify that the client does not create a request if the network-ID/name is empty.
    71  		_, err := client.NetworkInspect(context.Background(), "", types.NetworkInspectOptions{})
    72  		assert.Check(t, IsErrNotFound(err))
    73  	})
    74  	t.Run("no options", func(t *testing.T) {
    75  		r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{})
    76  		assert.NilError(t, err)
    77  		assert.Equal(t, r.Name, "mynetwork")
    78  	})
    79  	t.Run("verbose", func(t *testing.T) {
    80  		r, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Verbose: true})
    81  		assert.NilError(t, err)
    82  		assert.Equal(t, r.Name, "mynetwork")
    83  		_, ok := r.Services["web"]
    84  		if !ok {
    85  			t.Fatalf("expected service `web` missing in the verbose output")
    86  		}
    87  	})
    88  	t.Run("global scope", func(t *testing.T) {
    89  		_, err := client.NetworkInspect(context.Background(), "network_id", types.NetworkInspectOptions{Scope: "global"})
    90  		assert.ErrorContains(t, err, "Error: No such network: network_id")
    91  		assert.Check(t, IsErrNotFound(err))
    92  	})
    93  	t.Run("unknown network", func(t *testing.T) {
    94  		_, err := client.NetworkInspect(context.Background(), "unknown", types.NetworkInspectOptions{})
    95  		assert.ErrorContains(t, err, "Error: No such network: unknown")
    96  		assert.Check(t, IsErrNotFound(err))
    97  	})
    98  	t.Run("server error", func(t *testing.T) {
    99  		// Just testing that an internal server error is converted correctly by the client
   100  		_, err := client.NetworkInspect(context.Background(), "test-500-response", types.NetworkInspectOptions{})
   101  		assert.Check(t, errdefs.IsSystem(err))
   102  	})
   103  }