Skip to content

Commit

Permalink
feat(*): update Claim per cnab-spec (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice authored and jeremyrickard committed Aug 14, 2019
1 parent 5f351d2 commit 0ce7659
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 55 deletions.
5 changes: 0 additions & 5 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ func selectInvocationImage(d driver.Driver, c *claim.Claim) (bundle.InvocationIm

for _, ii := range c.Bundle.InvocationImages {
if d.Handles(ii.ImageType) {
if c.RelocationMap != nil {
if img, ok := c.RelocationMap[ii.Image]; ok {
ii.Image = img
}
}
return ii, nil
}
}
Expand Down
27 changes: 0 additions & 27 deletions action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/deislabs/cnab-go/bundle/definition"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type mockDriver struct {
Expand Down Expand Up @@ -517,29 +516,3 @@ func TestSelectInvocationImage_DriverIncompatible(t *testing.T) {
t.Fatalf("expected an error containing %q but got %q", want, got)
}
}

func TestSelectInvocationImage_MapHaveImages_NotPresentInMap(t *testing.T) {
c := &claim.Claim{
Bundle: mockBundle(),
RelocationMap: bundle.ImageRelocationMap{
"some-image": "some-other-image",
},
}
invImage, err := selectInvocationImage(&driver.DebugDriver{}, c)
require.NoError(t, err)

assert.Equal(t, "foo/bar:0.1.0", invImage.Image)
}

func TestSelectInvocationImage_MapHaveImages_IsPresentMap_returnsNewImageTag(t *testing.T) {
c := &claim.Claim{
Bundle: mockBundle(),
RelocationMap: bundle.ImageRelocationMap{
"foo/bar:0.1.0": "some/other:1.0",
},
}
invImage, err := selectInvocationImage(&driver.DebugDriver{}, c)
require.NoError(t, err)

assert.Equal(t, "some/other:1.0", invImage.Image)
}
5 changes: 0 additions & 5 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ type InvocationImage struct {
BaseImage `yaml:",inline"`
}

// ImageRelocationMap stores the relocated images
// The key is the Image in bundle.json and the value is the new Image
// from the relocated registry
type ImageRelocationMap map[string]string

// Location provides the location where a value should be written in
// the invocation image.
//
Expand Down
2 changes: 1 addition & 1 deletion bundle/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestCanReadParameterDefinition(t *testing.T) {
if p.ApplyTo[1] != action1 {
t.Errorf("Expected action '%s' but got '%s'", action1, p.ApplyTo[1])
}
if p.Required != true {
if !p.Required {
t.Errorf("Expected parameter to be required")
}
}
13 changes: 6 additions & 7 deletions claim/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type Claim struct {
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Bundle *bundle.Bundle `json:"bundle"`
Result Result `json:"result"`
Parameters map[string]interface{} `json:"parameters"`
Result Result `json:"result,omitempty"`
Parameters map[string]interface{} `json:"parameters,omitempty"`
// Outputs is a map from the names of outputs (defined in the bundle) to the contents of the files.
Outputs map[string]interface{} `json:"outputs"`
RelocationMap bundle.ImageRelocationMap `json:"relocationMap"`
Outputs map[string]interface{} `json:"outputs,omitempty"`
Custom interface{} `json:"custom,omitempty"`
}

// ValidName is a regular expression that indicates whether a name is a valid claim name.
Expand All @@ -67,9 +67,8 @@ func New(name string) (*Claim, error) {
Action: ActionUnknown,
Status: StatusUnknown,
},
Parameters: map[string]interface{}{},
Outputs: map[string]interface{}{},
RelocationMap: bundle.ImageRelocationMap{},
Parameters: map[string]interface{}{},
Outputs: map[string]interface{}{},
}, nil
}

Expand Down
112 changes: 106 additions & 6 deletions claim/claim_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package claim

import (
"encoding/json"
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/qri-io/jsonschema"

"github.com/deislabs/cnab-go/bundle"
)

Expand All @@ -20,18 +25,13 @@ func TestNew(t *testing.T) {

assert.Equal(t, map[string]interface{}{}, claim.Outputs)
assert.Equal(t, map[string]interface{}{}, claim.Parameters)
assert.Equal(t, bundle.ImageRelocationMap{}, claim.RelocationMap)
}

