2013-11-09 02:43:41 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2015-08-12 14:36:56 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2013-11-09 02:43:41 -05:00
|
|
|
"testing"
|
2015-08-12 14:36:56 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/provisioner/file"
|
|
|
|
"github.com/hashicorp/packer/provisioner/shell"
|
2013-11-09 02:43:41 -05:00
|
|
|
)
|
|
|
|
|
2015-10-08 19:37:14 -04:00
|
|
|
// TestUploadDownload verifies that basic upload / download functionality works
|
2015-08-12 14:36:56 -04:00
|
|
|
func TestUploadDownload(t *testing.T) {
|
|
|
|
ui := packer.TestUi(t)
|
|
|
|
|
|
|
|
tpl, err := template.Parse(strings.NewReader(dockerBuilderConfig))
|
|
|
|
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{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := builder.Prepare(tpl.Builders["docker"].Config)
|
2015-08-12 14:36:56 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing configuration %s", err)
|
|
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatal("Encountered configuration warnings; aborting")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the provisioners
|
|
|
|
upload := &file.Provisioner{}
|
|
|
|
err = upload.Prepare(tpl.Provisioners[0].Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing upload: %s", err)
|
|
|
|
}
|
|
|
|
download := &file.Provisioner{}
|
|
|
|
err = download.Prepare(tpl.Provisioners[1].Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing download: %s", err)
|
|
|
|
}
|
2015-08-12 15:16:26 -04:00
|
|
|
// Preemptive cleanup. Honestly I don't know why you would want to get rid
|
|
|
|
// of my strawberry cake. It's so tasty! Do you not like cake? Are you a
|
|
|
|
// cake-hater? Or are you keeping all the cake all for yourself? So selfish!
|
|
|
|
defer os.Remove("my-strawberry-cake")
|
2015-08-12 14:36:56 -04:00
|
|
|
|
|
|
|
// Add hooks so the provisioners run during the build
|
|
|
|
hooks := map[string][]packer.Hook{}
|
|
|
|
hooks[packer.HookProvision] = []packer.Hook{
|
|
|
|
&packer.ProvisionHook{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: []*packer.HookedProvisioner{
|
2018-08-31 05:54:12 -04:00
|
|
|
{Provisioner: upload, Config: nil, TypeName: ""},
|
|
|
|
{Provisioner: download, Config: nil, TypeName: ""},
|
2015-08-12 14:36:56 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
hook := &packer.DispatchHook{Mapping: hooks}
|
|
|
|
|
|
|
|
// Run things
|
2019-03-22 09:53:28 -04:00
|
|
|
artifact, err := builder.Run(context.Background(), ui, hook)
|
2015-08-12 14:36:56 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error running build %s", err)
|
|
|
|
}
|
|
|
|
// Preemptive cleanup
|
|
|
|
defer artifact.Destroy()
|
|
|
|
|
|
|
|
// Verify that the thing we downloaded is the same thing we sent up.
|
2015-08-12 15:16:26 -04:00
|
|
|
// Complain loudly if it isn't.
|
|
|
|
inputFile, err := ioutil.ReadFile("test-fixtures/onecakes/strawberry")
|
2015-08-12 14:36:56 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read input file: %s", err)
|
|
|
|
}
|
2015-08-12 15:16:26 -04:00
|
|
|
outputFile, err := ioutil.ReadFile("my-strawberry-cake")
|
2015-08-12 14:36:56 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read output file: %s", err)
|
|
|
|
}
|
|
|
|
if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) {
|
2015-08-12 15:16:26 -04:00
|
|
|
t.Fatalf("Input and output files do not match\n"+
|
|
|
|
"Input:\n%s\nOutput:\n%s\n", inputFile, outputFile)
|
2015-08-12 14:36:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 20:45:01 -04:00
|
|
|
// TestLargeDownload verifies that files are the appropriate size after being
|
2015-10-08 19:37:14 -04:00
|
|
|
// downloaded. This is to identify and fix the race condition in #2793. You may
|
|
|
|
// need to use github.com/cbednarski/rerun to verify since this problem occurs
|
|
|
|
// only intermittently.
|
|
|
|
func TestLargeDownload(t *testing.T) {
|
2015-10-08 20:15:27 -04:00
|
|
|
ui := packer.TestUi(t)
|
|
|
|
|
|
|
|
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{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := builder.Prepare(tpl.Builders["docker"].Config)
|
2015-10-08 20:15:27 -04:00
|
|
|
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{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: []*packer.HookedProvisioner{
|
2018-08-31 05:54:12 -04:00
|
|
|
{Provisioner: shell, Config: nil, TypeName: ""},
|
|
|
|
{Provisioner: downloadCupcake, Config: nil, TypeName: ""},
|
|
|
|
{Provisioner: downloadBigcake, Config: nil, TypeName: ""},
|
2015-10-08 20:15:27 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
hook := &packer.DispatchHook{Mapping: hooks}
|
|
|
|
|
|
|
|
// Run things
|
2019-03-22 09:53:28 -04:00
|
|
|
artifact, err := builder.Run(context.Background(), ui, hook)
|
2015-10-08 20:15:27 -04:00
|
|
|
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 {
|
2015-10-08 20:43:54 -04:00
|
|
|
t.Fatalf("Unable to stat cupcake file: %s", err)
|
2015-10-08 20:15:27 -04:00
|
|
|
}
|
|
|
|
cupcakeExpected := int64(2097152)
|
|
|
|
if cupcake.Size() != cupcakeExpected {
|
2015-10-08 20:43:54 -04:00
|
|
|
t.Errorf("Expected cupcake to be %d bytes; found %d", cupcakeExpected, cupcake.Size())
|
2015-10-08 20:15:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bigcake, err := os.Stat("bigcake")
|
|
|
|
if err != nil {
|
2015-10-08 20:43:54 -04:00
|
|
|
t.Fatalf("Unable to stat bigcake file: %s", err)
|
2015-10-08 20:15:27 -04:00
|
|
|
}
|
|
|
|
bigcakeExpected := int64(104857600)
|
|
|
|
if bigcake.Size() != bigcakeExpected {
|
2015-10-08 20:43:54 -04:00
|
|
|
t.Errorf("Expected bigcake to be %d bytes; found %d", bigcakeExpected, bigcake.Size())
|
2015-10-08 20:15:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2017-11-04 16:06:36 -04:00
|
|
|
// t.Fatalf("Input and output files do not match\n"+
|
|
|
|
// "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile)
|
2015-10-08 20:15:27 -04:00
|
|
|
// }
|
2015-10-08 19:37:14 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:45:47 -04:00
|
|
|
// TestFixUploadOwner verifies that owner of uploaded files is the user the container is running as.
|
|
|
|
func TestFixUploadOwner(t *testing.T) {
|
2017-10-02 16:03:42 -04:00
|
|
|
ui := packer.TestUi(t)
|
|
|
|
|
2017-10-10 16:45:47 -04:00
|
|
|
tpl, err := template.Parse(strings.NewReader(testFixUploadOwnerTemplate))
|
2017-10-02 16:03:42 -04:00
|
|
|
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{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := builder.Prepare(tpl.Builders["docker"].Config)
|
2017-10-02 16:03:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing configuration %s", err)
|
|
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatal("Encountered configuration warnings; aborting")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the provisioners
|
|
|
|
fileProvisioner := &file.Provisioner{}
|
|
|
|
err = fileProvisioner.Prepare(tpl.Provisioners[0].Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing single file upload provisioner: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirProvisioner := &file.Provisioner{}
|
|
|
|
err = dirProvisioner.Prepare(tpl.Provisioners[1].Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing directory upload provisioner: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
shellProvisioner := &shell.Provisioner{}
|
|
|
|
err = shellProvisioner.Prepare(tpl.Provisioners[2].Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing shell provisioner: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyProvisioner := &shell.Provisioner{}
|
|
|
|
err = verifyProvisioner.Prepare(tpl.Provisioners[3].Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error preparing verification provisioner: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add hooks so the provisioners run during the build
|
|
|
|
hooks := map[string][]packer.Hook{}
|
|
|
|
hooks[packer.HookProvision] = []packer.Hook{
|
|
|
|
&packer.ProvisionHook{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: []*packer.HookedProvisioner{
|
2018-08-31 05:54:12 -04:00
|
|
|
{Provisioner: fileProvisioner, Config: nil, TypeName: ""},
|
|
|
|
{Provisioner: dirProvisioner, Config: nil, TypeName: ""},
|
|
|
|
{Provisioner: shellProvisioner, Config: nil, TypeName: ""},
|
|
|
|
{Provisioner: verifyProvisioner, Config: nil, TypeName: ""},
|
2017-10-02 16:03:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
hook := &packer.DispatchHook{Mapping: hooks}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
artifact, err := builder.Run(context.Background(), ui, hook)
|
2017-10-02 16:03:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error running build %s", err)
|
|
|
|
}
|
|
|
|
defer artifact.Destroy()
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:36:56 -04:00
|
|
|
const dockerBuilderConfig = `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "docker",
|
2015-10-08 20:15:27 -04:00
|
|
|
"image": "ubuntu",
|
2015-10-08 19:37:14 -04:00
|
|
|
"discard": true,
|
2015-08-12 14:36:56 -04:00
|
|
|
"run_command": ["-d", "-i", "-t", "{{.Image}}", "/bin/sh"]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "file",
|
2015-08-12 15:16:26 -04:00
|
|
|
"source": "test-fixtures/onecakes/strawberry",
|
|
|
|
"destination": "/strawberry-cake"
|
2015-08-12 14:36:56 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "file",
|
2015-08-12 15:16:26 -04:00
|
|
|
"source": "/strawberry-cake",
|
|
|
|
"destination": "my-strawberry-cake",
|
2015-08-12 14:36:56 -04:00
|
|
|
"direction": "download"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2015-10-08 19:37:14 -04:00
|
|
|
|
|
|
|
const dockerLargeBuilderConfig = `
|
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "docker",
|
2015-10-08 20:15:27 -04:00
|
|
|
"image": "ubuntu",
|
2015-10-08 19:37:14 -04:00
|
|
|
"discard": true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "shell",
|
|
|
|
"inline": [
|
|
|
|
"dd if=/dev/urandom of=/tmp/cupcake bs=1M count=2",
|
2015-10-08 20:15:27 -04:00
|
|
|
"dd if=/dev/urandom of=/tmp/bigcake bs=1M count=100",
|
2015-10-08 19:37:14 -04:00
|
|
|
"sync",
|
2015-10-08 20:15:27 -04:00
|
|
|
"md5sum /tmp/cupcake /tmp/bigcake"
|
2015-10-08 19:37:14 -04:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "file",
|
|
|
|
"source": "/tmp/cupcake",
|
|
|
|
"destination": "cupcake",
|
|
|
|
"direction": "download"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "file",
|
2015-10-08 20:15:27 -04:00
|
|
|
"source": "/tmp/bigcake",
|
|
|
|
"destination": "bigcake",
|
2015-10-08 19:37:14 -04:00
|
|
|
"direction": "download"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2017-10-02 16:03:42 -04:00
|
|
|
|
2017-10-10 16:45:47 -04:00
|
|
|
const testFixUploadOwnerTemplate = `
|
2017-10-02 16:03:42 -04:00
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "docker",
|
|
|
|
"image": "ubuntu",
|
|
|
|
"discard": true,
|
|
|
|
"run_command": ["-d", "-i", "-t", "-u", "42", "{{.Image}}", "/bin/sh"]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "file",
|
|
|
|
"source": "test-fixtures/onecakes/strawberry",
|
|
|
|
"destination": "/tmp/strawberry-cake"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "file",
|
|
|
|
"source": "test-fixtures/manycakes",
|
|
|
|
"destination": "/tmp/"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "shell",
|
|
|
|
"inline": "touch /tmp/testUploadOwner"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "shell",
|
|
|
|
"inline": [
|
|
|
|
"[ $(stat -c %u /tmp/strawberry-cake) -eq 42 ] || (echo 'Invalid owner of /tmp/strawberry-cake' && exit 1)",
|
|
|
|
"[ $(stat -c %u /tmp/testUploadOwner) -eq 42 ] || (echo 'Invalid owner of /tmp/testUploadOwner' && exit 1)",
|
|
|
|
"find /tmp/manycakes | xargs -n1 -IFILE /bin/sh -c '[ $(stat -c %u FILE) -eq 42 ] || (echo \"Invalid owner of FILE\" && exit 1)'"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|