github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/charmhub/resources_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charmhub 5 6 import ( 7 "context" 8 "encoding/json" 9 "net/http" 10 "net/http/httptest" 11 12 "github.com/juju/errors" 13 "github.com/juju/testing" 14 jc "github.com/juju/testing/checkers" 15 "go.uber.org/mock/gomock" 16 gc "gopkg.in/check.v1" 17 18 "github.com/juju/juju/charmhub/path" 19 "github.com/juju/juju/charmhub/transport" 20 ) 21 22 type ResourcesSuite struct { 23 testing.IsolationSuite 24 } 25 26 var _ = gc.Suite(&ResourcesSuite{}) 27 28 func (s *ResourcesSuite) TestListResourceRevisions(c *gc.C) { 29 ctrl := gomock.NewController(c) 30 defer ctrl.Finish() 31 32 baseURL := MustParseURL(c, "http://api.foo.bar") 33 34 path := path.MakePath(baseURL) 35 name := "meshuggah" 36 resource := "image" 37 38 restClient := NewMockRESTClient(ctrl) 39 s.expectGet(c, restClient, path, name, resource) 40 41 client := newResourcesClient(path, restClient, &FakeLogger{}) 42 response, err := client.ListResourceRevisions(context.Background(), name, resource) 43 c.Assert(err, jc.ErrorIsNil) 44 c.Assert(response, gc.HasLen, 3) 45 } 46 47 func (s *ResourcesSuite) TestListResourceRevisionsFailure(c *gc.C) { 48 ctrl := gomock.NewController(c) 49 defer ctrl.Finish() 50 51 baseURL := MustParseURL(c, "http://api.foo.bar") 52 53 path := path.MakePath(baseURL) 54 name := "meshuggah" 55 resource := "image" 56 57 restClient := NewMockRESTClient(ctrl) 58 s.expectGetFailure(restClient) 59 60 client := newResourcesClient(path, restClient, &FakeLogger{}) 61 _, err := client.ListResourceRevisions(context.Background(), name, resource) 62 c.Assert(err, gc.Not(jc.ErrorIsNil)) 63 } 64 65 func (s *ResourcesSuite) expectGet(c *gc.C, client *MockRESTClient, p path.Path, charm, resource string) { 66 namedPath, err := p.Join(charm, resource, "revisions") 67 c.Assert(err, jc.ErrorIsNil) 68 69 client.EXPECT().Get(gomock.Any(), namedPath, gomock.Any()).Do(func(_ context.Context, _ path.Path, response *transport.ResourcesResponse) { 70 response.Revisions = make([]transport.ResourceRevision, 3) 71 }).Return(restResponse{}, nil) 72 } 73 74 func (s *ResourcesSuite) expectGetFailure(client *MockRESTClient) { 75 client.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(restResponse{StatusCode: http.StatusInternalServerError}, errors.Errorf("boom")) 76 } 77 78 func (s *ResourcesSuite) TestListResourceRevisionsRequestPayload(c *gc.C) { 79 resourcesResponse := transport.ResourcesResponse{Revisions: []transport.ResourceRevision{ 80 {Name: "image", Revision: 3, Type: "image"}, 81 {Name: "image", Revision: 2, Type: "image"}, 82 {Name: "image", Revision: 1, Type: "image"}, 83 }} 84 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 85 w.Header().Set("Content-Type", "application/json") 86 w.WriteHeader(http.StatusOK) 87 88 err := json.NewEncoder(w).Encode(resourcesResponse) 89 c.Assert(err, jc.ErrorIsNil) 90 }) 91 92 server := httptest.NewServer(handler) 93 defer server.Close() 94 95 basePath, err := basePath(server.URL) 96 c.Assert(err, jc.ErrorIsNil) 97 98 resourcesPath, err := basePath.Join("resources") 99 c.Assert(err, jc.ErrorIsNil) 100 101 apiRequester := newAPIRequester(DefaultHTTPClient(&FakeLogger{}), &FakeLogger{}) 102 restClient := newHTTPRESTClient(apiRequester) 103 104 client := newResourcesClient(resourcesPath, restClient, &FakeLogger{}) 105 response, err := client.ListResourceRevisions(context.Background(), "wordpress", "image") 106 c.Assert(err, jc.ErrorIsNil) 107 c.Assert(response, gc.DeepEquals, resourcesResponse.Revisions) 108 }