2014-04-26 12:22:10 -04:00
|
|
|
package compress
|
|
|
|
|
2015-06-16 19:31:09 -04:00
|
|
|
import (
|
2015-06-18 22:08:13 -04:00
|
|
|
"compress/gzip"
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2015-06-16 19:31:09 -04:00
|
|
|
"fmt"
|
2015-06-18 22:08:13 -04:00
|
|
|
"io/ioutil"
|
2015-06-16 19:31:09 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/file"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template"
|
2015-06-16 19:31:09 -04:00
|
|
|
)
|
|
|
|
|
2015-06-18 06:55:51 -04:00
|
|
|
func TestDetectFilename(t *testing.T) {
|
|
|
|
// Test default / fallback with no file extension
|
|
|
|
nakedFilename := Config{OutputPath: "test"}
|
|
|
|
nakedFilename.detectFromFilename()
|
|
|
|
if nakedFilename.Archive != "tar" {
|
|
|
|
t.Error("Expected to find tar archive setting")
|
|
|
|
}
|
|
|
|
if nakedFilename.Algorithm != "pgzip" {
|
|
|
|
t.Error("Expected to find pgzip algorithm setting")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test .archive
|
|
|
|
zipFilename := Config{OutputPath: "test.zip"}
|
|
|
|
zipFilename.detectFromFilename()
|
|
|
|
if zipFilename.Archive != "zip" {
|
|
|
|
t.Error("Expected to find zip archive setting")
|
|
|
|
}
|
|
|
|
if zipFilename.Algorithm != "" {
|
|
|
|
t.Error("Expected to find empty algorithm setting")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test .compress
|
|
|
|
lz4Filename := Config{OutputPath: "test.lz4"}
|
|
|
|
lz4Filename.detectFromFilename()
|
|
|
|
if lz4Filename.Archive != "" {
|
|
|
|
t.Error("Expected to find empty archive setting")
|
|
|
|
}
|
|
|
|
if lz4Filename.Algorithm != "lz4" {
|
|
|
|
t.Error("Expected to find lz4 algorithm setting")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test .archive.compress with some.extra.dots...
|
|
|
|
lotsOfDots := Config{OutputPath: "test.blah.bloo.blee.tar.lz4"}
|
|
|
|
lotsOfDots.detectFromFilename()
|
|
|
|
if lotsOfDots.Archive != "tar" {
|
|
|
|
t.Error("Expected to find tar archive setting")
|
|
|
|
}
|
|
|
|
if lotsOfDots.Algorithm != "lz4" {
|
|
|
|
t.Error("Expected to find lz4 algorithm setting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:08:13 -04:00
|
|
|
const expectedFileContents = "Hello world!"
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
func TestSimpleCompress(t *testing.T) {
|
2015-06-18 21:25:46 -04:00
|
|
|
const config = `
|
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "compress",
|
|
|
|
"output": "package.tar.gz"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
artifact := testArchive(t, config)
|
2015-06-18 21:21:03 -04:00
|
|
|
defer artifact.Destroy()
|
2015-06-16 19:31:09 -04:00
|
|
|
|
|
|
|
fi, err := os.Stat("package.tar.gz")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to read archive: %s", err)
|
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
|
|
|
t.Error("Archive should not be a directory")
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 14:30:49 -04:00
|
|
|
|
2015-06-18 03:47:33 -04:00
|
|
|
func TestZipArchive(t *testing.T) {
|
2015-06-18 21:25:46 -04:00
|
|
|
const config = `
|
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "compress",
|
|
|
|
"output": "package.zip"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
artifact := testArchive(t, config)
|
2015-06-18 21:21:03 -04:00
|
|
|
defer artifact.Destroy()
|
2015-06-18 03:47:33 -04:00
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
// Verify things look good
|
|
|
|
_, err := os.Stat("package.zip")
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
2015-06-18 21:21:03 -04:00
|
|
|
t.Errorf("Unable to read archive: %s", err)
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
2015-06-18 21:21:03 -04:00
|
|
|
}
|
2015-06-18 03:47:33 -04:00
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
func TestTarArchive(t *testing.T) {
|
2015-06-18 21:25:46 -04:00
|
|
|
const config = `
|
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "compress",
|
|
|
|
"output": "package.tar"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
artifact := testArchive(t, config)
|
2015-06-18 21:21:03 -04:00
|
|
|
defer artifact.Destroy()
|
2015-06-18 03:47:33 -04:00
|
|
|
|
|
|
|
// Verify things look good
|
2015-06-18 21:21:03 -04:00
|
|
|
_, err := os.Stat("package.tar")
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to read archive: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
func TestCompressOptions(t *testing.T) {
|
2015-06-18 21:25:46 -04:00
|
|
|
const config = `
|
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "compress",
|
|
|
|
"output": "package.gz",
|
|
|
|
"compression_level": 9
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
artifact := testArchive(t, config)
|
2015-06-18 21:21:03 -04:00
|
|
|
defer artifact.Destroy()
|
|
|
|
|
2015-06-18 22:08:13 -04:00
|
|
|
filename := "package.gz"
|
|
|
|
archive, _ := os.Open(filename)
|
|
|
|
gzipReader, _ := gzip.NewReader(archive)
|
|
|
|
data, _ := ioutil.ReadAll(gzipReader)
|
|
|
|
|
|
|
|
if string(data) != expectedFileContents {
|
|
|
|
t.Errorf("Expected:\n%s\nFound:\n%s\n", expectedFileContents, data)
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
2015-06-18 21:21:03 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 19:34:24 -04:00
|
|
|
func TestCompressInterpolation(t *testing.T) {
|
|
|
|
const config = `
|
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
{
|
|
|
|
"type": "compress",
|
2015-08-04 22:30:57 -04:00
|
|
|
"output": "{{ build_name}}-{{ .BuildName }}-{{.BuilderType}}.gz"
|
2015-08-03 19:34:24 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
artifact := testArchive(t, config)
|
|
|
|
defer artifact.Destroy()
|
|
|
|
|
2015-08-04 22:30:57 -04:00
|
|
|
// You can interpolate using the .BuildName variable or build_name global
|
|
|
|
// function. We'll check both.
|
|
|
|
filename := "chocolate-vanilla-file.gz"
|
2015-08-03 19:34:24 -04:00
|
|
|
archive, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read %s: %s", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gzipReader, _ := gzip.NewReader(archive)
|
|
|
|
data, _ := ioutil.ReadAll(gzipReader)
|
|
|
|
|
|
|
|
if string(data) != expectedFileContents {
|
|
|
|
t.Errorf("Expected:\n%s\nFound:\n%s\n", expectedFileContents, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
// Test Helpers
|
2015-06-18 03:47:33 -04:00
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func setup(t *testing.T) (packersdk.Ui, packersdk.Artifact, error) {
|
2015-06-18 21:21:03 -04:00
|
|
|
// Create fake UI and Cache
|
2020-12-02 13:50:37 -05:00
|
|
|
ui := packersdk.TestUi(t)
|
2015-06-18 21:21:03 -04:00
|
|
|
|
|
|
|
// Create config for file builder
|
|
|
|
const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
|
|
|
|
tpl, err := template.Parse(strings.NewReader(fileConfig))
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
2015-06-18 21:21:03 -04:00
|
|
|
return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
// Prepare the file builder
|
|
|
|
builder := file.Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := builder.Prepare(tpl.Builders["file"].Config)
|
2015-06-18 21:21:03 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
for _, warn := range warnings {
|
|
|
|
return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
2015-06-18 21:21:03 -04:00
|
|
|
return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
// Run the file builder
|
2019-03-22 09:56:02 -04:00
|
|
|
artifact, err := builder.Run(context.Background(), ui, nil)
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
2015-06-18 21:21:03 -04:00
|
|
|
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
2015-06-18 21:21:03 -04:00
|
|
|
|
|
|
|
return ui, artifact, err
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func testArchive(t *testing.T, config string) packersdk.Artifact {
|
2015-06-18 03:47:33 -04:00
|
|
|
ui, artifact, err := setup(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error bootstrapping test: %s", err)
|
|
|
|
}
|
|
|
|
if artifact != nil {
|
|
|
|
defer artifact.Destroy()
|
|
|
|
}
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
tpl, err := template.Parse(strings.NewReader(config))
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to parse test config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
compressor := PostProcessor{}
|
|
|
|
compressor.Configure(tpl.PostProcessors[0][0].Config)
|
2015-08-04 22:30:57 -04:00
|
|
|
|
|
|
|
// I get the feeling these should be automatically available somewhere, but
|
|
|
|
// some of the post-processors construct this manually.
|
|
|
|
compressor.config.ctx.BuildName = "chocolate"
|
|
|
|
compressor.config.PackerBuildName = "vanilla"
|
|
|
|
compressor.config.PackerBuilderType = "file"
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
artifactOut, _, _, err := compressor.PostProcess(context.Background(), ui, artifact)
|
2015-06-18 03:47:33 -04:00
|
|
|
if err != nil {
|
2015-06-18 21:21:03 -04:00
|
|
|
t.Fatalf("Failed to compress artifact: %s", err)
|
2015-06-18 03:47:33 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 21:21:03 -04:00
|
|
|
return artifactOut
|
2015-06-16 14:30:49 -04:00
|
|
|
}
|