1
0
mirror of https://github.com/moby/buildkit.git synced 2025-11-27 04:01:46 +03:00

sourcepolicy: fix policy so last rule wins

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff
2022-12-13 18:50:12 +00:00
committed by Tonis Tiigi
parent adea592577
commit c40f30e45b
7 changed files with 152 additions and 87 deletions

View File

@@ -5,14 +5,14 @@ import (
"testing"
"github.com/moby/buildkit/solver/pb"
spb "github.com/moby/buildkit/sourcepolicy/pb"
"github.com/stretchr/testify/require"
)
func TestMutate(t *testing.T) {
type testCaseOp struct {
op *pb.Op
dest string
destAttrs map[string]string
rule *spb.Rule
expected bool
expectedOp *pb.Op
expectedErr string
@@ -27,7 +27,14 @@ func TestMutate(t *testing.T) {
},
},
},
dest: "docker-image://docker.io/library/busybox:1.34.1-uclibc@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
rule: &spb.Rule{
Selector: &spb.Selector{
Identifier: "docker-image://docker.io/library/busybox:1.34.1-uclibc",
},
Updates: &spb.Update{
Identifier: "docker-image://docker.io/library/busybox:1.34.1-uclibc@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
},
},
expected: true,
expectedOp: &pb.Op{
Op: &pb.Op_Source{
@@ -45,7 +52,14 @@ func TestMutate(t *testing.T) {
},
},
},
dest: "docker-image://docker.io/library/busybox:latest@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
rule: &spb.Rule{
Selector: &spb.Selector{
Identifier: "docker-image://docker.io/library/busybox",
},
Updates: &spb.Update{
Identifier: "docker-image://docker.io/library/busybox:latest@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
},
},
expected: true,
expectedOp: &pb.Op{
Op: &pb.Op_Source{
@@ -64,7 +78,14 @@ func TestMutate(t *testing.T) {
},
},
},
dest: "docker-image://docker.io/library/busybox:latest@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
rule: &spb.Rule{
Selector: &spb.Selector{
Identifier: "docker-image://docker.io/library/busybox:latest*",
},
Updates: &spb.Update{
Identifier: "docker-image://docker.io/library/busybox:latest@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
},
},
expected: true,
expectedOp: &pb.Op{
Op: &pb.Op_Source{
@@ -82,9 +103,14 @@ func TestMutate(t *testing.T) {
},
},
},
dest: "https://raw.githubusercontent.com/moby/buildkit/v0.10.1/README.md",
destAttrs: map[string]string{pb.AttrHTTPChecksum: "sha256:6e4b94fc270e708e1068be28bd3551dc6917a4fc5a61293d51bb36e6b75c4b53"},
expected: true,
rule: &spb.Rule{
Selector: &spb.Selector{},
Updates: &spb.Update{
Identifier: "https://raw.githubusercontent.com/moby/buildkit/v0.10.1/README.md",
Attrs: map[string]string{pb.AttrHTTPChecksum: "sha256:6e4b94fc270e708e1068be28bd3551dc6917a4fc5a61293d51bb36e6b75c4b53"},
},
},
expected: true,
expectedOp: &pb.Op{
Op: &pb.Op_Source{
Source: &pb.SourceOp{
@@ -101,9 +127,9 @@ func TestMutate(t *testing.T) {
ctx := context.Background()
for _, tc := range testCases {
op := *tc.op
t.Run(op.String(), func(t *testing.T) {
mutated, err := mutate(ctx, op.GetSource(), tc.dest, tc.destAttrs)
src := op.GetSource()
mutated, err := mutate(ctx, src, tc.rule, &selectorCache{Selector: tc.rule.Selector}, src.GetIdentifier())
require.Equal(t, tc.expected, mutated)
if tc.expectedErr != "" {
require.Error(t, err, tc.expectedErr)