|
| 1 | +// Copyright 2016 The Linux Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package schema_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/opencontainers/image-spec/schema" |
| 22 | +) |
| 23 | + |
| 24 | +func TestManifestList(t *testing.T) { |
| 25 | + for i, tt := range []struct { |
| 26 | + manifestList string |
| 27 | + fail bool |
| 28 | + }{ |
| 29 | + // expected failure: mediaType does not match pattern |
| 30 | + { |
| 31 | + manifestList: ` |
| 32 | +{ |
| 33 | + "schemaVersion": 2, |
| 34 | + "mediaType": "invalid", |
| 35 | + "manifests": [ |
| 36 | + { |
| 37 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 38 | + "size": 7143, |
| 39 | + "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f", |
| 40 | + "platform": { |
| 41 | + "architecture": "ppc64le", |
| 42 | + "os": "linux" |
| 43 | + } |
| 44 | + } |
| 45 | + ] |
| 46 | +} |
| 47 | +`, |
| 48 | + fail: true, |
| 49 | + }, |
| 50 | + |
| 51 | + // expected failure: manifest.size is string, expected integer |
| 52 | + { |
| 53 | + manifestList: ` |
| 54 | +{ |
| 55 | + "schemaVersion": 2, |
| 56 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 57 | + "manifests": [ |
| 58 | + { |
| 59 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 60 | + "size": "7682", |
| 61 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270", |
| 62 | + "platform": { |
| 63 | + "architecture": "amd64", |
| 64 | + "os": "linux", |
| 65 | + "features": [ |
| 66 | + "sse4" |
| 67 | + ] |
| 68 | + } |
| 69 | + } |
| 70 | + ] |
| 71 | +} |
| 72 | +`, |
| 73 | + fail: true, |
| 74 | + }, |
| 75 | + |
| 76 | + // expected failure: manifest.digest is missing, expected required |
| 77 | + { |
| 78 | + manifestList: ` |
| 79 | +{ |
| 80 | + "schemaVersion": 2, |
| 81 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 82 | + "manifests": [ |
| 83 | + { |
| 84 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 85 | + "size": 7682, |
| 86 | + "platform": { |
| 87 | + "architecture": "amd64", |
| 88 | + "os": "linux", |
| 89 | + "features": [ |
| 90 | + "sse4" |
| 91 | + ] |
| 92 | + } |
| 93 | + } |
| 94 | + ] |
| 95 | +} |
| 96 | +`, |
| 97 | + fail: true, |
| 98 | + }, |
| 99 | + |
| 100 | + // expected failure: manifest.platform is missing, expected required |
| 101 | + { |
| 102 | + manifestList: ` |
| 103 | +{ |
| 104 | + "schemaVersion": 2, |
| 105 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 106 | + "manifests": [ |
| 107 | + { |
| 108 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 109 | + "size": 7682, |
| 110 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270" |
| 111 | + } |
| 112 | + ] |
| 113 | +} |
| 114 | +`, |
| 115 | + fail: true, |
| 116 | + }, |
| 117 | + |
| 118 | + // expected failure: invalid referenced manifest media type |
| 119 | + { |
| 120 | + manifestList: ` |
| 121 | +{ |
| 122 | + "schemaVersion": 2, |
| 123 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 124 | + "manifests": [ |
| 125 | + { |
| 126 | + "mediaType": "invalid", |
| 127 | + "size": 7682, |
| 128 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270", |
| 129 | + "platform": { |
| 130 | + "architecture": "amd64", |
| 131 | + "os": "linux", |
| 132 | + "features": [ |
| 133 | + "sse4" |
| 134 | + ] |
| 135 | + } |
| 136 | + } |
| 137 | + ] |
| 138 | +} |
| 139 | +`, |
| 140 | + fail: true, |
| 141 | + }, |
| 142 | + |
| 143 | + // expected failure: empty referenced manifest media type |
| 144 | + { |
| 145 | + manifestList: ` |
| 146 | +{ |
| 147 | + "schemaVersion": 2, |
| 148 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 149 | + "manifests": [ |
| 150 | + { |
| 151 | + "mediaType": "", |
| 152 | + "size": 7682, |
| 153 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270", |
| 154 | + "platform": { |
| 155 | + "architecture": "amd64", |
| 156 | + "os": "linux", |
| 157 | + "features": [ |
| 158 | + "sse4" |
| 159 | + ] |
| 160 | + } |
| 161 | + } |
| 162 | + ] |
| 163 | +} |
| 164 | +`, |
| 165 | + fail: true, |
| 166 | + }, |
| 167 | + |
| 168 | + // valid manifest list, with optional fields |
| 169 | + { |
| 170 | + manifestList: ` |
| 171 | +{ |
| 172 | + "schemaVersion": 2, |
| 173 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 174 | + "manifests": [ |
| 175 | + { |
| 176 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 177 | + "size": 7143, |
| 178 | + "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f", |
| 179 | + "platform": { |
| 180 | + "architecture": "ppc64le", |
| 181 | + "os": "linux" |
| 182 | + } |
| 183 | + }, |
| 184 | + { |
| 185 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 186 | + "size": 7682, |
| 187 | + "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270", |
| 188 | + "platform": { |
| 189 | + "architecture": "amd64", |
| 190 | + "os": "linux", |
| 191 | + "features": [ |
| 192 | + "sse4" |
| 193 | + ] |
| 194 | + } |
| 195 | + } |
| 196 | + ], |
| 197 | + "annotations": { |
| 198 | + "com.example.key1": "value1", |
| 199 | + "com.example.key2": "value2" |
| 200 | + } |
| 201 | +} |
| 202 | +`, |
| 203 | + fail: false, |
| 204 | + }, |
| 205 | + |
| 206 | + // valid manifest list, with required fields only |
| 207 | + { |
| 208 | + manifestList: ` |
| 209 | +{ |
| 210 | + "schemaVersion": 2, |
| 211 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 212 | + "manifests": [ |
| 213 | + { |
| 214 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 215 | + "size": 7143, |
| 216 | + "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f", |
| 217 | + "platform": { |
| 218 | + "architecture": "ppc64le", |
| 219 | + "os": "linux" |
| 220 | + } |
| 221 | + } |
| 222 | + ] |
| 223 | +} |
| 224 | +`, |
| 225 | + fail: false, |
| 226 | + }, |
| 227 | + |
| 228 | + // valid manifest list, with customized media type of referenced manifest |
| 229 | + { |
| 230 | + manifestList: ` |
| 231 | +{ |
| 232 | + "schemaVersion": 2, |
| 233 | + "mediaType": "application/vnd.oci.image.manifest.list.v1+json", |
| 234 | + "manifests": [ |
| 235 | + { |
| 236 | + "mediaType": "application/customized.manifest+json", |
| 237 | + "size": 7143, |
| 238 | + "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f", |
| 239 | + "platform": { |
| 240 | + "architecture": "ppc64le", |
| 241 | + "os": "linux" |
| 242 | + } |
| 243 | + } |
| 244 | + ] |
| 245 | +} |
| 246 | +`, |
| 247 | + fail: false, |
| 248 | + }, |
| 249 | + } { |
| 250 | + r := strings.NewReader(tt.manifestList) |
| 251 | + err := schema.MediaTypeManifestList.Validate(r) |
| 252 | + |
| 253 | + if got := err != nil; tt.fail != got { |
| 254 | + t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err) |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments