Skip to content

Commit c04fca2

Browse files
author
Sergiusz Urbaniak
committed
schema: add more tests
Currently samples in the specification are being tested. This adds additional tests. Fixes #83 Signed-off-by: Sergiusz Urbaniak <[email protected]>
1 parent b72e75a commit c04fca2

File tree

2 files changed

+287
-0
lines changed

2 files changed

+287
-0
lines changed

schema/config_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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 TestConfig(t *testing.T) {
25+
for i, tt := range []struct {
26+
config string
27+
fail bool
28+
}{
29+
// expected failure: field "os" has numeric value, must be string
30+
{
31+
config: `
32+
{
33+
"architecture": "amd64",
34+
"os": 123
35+
}
36+
`,
37+
fail: true,
38+
},
39+
40+
// expected failure: field "config.User" has numeric value, must be string
41+
{
42+
config: `
43+
{
44+
"created": "2015-10-31T22:22:56.015925234Z",
45+
"author": "Alyssa P. Hacker &[email protected]&gt",
46+
"architecture": "amd64",
47+
"os": "linux",
48+
"config": {
49+
"User": 1234
50+
}
51+
}
52+
`,
53+
fail: true,
54+
},
55+
56+
// expected failue: history has string value, must be an array
57+
{
58+
config: `
59+
{
60+
"history": "should be an array"
61+
}
62+
`,
63+
fail: true,
64+
},
65+
66+
// expected failure: Env has numeric value, must be a string
67+
{
68+
config: `
69+
{
70+
"config": {
71+
"Env": [
72+
7353
73+
]
74+
}
75+
}
76+
`,
77+
fail: true,
78+
},
79+
80+
// expected failure: config.Volumes has string array, must be an object (string set)
81+
{
82+
config: `
83+
{
84+
"config": {
85+
"Volumes": [
86+
"/var/job-result-data",
87+
"/var/log/my-app-logs"
88+
]
89+
}
90+
}
91+
`,
92+
fail: true,
93+
},
94+
95+
// expected failue: invalid JSON
96+
{
97+
config: `invalid JSON`,
98+
fail: true,
99+
},
100+
101+
{
102+
config: `
103+
{
104+
"created": "2015-10-31T22:22:56.015925234Z",
105+
"author": "Alyssa P. Hacker &[email protected]&gt",
106+
"architecture": "amd64",
107+
"os": "linux",
108+
"config": {
109+
"User": "1:1",
110+
"Memory": 2048,
111+
"MemorySwap": 4096,
112+
"CpuShares": 8,
113+
"ExposedPorts": {
114+
"8080/tcp": {}
115+
},
116+
"Env": [
117+
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
118+
"FOO=docker_is_a_really",
119+
"BAR=great_tool_you_know"
120+
],
121+
"Entrypoint": [
122+
"/bin/sh"
123+
],
124+
"Cmd": [
125+
"--foreground",
126+
"--config",
127+
"/etc/my-app.d/default.cfg"
128+
],
129+
"Volumes": {
130+
"/var/job-result-data": {},
131+
"/var/log/my-app-logs": {}
132+
},
133+
"WorkingDir": "/home/alice"
134+
},
135+
"rootfs": {
136+
"diff_ids": [
137+
"sha256:9d3dd9504c685a304985025df4ed0283e47ac9ffa9bd0326fddf4d59513f0827",
138+
"sha256:2b689805fbd00b2db1df73fae47562faac1a626d5f61744bfe29946ecff5d73d"
139+
],
140+
"type": "layers"
141+
},
142+
"history": [
143+
{
144+
"created": "2015-10-31T22:22:54.690851953Z",
145+
"created_by": "/bin/sh -c #(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"
146+
},
147+
{
148+
"created": "2015-10-31T22:22:55.613815829Z",
149+
"created_by": "/bin/sh -c #(nop) CMD [\"sh\"]",
150+
"empty_layer": true
151+
}
152+
]
153+
}
154+
`,
155+
fail: false,
156+
},
157+
} {
158+
r := strings.NewReader(tt.config)
159+
err := schema.MediaTypeImageSerializationConfig.Validate(r)
160+
161+
if got := err != nil; tt.fail != got {
162+
t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err)
163+
}
164+
}
165+
}

schema/manifest_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 TestManifest(t *testing.T) {
25+
for i, tt := range []struct {
26+
manifest string
27+
fail bool
28+
}{
29+
// expected failure: mediaType does not match pattern
30+
{
31+
manifest: `
32+
{
33+
"schemaVersion": 2,
34+
"mediaType": "invalid"
35+
}
36+
`,
37+
fail: true,
38+
},
39+
40+
// expected failure: config.size is integer, expected string
41+
{
42+
manifest: `
43+
{
44+
"schemaVersion": 2,
45+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
46+
"config": {
47+
"mediaType": "application/vnd.oci.image.serialization.v1+json",
48+
"size": "1470",
49+
"digest": "sha256:c86f7763873b6c0aae22d963bab59b4f5debbed6685761b5951584f6efb0633b"
50+
}
51+
}
52+
`,
53+
fail: true,
54+
},
55+
56+
// expected failure: layers.size is string, expected integer
57+
{
58+
manifest: `
59+
{
60+
"schemaVersion": 2,
61+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
62+
"config": {
63+
"mediaType": "application/vnd.oci.image.serialization.v1+json",
64+
"size": 1470,
65+
"digest": "sha256:c86f7763873b6c0aae22d963bab59b4f5debbed6685761b5951584f6efb0633b"
66+
},
67+
"layers": [
68+
{
69+
"mediaType": "application/vnd.oci.image.serialization.rootfs.tar.gzip",
70+
"size": "675598"
71+
}
72+
]
73+
}
74+
`,
75+
fail: true,
76+
},
77+
78+
// valid manifest
79+
{
80+
manifest: `
81+
{
82+
"schemaVersion": 2,
83+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
84+
"config": {
85+
"mediaType": "application/vnd.oci.image.serialization.v1+json",
86+
"size": 1470,
87+
"digest": "sha256:c86f7763873b6c0aae22d963bab59b4f5debbed6685761b5951584f6efb0633b"
88+
},
89+
"layers": [
90+
{
91+
"mediaType": "application/vnd.oci.image.serialization.rootfs.tar.gzip",
92+
"size": 675598,
93+
"digest": "sha256:9d3dd9504c685a304985025df4ed0283e47ac9ffa9bd0326fddf4d59513f0827"
94+
},
95+
{
96+
"mediaType": "application/vnd.oci.image.serialization.rootfs.tar.gzip",
97+
"size": 156,
98+
"digest": "sha256:2b689805fbd00b2db1df73fae47562faac1a626d5f61744bfe29946ecff5d73d"
99+
},
100+
{
101+
"mediaType": "application/vnd.oci.image.serialization.rootfs.tar.gzip",
102+
"size": 148,
103+
"digest": "sha256:c57089565e894899735d458f0fd4bb17a0f1e0df8d72da392b85c9b35ee777cd"
104+
}
105+
],
106+
"annotations": {
107+
"key1": "value1",
108+
"key2": "value2"
109+
}
110+
}
111+
`,
112+
fail: false,
113+
},
114+
} {
115+
r := strings.NewReader(tt.manifest)
116+
err := schema.MediaTypeManifest.Validate(r)
117+
118+
if got := err != nil; tt.fail != got {
119+
t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err)
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)