From cc153aa71f309c6682e38757ac2183480c1ce295 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Thu, 8 Oct 2015 17:15:27 -0700 Subject: [PATCH] Added a test to verify byte size for larger files downloaded from docker --- builder/docker/communicator_test.go | 118 +++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 11 deletions(-) diff --git a/builder/docker/communicator_test.go b/builder/docker/communicator_test.go index 48bc5035f..8448a6054 100644 --- a/builder/docker/communicator_test.go +++ b/builder/docker/communicator_test.go @@ -10,6 +10,7 @@ import ( "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/provisioner/file" + "github.com/mitchellh/packer/provisioner/shell" "github.com/mitchellh/packer/template" ) @@ -103,12 +104,107 @@ func TestUploadDownload(t *testing.T) { // need to use github.com/cbednarski/rerun to verify since this problem occurs // only intermittently. func TestLargeDownload(t *testing.T) { - // cupcake is 2097152 bytes - // weddingcake is 104857600 bytes - // we will want to verify the size of the file after we download it + ui := packer.TestUi(t) + cache := &packer.FileCache{CacheDir: os.TempDir()} + + tpl, err := template.Parse(strings.NewReader(dockerLargeBuilderConfig)) + if err != nil { + t.Fatalf("Unable to parse config: %s", err) + } + + if os.Getenv("PACKER_ACC") == "" { + t.Skip("This test is only run with PACKER_ACC=1") + } + cmd := exec.Command("docker", "-v") + cmd.Run() + if !cmd.ProcessState.Success() { + t.Error("docker command not found; please make sure docker is installed") + } + + // Setup the builder + builder := &Builder{} + warnings, err := builder.Prepare(tpl.Builders["docker"].Config) + if err != nil { + t.Fatalf("Error preparing configuration %s", err) + } + if len(warnings) > 0 { + t.Fatal("Encountered configuration warnings; aborting") + } + + // Setup the provisioners + shell := &shell.Provisioner{} + err = shell.Prepare(tpl.Provisioners[0].Config) + if err != nil { + t.Fatalf("Error preparing shell provisioner: %s", err) + } + downloadCupcake := &file.Provisioner{} + err = downloadCupcake.Prepare(tpl.Provisioners[1].Config) + if err != nil { + t.Fatalf("Error preparing downloadCupcake: %s", err) + } + downloadBigcake := &file.Provisioner{} + err = downloadBigcake.Prepare(tpl.Provisioners[2].Config) + if err != nil { + t.Fatalf("Error preparing downloadBigcake: %s", err) + } + + // Preemptive cleanup. + defer os.Remove("cupcake") + defer os.Remove("bigcake") + + // Add hooks so the provisioners run during the build + hooks := map[string][]packer.Hook{} + hooks[packer.HookProvision] = []packer.Hook{ + &packer.ProvisionHook{ + Provisioners: []packer.Provisioner{ + shell, + downloadCupcake, + downloadBigcake, + }, + }, + } + hook := &packer.DispatchHook{Mapping: hooks} + + // Run things + artifact, err := builder.Run(ui, hook, cache) + if err != nil { + t.Fatalf("Error running build %s", err) + } + // Preemptive cleanup + defer artifact.Destroy() + + // Verify that the things we downloaded are the right size. Complain loudly + // if they are not. + // + // cupcake should be 2097152 bytes + // bigcake should be 104857600 bytes + cupcake, err := os.Stat("cupcake") + if err != nil { + t.Fatalf("Unable to stat cupcake file") + } + cupcakeExpected := int64(2097152) + if cupcake.Size() != cupcakeExpected { + t.Errorf("Expected cupcake to be %s bytes; found %s", cupcakeExpected, cupcake.Size()) + } + + bigcake, err := os.Stat("bigcake") + if err != nil { + t.Fatalf("Unable to stat bigcake file") + } + bigcakeExpected := int64(104857600) + if bigcake.Size() != bigcakeExpected { + t.Errorf("Expected bigcake to be %s bytes; found %s", bigcakeExpected, bigcake.Size()) + } + + // TODO if we can, calculate a sha inside the container and compare to the + // one we get after we pull it down. We will probably have to parse the log + // or ui output to do this because we use /dev/urandom to create the file. + + // if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) { + // t.Fatalf("Input and output files do not match\n"+ + // "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile) + // } - cupcake - weddingcake } const dockerBuilderConfig = ` @@ -116,7 +212,7 @@ const dockerBuilderConfig = ` "builders": [ { "type": "docker", - "image": "alpine", + "image": "ubuntu", "discard": true, "run_command": ["-d", "-i", "-t", "{{.Image}}", "/bin/sh"] } @@ -142,7 +238,7 @@ const dockerLargeBuilderConfig = ` "builders": [ { "type": "docker", - "image": "alpine", + "image": "ubuntu", "discard": true } ], @@ -151,9 +247,9 @@ const dockerLargeBuilderConfig = ` "type": "shell", "inline": [ "dd if=/dev/urandom of=/tmp/cupcake bs=1M count=2", - "dd if=/dev/urandom of=/tmp/weddingcake bs=1M count=100", + "dd if=/dev/urandom of=/tmp/bigcake bs=1M count=100", "sync", - "md5sum /tmp/cupcake /tmp/weddingcake" + "md5sum /tmp/cupcake /tmp/bigcake" ] }, { @@ -164,8 +260,8 @@ const dockerLargeBuilderConfig = ` }, { "type": "file", - "source": "/tmp/weddingcake", - "destination": "weddingcake", + "source": "/tmp/bigcake", + "destination": "bigcake", "direction": "download" } ]