File: export_test.go

package info (click to toggle)
docker-buildx 0.19.3%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,852 kB
  • sloc: sh: 318; makefile: 73
file content (119 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download
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
package buildflags

import (
	"cmp"
	"slices"
	"testing"

	"github.com/moby/buildkit/exporter/containerimage/exptypes"
	ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestParseAnnotations(t *testing.T) {
	tests := []struct {
		name    string
		in      []string
		want    map[exptypes.AnnotationKey]string
		wantErr string
	}{
		{
			name: "basic",
			in:   []string{"a=b"},
			want: map[exptypes.AnnotationKey]string{
				{Key: "a"}: "b",
			},
		},
		{
			name: "reverse-DNS key",
			in:   []string{"com.example=a"},
			want: map[exptypes.AnnotationKey]string{
				{Key: "com.example"}: "a",
			},
		},
		{
			name: "specify type",
			in:   []string{"manifest:com.example=a"},
			want: map[exptypes.AnnotationKey]string{
				{Type: "manifest", Key: "com.example"}: "a",
			},
		},
		{
			name:    "specify bad type",
			in:      []string{"bad:com.example=a"},
			wantErr: "unknown annotation type",
		},
		{
			name: "specify type and platform",
			in:   []string{"manifest[plat/form]:com.example=a"},
			want: map[exptypes.AnnotationKey]string{
				{
					Type: "manifest",
					Platform: &ocispecs.Platform{
						OS:           "plat",
						Architecture: "form",
					},
					Key: "com.example",
				}: "a",
			},
		},
		{
			name: "specify multiple types",
			in:   []string{"index,manifest:com.example=a"},
			want: map[exptypes.AnnotationKey]string{
				{Type: "index", Key: "com.example"}:    "a",
				{Type: "manifest", Key: "com.example"}: "a",
			},
		},
		{
			name: "specify multiple types and platform",
			in:   []string{"index,manifest[plat/form]:com.example=a"},
			want: map[exptypes.AnnotationKey]string{
				{Type: "index", Key: "com.example"}: "a",
				{
					Type: "manifest",
					Platform: &ocispecs.Platform{
						OS:           "plat",
						Architecture: "form",
					},
					Key: "com.example",
				}: "a",
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := ParseAnnotations(test.in)
			if test.wantErr != "" {
				require.ErrorContains(t, err, test.wantErr)
			} else {
				require.NoError(t, err)
			}

			// Can't compare maps with pointer in their keys, need to extract and sort the map entries
			wantKVs := entries(test.want)
			gotKVs := entries(got)

			assert.Equal(t, wantKVs, gotKVs)
		})
	}
}

type kv struct {
	Key exptypes.AnnotationKey
	Val string
}

func entries(in map[exptypes.AnnotationKey]string) []kv {
	var out []kv
	for k, v := range in {
		out = append(out, kv{k, v})
	}

	sortFunc := func(a, b kv) int { return cmp.Compare(a.Key.String(), b.Key.String()) }
	slices.SortFunc(out, sortFunc)

	return out
}