From d041a79d2010103a7e4cc4d3aa078eac6396b629 Mon Sep 17 00:00:00 2001 From: Vaughn Dice Date: Tue, 3 Mar 2020 15:48:32 -0700 Subject: [PATCH] feat(action): add cnab/claim.json to op files Signed-off-by: Vaughn Dice --- action/action.go | 7 ++++++- action/action_test.go | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/action/action.go b/action/action.go index 4c07b8f6..b6d128d4 100644 --- a/action/action.go +++ b/action/action.go @@ -194,7 +194,6 @@ func opFromClaim(action string, stateless bool, c *claim.Claim, ii bundle.Invoca if err != nil { return nil, fmt.Errorf("failed to marshal bundle contents: %s", err) } - files["/cnab/bundle.json"] = string(bundleBytes) imgMap, err := getImageMap(c.Bundle) @@ -203,6 +202,12 @@ func opFromClaim(action string, stateless bool, c *claim.Claim, ii bundle.Invoca } files["/cnab/app/image-map.json"] = string(imgMap) + claimBytes, err := json.Marshal(c) + if err != nil { + return nil, fmt.Errorf("failed to marshal claim: %s", err) + } + files["/cnab/claim.json"] = string(claimBytes) + env["CNAB_ACTION"] = action env["CNAB_BUNDLE_NAME"] = c.Bundle.Name env["CNAB_BUNDLE_VERSION"] = c.Bundle.Version diff --git a/action/action_test.go b/action/action_test.go index 8ce9d3c3..64bf85c8 100644 --- a/action/action_test.go +++ b/action/action_test.go @@ -178,12 +178,17 @@ func mockBundle() *bundle.Bundle { func TestOpFromClaim(t *testing.T) { c := newClaim() + // the monotonic clock reading from time.Now() proves problematic + // (it is lost after json.Unmarshal), so just set to static date for testing + created := time.Date(2020, time.March, 3, 1, 2, 3, 4, time.UTC) + c.Created = created + c.Modified = created c.Parameters = map[string]interface{}{ "param_one": "oneval", "param_two": "twoval", "param_three": "threeval", - "param_array": []string{"first-value", "second-value"}, - "param_object": map[string]string{ + "param_array": []interface{}{"first-value", "second-value"}, + "param_object": map[string]interface{}{ "first-key": "first-value", "second-key": "second-value", }, @@ -214,6 +219,7 @@ func TestOpFromClaim(t *testing.T) { is.Equal(op.Files["/param/param_quoted_string"], `"quoted value"`) is.Contains(op.Files, "/cnab/app/image-map.json") is.Contains(op.Files, "/cnab/bundle.json") + is.Contains(op.Files, "/cnab/claim.json") is.Contains(op.Outputs, "/tmp/some/path") var imgMap map[string]bundle.Image @@ -224,6 +230,10 @@ func TestOpFromClaim(t *testing.T) { is.NoError(json.Unmarshal([]byte(op.Files["/cnab/bundle.json"]), &bundle)) is.Equal(c.Bundle, bundle) + var claim *claim.Claim + is.NoError(json.Unmarshal([]byte(op.Files["/cnab/claim.json"]), &claim)) + is.Equal(c, claim) + is.Len(op.Parameters, 7) is.Nil(op.Out) }