1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
package sourcepolicy
import (
"context"
"testing"
"github.com/moby/buildkit/solver/pb"
spb "github.com/moby/buildkit/sourcepolicy/pb"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
func TestMutate(t *testing.T) {
type testCaseOp struct {
op *pb.Op
rule *spb.Rule
expected bool
expectedOp *pb.Op
expectedErr string
}
testCases := []testCaseOp{
{
op: &pb.Op{
Op: &pb.Op_Source{
Source: &pb.SourceOp{
Identifier: "docker-image://docker.io/library/busybox:1.34.1-uclibc",
},
},
},
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{
Source: &pb.SourceOp{
Identifier: "docker-image://docker.io/library/busybox:1.34.1-uclibc@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
},
},
},
},
{
op: &pb.Op{
Op: &pb.Op_Source{
Source: &pb.SourceOp{
Identifier: "docker-image://docker.io/library/busybox",
},
},
},
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{
Source: &pb.SourceOp{
Identifier: "docker-image://docker.io/library/busybox:latest@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
},
},
},
},
{
// Discard the existing digest that might have been resolved by the Dockerfile frontend's MetaResolver.
op: &pb.Op{
Op: &pb.Op_Source{
Source: &pb.SourceOp{
Identifier: "docker-image://docker.io/library/busybox:latest@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
},
},
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{
Source: &pb.SourceOp{
Identifier: "docker-image://docker.io/library/busybox:latest@sha256:3614ca5eacf0a3a1bcc361c939202a974b4902b9334ff36eb29ffe9011aaad83",
},
},
},
},
{
op: &pb.Op{
Op: &pb.Op_Source{
Source: &pb.SourceOp{
Identifier: "https://raw.githubusercontent.com/moby/buildkit/v0.10.1/README.md",
},
},
},
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{
Identifier: "https://raw.githubusercontent.com/moby/buildkit/v0.10.1/README.md",
Attrs: map[string]string{
pb.AttrHTTPChecksum: "sha256:6e4b94fc270e708e1068be28bd3551dc6917a4fc5a61293d51bb36e6b75c4b53",
},
},
},
},
},
}
ctx := context.Background()
for _, tc := range testCases {
op := tc.op
t.Run(op.String(), func(t *testing.T) {
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)
} else {
require.True(t, proto.Equal(tc.expectedOp, op))
}
})
}
}
|