-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buildflags: marshal attestations into json with extra attributes corr…
…ectly `MarshalJSON` would not include the extra attributes because it iterated over the target map rather than the source map. Also fixes JSON unmarshaling for SSH and secrets. The intention was to unmarshal into the struct, but `UnmarshalText` takes priority over the default struct unmarshaling so it didn't work as intended. Tests have been added for all marshaling and unmarshaling methods. Signed-off-by: Jonathan A. Sternberg <[email protected]>
- Loading branch information
1 parent
b4a0dee
commit 3aed658
Showing
7 changed files
with
353 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package buildflags | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestAttests(t *testing.T) { | ||
t.Run("MarshalJSON", func(t *testing.T) { | ||
attests := Attests{ | ||
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, | ||
{Type: "sbom", Disabled: true}, | ||
} | ||
|
||
expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]` | ||
actual, err := json.Marshal(attests) | ||
require.NoError(t, err) | ||
require.JSONEq(t, expected, string(actual)) | ||
}) | ||
|
||
t.Run("UnmarshalJSON", func(t *testing.T) { | ||
in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]` | ||
|
||
var actual Attests | ||
err := json.Unmarshal([]byte(in), &actual) | ||
require.NoError(t, err) | ||
|
||
expected := Attests{ | ||
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, | ||
{Type: "sbom", Disabled: true, Attrs: map[string]string{}}, | ||
} | ||
require.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("FromCtyValue", func(t *testing.T) { | ||
in := cty.TupleVal([]cty.Value{ | ||
cty.ObjectVal(map[string]cty.Value{ | ||
"type": cty.StringVal("provenance"), | ||
"mode": cty.StringVal("max"), | ||
}), | ||
cty.StringVal("type=sbom,disabled=true"), | ||
}) | ||
|
||
var actual Attests | ||
err := actual.FromCtyValue(in, nil) | ||
require.NoError(t, err) | ||
|
||
expected := Attests{ | ||
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, | ||
{Type: "sbom", Disabled: true, Attrs: map[string]string{}}, | ||
} | ||
require.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("ToCtyValue", func(t *testing.T) { | ||
attests := Attests{ | ||
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, | ||
{Type: "sbom", Disabled: true}, | ||
} | ||
|
||
actual := attests.ToCtyValue() | ||
expected := cty.ListVal([]cty.Value{ | ||
cty.MapVal(map[string]cty.Value{ | ||
"type": cty.StringVal("provenance"), | ||
"mode": cty.StringVal("max"), | ||
}), | ||
cty.MapVal(map[string]cty.Value{ | ||
"type": cty.StringVal("sbom"), | ||
"disabled": cty.StringVal("true"), | ||
}), | ||
}) | ||
|
||
result := actual.Equals(expected) | ||
require.True(t, result.True()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package buildflags | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestSecrets(t *testing.T) { | ||
t.Run("MarshalJSON", func(t *testing.T) { | ||
secrets := Secrets{ | ||
{ID: "mysecret", FilePath: "/local/secret"}, | ||
{ID: "mysecret2", Env: "TOKEN"}, | ||
} | ||
|
||
expected := `[{"id":"mysecret","src":"/local/secret"},{"id":"mysecret2","env":"TOKEN"}]` | ||
actual, err := json.Marshal(secrets) | ||
require.NoError(t, err) | ||
require.JSONEq(t, expected, string(actual)) | ||
}) | ||
|
||
t.Run("UnmarshalJSON", func(t *testing.T) { | ||
in := `[{"id":"mysecret","src":"/local/secret"},{"id":"mysecret2","env":"TOKEN"}]` | ||
|
||
var actual Secrets | ||
err := json.Unmarshal([]byte(in), &actual) | ||
require.NoError(t, err) | ||
|
||
expected := Secrets{ | ||
{ID: "mysecret", FilePath: "/local/secret"}, | ||
{ID: "mysecret2", Env: "TOKEN"}, | ||
} | ||
require.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("FromCtyValue", func(t *testing.T) { | ||
in := cty.TupleVal([]cty.Value{ | ||
cty.ObjectVal(map[string]cty.Value{ | ||
"id": cty.StringVal("mysecret"), | ||
"src": cty.StringVal("/local/secret"), | ||
}), | ||
cty.ObjectVal(map[string]cty.Value{ | ||
"id": cty.StringVal("mysecret2"), | ||
"env": cty.StringVal("TOKEN"), | ||
}), | ||
}) | ||
|
||
var actual Secrets | ||
err := actual.FromCtyValue(in, nil) | ||
require.NoError(t, err) | ||
|
||
expected := Secrets{ | ||
{ID: "mysecret", FilePath: "/local/secret"}, | ||
{ID: "mysecret2", Env: "TOKEN"}, | ||
} | ||
require.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("ToCtyValue", func(t *testing.T) { | ||
secrets := Secrets{ | ||
{ID: "mysecret", FilePath: "/local/secret"}, | ||
{ID: "mysecret2", Env: "TOKEN"}, | ||
} | ||
|
||
actual := secrets.ToCtyValue() | ||
expected := cty.ListVal([]cty.Value{ | ||
cty.ObjectVal(map[string]cty.Value{ | ||
"id": cty.StringVal("mysecret"), | ||
"src": cty.StringVal("/local/secret"), | ||
"env": cty.StringVal(""), | ||
}), | ||
cty.ObjectVal(map[string]cty.Value{ | ||
"id": cty.StringVal("mysecret2"), | ||
"src": cty.StringVal(""), | ||
"env": cty.StringVal("TOKEN"), | ||
}), | ||
}) | ||
|
||
result := actual.Equals(expected) | ||
require.True(t, result.True()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.