|
| 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 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/opencontainers/image-spec/schema" |
| 23 | +) |
| 24 | + |
| 25 | +func TestArtifact(t *testing.T) { |
| 26 | + for i, tt := range []struct { |
| 27 | + manifest string |
| 28 | + fail bool |
| 29 | + }{ |
| 30 | + // expected failure: mediaType does not match pattern |
| 31 | + { |
| 32 | + manifest: ` |
| 33 | +{ |
| 34 | + "mediaType": "invalid", |
| 35 | + "artifactType": "application/example", |
| 36 | + "blobs": [ |
| 37 | + { |
| 38 | + "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", |
| 39 | + "size": 148, |
| 40 | + "digest": "sha256:c57089565e894899735d458f0fd4bb17a0f1e0df8d72da392b85c9b35ee777cd" |
| 41 | + } |
| 42 | + ] |
| 43 | +} |
| 44 | +`, |
| 45 | + fail: true, |
| 46 | + }, |
| 47 | + |
| 48 | + // expected failure: invalid artifact mediaType |
| 49 | + { |
| 50 | + manifest: ` |
| 51 | +{ |
| 52 | + "mediaType": "application/vnd.oci.artifact.manifest.v1+json", |
| 53 | + "artifactType": "invalid", |
| 54 | + "blobs": [ |
| 55 | + { |
| 56 | + "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", |
| 57 | + "size": "148", |
| 58 | + "digest": "sha256:c57089565e894899735d458f0fd4bb17a0f1e0df8d72da392b85c9b35ee777cd" |
| 59 | + } |
| 60 | + ] |
| 61 | +} |
| 62 | +`, |
| 63 | + fail: true, |
| 64 | + }, |
| 65 | + |
| 66 | + // expected failure: blob[0].size is a string, expected integer |
| 67 | + { |
| 68 | + manifest: ` |
| 69 | +{ |
| 70 | + "mediaType": "application/vnd.oci.artifact.manifest.v1+json", |
| 71 | + "artifactType": "application/example", |
| 72 | + "blobs": [ |
| 73 | + { |
| 74 | + "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", |
| 75 | + "size": "148", |
| 76 | + "digest": "sha256:c57089565e894899735d458f0fd4bb17a0f1e0df8d72da392b85c9b35ee777cd" |
| 77 | + } |
| 78 | + ] |
| 79 | +} |
| 80 | +`, |
| 81 | + fail: true, |
| 82 | + }, |
| 83 | + |
| 84 | + // expected failure: subject: size is required |
| 85 | + { |
| 86 | + manifest: ` |
| 87 | +{ |
| 88 | + "mediaType": "application/vnd.oci.artifact.manifest.v1+json", |
| 89 | + "artifactType": "application/example", |
| 90 | + "subject": { |
| 91 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 92 | + "digest": "sha256:c86f7763873b6c0aae22d963bab59b4f5debbed6685761b5951584f6efb0633b" |
| 93 | + }, |
| 94 | + "annotations": { |
| 95 | + "key1": "value1", |
| 96 | + "key2": "value2" |
| 97 | + } |
| 98 | +} |
| 99 | +`, |
| 100 | + fail: true, |
| 101 | + }, |
| 102 | + |
| 103 | + // valid manifest with optional fields |
| 104 | + { |
| 105 | + manifest: ` |
| 106 | +{ |
| 107 | + "mediaType": "application/vnd.oci.artifact.manifest.v1+json", |
| 108 | + "artifactType": "application/example", |
| 109 | + "blobs": [ |
| 110 | + { |
| 111 | + "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", |
| 112 | + "size": 675598, |
| 113 | + "digest": "sha256:9d3dd9504c685a304985025df4ed0283e47ac9ffa9bd0326fddf4d59513f0827" |
| 114 | + }, |
| 115 | + { |
| 116 | + "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", |
| 117 | + "size": 156, |
| 118 | + "digest": "sha256:2b689805fbd00b2db1df73fae47562faac1a626d5f61744bfe29946ecff5d73d" |
| 119 | + }, |
| 120 | + { |
| 121 | + "mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", |
| 122 | + "size": 148, |
| 123 | + "digest": "sha256:c57089565e894899735d458f0fd4bb17a0f1e0df8d72da392b85c9b35ee777cd" |
| 124 | + } |
| 125 | + ], |
| 126 | + "subject": { |
| 127 | + "mediaType": "application/vnd.oci.image.manifest.v1+json", |
| 128 | + "size": 1470, |
| 129 | + "digest": "sha256:c86f7763873b6c0aae22d963bab59b4f5debbed6685761b5951584f6efb0633b" |
| 130 | + }, |
| 131 | + "annotations": { |
| 132 | + "key1": "value1", |
| 133 | + "key2": "value2" |
| 134 | + } |
| 135 | +} |
| 136 | +`, |
| 137 | + fail: false, |
| 138 | + }, |
| 139 | + } { |
| 140 | + r := strings.NewReader(tt.manifest) |
| 141 | + err := schema.ValidatorMediaTypeArtifact.Validate(r) |
| 142 | + |
| 143 | + if got := err != nil; tt.fail != got { |
| 144 | + t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err) |
| 145 | + fmt.Println(tt.manifest) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments