From 27f522faa49a01d3704091d56c4fd634b426c317 Mon Sep 17 00:00:00 2001 From: Vaughn Dice Date: Mon, 2 Mar 2020 14:31:03 -0700 Subject: [PATCH 1/2] feat(claim): add Result validation Signed-off-by: Vaughn Dice --- claim/claim.go | 29 ++++++++++++++++++++++------- claim/claim_test.go | 27 +++++++++++++++++++++++++++ claim/testdata/claim.default.json | 2 +- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/claim/claim.go b/claim/claim.go index 070e4674..1c7ebb10 100644 --- a/claim/claim.go +++ b/claim/claim.go @@ -48,9 +48,8 @@ type Claim struct { BundleReference string `json:"bundleReference,omitempty"` 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,omitempty"` - Custom interface{} `json:"custom,omitempty"` + 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. @@ -90,13 +89,26 @@ func (c *Claim) Update(action, status string) { c.Revision = ULID() } -// Result tracks the result of a Duffle operation on a CNAB installation +// Result tracks the result of an operation on a CNAB installation type Result struct { - Message string `json:"message"` + Message string `json:"message,omitempty"` Action string `json:"action"` Status string `json:"status"` } +// Validate the Result +func (r Result) Validate() error { + if r.Action == "" { + return errors.New("the action must be provided") + } + + switch r.Status { + case StatusFailure, StatusPending, StatusSuccess, StatusUnknown: + return nil + } + return fmt.Errorf("invalid status: %s", r.Status) +} + // ULID generates a string representation of a ULID. func ULID() string { now := time.Now() @@ -106,9 +118,12 @@ func ULID() string { // Validate the Claim func (c Claim) Validate() error { + // validate the schemaVersion err := c.SchemaVersion.Validate() if err != nil { - return errors.Wrapf(err, "claim validation failed") + return errors.Wrap(err, "claim validation failed") } - return nil + + // validate the Result + return errors.Wrap(c.Result.Validate(), "claim validation failed") } diff --git a/claim/claim_test.go b/claim/claim_test.go index d1dbd308..fd0fa090 100644 --- a/claim/claim_test.go +++ b/claim/claim_test.go @@ -136,6 +136,33 @@ func TestValidateExampleClaim(t *testing.T) { `claim validation failed: invalid schema version "not-semver": Invalid Semantic Version`) } +func TestValidate_InvalidResult(t *testing.T) { + claim := exampleClaim + + t.Run("if result is empty, validation should fail", func(t *testing.T) { + claim.Result = Result{} + err := claim.Validate() + assert.EqualError(t, err, "claim validation failed: the action must be provided") + }) + + t.Run("if result has empty action, validation should fail", func(t *testing.T) { + claim.Result = Result{ + Status: StatusSuccess, + } + err := claim.Validate() + assert.EqualError(t, err, "claim validation failed: the action must be provided") + }) + + t.Run("if result has invalid status, validation should fail", func(t *testing.T) { + claim.Result = Result{ + Action: "install", + Status: "invalidStatus", + } + err := claim.Validate() + assert.EqualError(t, err, "claim validation failed: invalid status: invalidStatus") + }) +} + func TestMarshal_AllFields(t *testing.T) { bytes, err := json.Marshal(exampleClaim) assert.NoError(t, err, "failed to json.Marshal claim") diff --git a/claim/testdata/claim.default.json b/claim/testdata/claim.default.json index 0251d122..ab7881af 100644 --- a/claim/testdata/claim.default.json +++ b/claim/testdata/claim.default.json @@ -1 +1 @@ -{"schemaVersion":"v1.0.0-WD","installation":"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"}} \ No newline at end of file +{"schemaVersion":"v1.0.0-WD","installation":"my_claim","revision":"revision","created":"1983-04-18T01:02:03.000000004Z","modified":"1983-04-18T01:02:03.000000004Z","bundle":null,"result":{"action":"unknown","status":"unknown"}} \ No newline at end of file From efe72ed0c25ee4e7ffb16fe975996df1d917714c Mon Sep 17 00:00:00 2001 From: Vaughn Dice Date: Wed, 4 Mar 2020 12:06:08 -0700 Subject: [PATCH 2/2] skip claim schema test Signed-off-by: Vaughn Dice --- claim/claim_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/claim/claim_test.go b/claim/claim_test.go index fd0fa090..14ad2110 100644 --- a/claim/claim_test.go +++ b/claim/claim_test.go @@ -174,6 +174,7 @@ func TestMarshal_AllFields(t *testing.T) { } func TestClaimSchema(t *testing.T) { + t.Skip("This test is pending an alternate, offline-friendly implementation; see https://github.com/cnabio/cnab-go/issues/194") claimBytes, err := json.Marshal(exampleClaim) assert.NoError(t, err, "failed to json.Marshal the claim")