Add attest extra args tests

Signed-off-by: Laurent Goderre <laurent.goderre@docker.com>
This commit is contained in:
Laurent Goderre 2025-02-27 09:56:46 -05:00
parent 921b576f3a
commit 4e91fe6507
2 changed files with 42 additions and 4 deletions

View File

@ -608,7 +608,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
target "app" { target "app" {
attest = [ attest = [
{ type = "provenance", mode = "max" }, { type = "provenance", mode = "max" },
"type=sbom,disabled=true", "type=sbom,disabled=true,generator=foo,\"ENV1=bar,baz\",ENV2=hello",
] ]
cache-from = [ cache-from = [
@ -641,7 +641,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(c.Targets)) require.Equal(t, 1, len(c.Targets))
require.Equal(t, []string{"type=provenance,mode=max", "type=sbom,disabled=true"}, stringify(c.Targets[0].Attest)) require.Equal(t, []string{"type=provenance,mode=max", "type=sbom,disabled=true,\"ENV1=bar,baz\",ENV2=hello,generator=foo"}, stringify(c.Targets[0].Attest))
require.Equal(t, []string{"type=local,dest=../out", "type=oci,dest=../out.tar"}, stringify(c.Targets[0].Outputs)) require.Equal(t, []string{"type=local,dest=../out", "type=oci,dest=../out.tar"}, stringify(c.Targets[0].Outputs))
require.Equal(t, []string{"type=local,src=path/to/cache", "user/app:cache"}, stringify(c.Targets[0].CacheFrom)) require.Equal(t, []string{"type=local,src=path/to/cache", "user/app:cache"}, stringify(c.Targets[0].CacheFrom))
require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[0].CacheTo)) require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[0].CacheTo))

View File

@ -13,16 +13,21 @@ func TestAttests(t *testing.T) {
attests := Attests{ attests := Attests{
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, {Type: "provenance", Attrs: map[string]string{"mode": "max"}},
{Type: "sbom", Disabled: true}, {Type: "sbom", Disabled: true},
{Type: "sbom", Attrs: map[string]string{
"generator": "scanner",
"ENV1": `"foo,bar"`,
"Env2": "hello",
}},
} }
expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]` expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true},{"ENV1":"\"foo,bar\"","Env2":"hello","generator":"scanner","type":"sbom"}]`
actual, err := json.Marshal(attests) actual, err := json.Marshal(attests)
require.NoError(t, err) require.NoError(t, err)
require.JSONEq(t, expected, string(actual)) require.JSONEq(t, expected, string(actual))
}) })
t.Run("UnmarshalJSON", func(t *testing.T) { t.Run("UnmarshalJSON", func(t *testing.T) {
in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]` in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true},{"ENV1":"\"foo,bar\"","Env2":"hello","generator":"scanner","type":"sbom"}]`
var actual Attests var actual Attests
err := json.Unmarshal([]byte(in), &actual) err := json.Unmarshal([]byte(in), &actual)
@ -31,6 +36,11 @@ func TestAttests(t *testing.T) {
expected := Attests{ expected := Attests{
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, {Type: "provenance", Attrs: map[string]string{"mode": "max"}},
{Type: "sbom", Disabled: true, Attrs: map[string]string{}}, {Type: "sbom", Disabled: true, Attrs: map[string]string{}},
{Type: "sbom", Disabled: false, Attrs: map[string]string{
"generator": "scanner",
"ENV1": `"foo,bar"`,
"Env2": "hello",
}},
} }
require.Equal(t, expected, actual) require.Equal(t, expected, actual)
}) })
@ -41,7 +51,14 @@ func TestAttests(t *testing.T) {
"type": cty.StringVal("provenance"), "type": cty.StringVal("provenance"),
"mode": cty.StringVal("max"), "mode": cty.StringVal("max"),
}), }),
cty.ObjectVal(map[string]cty.Value{
"type": cty.StringVal("sbom"),
"generator": cty.StringVal("scan"),
"ENV1": cty.StringVal(`foo,bar`),
"Env2": cty.StringVal(`hello`),
}),
cty.StringVal("type=sbom,disabled=true"), cty.StringVal("type=sbom,disabled=true"),
cty.StringVal(`type=sbom,generator=scan,"FOO=bar,baz",Hello=World`),
}) })
var actual Attests var actual Attests
@ -50,7 +67,17 @@ func TestAttests(t *testing.T) {
expected := Attests{ expected := Attests{
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, {Type: "provenance", Attrs: map[string]string{"mode": "max"}},
{Type: "sbom", Attrs: map[string]string{
"generator": "scan",
"ENV1": "foo,bar",
"Env2": "hello",
}},
{Type: "sbom", Disabled: true, Attrs: map[string]string{}}, {Type: "sbom", Disabled: true, Attrs: map[string]string{}},
{Type: "sbom", Attrs: map[string]string{
"generator": "scan",
"FOO": "bar,baz",
"Hello": "World",
}},
} }
require.Equal(t, expected, actual) require.Equal(t, expected, actual)
}) })
@ -59,6 +86,11 @@ func TestAttests(t *testing.T) {
attests := Attests{ attests := Attests{
{Type: "provenance", Attrs: map[string]string{"mode": "max"}}, {Type: "provenance", Attrs: map[string]string{"mode": "max"}},
{Type: "sbom", Disabled: true}, {Type: "sbom", Disabled: true},
{Type: "sbom", Attrs: map[string]string{
"generator": "scan",
"ENV1": `"foo,bar"`,
"Env2": "hello",
}},
} }
actual := attests.ToCtyValue() actual := attests.ToCtyValue()
@ -71,6 +103,12 @@ func TestAttests(t *testing.T) {
"type": cty.StringVal("sbom"), "type": cty.StringVal("sbom"),
"disabled": cty.StringVal("true"), "disabled": cty.StringVal("true"),
}), }),
cty.MapVal(map[string]cty.Value{
"type": cty.StringVal("sbom"),
"generator": cty.StringVal("scan"),
"ENV1": cty.StringVal(`"foo,bar"`),
"Env2": cty.StringVal("hello"),
}),
}) })
result := actual.Equals(expected) result := actual.Equals(expected)