|
1 | 1 | package cache // import "github.com/docker/docker/image/cache" |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "runtime" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/containerd/containerd/platforms" |
6 | 8 | "github.com/docker/docker/api/types/container" |
7 | 9 | "github.com/docker/docker/api/types/strslice" |
8 | 10 | "github.com/docker/go-connections/nat" |
| 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 platforms.Platform |
| 136 | + image platforms.Platform |
| 137 | + expected bool |
| 138 | + }{ |
| 139 | + { |
| 140 | + name: "same os and arch", |
| 141 | + builder: platforms.Platform{Architecture: "amd64", OS: runtime.GOOS}, |
| 142 | + image: platforms.Platform{Architecture: "amd64", OS: runtime.GOOS}, |
| 143 | + expected: true, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "same os different arch", |
| 147 | + builder: platforms.Platform{Architecture: "amd64", OS: runtime.GOOS}, |
| 148 | + image: platforms.Platform{Architecture: "arm64", OS: runtime.GOOS}, |
| 149 | + expected: false, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "same os smaller host variant", |
| 153 | + builder: platforms.Platform{Variant: "v7", Architecture: "arm", OS: runtime.GOOS}, |
| 154 | + image: platforms.Platform{Variant: "v8", Architecture: "arm", OS: runtime.GOOS}, |
| 155 | + expected: false, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "same os higher host variant", |
| 159 | + builder: platforms.Platform{Variant: "v8", Architecture: "arm", OS: runtime.GOOS}, |
| 160 | + image: platforms.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: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.22621"}, |
| 167 | + image: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 168 | + expected: true, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "different revision", |
| 172 | + builder: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.1234"}, |
| 173 | + image: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 174 | + expected: true, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "different major", |
| 178 | + builder: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "11.0.17763.5329"}, |
| 179 | + image: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 180 | + expected: false, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "different minor same osver", |
| 184 | + builder: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 185 | + image: platforms.Platform{Architecture: "amd64", OS: "windows", OSVersion: "10.1.17763.5329"}, |
| 186 | + expected: false, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "different arch same osver", |
| 190 | + builder: platforms.Platform{Architecture: "arm64", OS: "windows", OSVersion: "10.0.17763.5329"}, |
| 191 | + image: platforms.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