func TestUpdate(t *testing.T) {
relocationMap := bundle.ImageRelocationMap{
"some.registry/image1": "some.other.registry/image1",
}
claim, err := New("claim")
assert.NoError(t, err)
oldMod := claim.Modified
oldUlid := claim.Revision
claim.RelocationMap = relocationMap

time.Sleep(1 * time.Millisecond) // Force the Update to happen at a new time. For those of us who remembered to press the Turbo button.

Expand All @@ -42,7 +42,6 @@ func TestUpdate(t *testing.T) {
is.NotEqual(oldUlid, claim.Revision)
is.Equal("install", claim.Result.Action)
is.Equal("success", claim.Result.Status)
is.Equal(relocationMap, claim.RelocationMap)
}

func TestValidName(t *testing.T) {
Expand All @@ -60,3 +59,104 @@ func TestValidName(t *testing.T) {
is.Equal(expect, ValidName.MatchString(name))
}
}

var (
staticRevision = "revision"
staticDate = time.Date(1983, time.April, 18, 1, 2, 3, 4, time.UTC)
exampleBundle = bundle.Bundle{
SchemaVersion: "schemaVersion",
Name: "mybun",
Version: "v0.1.0",
Description: "this is my bundle",
}
)

func TestMarshal_New(t *testing.T) {
claim, err := New("my_claim")
assert.NoError(t, err)

// override dynamic fields for testing
claim.Revision = staticRevision
claim.Created = staticDate
claim.Modified = staticDate

bytes, err := json.Marshal(claim)
assert.NoError(t, err, "failed to json.Marshal claim")

wantClaim, err := ioutil.ReadFile("testdata/claim.default.json")
assert.NoError(t, err, "failed to read test claim")

assert.Equal(t, string(wantClaim), string(bytes), "marshaled claim does not match expected")
}

var exampleClaim = Claim{
Name: "my_claim",
Revision: staticRevision,
Created: staticDate,
Modified: staticDate,
Bundle: &exampleBundle,
Result: Result{
Action: ActionInstall,
Message: "result message",
Status: StatusUnderway,
},
Parameters: map[string]interface{}{
"myparam": "myparamvalue",
},
Outputs: map[string]interface{}{
"myoutput": "myoutputvalue",
},
Custom: []string{
"anything goes",
},
}

func TestMarshal_AllFields(t *testing.T) {
bytes, err := json.Marshal(exampleClaim)
assert.NoError(t, err, "failed to json.Marshal claim")

wantClaim, err := ioutil.ReadFile("testdata/claim.allfields.json")
assert.NoError(t, err, "failed to read test claim")

assert.Equal(t, string(wantClaim), string(bytes), "marshaled claim does not match expected")
}

func TestClaimSchema(t *testing.T) {
t.Skip("this test is currently a work in progress; see issue comment below")

claimBytes, err := json.Marshal(exampleClaim)
assert.NoError(t, err, "failed to json.Marshal the claim")

url := "https://raw.githubusercontent.com/deislabs/cnab-spec/master/schema/claim.schema.json"
req, err := http.NewRequest("GET", url, nil)
assert.NoError(t, err, "failed to construct GET request for fetching claim schema")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "failed to get claim schema")

defer res.Body.Close()
schemaData, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err, "failed to read claim schema")

rs := &jsonschema.RootSchema{}
// This currently fails; needs https://github.com/deislabs/cnab-spec/pull/243
err = json.Unmarshal(schemaData, rs)
assert.NoError(t, err, "failed to json.Unmarshal root claim schema")

// This currently fails due to https://github.com/deislabs/cnab-spec/issues/241
// Thus, since the referenced bundle schema can't be fetched, schema validation is impaired
// Alternatively, we could read the qri-o/jsonschema docs to see how we might 'seed' our Validator
// with a fetched version of the bundle schema (from GitHub, as above for the claim schema)
err = rs.FetchRemoteReferences()
assert.NoError(t, err, "failed to fetch remote references declared by claim schema")

errors, err := rs.ValidateBytes(claimBytes)
assert.NoError(t, err, "failed to validate claim")

if len(errors) > 0 {
t.Log("claim validation against the JSON schema failed:")
for _, error := range errors {
t.Log(error)
}
t.Fail()
}
}
4 changes: 0 additions & 4 deletions claim/claimstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ func TestCanSaveReadAndDelete(t *testing.T) {
claim, err := New("foo")
is.NoError(err)
claim.Bundle = &bundle.Bundle{Name: "foobundle", Version: "0.1.2"}
claim.RelocationMap = bundle.ImageRelocationMap{
"some.registry/image1": "some.other.registry/image1",
}

tempDir, err := ioutil.TempDir("", "duffletest")
if err != nil {
Expand All @@ -37,7 +34,6 @@ func TestCanSaveReadAndDelete(t *testing.T) {
c, err := store.Read("foo")
is.NoError(err, "Failed to read: %s", err)
is.Equal(c.Bundle, claim.Bundle, "Expected to read back bundle %s, got %s", claim.Bundle.Name, c.Bundle.Name)
is.Equal("some.other.registry/image1", c.RelocationMap["some.registry/image1"])

claims, err := store.List()
is.NoError(err, "Failed to list: %s", err)
Expand Down
1 change: 1 addition & 0 deletions claim/testdata/claim.allfields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"my_claim","revision":"revision","created":"1983-04-18T01:02:03.000000004Z","modified":"1983-04-18T01:02:03.000000004Z","bundle":{"schemaVersion":"schemaVersion","name":"mybun","version":"v0.1.0","description":"this is my bundle","invocationImages":null},"result":{"message":"result message","action":"install","status":"underway"},"parameters":{"myparam":"myparamvalue"},"outputs":{"myoutput":"myoutputvalue"},"custom":["anything goes"]}
1 change: 1 addition & 0 deletions claim/testdata/claim.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"my_claim","revision":"revision","created":"1983-04-18T01:02:03.000000004Z","modified":"1983-04-18T01:02:03.000000004Z","bundle":null,"result":{"message":"","action":"unknown","status":"unknown"}}

0 comments on commit 0ce7659

Please sign in to comment.