...

Source file src/github.com/opencontainers/image-spec/schema/config_test.go

Documentation: github.com/opencontainers/image-spec/schema

     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      "rootfs": {
    36        "diff_ids": [
    37          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
    38        ],
    39        "type": "layers"
    40      }
    41  }
    42  `,
    43  			fail: true,
    44  		},
    45  		// expected failure: field "variant" has numeric value, must be string
    46  		{
    47  			config: `
    48  {
    49      "architecture": "arm64",
    50      "variant": 123,
    51      "os": "linux",
    52      "rootfs": {
    53        "diff_ids": [
    54          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
    55        ],
    56        "type": "layers"
    57      }
    58  }
    59  `,
    60  			fail: true,
    61  		},
    62  
    63  		// expected failure: field "config.User" has numeric value, must be string
    64  		{
    65  			config: `
    66  {
    67      "created": "2015-10-31T22:22:56.015925234Z",
    68      "author": "Alyssa P. Hacker <alyspdev@example.com>",
    69      "architecture": "amd64",
    70      "os": "linux",
    71      "config": {
    72          "User": 1234
    73      },
    74      "rootfs": {
    75        "diff_ids": [
    76          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
    77        ],
    78        "type": "layers"
    79      }
    80  }
    81  `,
    82  			fail: true,
    83  		},
    84  
    85  		// expected failue: history has string value, must be an array
    86  		{
    87  			config: `
    88  {
    89      "history": "should be an array",
    90      "architecture": "amd64",
    91      "os": 123,
    92      "rootfs": {
    93        "diff_ids": [
    94          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
    95        ],
    96        "type": "layers"
    97      }
    98  }
    99  `,
   100  			fail: true,
   101  		},
   102  
   103  		// expected failure: Env has numeric value, must be a string
   104  		{
   105  			config: `
   106  {
   107      "architecture": "amd64",
   108      "os": 123,
   109      "config": {
   110          "Env": [
   111              7353
   112          ]
   113      },
   114      "rootfs": {
   115        "diff_ids": [
   116          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
   117        ],
   118        "type": "layers"
   119      }
   120  }
   121  `,
   122  			fail: true,
   123  		},
   124  
   125  		// expected failure: config.Volumes has string array, must be an object (string set)
   126  		{
   127  			config: `
   128  {
   129      "architecture": "amd64",
   130      "os": 123,
   131      "config": {
   132          "Volumes": [
   133              "/var/job-result-data",
   134              "/var/log/my-app-logs"
   135          ]
   136      },
   137      "rootfs": {
   138        "diff_ids": [
   139          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
   140        ],
   141        "type": "layers"
   142      }
   143  }
   144  `,
   145  			fail: true,
   146  		},
   147  
   148  		// expected failue: invalid JSON
   149  		{
   150  			config: `invalid JSON`,
   151  			fail:   true,
   152  		},
   153  
   154  		// valid config with optional fields
   155  		{
   156  			config: `
   157  {
   158      "created": "2015-10-31T22:22:56.015925234Z",
   159      "author": "Alyssa P. Hacker <alyspdev@example.com>",
   160      "architecture": "arm64",
   161      "variant": "v8",
   162      "os": "linux",
   163      "config": {
   164          "User": "1:1",
   165          "ExposedPorts": {
   166              "8080/tcp": {}
   167          },
   168          "Env": [
   169              "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
   170              "FOO=docker_is_a_really",
   171              "BAR=great_tool_you_know"
   172          ],
   173          "Entrypoint": [
   174              "/bin/sh"
   175          ],
   176          "Cmd": [
   177              "--foreground",
   178              "--config",
   179              "/etc/my-app.d/default.cfg"
   180          ],
   181          "Volumes": {
   182              "/var/job-result-data": {},
   183              "/var/log/my-app-logs": {}
   184          },
   185          "StopSignal": "SIGKILL",
   186          "WorkingDir": "/home/alice",
   187          "Labels": {
   188              "com.example.project.git.url": "https://example.com/project.git",
   189              "com.example.project.git.commit": "45a939b2999782a3f005621a8d0f29aa387e1d6b"
   190          }
   191      },
   192      "rootfs": {
   193        "diff_ids": [
   194          "sha256:9d3dd9504c685a304985025df4ed0283e47ac9ffa9bd0326fddf4d59513f0827",
   195          "sha256:2b689805fbd00b2db1df73fae47562faac1a626d5f61744bfe29946ecff5d73d"
   196        ],
   197        "type": "layers"
   198      },
   199      "history": [
   200        {
   201          "created": "2015-10-31T22:22:54.690851953Z",
   202          "created_by": "/bin/sh -c #(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"
   203        },
   204        {
   205          "created": "2015-10-31T22:22:55.613815829Z",
   206          "created_by": "/bin/sh -c #(nop) CMD [\"sh\"]",
   207          "empty_layer": true
   208        }
   209      ]
   210  }
   211  `,
   212  			fail: false,
   213  		},
   214  
   215  		// valid config with only required fields
   216  		{
   217  			config: `
   218  {
   219      "architecture": "amd64",
   220      "os": "linux",
   221      "rootfs": {
   222        "diff_ids": [
   223          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
   224        ],
   225        "type": "layers"
   226      }
   227  }
   228  `,
   229  			fail: false,
   230  		},
   231  		// expected failure: Env is invalid
   232  		{
   233  			config: `
   234  {
   235      "architecture": "amd64",
   236      "os": "linux",
   237      "config": {
   238          "Env": [
   239              "foo"
   240          ]
   241      },
   242      "rootfs": {
   243        "diff_ids": [
   244          "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"
   245        ],
   246        "type": "layers"
   247      }
   248  }
   249  `,
   250  			fail: true,
   251  		},
   252  	} {
   253  		r := strings.NewReader(tt.config)
   254  		err := schema.ValidatorMediaTypeImageConfig.Validate(r)
   255  
   256  		if got := err != nil; tt.fail != got {
   257  			t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err)
   258  		}
   259  	}
   260  }
   261  

View as plain text