|
1 | 1 | package cache // import "github.com/docker/docker/image/cache"
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "runtime" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/docker/docker/api/types/container"
|
7 | 8 | "github.com/docker/docker/api/types/strslice"
|
8 | 9 | "github.com/docker/go-connections/nat"
|
| 10 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 11 | + "gotest.tools/v3/assert" |
| 12 | + is "gotest.tools/v3/assert/cmp" |
9 | 13 | )
|
10 | 14 |
|
11 | 15 | // Just to make life easier
|
@@ -124,3 +128,79 @@ func TestCompare(t *testing.T) {
|
124 | 128 | }
|
125 | 129 | }
|
126 | 130 | }
|
| 131 | + |
| 132 | +func TestPlatformCompare(t *testing.T) { |
| 133 | + for _, tc := range []struct { |
| 134 | + name string |
| 135 | + builder ocispec.Platform |
| 136 | + image ocispec.Platform |
| 137 | + expected bool |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "same os and arch", |
| 141 | + builder: ocispec.Platform{Architecture: "amd64", OS: runtime.GOOS}, |
| 142 | + image: ocispec.Platform{Architecture: "amd64", OS: runtime.GOOS}, |
| 143 | + expected: true, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "same os different arch", |
| 147 | + builder: ocispec.Platform{Architecture: "amd64", OS: runtime.GOOS}, |
| 148 | + image: ocispec.Platform{Architecture: "arm64", OS: runtime.GOOS}, |
| 149 | + expected: false, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "same os smaller host variant", |
| 153 | + builder: ocispec.Platform{Variant: "v7", Architecture: "arm", OS: runtime.GOOS}, |
| 154 | + image: ocispec.Platform{Variant: "v8", Architecture: "arm", OS: runtime.GOOS}, |
| 155 | + expected: false, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "same os higher host variant", |
| 159 | + builder: ocispec.Platform{Variant: "v8", Architecture: "arm", OS: runtime.GOOS}, |
| 160 | + image: ocispec.Platform{Variant: "v7", Architecture: "arm", OS: runtime.GOOS}, |
| 161 | + expected: true, |
| 162 | + }, |
| 163 | + { |
| 164 | + // Test for https://github.com/moby/moby/issues/47307 |
| 165 | + name: "different build and revision", |
| 166 | + builder: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.22621"}, |
| 167 | + image: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 168 | + expected: true, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "different revision", |
| 172 | + builder: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.1234"}, |
| 173 | + image: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 174 | + expected: true, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "different major", |
| 178 | + builder: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "11.0.17763.5329"}, |
| 179 | + image: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 180 | + expected: false, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "different minor same osver", |
| 184 | + builder: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 185 | + image: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.1.17763.5329"}, |
| 186 | + expected: false, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "different arch same osver", |
| 190 | + builder: ocispec.Platform{Architecture: "arm64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 191 | + image: ocispec.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 192 | + expected: false, |
| 193 | + }, |
| 194 | + } { |
| 195 | + tc := tc |
| 196 | + // OSVersion comparison is only performed by containerd platform |
| 197 | + // matcher if built on Windows. |
| 198 | + if (tc.image.OSVersion != "" || tc.builder.OSVersion != "") && runtime.GOOS != "windows" { |
| 199 | + continue |
| 200 | + } |
| 201 | + |
| 202 | + t.Run(tc.name, func(t *testing.T) { |
| 203 | + assert.Check(t, is.Equal(comparePlatform(tc.builder, tc.image), tc.expected)) |
| 204 | + }) |
| 205 | + } |
| 206 | +} |
0 commit comments