Skip to content

Commit

Permalink
Merge pull request #190 from vdice/feat/result-validation
Browse files Browse the repository at this point in the history
feat(claim): add Result validation
  • Loading branch information
vdice authored Mar 4, 2020
2 parents 04dbdd4 + efe72ed commit 2a24d84
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
29 changes: 22 additions & 7 deletions claim/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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")
}
28 changes: 28 additions & 0 deletions claim/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -147,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")

Expand Down
2 changes: 1 addition & 1 deletion claim/testdata/claim.default.json
Original file line number Diff line number Diff line change
@@ -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"}}
{"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"}}

0 comments on commit 2a24d84

Please sign in to comment.