github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/client/image_push_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "strings" 9 "testing" 10 11 "golang.org/x/net/context" 12 13 "github.com/docker/docker/api/types" 14 ) 15 16 func TestImagePushReferenceError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(func(req *http.Request) (*http.Response, error) { 19 return nil, nil 20 }), 21 } 22 // An empty reference is an invalid reference 23 _, err := client.ImagePush(context.Background(), "", types.ImagePushOptions{}) 24 if err == nil || !strings.Contains(err.Error(), "invalid reference format") { 25 t.Fatalf("expected an error, got %v", err) 26 } 27 // An canonical reference cannot be pushed 28 _, err = client.ImagePush(context.Background(), "repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", types.ImagePushOptions{}) 29 if err == nil || err.Error() != "cannot push a digest reference" { 30 t.Fatalf("expected an error, got %v", err) 31 } 32 } 33 34 func TestImagePushAnyError(t *testing.T) { 35 client := &Client{ 36 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 37 } 38 _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{}) 39 if err == nil || err.Error() != "Error response from daemon: Server error" { 40 t.Fatalf("expected a Server Error, got %v", err) 41 } 42 } 43 44 func TestImagePushStatusUnauthorizedError(t *testing.T) { 45 client := &Client{ 46 client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), 47 } 48 _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{}) 49 if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { 50 t.Fatalf("expected an Unauthorized Error, got %v", err) 51 } 52 } 53 54 func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { 55 client := &Client{ 56 client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), 57 } 58 privilegeFunc := func() (string, error) { 59 return "", fmt.Errorf("Error requesting privilege") 60 } 61 _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{ 62 PrivilegeFunc: privilegeFunc, 63 }) 64 if err == nil || err.Error() != "Error requesting privilege" { 65 t.Fatalf("expected an error requesting privilege, got %v", err) 66 } 67 } 68 69 func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { 70 client := &Client{ 71 client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), 72 } 73 privilegeFunc := func() (string, error) { 74 return "a-auth-header", nil 75 } 76 _, err := client.ImagePush(context.Background(), "myimage", types.ImagePushOptions{ 77 PrivilegeFunc: privilegeFunc, 78 }) 79 if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { 80 t.Fatalf("expected an Unauthorized Error, got %v", err) 81 } 82 } 83 84 func TestImagePushWithPrivilegedFuncNoError(t *testing.T) { 85 expectedURL := "/images/myimage/push" 86 client := &Client{ 87 client: newMockClient(func(req *http.Request) (*http.Response, error) { 88 if !strings.HasPrefix(req.URL.Path, expectedURL) { 89 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 90 } 91 auth := req.Header.Get("X-Registry-Auth") 92 if auth == "NotValid" { 93 return &http.Response{ 94 StatusCode: http.StatusUnauthorized, 95 Body: ioutil.NopCloser(bytes.NewReader([]byte("Invalid credentials"))), 96 }, nil 97 } 98 if auth != "IAmValid" { 99 return nil, fmt.Errorf("Invalid auth header : expected %s, got %s", "IAmValid", auth) 100 } 101 query := req.URL.Query() 102 tag := query.Get("tag") 103 if tag != "tag" { 104 return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", "tag", tag) 105 } 106 return &http.Response{ 107 StatusCode: http.StatusOK, 108 Body: ioutil.NopCloser(bytes.NewReader([]byte("hello world"))), 109 }, nil 110 }), 111 } 112 privilegeFunc := func() (string, error) { 113 return "IAmValid", nil 114 } 115 resp, err := client.ImagePush(context.Background(), "myimage:tag", types.ImagePushOptions{ 116 RegistryAuth: "NotValid", 117 PrivilegeFunc: privilegeFunc, 118 }) 119 if err != nil { 120 t.Fatal(err) 121 } 122 body, err := ioutil.ReadAll(resp) 123 if err != nil { 124 t.Fatal(err) 125 } 126 if string(body) != "hello world" { 127 t.Fatalf("expected 'hello world', got %s", string(body)) 128 } 129 } 130 131 func TestImagePushWithoutErrors(t *testing.T) { 132 expectedOutput := "hello world" 133 expectedURLFormat := "/images/%s/push" 134 pullCases := []struct { 135 reference string 136 expectedImage string 137 expectedTag string 138 }{ 139 { 140 reference: "myimage", 141 expectedImage: "myimage", 142 expectedTag: "", 143 }, 144 { 145 reference: "myimage:tag", 146 expectedImage: "myimage", 147 expectedTag: "tag", 148 }, 149 } 150 for _, pullCase := range pullCases { 151 client := &Client{ 152 client: newMockClient(func(req *http.Request) (*http.Response, error) { 153 expectedURL := fmt.Sprintf(expectedURLFormat, pullCase.expectedImage) 154 if !strings.HasPrefix(req.URL.Path, expectedURL) { 155 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 156 } 157 query := req.URL.Query() 158 tag := query.Get("tag") 159 if tag != pullCase.expectedTag { 160 return nil, fmt.Errorf("tag not set in URL query properly. Expected '%s', got %s", pullCase.expectedTag, tag) 161 } 162 return &http.Response{ 163 StatusCode: http.StatusOK, 164 Body: ioutil.NopCloser(bytes.NewReader([]byte(expectedOutput))), 165 }, nil 166 }), 167 } 168 resp, err := client.ImagePush(context.Background(), pullCase.reference, types.ImagePushOptions{}) 169 if err != nil { 170 t.Fatal(err) 171 } 172 body, err := ioutil.ReadAll(resp) 173 if err != nil { 174 t.Fatal(err) 175 } 176 if string(body) != expectedOutput { 177 t.Fatalf("expected '%s', got %s", expectedOutput, string(body)) 178 } 179 } 180 }