...

Source file src/github.com/opencontainers/image-spec/schema/backwards_compatibility_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  	_ "crypto/sha256"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/opencontainers/go-digest"
    23  	"github.com/opencontainers/image-spec/schema"
    24  	v1 "github.com/opencontainers/image-spec/specs-go/v1"
    25  )
    26  
    27  var compatMap = map[string]string{
    28  	"application/vnd.docker.distribution.manifest.list.v2+json": v1.MediaTypeImageIndex,
    29  	"application/vnd.docker.distribution.manifest.v2+json":      v1.MediaTypeImageManifest,
    30  	"application/vnd.docker.image.rootfs.diff.tar.gzip":         v1.MediaTypeImageLayerGzip,
    31  	"application/vnd.docker.container.image.v1+json":            v1.MediaTypeImageConfig,
    32  }
    33  
    34  // convertFormats converts Docker v2.2 image format JSON documents to OCI
    35  // format by simply replacing instances of the strings found in the compatMap
    36  // found in the input string.
    37  func convertFormats(input string) string {
    38  	out := input
    39  	for k, v := range compatMap {
    40  		out = strings.Replace(out, k, v, -1)
    41  	}
    42  	return out
    43  }
    44  
    45  func TestBackwardsCompatibilityImageIndex(t *testing.T) {
    46  	for i, tt := range []struct {
    47  		imageIndex string
    48  		digest     digest.Digest
    49  		fail       bool
    50  	}{
    51  		{
    52  			digest: "sha256:4ffd0883f25635999f04ea543240a27c9a4341979ff7d46a9774f71512eebb1f",
    53  			imageIndex: `{
    54     "schemaVersion": 2,
    55     "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
    56     "manifests": [
    57        {
    58           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    59           "size": 2094,
    60           "digest": "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783",
    61           "platform": {
    62              "architecture": "ppc64le",
    63              "os": "linux"
    64           }
    65        },
    66        {
    67           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    68           "size": 1922,
    69           "digest": "sha256:ae1b0e06e8ade3a11267564a26e750585ba2259c0ecab59ab165ad1af41d1bdd",
    70           "platform": {
    71              "architecture": "amd64",
    72              "os": "linux"
    73           }
    74        },
    75        {
    76           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    77           "size": 2084,
    78           "digest": "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2",
    79           "platform": {
    80              "architecture": "s390x",
    81              "os": "linux"
    82           }
    83        },
    84        {
    85           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    86           "size": 2084,
    87           "digest": "sha256:07ebe243465ef4a667b78154ae6c3ea46fdb1582936aac3ac899ea311a701b40",
    88           "platform": {
    89              "architecture": "arm",
    90              "os": "linux",
    91              "variant": "v7"
    92           }
    93        },
    94        {
    95           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    96           "size": 2090,
    97           "digest": "sha256:fb2fc0707b86dafa9959fe3d29e66af8787aee4d9a23581714be65db4265ad8a",
    98           "platform": {
    99              "architecture": "arm64",
   100              "os": "linux",
   101              "variant": "v8"
   102           }
   103        }
   104     ]
   105  }`,
   106  			fail: false,
   107  		},
   108  	} {
   109  		got := digest.FromString(tt.imageIndex)
   110  		if tt.digest != got {
   111  			t.Errorf("test %d: expected digest %s but got %s", i, tt.digest, got)
   112  		}
   113  
   114  		imageIndex := convertFormats(tt.imageIndex)
   115  		r := strings.NewReader(imageIndex)
   116  		err := schema.ValidatorMediaTypeImageIndex.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  }
   123  
   124  func TestBackwardsCompatibilityManifest(t *testing.T) {
   125  	for i, tt := range []struct {
   126  		manifest string
   127  		digest   digest.Digest
   128  		fail     bool
   129  	}{
   130  		// manifest pulled from docker hub using hash value
   131  		//
   132  		// curl -L -H "Authorization: Bearer ..." -H \
   133  		// "Accept: application/vnd.docker.distribution.manifest.v2+json" \
   134  		// https://registry-1.docker.io/v2/library/docker/manifests/sha256:888206c77cd2811ec47e752ba291e5b7734e3ef137dfd222daadaca39a9f17bc
   135  		{
   136  			digest: "sha256:888206c77cd2811ec47e752ba291e5b7734e3ef137dfd222daadaca39a9f17bc",
   137  			manifest: `{
   138     "schemaVersion": 2,
   139     "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
   140     "config": {
   141        "mediaType": "application/octet-stream",
   142        "size": 3210,
   143        "digest": "sha256:5359a4f250650c20227055957e353e8f8a74152f35fe36f00b6b1f9fc19c8861"
   144     },
   145     "layers": [
   146        {
   147           "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
   148           "size": 2310272,
   149           "digest": "sha256:fae91920dcd4542f97c9350b3157139a5d901362c2abec284de5ebd1b45b4957"
   150        },
   151        {
   152           "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
   153           "size": 913022,
   154           "digest": "sha256:f384f6ab36adad485192f09379c0b58dc612a3cde82c551e082a7c29a87c95da"
   155        },
   156        {
   157           "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
   158           "size": 9861668,
   159           "digest": "sha256:ed0d2dd5e1a0e5e650a330a864c8a122e9aa91fa6ba9ac6f0bd1882e59df55e7"
   160        },
   161        {
   162           "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
   163           "size": 465,
   164           "digest": "sha256:ec4d00b58417c45f7ddcfde7bcad8c9d62a7d6d5d17cdc1f7d79bcb2e22c1491"
   165        }
   166     ]
   167  }`,
   168  			fail: false,
   169  		},
   170  	} {
   171  		got := digest.FromString(tt.manifest)
   172  		if tt.digest != got {
   173  			t.Errorf("test %d: expected digest %s but got %s", i, tt.digest, got)
   174  		}
   175  
   176  		manifest := convertFormats(tt.manifest)
   177  		r := strings.NewReader(manifest)
   178  		err := schema.ValidatorMediaTypeManifest.Validate(r)
   179  
   180  		if got := err != nil; tt.fail != got {
   181  			t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err)
   182  		}
   183  	}
   184  }
   185  
   186  func TestBackwardsCompatibilityConfig(t *testing.T) {
   187  	for i, tt := range []struct {
   188  		config string
   189  		digest digest.Digest
   190  		fail   bool
   191  	}{
   192  		// config pulled from docker hub blob store
   193  		//
   194  		// $ TOKEN=$(curl https://auth.docker.io/token\?service\=registry.docker.io\&scope\=repository:library/docker:pull  | jq -r .token)
   195  		// $ CONFIG_DIGEST=$(curl -H "Authorization: Bearer ${TOKEN}" -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' https://index.docker.io/v2/library/docker/manifests/1.12.1 | jq -r .config.digest)
   196  		// $ curl -LH "Authorization: Bearer ${TOKEN}" https://index.docker.io/v2/library/docker/blobs/${CONFIG_DIGEST}
   197  		{
   198  			digest: "sha256:a059ea7356d5b5a9e0f6352bfa463e7bd4721c2ade3ef168603826e0de6fe54b",
   199  			config: `{"architecture":"amd64","config":{"Hostname":"09713501c176","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","DOCKER_BUCKET=get.docker.com","DOCKER_VERSION=1.12.1","DOCKER_SHA256=05ceec7fd937e1416e5dce12b0b6e1c655907d349d52574319a1e875077ccb79"],"Cmd":["sh"],"Image":"sha256:32e2e3ccf2a4fbaa75b078bf539cd5ea2e374a4242665a5ec3f3c01e7a3eefb8","Volumes":null,"WorkingDir":"","Entrypoint":["docker-entrypoint.sh"],"OnBuild":[],"Labels":{}},"container":"15a30be053fb3069a7879b4ea537e84689d8e8e8ba94dc4dd499271506803ba1","container_config":{"Hostname":"09713501c176","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","DOCKER_BUCKET=get.docker.com","DOCKER_VERSION=1.12.1","DOCKER_SHA256=05ceec7fd937e1416e5dce12b0b6e1c655907d349d52574319a1e875077ccb79"],"Cmd":["/bin/sh","-c","#(nop) ","CMD [\"sh\"]"],"Image":"sha256:32e2e3ccf2a4fbaa75b078bf539cd5ea2e374a4242665a5ec3f3c01e7a3eefb8","Volumes":null,"WorkingDir":"","Entrypoint":["docker-entrypoint.sh"],"OnBuild":[],"Labels":{}},"created":"2016-10-10T23:04:00.821781828Z","docker_version":"1.12.1","history":[{"created":"2016-09-23T16:29:57.276868291Z","created_by":"/bin/sh -c #(nop) ADD file:d6ee3ba7a4d59b161917082cc7242c660c61bb3f3cc1549c7e2dfff2b0de7104 in / "},{"created":"2016-09-23T16:36:54.024611637Z","created_by":"/bin/sh -c apk add --no-cache \t\tca-certificates \t\tcurl \t\topenssl"},{"created":"2016-09-23T16:36:54.365914519Z","created_by":"/bin/sh -c #(nop)  ENV DOCKER_BUCKET=get.docker.com","empty_layer":true},{"created":"2016-09-23T16:36:54.662005049Z","created_by":"/bin/sh -c #(nop)  ENV DOCKER_VERSION=1.12.1","empty_layer":true},{"created":"2016-09-23T16:36:54.946033025Z","created_by":"/bin/sh -c #(nop)  ENV DOCKER_SHA256=05ceec7fd937e1416e5dce12b0b6e1c655907d349d52574319a1e875077ccb79","empty_layer":true},{"created":"2016-09-23T16:36:58.535084011Z","created_by":"/bin/sh -c set -x \t\u0026\u0026 curl -fSL \"https://${DOCKER_BUCKET}/builds/Linux/x86_64/docker-${DOCKER_VERSION}.tgz\" -o docker.tgz \t\u0026\u0026 echo \"${DOCKER_SHA256} *docker.tgz\" | sha256sum -c - \t\u0026\u0026 tar -xzvf docker.tgz \t\u0026\u0026 mv docker/* /usr/local/bin/ \t\u0026\u0026 rmdir docker \t\u0026\u0026 rm docker.tgz \t\u0026\u0026 docker -v"},{"created":"2016-10-10T23:04:00.334158993Z","created_by":"/bin/sh -c #(nop) COPY file:399605dc1850a60a586b5494ab538bad495fd6f94eabca0c5f8a26468ce6030f in /usr/local/bin/ "},{"created":"2016-10-10T23:04:00.577900192Z","created_by":"/bin/sh -c #(nop)  ENTRYPOINT [\"docker-entrypoint.sh\"]","empty_layer":true},{"created":"2016-10-10T23:04:00.821781828Z","created_by":"/bin/sh -c #(nop)  CMD [\"sh\"]","empty_layer":true}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:9007f5987db353ec398a223bc5a135c5a9601798ba20a1abba537ea2f8ac765f","sha256:1b06990ff0df8dad281fad7e6e4c5e91f32f8f8c095d6c74cf1e90a6f4407e28","sha256:9d12251ce74aac7619a83641ab72431a8d82e58bcd8a262c2bb0cdb280f1f3b5","sha256:17a7f292c2427adfc75c3a789bab8efec925dc38c5437bf83d2f528013ab80e2"]}}`,
   200  			fail:   false,
   201  		},
   202  		{
   203  			// fedora:23 from docker hub
   204  			// both Entrypoint and Cmd can be nullable
   205  			digest: "sha256:a20665eb1fe2912accb3d5dadaed360430df0d1aa46874875886947d61d3d4ee",
   206  			config: `{"architecture":"amd64","author":"Patrick Uiterwijk \u003cpatrick@puiterwijk.org\u003e","config":{"Hostname":"8dfe43d80430","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":null,"Image":"sha256:6986ae504bbf843512d680cc959484452034965db15f75ee8bdd1b107f61500b","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"container":"6249cd2c4b1d6b1bf05903364cbcb95781508994d6407c1564d494e748ea1b41","container_config":{"Hostname":"8dfe43d80430","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/bin/sh","-c","#(nop) ADD file:293a6e463aa402bb8f80eb5cfc937f375cedc6843abaeb9eccfe3923bb3fc80b in /"],"Image":"sha256:6986ae504bbf843512d680cc959484452034965db15f75ee8bdd1b107f61500b","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"created":"2016-06-10T18:44:31.784795904Z","docker_version":"1.10.3","history":[{"created":"2016-06-10T18:44:03.360264073Z","author":"Patrick Uiterwijk \u003cpatrick@puiterwijk.org\u003e","created_by":"/bin/sh -c #(nop) MAINTAINER Patrick Uiterwijk \u003cpatrick@puiterwijk.org\u003e","empty_layer":true},{"created":"2016-06-10T18:44:31.784795904Z","author":"Patrick Uiterwijk \u003cpatrick@puiterwijk.org\u003e","created_by":"/bin/sh -c #(nop) ADD file:293a6e463aa402bb8f80eb5cfc937f375cedc6843abaeb9eccfe3923bb3fc80b in /"}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:d43f38155a799dc53d8fbb9f3bc11f51805f4027cd5c3d10b9823201cd5b9400"]}}`,
   207  			fail:   false,
   208  		},
   209  	} {
   210  		got := digest.FromString(tt.config)
   211  		if tt.digest != got {
   212  			t.Errorf("test %d: expected digest %s but got %s", i, tt.digest, got)
   213  		}
   214  
   215  		config := convertFormats(tt.config)
   216  		r := strings.NewReader(config)
   217  		err := schema.ValidatorMediaTypeImageConfig.Validate(r)
   218  
   219  		if got := err != nil; tt.fail != got {
   220  			t.Errorf("test %d: expected validation failure %t but got %t, err %v", i, tt.fail, got, err)
   221  		}
   222  	}
   223  }
   224  

View as plain text