Skip to content

Commit

Permalink
Merge pull request #56 from DrDaveD/sylabsv1.4.6
Browse files Browse the repository at this point in the history
Update to sylabs v1.4.6
  • Loading branch information
DrDaveD authored Dec 29, 2023
2 parents 0d93710 + 3540237 commit 5d7841e
Show file tree
Hide file tree
Showing 18 changed files with 994 additions and 160 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: '1.19.x'
go-version: '1.20.x'

- name: Check go.mod and go.sum are tidy
run: |
Expand All @@ -35,7 +35,9 @@ jobs:
- name: Install Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.51
version: v1.54
skip-pkg-cache: true
skip-build-cache: true

- name: Run Lint
run: |
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ linters:
- containedctx
- contextcheck
- decorder
- depguard
- dogsled
- errcheck
- errchkjson
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goerr113
- gofumpt
- goimports
- goprintffuncname
Expand Down
58 changes: 29 additions & 29 deletions client/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -25,7 +25,7 @@ func (c *Client) getEntity(ctx context.Context, entityRef string) (*Entity, erro
}
var res EntityResponse
if err := json.Unmarshal(entJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding entity: %v", err)
return nil, fmt.Errorf("error decoding entity: %w", err)
}
return &res.Data, nil
}
Expand All @@ -39,7 +39,7 @@ func (c *Client) getCollection(ctx context.Context, collectionRef string) (*Coll
}
var res CollectionResponse
if err := json.Unmarshal(colJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding collection: %v", err)
return nil, fmt.Errorf("error decoding collection: %w", err)
}
return &res.Data, nil
}
Expand All @@ -53,7 +53,7 @@ func (c *Client) getContainer(ctx context.Context, containerRef string) (*Contai
}
var res ContainerResponse
if err := json.Unmarshal(conJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding container: %v", err)
return nil, fmt.Errorf("error decoding container: %w", err)
}
return &res.Data, nil
}
Expand All @@ -70,7 +70,7 @@ func (c *Client) createEntity(ctx context.Context, name string) (*Entity, error)
}
var res EntityResponse
if err := json.Unmarshal(entJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding entity: %v", err)
return nil, fmt.Errorf("error decoding entity: %w", err)
}
return &res.Data, nil
}
Expand All @@ -88,7 +88,7 @@ func (c *Client) createCollection(ctx context.Context, name string, entityID str
}
var res CollectionResponse
if err := json.Unmarshal(colJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding collection: %v", err)
return nil, fmt.Errorf("error decoding collection: %w", err)
}
return &res.Data, nil
}
Expand All @@ -106,7 +106,7 @@ func (c *Client) createContainer(ctx context.Context, name string, collectionID
}
var res ContainerResponse
if err := json.Unmarshal(conJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding container: %v", err)
return nil, fmt.Errorf("error decoding container: %w", err)
}
return &res.Data, nil
}
Expand All @@ -124,7 +124,7 @@ func (c *Client) createImage(ctx context.Context, hash string, containerID strin
}
var res ImageResponse
if err := json.Unmarshal(imgJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding image: %v", err)
return nil, fmt.Errorf("error decoding image: %w", err)
}
return &res.Data, nil
}
Expand Down Expand Up @@ -162,24 +162,24 @@ func (c *Client) getTags(ctx context.Context, containerID string) (TagMap, error
c.Logger.Logf("getTags calling %s", url)
req, err := c.newRequest(ctx, http.MethodGet, url, "", nil)
if err != nil {
return nil, fmt.Errorf("error creating request to server:\n\t%v", err)
return nil, fmt.Errorf("error creating request to server:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request to server:\n\t%v", err)
return nil, fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return nil, fmt.Errorf("creation did not succeed: %v", err)
return nil, fmt.Errorf("creation did not succeed: %w", err)
}
return nil, fmt.Errorf("unexpected http status code: %d", res.StatusCode)
return nil, fmt.Errorf("%w: unexpected http status code: %d", errHTTP, res.StatusCode)
}
var tagRes TagsResponse
err = json.NewDecoder(res.Body).Decode(&tagRes)
if err != nil {
return nil, fmt.Errorf("error decoding tags: %v", err)
return nil, fmt.Errorf("error decoding tags: %w", err)
}
return tagRes.Data, nil
}
Expand All @@ -190,23 +190,23 @@ func (c *Client) setTag(ctx context.Context, containerID string, t ImageTag) err
c.Logger.Logf("setTag calling %s", url)
s, err := json.Marshal(t)
if err != nil {
return fmt.Errorf("error encoding object to JSON:\n\t%v", err)
return fmt.Errorf("error encoding object to JSON:\n\t%w", err)
}
req, err := c.newRequest(ctx, http.MethodPost, url, "", bytes.NewBuffer(s))
if err != nil {
return fmt.Errorf("error creating POST request:\n\t%v", err)
return fmt.Errorf("error creating POST request:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("error making request to server:\n\t%v", err)
return fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return fmt.Errorf("creation did not succeed: %v", err)
return fmt.Errorf("creation did not succeed: %w", err)
}
return fmt.Errorf("creation did not succeed: http status code: %d", res.StatusCode)
return fmt.Errorf("%w: creation did not succeed: http status code: %d", errHTTP, res.StatusCode)
}
return nil
}
Expand Down Expand Up @@ -245,24 +245,24 @@ func (c *Client) getTagsV2(ctx context.Context, containerID string) (ArchTagMap,
c.Logger.Logf("getTagsV2 calling %s", url)
req, err := c.newRequest(ctx, http.MethodGet, url, "", nil)
if err != nil {
return nil, fmt.Errorf("error creating request to server:\n\t%v", err)
return nil, fmt.Errorf("error creating request to server:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request to server:\n\t%v", err)
return nil, fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return nil, fmt.Errorf("creation did not succeed: %v", err)
return nil, fmt.Errorf("creation did not succeed: %w", err)
}
return nil, fmt.Errorf("unexpected http status code: %d", res.StatusCode)
return nil, fmt.Errorf("%w: unexpected http status code: %d", errHTTP, res.StatusCode)
}
var tagRes ArchTagsResponse
err = json.NewDecoder(res.Body).Decode(&tagRes)
if err != nil {
return nil, fmt.Errorf("error decoding tags: %v", err)
return nil, fmt.Errorf("error decoding tags: %w", err)
}
return tagRes.Data, nil
}
Expand All @@ -273,23 +273,23 @@ func (c *Client) setTagV2(ctx context.Context, containerID string, t ArchImageTa
c.Logger.Logf("setTag calling %s", url)
s, err := json.Marshal(t)
if err != nil {
return fmt.Errorf("error encoding object to JSON:\n\t%v", err)
return fmt.Errorf("error encoding object to JSON:\n\t%w", err)
}
req, err := c.newRequest(ctx, http.MethodPost, url, "", bytes.NewBuffer(s))
if err != nil {
return fmt.Errorf("error creating POST request:\n\t%v", err)
return fmt.Errorf("error creating POST request:\n\t%w", err)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("error making request to server:\n\t%v", err)
return fmt.Errorf("error making request to server:\n\t%w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := jsonresp.ReadError(res.Body)
if err != nil {
return fmt.Errorf("creation did not succeed: %v", err)
return fmt.Errorf("creation did not succeed: %w", err)
}
return fmt.Errorf("creation did not succeed: http status code: %d", res.StatusCode)
return fmt.Errorf("%w: creation did not succeed: http status code: %d", errHTTP, res.StatusCode)
}
return nil
}
Expand All @@ -310,7 +310,7 @@ func (c *Client) GetImage(ctx context.Context, arch string, imageRef string) (*I
}
var res ImageResponse
if err := json.Unmarshal(imgJSON, &res); err != nil {
return nil, fmt.Errorf("error decoding image: %v", err)
return nil, fmt.Errorf("error decoding image: %w", err)
}
return &res.Data, nil
}
19 changes: 10 additions & 9 deletions client/api_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -8,6 +8,7 @@ package client
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -261,8 +262,8 @@ func Test_getEntity(t *testing.T) {
if err == nil && tt.expectError {
t.Errorf("Unexpected success. Expected error.")
}
if err != nil && err == ErrNotFound && tt.expectFound {
t.Errorf("Got found %v - expected %v", err != ErrNotFound, tt.expectFound)
if err != nil && errors.Is(err, ErrNotFound) && tt.expectFound {
t.Errorf("Got found %v - expected %v", !errors.Is(err, ErrNotFound), tt.expectFound)
}
if !reflect.DeepEqual(entity, tt.expectEntity) {
t.Errorf("Got entity %v - expected %v", entity, tt.expectEntity)
Expand Down Expand Up @@ -341,8 +342,8 @@ func Test_getCollection(t *testing.T) {
if err == nil && tt.expectError {
t.Errorf("Unexpected success. Expected error.")
}
if err != nil && err == ErrNotFound && tt.expectFound {
t.Errorf("Got found %v - expected %v", err != ErrNotFound, tt.expectFound)
if err != nil && errors.Is(err, ErrNotFound) && tt.expectFound {
t.Errorf("Got found %v - expected %v", !errors.Is(err, ErrNotFound), tt.expectFound)
}
if !reflect.DeepEqual(collection, tt.expectCollection) {
t.Errorf("Got entity %v - expected %v", collection, tt.expectCollection)
Expand Down Expand Up @@ -421,8 +422,8 @@ func Test_getContainer(t *testing.T) {
if err == nil && tt.expectError {
t.Errorf("Unexpected success. Expected error.")
}
if err != nil && err != ErrNotFound && tt.expectFound {
t.Errorf("Got found %v - expected %v", err != ErrNotFound, tt.expectFound)
if err != nil && !errors.Is(err, ErrNotFound) && tt.expectFound {
t.Errorf("Got found %v - expected %v", !errors.Is(err, ErrNotFound), tt.expectFound)
}
if !reflect.DeepEqual(container, tt.expectContainer) {
t.Errorf("Got container %v - expected %v", container, tt.expectContainer)
Expand Down Expand Up @@ -505,8 +506,8 @@ func Test_getImage(t *testing.T) {
if err == nil && tt.expectError {
t.Errorf("Unexpected success. Expected error.")
}
if err != nil && err != ErrNotFound && tt.expectFound {
t.Errorf("Got found %v - expected %v", err != ErrNotFound, tt.expectFound)
if err != nil && !errors.Is(err, ErrNotFound) && tt.expectFound {
t.Errorf("Got found %v - expected %v", !errors.Is(err, ErrNotFound), tt.expectFound)
}
if !reflect.DeepEqual(image, tt.expectImage) {
t.Errorf("Got image %v - expected %v", image, tt.expectImage)
Expand Down
8 changes: 2 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package client

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -17,9 +16,6 @@ import (
"github.com/go-log/log"
)

// ErrUnauthorized represents HTTP status "401 Unauthorized"
var ErrUnauthorized = errors.New("unauthorized")

// Config contains the client configuration.
type Config struct {
// Base URL of the service.
Expand Down Expand Up @@ -66,7 +62,7 @@ func NewClient(cfg *Config) (*Client, error) {
}

if bu == "" {
return nil, fmt.Errorf("no BaseURL supplied")
return nil, errNoBaseURL
}

// If baseURL has a path component, ensure it is terminated with a separator, to prevent
Expand All @@ -81,7 +77,7 @@ func NewClient(cfg *Config) (*Client, error) {
return nil, err
}
if baseURL.Scheme != "http" && baseURL.Scheme != "https" {
return nil, fmt.Errorf("unsupported protocol scheme %q", baseURL.Scheme)
return nil, fmt.Errorf("%w: unsupported protocol scheme %q", errHTTP, baseURL.Scheme)
}

c := &Client{
Expand Down
3 changes: 1 addition & 2 deletions client/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package client

import (
"context"
"errors"
"net/url"
)

// DeleteImage deletes requested imageRef.
func (c *Client) DeleteImage(ctx context.Context, imageRef, arch string) error {
if imageRef == "" || arch == "" {
return errors.New("imageRef and arch are required")
return errImageRefArchRequired
}

_, err := c.doDeleteRequest(ctx, "v1/images/"+imageRef+"?arch="+url.QueryEscape(arch))
Expand Down
9 changes: 4 additions & 5 deletions client/downloader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -7,7 +7,6 @@ package client

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -46,7 +45,7 @@ func minInt64(a, b int64) int64 {
// Download performs download of contents at url by writing 'size' bytes to 'dst' using credentials 'c'.
func (c *Client) multipartDownload(ctx context.Context, u string, creds credentials, w io.WriterAt, size int64, spec *Downloader, pb ProgressBar) error {
if size <= 0 {
return fmt.Errorf("invalid image size (%v)", size)
return fmt.Errorf("%w: invalid image size (%v)", errBadRequest, size)
}

// Initialize the progress bar using passed size
Expand Down Expand Up @@ -131,13 +130,13 @@ func parseContentRange(val string) (int64, error) {
e := strings.Split(val, " ")

if !strings.EqualFold(e[0], "bytes") {
return 0, errors.New("unexpected/malformed value")
return 0, errUnexpectedMalformedValue
}

rangeElems := strings.Split(e[1], "/")

if len(rangeElems) != 2 {
return 0, errors.New("unexpected/malformed value")
return 0, errUnexpectedMalformedValue
}

return strconv.ParseInt(rangeElems[1], 10, 0)
Expand Down
2 changes: 2 additions & 0 deletions client/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func (l *stdLogger) Logf(f string, v ...interface{}) {
}

func parseRangeHeader(t *testing.T, val string) (int64, int64) {
t.Helper()

if val == "" {
return 0, 0
}
Expand Down
Loading

0 comments on commit 5d7841e

Please sign in to comment.