github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/migrate/v1/migratev1_test.go (about)

     1  package v1
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"reflect"
    13  	"testing"
    14  
    15  	"github.com/docker/distribution/digest"
    16  	"github.com/docker/docker/distribution/metadata"
    17  	"github.com/docker/docker/image"
    18  	"github.com/docker/docker/layer"
    19  	"github.com/docker/docker/reference"
    20  )
    21  
    22  func TestMigrateRefs(t *testing.T) {
    23  	tmpdir, err := ioutil.TempDir("", "migrate-tags")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	defer os.RemoveAll(tmpdir)
    28  
    29  	ioutil.WriteFile(filepath.Join(tmpdir, "repositories-generic"), []byte(`{"Repositories":{"busybox":{"latest":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108","sha256:16a2a52884c2a9481ed267c2d46483eac7693b813a63132368ab098a71303f8a":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108"},"registry":{"2":"5d165b8e4b203685301c815e95663231691d383fd5e3d3185d1ce3f8dddead3d","latest":"8d5547a9f329b1d3f93198cd661fb5117e5a96b721c5cf9a2c389e7dd4877128"}}}`), 0600)
    30  
    31  	ta := &mockTagAdder{}
    32  	err = migrateRefs(tmpdir, "generic", ta, map[string]image.ID{
    33  		"5d165b8e4b203685301c815e95663231691d383fd5e3d3185d1ce3f8dddead3d": image.ID("sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"),
    34  		"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"),
    35  		"abcdef3434c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:56434342345ae68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"),
    36  	})
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	expected := map[string]string{
    42  		"busybox:latest": "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9",
    43  		"busybox@sha256:16a2a52884c2a9481ed267c2d46483eac7693b813a63132368ab098a71303f8a": "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9",
    44  		"registry:2": "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
    45  	}
    46  
    47  	if !reflect.DeepEqual(expected, ta.refs) {
    48  		t.Fatalf("Invalid migrated tags: expected %q, got %q", expected, ta.refs)
    49  	}
    50  
    51  	// second migration is no-op
    52  	ioutil.WriteFile(filepath.Join(tmpdir, "repositories-generic"), []byte(`{"Repositories":{"busybox":{"latest":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108"`), 0600)
    53  	err = migrateRefs(tmpdir, "generic", ta, map[string]image.ID{
    54  		"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"),
    55  	})
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if !reflect.DeepEqual(expected, ta.refs) {
    60  		t.Fatalf("Invalid migrated tags: expected %q, got %q", expected, ta.refs)
    61  	}
    62  }
    63  
    64  func TestMigrateContainers(t *testing.T) {
    65  	tmpdir, err := ioutil.TempDir("", "migrate-containers")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	defer os.RemoveAll(tmpdir)
    70  
    71  	err = addContainer(tmpdir, `{"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2015-11-10T21:42:40.604267436Z","FinishedAt":"2015-11-10T21:42:41.869265487Z"},"ID":"f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c","Created":"2015-11-10T21:42:40.433831551Z","Path":"sh","Args":[],"Config":{"Hostname":"f780ee3f80e6","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":null,"Cmd":["sh"],"Image":"busybox","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"2c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093","NetworkSettings":{"Bridge":"","EndpointID":"","Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"HairpinMode":false,"IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"MacAddress":"","NetworkID":"","PortMapping":null,"Ports":null,"SandboxKey":"","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null},"ResolvConfPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/resolv.conf","HostnamePath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hostname","HostsPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hosts","LogPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c-json.log","Name":"/determined_euclid","Driver":"overlay","ExecDriver":"native-0.2","MountLabel":"","ProcessLabel":"","RestartCount":0,"UpdateDns":false,"HasBeenStartedBefore":false,"MountPoints":{},"Volumes":{},"VolumesRW":{},"AppArmorProfile":""}`)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	// container with invalid image
    77  	err = addContainer(tmpdir, `{"State":{"Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2015-11-10T21:42:40.604267436Z","FinishedAt":"2015-11-10T21:42:41.869265487Z"},"ID":"e780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c","Created":"2015-11-10T21:42:40.433831551Z","Path":"sh","Args":[],"Config":{"Hostname":"f780ee3f80e6","Domainname":"","User":"","AttachStdin":true,"AttachStdout":true,"AttachStderr":true,"Tty":true,"OpenStdin":true,"StdinOnce":true,"Env":null,"Cmd":["sh"],"Image":"busybox","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"Image":"4c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093","NetworkSettings":{"Bridge":"","EndpointID":"","Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"HairpinMode":false,"IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"MacAddress":"","NetworkID":"","PortMapping":null,"Ports":null,"SandboxKey":"","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null},"ResolvConfPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/resolv.conf","HostnamePath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hostname","HostsPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/hosts","LogPath":"/var/lib/docker/containers/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c/f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c-json.log","Name":"/determined_euclid","Driver":"overlay","ExecDriver":"native-0.2","MountLabel":"","ProcessLabel":"","RestartCount":0,"UpdateDns":false,"HasBeenStartedBefore":false,"MountPoints":{},"Volumes":{},"VolumesRW":{},"AppArmorProfile":""}`)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	ls := &mockMounter{}
    83  
    84  	ifs, err := image.NewFSStoreBackend(filepath.Join(tmpdir, "imagedb"))
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	is, err := image.NewImageStore(ifs, ls)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	imgID, err := is.Create([]byte(`{"architecture":"amd64","config":{"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Cmd":["sh"],"Entrypoint":null,"Env":null,"Hostname":"23304fc829f9","Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Labels":null,"OnBuild":null,"OpenStdin":false,"StdinOnce":false,"Tty":false,"Volumes":null,"WorkingDir":"","Domainname":"","User":""},"container":"349b014153779e30093d94f6df2a43c7a0a164e05aa207389917b540add39b51","container_config":{"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Cmd":["/bin/sh","-c","#(nop) CMD [\"sh\"]"],"Entrypoint":null,"Env":null,"Hostname":"23304fc829f9","Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Labels":null,"OnBuild":null,"OpenStdin":false,"StdinOnce":false,"Tty":false,"Volumes":null,"WorkingDir":"","Domainname":"","User":""},"created":"2015-10-31T22:22:55.613815829Z","docker_version":"1.8.2","history":[{"created":"2015-10-31T22:22:54.690851953Z","created_by":"/bin/sh -c #(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"},{"created":"2015-10-31T22:22:55.613815829Z","created_by":"/bin/sh -c #(nop) CMD [\"sh\"]"}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:c6f988f4874bb0add23a778f753c65efe992244e148a1d2ec2a8b664fb66bbd1","sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef"]}}`))
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	err = migrateContainers(tmpdir, ls, is, map[string]image.ID{
   100  		"2c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093": imgID,
   101  	})
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	expected := []mountInfo{{
   107  		"f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c",
   108  		"f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c",
   109  		"sha256:c3191d32a37d7159b2e30830937d2e30268ad6c375a773a8994911a3aba9b93f",
   110  	}}
   111  	if !reflect.DeepEqual(expected, ls.mounts) {
   112  		t.Fatalf("invalid mounts: expected %q, got %q", expected, ls.mounts)
   113  	}
   114  
   115  	if actual, expected := ls.count, 0; actual != expected {
   116  		t.Fatalf("invalid active mounts: expected %d, got %d", expected, actual)
   117  	}
   118  
   119  	config2, err := ioutil.ReadFile(filepath.Join(tmpdir, "containers", "f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c", "config.v2.json"))
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	var config struct{ Image string }
   124  	err = json.Unmarshal(config2, &config)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	if actual, expected := config.Image, string(imgID); actual != expected {
   130  		t.Fatalf("invalid image pointer in migrated config: expected %q, got %q", expected, actual)
   131  	}
   132  
   133  }
   134  
   135  func TestMigrateImages(t *testing.T) {
   136  	tmpdir, err := ioutil.TempDir("", "migrate-images")
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	defer os.RemoveAll(tmpdir)
   141  
   142  	// busybox from 1.9
   143  	id1, err := addImage(tmpdir, `{"architecture":"amd64","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"23304fc829f9b9349416f6eb1afec162907eba3a328f51d53a17f8986f865d65","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"],"Image":"","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"created":"2015-10-31T22:22:54.690851953Z","docker_version":"1.8.2","layer_id":"sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57","os":"linux"}`, "", "")
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	id2, err := addImage(tmpdir, `{"architecture":"amd64","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["sh"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"349b014153779e30093d94f6df2a43c7a0a164e05aa207389917b540add39b51","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) CMD [\"sh\"]"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"created":"2015-10-31T22:22:55.613815829Z","docker_version":"1.8.2","layer_id":"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4","os":"linux","parent_id":"sha256:039b63dd2cbaa10d6015ea574392530571ed8d7b174090f032211285a71881d0"}`, id1, "")
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	ls := &mockRegistrar{}
   154  
   155  	ifs, err := image.NewFSStoreBackend(filepath.Join(tmpdir, "imagedb"))
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  
   160  	is, err := image.NewImageStore(ifs, ls)
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  
   165  	ms, err := metadata.NewFSMetadataStore(filepath.Join(tmpdir, "distribution"))
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	mappings := make(map[string]image.ID)
   170  
   171  	err = migrateImages(tmpdir, ls, is, ms, mappings)
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  
   176  	expected := map[string]image.ID{
   177  		id1: image.ID("sha256:ca406eaf9c26898414ff5b7b3a023c33310759d6203be0663dbf1b3a712f432d"),
   178  		id2: image.ID("sha256:a488bec94bb96b26a968f913d25ef7d8d204d727ca328b52b4b059c7d03260b6"),
   179  	}
   180  
   181  	if !reflect.DeepEqual(mappings, expected) {
   182  		t.Fatalf("invalid image mappings: expected %q, got %q", expected, mappings)
   183  	}
   184  
   185  	if actual, expected := ls.count, 2; actual != expected {
   186  		t.Fatalf("invalid register count: expected %q, got %q", expected, actual)
   187  	}
   188  	ls.count = 0
   189  
   190  	// next images are busybox from 1.8.2
   191  	_, err = addImage(tmpdir, `{"id":"17583c7dd0dae6244203b8029733bdb7d17fccbb2b5d93e2b24cf48b8bfd06e2","parent":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","created":"2015-10-31T22:22:55.613815829Z","container":"349b014153779e30093d94f6df2a43c7a0a164e05aa207389917b540add39b51","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) CMD [\"sh\"]"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"docker_version":"1.8.2","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["sh"],"Image":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"architecture":"amd64","os":"linux","Size":0}`, "", "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4")
   192  	if err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	_, err = addImage(tmpdir, `{"id":"d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498","created":"2015-10-31T22:22:54.690851953Z","container":"23304fc829f9b9349416f6eb1afec162907eba3a328f51d53a17f8986f865d65","container_config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":["/bin/sh","-c","#(nop) ADD file:a3bc1e842b69636f9df5256c49c5374fb4eef1e281fe3f282c65fb853ee171c5 in /"],"Image":"","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"docker_version":"1.8.2","config":{"Hostname":"23304fc829f9","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":null,"PublishService":"","Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":null,"Cmd":null,"Image":"","Volumes":null,"VolumeDriver":"","WorkingDir":"","Entrypoint":null,"NetworkDisabled":false,"MacAddress":"","OnBuild":null,"Labels":null},"architecture":"amd64","os":"linux","Size":1108935}`, "", "sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57")
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  
   201  	err = migrateImages(tmpdir, ls, is, ms, mappings)
   202  	if err != nil {
   203  		t.Fatal(err)
   204  	}
   205  
   206  	expected["d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498"] = image.ID("sha256:c091bb33854e57e6902b74c08719856d30b5593c7db6143b2b48376b8a588395")
   207  	expected["17583c7dd0dae6244203b8029733bdb7d17fccbb2b5d93e2b24cf48b8bfd06e2"] = image.ID("sha256:d963020e755ff2715b936065949472c1f8a6300144b922992a1a421999e71f07")
   208  
   209  	if actual, expected := ls.count, 2; actual != expected {
   210  		t.Fatalf("invalid register count: expected %q, got %q", expected, actual)
   211  	}
   212  
   213  	blobSumService := metadata.NewBlobSumService(ms)
   214  	blobsums, err := blobSumService.GetBlobSums(layer.EmptyLayer.DiffID())
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  
   219  	expectedBlobsums := []digest.Digest{
   220  		"sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57",
   221  		"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4",
   222  	}
   223  
   224  	if !reflect.DeepEqual(expectedBlobsums, blobsums) {
   225  		t.Fatalf("invalid blobsums: expected %q, got %q", expectedBlobsums, blobsums)
   226  	}
   227  
   228  }
   229  
   230  func TestMigrateUnsupported(t *testing.T) {
   231  	tmpdir, err := ioutil.TempDir("", "migrate-empty")
   232  	if err != nil {
   233  		t.Fatal(err)
   234  	}
   235  	defer os.RemoveAll(tmpdir)
   236  
   237  	err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
   238  	if err != errUnsupported {
   239  		t.Fatalf("expected unsupported error, got %q", err)
   240  	}
   241  }
   242  
   243  func addImage(dest, jsonConfig, parent, checksum string) (string, error) {
   244  	var config struct{ ID string }
   245  	if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
   246  		return "", err
   247  	}
   248  	if config.ID == "" {
   249  		b := make([]byte, 32)
   250  		rand.Read(b)
   251  		config.ID = hex.EncodeToString(b)
   252  	}
   253  	contDir := filepath.Join(dest, "graph", config.ID)
   254  	if err := os.MkdirAll(contDir, 0700); err != nil {
   255  		return "", err
   256  	}
   257  	if err := ioutil.WriteFile(filepath.Join(contDir, "json"), []byte(jsonConfig), 0600); err != nil {
   258  		return "", err
   259  	}
   260  	if parent != "" {
   261  		if err := ioutil.WriteFile(filepath.Join(contDir, "parent"), []byte(parent), 0600); err != nil {
   262  			return "", err
   263  		}
   264  	}
   265  	if checksum != "" {
   266  		if err := ioutil.WriteFile(filepath.Join(contDir, "checksum"), []byte(checksum), 0600); err != nil {
   267  			return "", err
   268  		}
   269  	}
   270  	return config.ID, nil
   271  }
   272  
   273  func addContainer(dest, jsonConfig string) error {
   274  	var config struct{ ID string }
   275  	if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
   276  		return err
   277  	}
   278  	contDir := filepath.Join(dest, "containers", config.ID)
   279  	if err := os.MkdirAll(contDir, 0700); err != nil {
   280  		return err
   281  	}
   282  	if err := ioutil.WriteFile(filepath.Join(contDir, "config.json"), []byte(jsonConfig), 0600); err != nil {
   283  		return err
   284  	}
   285  	return nil
   286  }
   287  
   288  type mockTagAdder struct {
   289  	refs map[string]string
   290  }
   291  
   292  func (t *mockTagAdder) AddTag(ref reference.Named, id image.ID, force bool) error {
   293  	if t.refs == nil {
   294  		t.refs = make(map[string]string)
   295  	}
   296  	t.refs[ref.String()] = id.String()
   297  	return nil
   298  }
   299  func (t *mockTagAdder) AddDigest(ref reference.Canonical, id image.ID, force bool) error {
   300  	return t.AddTag(ref, id, force)
   301  }
   302  
   303  type mockRegistrar struct {
   304  	layers map[layer.ChainID]*mockLayer
   305  	count  int
   306  }
   307  
   308  func (r *mockRegistrar) RegisterByGraphID(graphID string, parent layer.ChainID, tarDataFile string) (layer.Layer, error) {
   309  	r.count++
   310  	l := &mockLayer{}
   311  	if parent != "" {
   312  		p, exists := r.layers[parent]
   313  		if !exists {
   314  			return nil, fmt.Errorf("invalid parent %q", parent)
   315  		}
   316  		l.parent = p
   317  		l.diffIDs = append(l.diffIDs, p.diffIDs...)
   318  	}
   319  	l.diffIDs = append(l.diffIDs, layer.EmptyLayer.DiffID())
   320  	if r.layers == nil {
   321  		r.layers = make(map[layer.ChainID]*mockLayer)
   322  	}
   323  	r.layers[l.ChainID()] = l
   324  	return l, nil
   325  }
   326  func (r *mockRegistrar) Release(l layer.Layer) ([]layer.Metadata, error) {
   327  	return nil, nil
   328  }
   329  func (r *mockRegistrar) Get(layer.ChainID) (layer.Layer, error) {
   330  	return nil, nil
   331  }
   332  
   333  type mountInfo struct {
   334  	name, graphID, parent string
   335  }
   336  type mockMounter struct {
   337  	mounts []mountInfo
   338  	count  int
   339  }
   340  
   341  func (r *mockMounter) MountByGraphID(name string, graphID string, parent layer.ChainID) (layer.RWLayer, error) {
   342  	r.mounts = append(r.mounts, mountInfo{name, graphID, string(parent)})
   343  	r.count++
   344  	return nil, nil
   345  }
   346  func (r *mockMounter) Unmount(string) error {
   347  	r.count--
   348  	return nil
   349  }
   350  func (r *mockMounter) Get(layer.ChainID) (layer.Layer, error) {
   351  	return nil, nil
   352  }
   353  
   354  func (r *mockMounter) Release(layer.Layer) ([]layer.Metadata, error) {
   355  	return nil, nil
   356  }
   357  
   358  type mockLayer struct {
   359  	diffIDs []layer.DiffID
   360  	parent  *mockLayer
   361  }
   362  
   363  func (l *mockLayer) TarStream() (io.ReadCloser, error) {
   364  	return nil, nil
   365  }
   366  
   367  func (l *mockLayer) ChainID() layer.ChainID {
   368  	return layer.CreateChainID(l.diffIDs)
   369  }
   370  
   371  func (l *mockLayer) DiffID() layer.DiffID {
   372  	return l.diffIDs[len(l.diffIDs)-1]
   373  }
   374  
   375  func (l *mockLayer) Parent() layer.Layer {
   376  	if l.parent == nil {
   377  		return nil
   378  	}
   379  	return l.parent
   380  }
   381  
   382  func (l *mockLayer) Size() (int64, error) {
   383  	return 0, nil
   384  }
   385  
   386  func (l *mockLayer) DiffSize() (int64, error) {
   387  	return 0, nil
   388  }
   389  
   390  func (l *mockLayer) Metadata() (map[string]string, error) {
   391  	return nil, nil
   392  }