github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/network_create_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "golang.org/x/net/context" 14 ) 15 16 func TestNetworkCreateError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 21 _, err := client.NetworkCreate(context.Background(), "mynetwork", types.NetworkCreate{}) 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestNetworkCreate(t *testing.T) { 28 expectedURL := "/networks/create" 29 30 client := &Client{ 31 client: newMockClient(func(req *http.Request) (*http.Response, error) { 32 if !strings.HasPrefix(req.URL.Path, expectedURL) { 33 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 34 } 35 36 if req.Method != "POST" { 37 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 38 } 39 40 content, err := json.Marshal(types.NetworkCreateResponse{ 41 ID: "network_id", 42 Warning: "warning", 43 }) 44 if err != nil { 45 return nil, err 46 } 47 return &http.Response{ 48 StatusCode: http.StatusOK, 49 Body: ioutil.NopCloser(bytes.NewReader(content)), 50 }, nil 51 }), 52 } 53 54 networkResponse, err := client.NetworkCreate(context.Background(), "mynetwork", types.NetworkCreate{ 55 CheckDuplicate: true, 56 Driver: "mydriver", 57 EnableIPv6: true, 58 Internal: true, 59 Options: map[string]string{ 60 "opt-key": "opt-value", 61 }, 62 }) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if networkResponse.ID != "network_id" { 67 t.Fatalf("expected networkResponse.ID to be 'network_id', got %s", networkResponse.ID) 68 } 69 if networkResponse.Warning != "warning" { 70 t.Fatalf("expected networkResponse.Warning to be 'warning', got %s", networkResponse.Warning) 71 } 72 }