github.com/MaximeAubanel/moby@v1.13.1/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  	"runtime"
    14  	"testing"
    15  
    16  	"github.com/docker/distribution/digest"
    17  	"github.com/docker/docker/distribution/metadata"
    18  	"github.com/docker/docker/image"
    19  	"github.com/docker/docker/layer"
    20  	"github.com/docker/docker/reference"
    21  )
    22  
    23  func TestMigrateRefs(t *testing.T) {
    24  	tmpdir, err := ioutil.TempDir("", "migrate-tags")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	defer os.RemoveAll(tmpdir)
    29  
    30  	ioutil.WriteFile(filepath.Join(tmpdir, "repositories-generic"), []byte(`{"Repositories":{"busybox":{"latest":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108","sha256:16a2a52884c2a9481ed267c2d46483eac7693b813a63132368ab098a71303f8a":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108"},"registry":{"2":"5d165b8e4b203685301c815e95663231691d383fd5e3d3185d1ce3f8dddead3d","latest":"8d5547a9f329b1d3f93198cd661fb5117e5a96b721c5cf9a2c389e7dd4877128"}}}`), 0600)
    31  
    32  	ta := &mockTagAdder{}
    33  	err = migrateRefs(tmpdir, "generic", ta, map[string]image.ID{
    34  		"5d165b8e4b203685301c815e95663231691d383fd5e3d3185d1ce3f8dddead3d": image.ID("sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"),
    35  		"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"),
    36  		"abcdef3434c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:56434342345ae68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"),
    37  	})
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	expected := map[string]string{
    43  		"busybox:latest": "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9",
    44  		"busybox@sha256:16a2a52884c2a9481ed267c2d46483eac7693b813a63132368ab098a71303f8a": "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9",
    45  		"registry:2": "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae",
    46  	}
    47  
    48  	if !reflect.DeepEqual(expected, ta.refs) {
    49  		t.Fatalf("Invalid migrated tags: expected %q, got %q", expected, ta.refs)
    50  	}
    51  
    52  	// second migration is no-op
    53  	ioutil.WriteFile(filepath.Join(tmpdir, "repositories-generic"), []byte(`{"Repositories":{"busybox":{"latest":"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108"`), 0600)
    54  	err = migrateRefs(tmpdir, "generic", ta, map[string]image.ID{
    55  		"b3ca410aa2c115c05969a7b2c8cf8a9fcf62c1340ed6a601c9ee50df337ec108": image.ID("sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"),
    56  	})
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if !reflect.DeepEqual(expected, ta.refs) {
    61  		t.Fatalf("Invalid migrated tags: expected %q, got %q", expected, ta.refs)
    62  	}
    63  }
    64  
    65  func TestMigrateContainers(t *testing.T) {
    66  	// TODO Windows: Figure out why this is failing
    67  	if runtime.GOOS == "windows" {
    68  		t.Skip("Failing on Windows")
    69  	}
    70  	if runtime.GOARCH != "amd64" {
    71  		t.Skip("Test tailored to amd64 architecture")
    72  	}
    73  	tmpdir, err := ioutil.TempDir("", "migrate-containers")
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	defer os.RemoveAll(tmpdir)
    78  
    79  	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":""}`)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	// container with invalid image
    85  	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":""}`)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	ls := &mockMounter{}
    91  
    92  	ifs, err := image.NewFSStoreBackend(filepath.Join(tmpdir, "imagedb"))
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	is, err := image.NewImageStore(ifs, ls)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	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"]}}`))
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	err = migrateContainers(tmpdir, ls, is, map[string]image.ID{
   108  		"2c5ac3f849df8627fcf2822727f87c57f38b7129d3604fbc11d861fe856ff093": imgID,
   109  	})
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	expected := []mountInfo{{
   115  		"f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c",
   116  		"f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c",
   117  		"sha256:c3191d32a37d7159b2e30830937d2e30268ad6c375a773a8994911a3aba9b93f",
   118  	}}
   119  	if !reflect.DeepEqual(expected, ls.mounts) {
   120  		t.Fatalf("invalid mounts: expected %q, got %q", expected, ls.mounts)
   121  	}
   122  
   123  	if actual, expected := ls.count, 0; actual != expected {
   124  		t.Fatalf("invalid active mounts: expected %d, got %d", expected, actual)
   125  	}
   126  
   127  	config2, err := ioutil.ReadFile(filepath.Join(tmpdir, "containers", "f780ee3f80e66e9b432a57049597118a66aab8932be88e5628d4c824edbee37c", "config.v2.json"))
   128  	if err != nil {
   129  		t.Fatal(err)
   130  	}
   131  	var config struct{ Image string }
   132  	err = json.Unmarshal(config2, &config)
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  
   137  	if actual, expected := config.Image, string(imgID); actual != expected {
   138  		t.Fatalf("invalid image pointer in migrated config: expected %q, got %q", expected, actual)
   139  	}
   140  
   141  }
   142  
   143  func TestMigrateImages(t *testing.T) {
   144  	// TODO Windows: Figure out why this is failing
   145  	if runtime.GOOS == "windows" {
   146  		t.Skip("Failing on Windows")
   147  	}
   148  	if runtime.GOARCH != "amd64" {
   149  		t.Skip("Test tailored to amd64 architecture")
   150  	}
   151  	tmpdir, err := ioutil.TempDir("", "migrate-images")
   152  	if err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	defer os.RemoveAll(tmpdir)
   156  
   157  	// busybox from 1.9
   158  	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"}`, "", "")
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  
   163  	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, "")
   164  	if err != nil {
   165  		t.Fatal(err)
   166  	}
   167  
   168  	ls := &mockRegistrar{}
   169  
   170  	ifs, err := image.NewFSStoreBackend(filepath.Join(tmpdir, "imagedb"))
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  
   175  	is, err := image.NewImageStore(ifs, ls)
   176  	if err != nil {
   177  		t.Fatal(err)
   178  	}
   179  
   180  	ms, err := metadata.NewFSMetadataStore(filepath.Join(tmpdir, "distribution"))
   181  	if err != nil {
   182  		t.Fatal(err)
   183  	}
   184  	mappings := make(map[string]image.ID)
   185  
   186  	err = migrateImages(tmpdir, ls, is, ms, mappings)
   187  	if err != nil {
   188  		t.Fatal(err)
   189  	}
   190  
   191  	expected := map[string]image.ID{
   192  		id1: image.ID("sha256:ca406eaf9c26898414ff5b7b3a023c33310759d6203be0663dbf1b3a712f432d"),
   193  		id2: image.ID("sha256:a488bec94bb96b26a968f913d25ef7d8d204d727ca328b52b4b059c7d03260b6"),
   194  	}
   195  
   196  	if !reflect.DeepEqual(mappings, expected) {
   197  		t.Fatalf("invalid image mappings: expected %q, got %q", expected, mappings)
   198  	}
   199  
   200  	if actual, expected := ls.count, 2; actual != expected {
   201  		t.Fatalf("invalid register count: expected %q, got %q", expected, actual)
   202  	}
   203  	ls.count = 0
   204  
   205  	// next images are busybox from 1.8.2
   206  	_, 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")
   207  	if err != nil {
   208  		t.Fatal(err)
   209  	}
   210  
   211  	_, 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")
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  
   216  	err = migrateImages(tmpdir, ls, is, ms, mappings)
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  
   221  	expected["d1592a710ac323612bd786fa8ac20727c58d8a67847e5a65177c594f43919498"] = image.ID("sha256:c091bb33854e57e6902b74c08719856d30b5593c7db6143b2b48376b8a588395")
   222  	expected["17583c7dd0dae6244203b8029733bdb7d17fccbb2b5d93e2b24cf48b8bfd06e2"] = image.ID("sha256:d963020e755ff2715b936065949472c1f8a6300144b922992a1a421999e71f07")
   223  
   224  	if actual, expected := ls.count, 2; actual != expected {
   225  		t.Fatalf("invalid register count: expected %q, got %q", expected, actual)
   226  	}
   227  
   228  	v2MetadataService := metadata.NewV2MetadataService(ms)
   229  	receivedMetadata, err := v2MetadataService.GetMetadata(layer.EmptyLayer.DiffID())
   230  	if err != nil {
   231  		t.Fatal(err)
   232  	}
   233  
   234  	expectedMetadata := []metadata.V2Metadata{
   235  		{Digest: digest.Digest("sha256:55dc925c23d1ed82551fd018c27ac3ee731377b6bad3963a2a4e76e753d70e57")},
   236  		{Digest: digest.Digest("sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4")},
   237  	}
   238  
   239  	if !reflect.DeepEqual(expectedMetadata, receivedMetadata) {
   240  		t.Fatalf("invalid metadata: expected %q, got %q", expectedMetadata, receivedMetadata)
   241  	}
   242  
   243  }
   244  
   245  func TestMigrateUnsupported(t *testing.T) {
   246  	tmpdir, err := ioutil.TempDir("", "migrate-empty")
   247  	if err != nil {
   248  		t.Fatal(err)
   249  	}
   250  	defer os.RemoveAll(tmpdir)
   251  
   252  	err = os.MkdirAll(filepath.Join(tmpdir, "graph"), 0700)
   253  	if err != nil {
   254  		t.Fatal(err)
   255  	}
   256  
   257  	err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
   258  	if err != errUnsupported {
   259  		t.Fatalf("expected unsupported error, got %q", err)
   260  	}
   261  }
   262  
   263  func TestMigrateEmptyDir(t *testing.T) {
   264  	tmpdir, err := ioutil.TempDir("", "migrate-empty")
   265  	if err != nil {
   266  		t.Fatal(err)
   267  	}
   268  	defer os.RemoveAll(tmpdir)
   269  
   270  	err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
   271  	if err != nil {
   272  		t.Fatal(err)
   273  	}
   274  }
   275  
   276  func addImage(dest, jsonConfig, parent, checksum string) (string, error) {
   277  	var config struct{ ID string }
   278  	if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
   279  		return "", err
   280  	}
   281  	if config.ID == "" {
   282  		b := make([]byte, 32)
   283  		rand.Read(b)
   284  		config.ID = hex.EncodeToString(b)
   285  	}
   286  	contDir := filepath.Join(dest, "graph", config.ID)
   287  	if err := os.MkdirAll(contDir, 0700); err != nil {
   288  		return "", err
   289  	}
   290  	if err := ioutil.WriteFile(filepath.Join(contDir, "json"), []byte(jsonConfig), 0600); err != nil {
   291  		return "", err
   292  	}
   293  	if checksum != "" {
   294  		if err := ioutil.WriteFile(filepath.Join(contDir, "checksum"), []byte(checksum), 0600); err != nil {
   295  			return "", err
   296  		}
   297  	}
   298  	if err := ioutil.WriteFile(filepath.Join(contDir, ".migration-diffid"), []byte(layer.EmptyLayer.DiffID()), 0600); err != nil {
   299  		return "", err
   300  	}
   301  	if err := ioutil.WriteFile(filepath.Join(contDir, ".migration-size"), []byte("0"), 0600); err != nil {
   302  		return "", err
   303  	}
   304  	if parent != "" {
   305  		if err := ioutil.WriteFile(filepath.Join(contDir, "parent"), []byte(parent), 0600); err != nil {
   306  			return "", err
   307  		}
   308  	}
   309  	if checksum != "" {
   310  		if err := ioutil.WriteFile(filepath.Join(contDir, "checksum"), []byte(checksum), 0600); err != nil {
   311  			return "", err
   312  		}
   313  	}
   314  	return config.ID, nil
   315  }
   316  
   317  func addContainer(dest, jsonConfig string) error {
   318  	var config struct{ ID string }
   319  	if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
   320  		return err
   321  	}
   322  	contDir := filepath.Join(dest, "containers", config.ID)
   323  	if err := os.MkdirAll(contDir, 0700); err != nil {
   324  		return err
   325  	}
   326  	if err := ioutil.WriteFile(filepath.Join(contDir, "config.json"), []byte(jsonConfig), 0600); err != nil {
   327  		return err
   328  	}
   329  	return nil
   330  }
   331  
   332  type mockTagAdder struct {
   333  	refs map[string]string
   334  }
   335  
   336  func (t *mockTagAdder) AddTag(ref reference.Named, id digest.Digest, force bool) error {
   337  	if t.refs == nil {
   338  		t.refs = make(map[string]string)
   339  	}
   340  	t.refs[ref.String()] = id.String()
   341  	return nil
   342  }
   343  func (t *mockTagAdder) AddDigest(ref reference.Canonical, id digest.Digest, force bool) error {
   344  	return t.AddTag(ref, id, force)
   345  }
   346  
   347  type mockRegistrar struct {
   348  	layers map[layer.ChainID]*mockLayer
   349  	count  int
   350  }
   351  
   352  func (r *mockRegistrar) RegisterByGraphID(graphID string, parent layer.ChainID, diffID layer.DiffID, tarDataFile string, size int64) (layer.Layer, error) {
   353  	r.count++
   354  	l := &mockLayer{}
   355  	if parent != "" {
   356  		p, exists := r.layers[parent]
   357  		if !exists {
   358  			return nil, fmt.Errorf("invalid parent %q", parent)
   359  		}
   360  		l.parent = p
   361  		l.diffIDs = append(l.diffIDs, p.diffIDs...)
   362  	}
   363  	l.diffIDs = append(l.diffIDs, diffID)
   364  	if r.layers == nil {
   365  		r.layers = make(map[layer.ChainID]*mockLayer)
   366  	}
   367  	r.layers[l.ChainID()] = l
   368  	return l, nil
   369  }
   370  func (r *mockRegistrar) Release(l layer.Layer) ([]layer.Metadata, error) {
   371  	return nil, nil
   372  }
   373  func (r *mockRegistrar) Get(layer.ChainID) (layer.Layer, error) {
   374  	return nil, nil
   375  }
   376  
   377  type mountInfo struct {
   378  	name, graphID, parent string
   379  }
   380  type mockMounter struct {
   381  	mounts []mountInfo
   382  	count  int
   383  }
   384  
   385  func (r *mockMounter) CreateRWLayerByGraphID(name string, graphID string, parent layer.ChainID) error {
   386  	r.mounts = append(r.mounts, mountInfo{name, graphID, string(parent)})
   387  	return nil
   388  }
   389  func (r *mockMounter) Unmount(string) error {
   390  	r.count--
   391  	return nil
   392  }
   393  func (r *mockMounter) Get(layer.ChainID) (layer.Layer, error) {
   394  	return nil, nil
   395  }
   396  
   397  func (r *mockMounter) Release(layer.Layer) ([]layer.Metadata, error) {
   398  	return nil, nil
   399  }
   400  
   401  type mockLayer struct {
   402  	diffIDs []layer.DiffID
   403  	parent  *mockLayer
   404  }
   405  
   406  func (l *mockLayer) TarStream() (io.ReadCloser, error) {
   407  	return nil, nil
   408  }
   409  func (l *mockLayer) TarStreamFrom(layer.ChainID) (io.ReadCloser, error) {
   410  	return nil, nil
   411  }
   412  
   413  func (l *mockLayer) ChainID() layer.ChainID {
   414  	return layer.CreateChainID(l.diffIDs)
   415  }
   416  
   417  func (l *mockLayer) DiffID() layer.DiffID {
   418  	return l.diffIDs[len(l.diffIDs)-1]
   419  }
   420  
   421  func (l *mockLayer) Parent() layer.Layer {
   422  	if l.parent == nil {
   423  		return nil
   424  	}
   425  	return l.parent
   426  }
   427  
   428  func (l *mockLayer) Size() (int64, error) {
   429  	return 0, nil
   430  }
   431  
   432  func (l *mockLayer) DiffSize() (int64, error) {
   433  	return 0, nil
   434  }
   435  
   436  func (l *mockLayer) Metadata() (map[string]string, error) {
   437  	return nil, nil
   438  }