fix docker acceptance tests. Turns out they were broken on the main branch too; needed to move noop progress bar into the sdk to be used in the builder testui.

This commit is contained in:
Megan Marsh 2020-12-03 14:58:07 -08:00
parent 8f51a8bfae
commit 352f064b55
12 changed files with 213 additions and 344 deletions

View File

@ -35,5 +35,8 @@ func (a *ExportArtifact) State(name string) interface{} {
}
func (a *ExportArtifact) Destroy() error {
if a.path != "" {
return os.Remove(a.path)
}
return nil
}

View File

@ -1,99 +1,107 @@
package docker
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"github.com/hashicorp/packer/packer"
builderT "github.com/hashicorp/packer/packer-plugin-sdk/acctest"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/template"
"github.com/hashicorp/packer/provisioner/file"
"github.com/hashicorp/packer/provisioner/shell"
)
// RenderConfig helps create dynamic packer template configs for parsing by
// builderT without having to write the config to a file.
func RenderConfig(builderConfig map[string]interface{}, provisionerConfig []map[string]interface{}) string {
// set up basic build template
t := map[string][]map[string]interface{}{
"builders": {
// Setup basic docker config
map[string]interface{}{
"type": "test",
"image": "ubuntu",
"discard": true,
},
},
"provisioners": []map[string]interface{}{},
}
// apply special builder overrides
for k, v := range builderConfig {
t["builders"][0][k] = v
}
// Apply special provisioner overrides
for _, v := range provisionerConfig {
t["provisioners"] = append(t["provisioners"], v)
}
j, _ := json.Marshal(t)
return string(j)
}
// TestUploadDownload verifies that basic upload / download functionality works
func TestUploadDownload(t *testing.T) {
ui := packersdk.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")
}
dockerBuilderExtraConfig := map[string]interface{}{
"run_command": []string{"-d", "-i", "-t", "{{.Image}}", "/bin/sh"},
}
dockerProvisionerConfig := []map[string]interface{}{
{
"type": "file",
"source": "test-fixtures/onecakes/strawberry",
"destination": "/strawberry-cake",
},
{
"type": "file",
"source": "/strawberry-cake",
"destination": "my-strawberry-cake",
"direction": "download",
},
}
configString := RenderConfig(dockerBuilderExtraConfig, dockerProvisionerConfig)
// this should be a precheck
cmd := exec.Command("docker", "-v")
cmd.Run()
if !cmd.ProcessState.Success() {
err := cmd.Run()
if err != nil {
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
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)
}
// 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")
// Add hooks so the provisioners run during the build
hooks := map[string][]packersdk.Hook{}
hooks[packersdk.HookProvision] = []packersdk.Hook{
&packer.ProvisionHook{
Provisioners: []*packer.HookedProvisioner{
{Provisioner: upload, Config: nil, TypeName: ""},
{Provisioner: download, Config: nil, TypeName: ""},
},
},
}
hook := &packersdk.DispatchHook{Mapping: hooks}
// Run things
artifact, err := builder.Run(context.Background(), ui, hook)
if err != nil {
t.Fatalf("Error running build %s", err)
}
// Preemptive cleanup
defer artifact.Destroy()
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: configString,
Check: func(a []packersdk.Artifact) error {
// Verify that the thing we downloaded is the same thing we sent up.
// Complain loudly if it isn't.
inputFile, err := ioutil.ReadFile("test-fixtures/onecakes/strawberry")
if err != nil {
t.Fatalf("Unable to read input file: %s", err)
return fmt.Errorf("Unable to read input file: %s", err)
}
outputFile, err := ioutil.ReadFile("my-strawberry-cake")
if err != nil {
t.Fatalf("Unable to read output file: %s", err)
return fmt.Errorf("Unable to read output file: %s", err)
}
if sha256.Sum256(inputFile) != sha256.Sum256(outputFile) {
t.Fatalf("Input and output files do not match\n"+
return fmt.Errorf("Input and output files do not match\n"+
"Input:\n%s\nOutput:\n%s\n", inputFile, outputFile)
}
return nil
},
Teardown: func() error {
// 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!
os.Remove("my-strawberry-cake")
return nil
},
})
}
// TestLargeDownload verifies that files are the appropriate size after being
@ -101,74 +109,47 @@ 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) {
ui := packersdk.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")
}
dockerProvisionerConfig := []map[string]interface{}{
{
"type": "shell",
"inline": []string{
"dd if=/dev/urandom of=/tmp/cupcake bs=1M count=2",
"dd if=/dev/urandom of=/tmp/bigcake bs=1M count=100",
"sync",
"md5sum /tmp/cupcake /tmp/bigcake",
},
},
{
"type": "file",
"source": "/tmp/cupcake",
"destination": "cupcake",
"direction": "download",
},
{
"type": "file",
"source": "/tmp/bigcake",
"destination": "bigcake",
"direction": "download",
},
}
configString := RenderConfig(map[string]interface{}{}, dockerProvisionerConfig)
// this should be a precheck
cmd := exec.Command("docker", "-v")
cmd.Run()
if !cmd.ProcessState.Success() {
err := cmd.Run()
if err != nil {
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][]packersdk.Hook{}
hooks[packersdk.HookProvision] = []packersdk.Hook{
&packer.ProvisionHook{
Provisioners: []*packer.HookedProvisioner{
{Provisioner: shell, Config: nil, TypeName: ""},
{Provisioner: downloadCupcake, Config: nil, TypeName: ""},
{Provisioner: downloadBigcake, Config: nil, TypeName: ""},
},
},
}
hook := &packersdk.DispatchHook{Mapping: hooks}
// Run things
artifact, err := builder.Run(context.Background(), ui, hook)
if err != nil {
t.Fatalf("Error running build %s", err)
}
// Preemptive cleanup
defer artifact.Destroy()
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: configString,
Check: func(a []packersdk.Artifact) error {
// Verify that the things we downloaded are the right size. Complain loudly
// if they are not.
//
@ -200,177 +181,61 @@ func TestLargeDownload(t *testing.T) {
// t.Fatalf("Input and output files do not match\n"+
// "Input:\n%s\nOutput:\n%s\n", inputFile, outputFile)
// }
return nil
},
Teardown: func() error {
os.Remove("cupcake")
os.Remove("bigcake")
return nil
},
})
}
// TestFixUploadOwner verifies that owner of uploaded files is the user the container is running as.
func TestFixUploadOwner(t *testing.T) {
ui := packersdk.TestUi(t)
tpl, err := template.Parse(strings.NewReader(testFixUploadOwnerTemplate))
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() {
err := cmd.Run()
if err != nil {
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")
dockerBuilderExtraConfig := map[string]interface{}{
"run_command": []string{"-d", "-i", "-t", "-u", "42", "{{.Image}}", "/bin/sh"},
}
// 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][]packersdk.Hook{}
hooks[packersdk.HookProvision] = []packersdk.Hook{
&packer.ProvisionHook{
Provisioners: []*packer.HookedProvisioner{
{Provisioner: fileProvisioner, Config: nil, TypeName: ""},
{Provisioner: dirProvisioner, Config: nil, TypeName: ""},
{Provisioner: shellProvisioner, Config: nil, TypeName: ""},
{Provisioner: verifyProvisioner, Config: nil, TypeName: ""},
},
},
}
hook := &packersdk.DispatchHook{Mapping: hooks}
artifact, err := builder.Run(context.Background(), ui, hook)
if err != nil {
t.Fatalf("Error running build %s", err)
}
defer artifact.Destroy()
}
const dockerBuilderConfig = `
{
"builders": [
{
"type": "docker",
"image": "ubuntu",
"discard": true,
"run_command": ["-d", "-i", "-t", "{{.Image}}", "/bin/sh"]
}
],
"provisioners": [
testFixUploadOwnerProvisionersTemplate := []map[string]interface{}{
{
"type": "file",
"source": "test-fixtures/onecakes/strawberry",
"destination": "/strawberry-cake"
},
{
"type": "file",
"source": "/strawberry-cake",
"destination": "my-strawberry-cake",
"direction": "download"
}
]
}
`
const dockerLargeBuilderConfig = `
{
"builders": [
{
"type": "docker",
"image": "ubuntu",
"discard": true
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"dd if=/dev/urandom of=/tmp/cupcake bs=1M count=2",
"dd if=/dev/urandom of=/tmp/bigcake bs=1M count=100",
"sync",
"md5sum /tmp/cupcake /tmp/bigcake"
]
},
{
"type": "file",
"source": "/tmp/cupcake",
"destination": "cupcake",
"direction": "download"
},
{
"type": "file",
"source": "/tmp/bigcake",
"destination": "bigcake",
"direction": "download"
}
]
}
`
const testFixUploadOwnerTemplate = `
{
"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"
"destination": "/tmp/strawberry-cake",
},
{
"type": "file",
"source": "test-fixtures/manycakes",
"destination": "/tmp/"
"destination": "/tmp/",
},
{
"type": "shell",
"inline": "touch /tmp/testUploadOwner"
"inline": "touch /tmp/testUploadOwner",
},
{
"type": "shell",
"inline": [
"inline": []string{
"[ $(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)'"
]
"find /tmp/manycakes | xargs -n1 -IFILE /bin/sh -c '[ $(stat -c %u FILE) -eq 42 ] || (echo \"Invalid owner of FILE\" && exit 1)'",
},
},
}
]
configString := RenderConfig(dockerBuilderExtraConfig, testFixUploadOwnerProvisionersTemplate)
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: configString,
})
}
`

View File

@ -200,7 +200,7 @@ func wrappedMain() int {
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stdout,
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
ui = basicUi
if !inPlugin {

View File

@ -163,10 +163,12 @@ func Test(t TestT, c TestCase) {
// Run it! We use a temporary directory for caching and discard
// any UI output. We discard since it shows up in logs anyways.
log.Printf("[DEBUG] Running 'test' build")
// ui := packersdk.TestUi(t)
ui := &packersdk.BasicUi{
Reader: os.Stdin,
Writer: ioutil.Discard,
ErrorWriter: ioutil.Discard,
PB: &packersdk.NoopProgressTracker{},
}
artifacts, err := build.Run(context.Background(), ui)
if err != nil {

View File

@ -17,7 +17,6 @@ import (
"github.com/google/go-cmp/cmp"
urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/tmp"
@ -246,7 +245,7 @@ func TestStepDownload_download(t *testing.T) {
ui := &packersdk.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
dir := createTempDir(t)

View File

@ -7,7 +7,6 @@ import (
"os"
"testing"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
@ -17,7 +16,7 @@ func testState(t *testing.T) multistep.StateBag {
state.Put("ui", &packersdk.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
})
return state
}

View File

@ -150,3 +150,12 @@ func (rw *BasicUi) Machine(t string, args ...string) {
func (rw *BasicUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) {
return rw.PB.TrackProgress(src, currentSize, totalSize, stream)
}
// NoopProgressTracker is a progress tracker
// that displays nothing.
type NoopProgressTracker struct{}
// TrackProgress returns stream
func (*NoopProgressTracker) TrackProgress(_ string, _, _ int64, stream io.ReadCloser) io.ReadCloser {
return stream
}

View File

@ -13,6 +13,7 @@ func TestUi(t *testing.T) Ui {
Reader: &buf,
Writer: ioutil.Discard,
ErrorWriter: ioutil.Discard,
PB: &NoopProgressTracker{},
}
}

View File

@ -1,12 +0,0 @@
package packer
import "io"
// NoopProgressTracker is a progress tracker
// that displays nothing.
type NoopProgressTracker struct{}
// TrackProgress returns stream
func (*NoopProgressTracker) TrackProgress(_ string, _, _ int64, stream io.ReadCloser) io.ReadCloser {
return stream
}

View File

@ -1,3 +1,7 @@
package packer
type UiProgressBar = NoopProgressTracker
import (
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
type UiProgressBar = packersdk.NoopProgressTracker

View File

@ -31,7 +31,7 @@ const (
)
type NoopUi struct {
PB NoopProgressTracker
PB packersdk.NoopProgressTracker
}
var _ packersdk.Ui = new(NoopUi)
@ -173,7 +173,7 @@ func (u *TargetedUI) TrackProgress(src string, currentSize, totalSize int64, str
// to the given Writer.
type MachineReadableUi struct {
Writer io.Writer
PB NoopProgressTracker
PB packersdk.NoopProgressTracker
}
var _ packersdk.Ui = new(MachineReadableUi)

View File

@ -10,7 +10,6 @@ import (
"strings"
"testing"
"github.com/hashicorp/packer/packer"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
@ -127,7 +126,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{
Writer: b,
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -186,7 +185,7 @@ func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) {
b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{
Writer: b,
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -256,7 +255,7 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) {
b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{
Writer: b,
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -308,7 +307,7 @@ func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) {
b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{
Writer: b,
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
comm := &packersdk.MockCommunicator{}
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
@ -366,7 +365,7 @@ func TestProvisionDownloadMkdirAll(t *testing.T) {
b := bytes.NewBuffer(nil)
ui := &packersdk.BasicUi{
Writer: b,
PB: &packer.NoopProgressTracker{},
PB: &packersdk.NoopProgressTracker{},
}
comm := &packersdk.MockCommunicator{}
err = p.ProvisionDownload(ui, comm)