...

Source file src/github.com/opencontainers/image-spec/schema/imageindex_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 TestImageIndex(t *testing.T) {
    25  	for i, tt := range []struct {
    26  		imageIndex string
    27  		fail       bool
    28  	}{
    29  		// expected failure: mediaType does not match pattern
    30  		{
    31  			imageIndex: `
    32  {
    33    "schemaVersion": 2,
    34    "mediaType": "application/vnd.oci.image.index.v1+json",
    35    "manifests": [
    36      {
    37        "mediaType": "invalid",
    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  			imageIndex: `
    54  {
    55    "schemaVersion": 2,
    56    "mediaType": "application/vnd.oci.image.index.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        }
    66      }
    67    ]
    68  }
    69  `,
    70  			fail: true,
    71  		},
    72  
    73  		// expected failure: manifest.digest is missing, expected required
    74  		{
    75  			imageIndex: `
    76  {
    77    "schemaVersion": 2,
    78    "mediaType": "application/vnd.oci.image.index.v1+json",
    79    "manifests": [
    80      {
    81        "mediaType": "application/vnd.oci.image.manifest.v1+json",
    82        "size": 7682,
    83        "platform": {
    84          "architecture": "amd64",
    85          "os": "linux"
    86        }
    87      }
    88    ]
    89  }
    90  `,
    91  			fail: true,
    92  		},
    93  
    94  		// expected failure: in the optional field platform platform.architecture is missing, expected required
    95  		{
    96  			imageIndex: `
    97  {
    98    "schemaVersion": 2,
    99    "mediaType": "application/vnd.oci.image.index.v1+json",
   100    "manifests": [
   101      {
   102        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   103        "size": 7682,
   104        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
   105        "platform": {
   106          "os": "linux"
   107        }
   108      }
   109    ]
   110  }
   111  `,
   112  			fail: true,
   113  		},
   114  
   115  		// expected failure: invalid referenced manifest media type
   116  		{
   117  			imageIndex: `
   118  {
   119    "schemaVersion": 2,
   120    "mediaType": "application/vnd.oci.image.index.v1+json",
   121    "manifests": [
   122      {
   123        "mediaType": "invalid",
   124        "size": 7682,
   125        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
   126        "platform": {
   127          "architecture": "amd64",
   128          "os": "linux"
   129        }
   130      }
   131    ]
   132  }
   133  `,
   134  			fail: true,
   135  		},
   136  
   137  		// expected failure: empty referenced manifest media type
   138  		{
   139  			imageIndex: `
   140  {
   141    "schemaVersion": 2,
   142    "mediaType": "application/vnd.oci.image.index.v1+json",
   143    "manifests": [
   144      {
   145        "mediaType": "",
   146        "size": 7682,
   147        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
   148        "platform": {
   149          "architecture": "amd64",
   150          "os": "linux"
   151        }
   152      }
   153    ]
   154  }
   155  `,
   156  			fail: true,
   157  		},
   158  
   159  		// valid image index, with optional fields
   160  		{
   161  			imageIndex: `
   162  {
   163    "schemaVersion": 2,
   164    "mediaType": "application/vnd.oci.image.index.v1+json",
   165    "manifests": [
   166      {
   167        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   168        "size": 7143,
   169        "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
   170        "platform": {
   171          "architecture": "ppc64le",
   172          "os": "linux"
   173        }
   174      },
   175      {
   176        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   177        "size": 7682,
   178        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
   179        "platform": {
   180          "architecture": "amd64",
   181          "os": "linux"
   182        }
   183      }
   184    ],
   185    "annotations": {
   186      "com.example.key1": "value1",
   187      "com.example.key2": "value2"
   188    }
   189  }
   190  `,
   191  			fail: false,
   192  		},
   193  
   194  		// valid image index, with required fields only
   195  		{
   196  			imageIndex: `
   197  {
   198    "schemaVersion": 2,
   199    "mediaType": "application/vnd.oci.image.index.v1+json",
   200    "manifests": [
   201      {
   202        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   203        "size": 7143,
   204        "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f"
   205      }
   206    ]
   207  }
   208  `,
   209  			fail: false,
   210  		},
   211  
   212  		// valid image index, with customized media type of referenced manifest
   213  		{
   214  			imageIndex: `
   215  {
   216    "schemaVersion": 2,
   217    "mediaType": "application/vnd.oci.image.index.v1+json",
   218    "manifests": [
   219      {
   220        "mediaType": "application/customized.manifest+json",
   221        "size": 7143,
   222        "digest": "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
   223        "platform": {
   224          "architecture": "ppc64le",
   225          "os": "linux"
   226        }
   227      }
   228    ]
   229  }
   230  `,
   231  			fail: false,
   232  		},
   233  
   234  		// valid image index with artifactType and manifests
   235  		{
   236  			imageIndex: `
   237  {
   238    "schemaVersion": 2,
   239    "mediaType" : "application/vnd.oci.image.index.v1+json",
   240    "artifactType": "application/vnd.example+type",
   241    "manifests": [
   242      {
   243        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   244        "size": 7143,
   245        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270"
   246      },
   247      {
   248        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   249        "artifactType": "application/vnd.example1+type",
   250        "size": 506,
   251        "digest": "sha256:99953afc4b90c7d78079d189ae10da0a1002e6be5e9e8dedaf9f7f29def42111"
   252      }
   253    ]
   254  }
   255  `,
   256  			fail: false,
   257  		},
   258  
   259  		// valid image index with a subject field
   260  		{
   261  			imageIndex: `
   262  {
   263    "schemaVersion": 2,
   264    "mediaType": "application/vnd.oci.image.index.v1+json",
   265    "manifests": [
   266      {
   267        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   268        "size": 7682,
   269        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
   270        "platform": {
   271          "architecture": "amd64",
   272          "os": "linux"
   273        }
   274      }
   275    ],
   276    "subject" : {
   277      "mediaType": "application/vnd.oci.image.manifest.v1+json",
   278      "size": 1234,
   279      "digest": "sha256:220a60ecd4a3c32c282622a625a54db9ba0ff55b5ba9c29c7064a2bc358b6a3e"
   280    }
   281  }
   282  `,
   283  			fail: false,
   284  		},
   285  
   286  		// expected failure, invalid subject field
   287  		{
   288  			imageIndex: `
   289  {
   290    "schemaVersion": 2,
   291    "mediaType": "application/vnd.oci.image.index.v1+json",
   292    "manifests": [
   293      {
   294        "mediaType": "application/vnd.oci.image.manifest.v1+json",
   295        "size": 7682,
   296        "digest": "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
   297        "platform": {
   298          "architecture": "amd64",
   299          "os": "linux"
   300        }
   301      }
   302    ],
   303    "subject" : "nope"
   304  }
   305  `,
   306  			fail: true,
   307  		},
   308  	} {
   309  		r := strings.NewReader(tt.imageIndex)
   310  		err := schema.ValidatorMediaTypeImageIndex.Validate(r)
   311  
   312  		if got := err != nil; tt.fail != got {
   313  			t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err)
   314  		}
   315  	}
   316  }
   317  

View as plain text