github.com/skanehira/moby@v17.12.1-ce-rc2+incompatible/client/image_search_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 "github.com/docker/docker/api/types/filters" 14 "github.com/docker/docker/api/types/registry" 15 "golang.org/x/net/context" 16 ) 17 18 func TestImageSearchAnyError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 _, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{}) 23 if err == nil || err.Error() != "Error response from daemon: Server error" { 24 t.Fatalf("expected a Server Error, got %v", err) 25 } 26 } 27 28 func TestImageSearchStatusUnauthorizedError(t *testing.T) { 29 client := &Client{ 30 client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), 31 } 32 _, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{}) 33 if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { 34 t.Fatalf("expected an Unauthorized Error, got %v", err) 35 } 36 } 37 38 func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { 39 client := &Client{ 40 client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), 41 } 42 privilegeFunc := func() (string, error) { 43 return "", fmt.Errorf("Error requesting privilege") 44 } 45 _, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{ 46 PrivilegeFunc: privilegeFunc, 47 }) 48 if err == nil || err.Error() != "Error requesting privilege" { 49 t.Fatalf("expected an error requesting privilege, got %v", err) 50 } 51 } 52 53 func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { 54 client := &Client{ 55 client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), 56 } 57 privilegeFunc := func() (string, error) { 58 return "a-auth-header", nil 59 } 60 _, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{ 61 PrivilegeFunc: privilegeFunc, 62 }) 63 if err == nil || err.Error() != "Error response from daemon: Unauthorized error" { 64 t.Fatalf("expected an Unauthorized Error, got %v", err) 65 } 66 } 67 68 func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) { 69 expectedURL := "/images/search" 70 client := &Client{ 71 client: newMockClient(func(req *http.Request) (*http.Response, error) { 72 if !strings.HasPrefix(req.URL.Path, expectedURL) { 73 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 74 } 75 auth := req.Header.Get("X-Registry-Auth") 76 if auth == "NotValid" { 77 return &http.Response{ 78 StatusCode: http.StatusUnauthorized, 79 Body: ioutil.NopCloser(bytes.NewReader([]byte("Invalid credentials"))), 80 }, nil 81 } 82 if auth != "IAmValid" { 83 return nil, fmt.Errorf("Invalid auth header : expected 'IAmValid', got %s", auth) 84 } 85 query := req.URL.Query() 86 term := query.Get("term") 87 if term != "some-image" { 88 return nil, fmt.Errorf("term not set in URL query properly. Expected 'some-image', got %s", term) 89 } 90 content, err := json.Marshal([]registry.SearchResult{ 91 { 92 Name: "anything", 93 }, 94 }) 95 if err != nil { 96 return nil, err 97 } 98 return &http.Response{ 99 StatusCode: http.StatusOK, 100 Body: ioutil.NopCloser(bytes.NewReader(content)), 101 }, nil 102 }), 103 } 104 privilegeFunc := func() (string, error) { 105 return "IAmValid", nil 106 } 107 results, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{ 108 RegistryAuth: "NotValid", 109 PrivilegeFunc: privilegeFunc, 110 }) 111 if err != nil { 112 t.Fatal(err) 113 } 114 if len(results) != 1 { 115 t.Fatalf("expected 1 result, got %v", results) 116 } 117 } 118 119 func TestImageSearchWithoutErrors(t *testing.T) { 120 expectedURL := "/images/search" 121 filterArgs := filters.NewArgs() 122 filterArgs.Add("is-automated", "true") 123 filterArgs.Add("stars", "3") 124 125 expectedFilters := `{"is-automated":{"true":true},"stars":{"3":true}}` 126 127 client := &Client{ 128 client: newMockClient(func(req *http.Request) (*http.Response, error) { 129 if !strings.HasPrefix(req.URL.Path, expectedURL) { 130 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 131 } 132 query := req.URL.Query() 133 term := query.Get("term") 134 if term != "some-image" { 135 return nil, fmt.Errorf("term not set in URL query properly. Expected 'some-image', got %s", term) 136 } 137 filters := query.Get("filters") 138 if filters != expectedFilters { 139 return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", expectedFilters, filters) 140 } 141 content, err := json.Marshal([]registry.SearchResult{ 142 { 143 Name: "anything", 144 }, 145 }) 146 if err != nil { 147 return nil, err 148 } 149 return &http.Response{ 150 StatusCode: http.StatusOK, 151 Body: ioutil.NopCloser(bytes.NewReader(content)), 152 }, nil 153 }), 154 } 155 results, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{ 156 Filters: filterArgs, 157 }) 158 if err != nil { 159 t.Fatal(err) 160 } 161 if len(results) != 1 { 162 t.Fatalf("expected a result, got %v", results) 163 } 164 }