Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unmarshal for claim results #283

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions claim/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,13 @@ func (r Results) Swap(i, j int) {
// OutputMetadata is the output metadata from an operation.
// Any metadata can be stored, however this provides methods
// for safely querying and retrieving well-known metadata.
type OutputMetadata map[string]interface{}
type OutputMetadata map[string]map[string]string

// GetMetadata for the specified output and key.
func (o OutputMetadata) GetMetadata(outputName string, metadataKey string) (string, bool) {
if output, ok := o[outputName]; ok {
if outputMetadata, ok := output.(map[string]string); ok {
if value, ok := outputMetadata[metadataKey]; ok {
return value, true
}
if value, ok := output[metadataKey]; ok {
return value, true
}
}

Expand All @@ -122,20 +120,15 @@ func (o *OutputMetadata) SetMetadata(outputName string, metadataKey string, valu
}
metadata := *o

output, ok := metadata[outputName]
outputMetadata, ok := metadata[outputName]
if !ok {
output = map[string]string{
outputMetadata = map[string]string{
metadataKey: value,
}
metadata[outputName] = output
metadata[outputName] = outputMetadata
return nil
}

outputMetadata, ok := output.(map[string]string)
if !ok {
return errors.Errorf("cannot set the claim result's OutputMetadata[%s][%s] because it is not type map[string]string but %T", outputName, metadataKey, output)
}

outputMetadata[metadataKey] = value
return nil
}
Expand Down
41 changes: 18 additions & 23 deletions claim/result_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package claim

import (
"encoding/json"
"io/ioutil"
"sort"
"testing"

Expand Down Expand Up @@ -99,16 +101,6 @@ func TestResultOutputs_SetMetadata(t *testing.T) {
require.NoError(t, err, "SetMetadata failed")
assert.Equal(t, wantoutputs, outputs, "SetMetadata did not produce the expected structure")
})

t.Run("existing invalid structure", func(t *testing.T) {
outputs := OutputMetadata{
outputName: map[string]interface{}{
metadataKey: struct{}{},
},
}
err := outputs.SetMetadata(outputName, metadataKey, metadataValue)
require.EqualError(t, err, "cannot set the claim result's OutputMetadata[test1][so-meta] because it is not type map[string]string but map[string]interface {}")
})
}

func TestResultOutputs_GetMetadata(t *testing.T) {
Expand Down Expand Up @@ -144,19 +136,6 @@ func TestResultOutputs_GetMetadata(t *testing.T) {
require.False(t, ok, "GetMetadata should report that it did not find the value")
assert.Empty(t, gotValue, "GetMetadata should return an empty value when one isn't found")
})

t.Run("output has different structure", func(t *testing.T) {
outputs := OutputMetadata{
outputName: map[string]interface{}{
"other": struct{}{},
},
}

gotValue, ok := outputs.GetMetadata(outputName, metadataKey)
require.False(t, ok, "GetMetadata should report that it did not find the value")
assert.Empty(t, gotValue, "GetMetadata should return an empty value when one isn't found")
})

}

func TestResultOutputs_SetContentDigest(t *testing.T) {
Expand Down Expand Up @@ -265,3 +244,19 @@ func TestResult_HasLogs(t *testing.T) {
r.OutputMetadata.SetGeneratedByBundle(OutputInvocationImageLogs, true)
assert.True(t, r.HasLogs(), "expected HasLogs to return true")
}

// Verify that when we unmarshal a result, the output metadata can be read back
func TestResult_UnmarshalOutputMetadata(t *testing.T) {
data, err := ioutil.ReadFile("testdata/result.json")
require.NoError(t, err, "error reading testdata result")

var result Result
err = json.Unmarshal(data, &result)
require.NoError(t, err, "error unmarshaling result")

contentDigest, _ := result.OutputMetadata.GetContentDigest(OutputInvocationImageLogs)
assert.Equal(t, "sha256:28ccd0529aa1edefb0e771a28c31c0193f656718af985fed197235ba98fc5696", contentDigest, "the content digest output metadata could not be read")

generatedFlag, _ := result.OutputMetadata.GetGeneratedByBundle("output1")
assert.True(t, generatedFlag, "the generatedByBundle output metadata could not be read")
}
16 changes: 16 additions & 0 deletions claim/testdata/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "01G1VJH2HP97B5B0N5S37KYMVG",
"claimId": "01G1VJGY43HT3KZN82DS6DDPWK",
"created": "2022-04-29T16:09:47.190534-05:00",
"status": "succeeded",
"outputs": {
"io.cnab.outputs.invocationImageLogs": {
"contentDigest": "sha256:28ccd0529aa1edefb0e771a28c31c0193f656718af985fed197235ba98fc5696",
"generatedByBundle": "false"
},
"output1": {
"contentDigest": "sha256:abc123",
"generatedByBundle": "true"
}
}
}