|
| 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]>", |
| 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]>", |
| 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 | +} |
0 commit comments