...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/catalog_test.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remote
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"net/url"
    24  	"testing"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	"github.com/google/go-containerregistry/pkg/name"
    28  )
    29  
    30  func TestCatalogPage(t *testing.T) {
    31  	cases := []struct {
    32  		name         string
    33  		responseBody []byte
    34  		wantErr      bool
    35  		wantRepos    []string
    36  	}{{
    37  		name:         "success",
    38  		responseBody: []byte(`{"repositories":["test/test","foo/bar"]}`),
    39  		wantErr:      false,
    40  		wantRepos:    []string{"test/test", "foo/bar"},
    41  	}, {
    42  		name:         "not json",
    43  		responseBody: []byte("notjson"),
    44  		wantErr:      true,
    45  	}}
    46  	// TODO: add test cases for pagination
    47  
    48  	for _, tc := range cases {
    49  		t.Run(tc.name, func(t *testing.T) {
    50  			catalogPath := "/v2/_catalog"
    51  			server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  				switch r.URL.Path {
    53  				case "/v2/":
    54  					w.WriteHeader(http.StatusOK)
    55  				case catalogPath:
    56  					if r.Method != http.MethodGet {
    57  						t.Errorf("Method; got %v, want %v", r.Method, http.MethodGet)
    58  					}
    59  
    60  					w.Write(tc.responseBody)
    61  				default:
    62  					t.Fatalf("Unexpected path: %v", r.URL.Path)
    63  				}
    64  			}))
    65  			defer server.Close()
    66  			u, err := url.Parse(server.URL)
    67  			if err != nil {
    68  				t.Fatalf("url.Parse(%v) = %v", server.URL, err)
    69  			}
    70  
    71  			reg, err := name.NewRegistry(u.Host)
    72  			if err != nil {
    73  				t.Fatalf("name.NewRegistry(%v) = %v", u.Host, err)
    74  			}
    75  
    76  			repos, err := CatalogPage(reg, "", 100)
    77  			if (err != nil) != tc.wantErr {
    78  				t.Errorf("CatalogPage() wrong error: %v, want %v: %v\n", (err != nil), tc.wantErr, err)
    79  			}
    80  
    81  			if diff := cmp.Diff(tc.wantRepos, repos); diff != "" {
    82  				t.Errorf("CatalogPage() wrong repos (-want +got) = %s", diff)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestCatalog(t *testing.T) {
    89  	cases := []struct {
    90  		name      string
    91  		pages     [][]byte
    92  		wantErr   bool
    93  		wantRepos []string
    94  	}{{
    95  		name: "success",
    96  		pages: [][]byte{
    97  			[]byte(`{"repositories":["test/one","test/two"]}`),
    98  			[]byte(`{"repositories":["test/three","test/four"]}`),
    99  		},
   100  		wantErr:   false,
   101  		wantRepos: []string{"test/one", "test/two", "test/three", "test/four"},
   102  	}, {
   103  		name:    "not json",
   104  		pages:   [][]byte{[]byte("notjson")},
   105  		wantErr: true,
   106  	}}
   107  
   108  	for _, tc := range cases {
   109  		t.Run(tc.name, func(t *testing.T) {
   110  			catalogPath := "/v2/_catalog"
   111  			pageTwo := "/v2/_catalog_two"
   112  			server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   113  				page := 0
   114  				switch r.URL.Path {
   115  				case "/v2/":
   116  					w.WriteHeader(http.StatusOK)
   117  				case pageTwo:
   118  					page = 1
   119  					fallthrough
   120  				case catalogPath:
   121  					if r.Method != http.MethodGet {
   122  						t.Errorf("Method; got %v, want %v", r.Method, http.MethodGet)
   123  					}
   124  
   125  					if page == 0 {
   126  						w.Header().Set("Link", fmt.Sprintf("<%s>", pageTwo))
   127  					}
   128  					w.Write(tc.pages[page])
   129  				default:
   130  					t.Fatalf("Unexpected path: %v", r.URL.Path)
   131  				}
   132  			}))
   133  			defer server.Close()
   134  			u, err := url.Parse(server.URL)
   135  			if err != nil {
   136  				t.Fatalf("url.Parse(%v) = %v", server.URL, err)
   137  			}
   138  
   139  			reg, err := name.NewRegistry(u.Host)
   140  			if err != nil {
   141  				t.Fatalf("name.NewRegistry(%v) = %v", u.Host, err)
   142  			}
   143  
   144  			repos, err := Catalog(context.Background(), reg)
   145  			if (err != nil) != tc.wantErr {
   146  				t.Errorf("Catalog() wrong error: %v, want %v: %v\n", (err != nil), tc.wantErr, err)
   147  			}
   148  
   149  			if diff := cmp.Diff(tc.wantRepos, repos); diff != "" {
   150  				t.Errorf("Catalog() wrong repos (-want +got) = %s", diff)
   151  			}
   152  		})
   153  	}
   154  }
   155  
   156  func TestCancelledCatalog(t *testing.T) {
   157  	ctx, cancel := context.WithCancel(context.Background())
   158  	cancel()
   159  
   160  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   161  		switch r.URL.Path {
   162  		case "/v2/":
   163  			w.WriteHeader(http.StatusOK)
   164  		default:
   165  			t.Fatalf("Unexpected path: %v", r.URL.Path)
   166  		}
   167  	}))
   168  	defer server.Close()
   169  	u, err := url.Parse(server.URL)
   170  	if err != nil {
   171  		t.Fatalf("url.Parse(%v) = %v", server.URL, err)
   172  	}
   173  
   174  	reg, err := name.NewRegistry(u.Host)
   175  	if err != nil {
   176  		t.Fatalf("name.NewRegistry(%v) = %v", u.Host, err)
   177  	}
   178  
   179  	_, err = Catalog(ctx, reg)
   180  	if want, got := context.Canceled, err; !errors.Is(got, want) {
   181  		t.Errorf("wanted %v got %v", want, got)
   182  	}
   183  }
   184  

View as plain text