Skip to content

Commit

Permalink
feat: add supported statuses in registry package
Browse files Browse the repository at this point in the history
  • Loading branch information
kolsean committed Jun 23, 2023
1 parent 61168da commit 0346d85
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
59 changes: 57 additions & 2 deletions pkg/v1/registry/schemas.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package registry

import "time"
import (
"encoding/json"
"time"
)

// Registry represents an unmarshalled registry from an API response.
type Registry struct {
Expand All @@ -14,7 +17,7 @@ type Registry struct {
CreatedAt time.Time `json:"createdAt"`

// Status is a status of the registry.
Status string `json:"status"`
Status Status `json:"status"`

// Size is a registry storage usage in bytes.
Size int64 `json:"size"`
Expand All @@ -25,3 +28,55 @@ type Registry struct {
// Used is a registry storage percentage usage.
Used float32 `json:"used"`
}

// Status represents a custom type for various registry statuses.
type Status string

const (
StatusActive Status = "ACTIVE"
StatusCreating Status = "CREATING"
StatusDeleting Status = "DELETING"
StatusGC Status = "GARBAGE_COLLECTION"
StatusError Status = "ERROR"
StatusUnknown Status = "UNKNOWN"
)

func getSupportedStatuses() []Status {
return []Status{
StatusActive,
StatusCreating,
StatusDeleting,
StatusGC,
StatusError,
}
}

func isStatusSupported(s Status) bool {
for _, v := range getSupportedStatuses() {
if s == v {
return true
}
}

return false
}

func (result *Registry) UnmarshalJSON(b []byte) error {
type tmp Registry
var s struct {
tmp
}

if err := json.Unmarshal(b, &s); err != nil {
return err
}

*result = Registry(s.tmp)

// Check cluster status.
if !isStatusSupported(s.tmp.Status) {
result.Status = StatusUnknown
}

return nil
}
20 changes: 20 additions & 0 deletions pkg/v1/registry/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ var expectedGetRegistryResponse = &registry.Registry{
Used: 2.33,
}

const testGetRegistryWithUnknownStatusResponseRaw = `{
"id": "9f3b5b5e-1b5a-4b5c-9b5a-5b5c1b5a4b5c",
"name": "test-registry",
"createdAt": "2022-10-25T10:25:22.556Z",
"status": "UNEXPECTED",
"size": 500000000,
"sizeLimit": 21474836480,
"used": 2.33
}`

var expectedGetRegistryWithUnknownStatusResponse = &registry.Registry{
ID: "9f3b5b5e-1b5a-4b5c-9b5a-5b5c1b5a4b5c",
Name: "test-registry",
CreatedAt: time.Date(2022, 10, 25, 10, 25, 22, 556000000, time.UTC),
Status: "UNKNOWN",
Size: 500000000,
SizeLimit: 21474836480,
Used: 2.33,
}

const testListRegistriesResponseRaw = `[
{
"createdAt": "2022-06-22T10:13:45.895721Z",
Expand Down
41 changes: 41 additions & 0 deletions pkg/v1/registry/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,47 @@ func TestGet(t *testing.T) {
}
}

func TestGetWithUnknownStatus(t *testing.T) {
endpointCalled := false
testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()

testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/api/v1/registries/" + testRegistryID,
RawResponse: testGetRegistryWithUnknownStatusResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
testClient := &v1.ServiceClient{
HTTPClient: &http.Client{},
Token: testutils.TokenID,
Endpoint: testEnv.Server.URL + "/api/v1",
UserAgent: testutils.UserAgent,
}

actual, httpResponse, err := registry.Get(ctx, testClient, testRegistryID)
if err != nil {
t.Fatal(err)
}
if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if httpResponse == nil {
t.Fatal("expected an HTTP response from the Get method")
}
if httpResponse.StatusCode != http.StatusOK {
t.Fatalf("expected %d status in the HTTP response, but got %d",
http.StatusOK, httpResponse.StatusCode)
}
if !reflect.DeepEqual(expectedGetRegistryWithUnknownStatusResponse, actual) {
t.Fatalf("expected %#v, but got %#v", expectedGetRegistryWithUnknownStatusResponse, actual)
}
}

func TestList(t *testing.T) {
endpointCalled := false
testEnv := testutils.SetupTestEnv()
Expand Down

0 comments on commit 0346d85

Please sign in to comment.