github.com/aclaygray/packer@v1.3.2/post-processor/compress/post-processor_test.go (about)

     1  package compress
     2  
     3  import (
     4  	"compress/gzip"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/hashicorp/packer/builder/file"
    12  	"github.com/hashicorp/packer/packer"
    13  	"github.com/hashicorp/packer/template"
    14  )
    15  
    16  func TestDetectFilename(t *testing.T) {
    17  	// Test default / fallback with no file extension
    18  	nakedFilename := Config{OutputPath: "test"}
    19  	nakedFilename.detectFromFilename()
    20  	if nakedFilename.Archive != "tar" {
    21  		t.Error("Expected to find tar archive setting")
    22  	}
    23  	if nakedFilename.Algorithm != "pgzip" {
    24  		t.Error("Expected to find pgzip algorithm setting")
    25  	}
    26  
    27  	// Test .archive
    28  	zipFilename := Config{OutputPath: "test.zip"}
    29  	zipFilename.detectFromFilename()
    30  	if zipFilename.Archive != "zip" {
    31  		t.Error("Expected to find zip archive setting")
    32  	}
    33  	if zipFilename.Algorithm != "" {
    34  		t.Error("Expected to find empty algorithm setting")
    35  	}
    36  
    37  	// Test .compress
    38  	lz4Filename := Config{OutputPath: "test.lz4"}
    39  	lz4Filename.detectFromFilename()
    40  	if lz4Filename.Archive != "" {
    41  		t.Error("Expected to find empty archive setting")
    42  	}
    43  	if lz4Filename.Algorithm != "lz4" {
    44  		t.Error("Expected to find lz4 algorithm setting")
    45  	}
    46  
    47  	// Test .archive.compress with some.extra.dots...
    48  	lotsOfDots := Config{OutputPath: "test.blah.bloo.blee.tar.lz4"}
    49  	lotsOfDots.detectFromFilename()
    50  	if lotsOfDots.Archive != "tar" {
    51  		t.Error("Expected to find tar archive setting")
    52  	}
    53  	if lotsOfDots.Algorithm != "lz4" {
    54  		t.Error("Expected to find lz4 algorithm setting")
    55  	}
    56  }
    57  
    58  const expectedFileContents = "Hello world!"
    59  
    60  func TestSimpleCompress(t *testing.T) {
    61  	const config = `
    62  	{
    63  	    "post-processors": [
    64  	        {
    65  	            "type": "compress",
    66  	            "output": "package.tar.gz"
    67  	        }
    68  	    ]
    69  	}
    70  	`
    71  	artifact := testArchive(t, config)
    72  	defer artifact.Destroy()
    73  
    74  	fi, err := os.Stat("package.tar.gz")
    75  	if err != nil {
    76  		t.Errorf("Unable to read archive: %s", err)
    77  	}
    78  	if fi.IsDir() {
    79  		t.Error("Archive should not be a directory")
    80  	}
    81  }
    82  
    83  func TestZipArchive(t *testing.T) {
    84  	const config = `
    85  	{
    86  	    "post-processors": [
    87  	        {
    88  	            "type": "compress",
    89  	            "output": "package.zip"
    90  	        }
    91  	    ]
    92  	}
    93  	`
    94  
    95  	artifact := testArchive(t, config)
    96  	defer artifact.Destroy()
    97  
    98  	// Verify things look good
    99  	_, err := os.Stat("package.zip")
   100  	if err != nil {
   101  		t.Errorf("Unable to read archive: %s", err)
   102  	}
   103  }
   104  
   105  func TestTarArchive(t *testing.T) {
   106  	const config = `
   107  	{
   108  	    "post-processors": [
   109  	        {
   110  	            "type": "compress",
   111  	            "output": "package.tar"
   112  	        }
   113  	    ]
   114  	}
   115  	`
   116  
   117  	artifact := testArchive(t, config)
   118  	defer artifact.Destroy()
   119  
   120  	// Verify things look good
   121  	_, err := os.Stat("package.tar")
   122  	if err != nil {
   123  		t.Errorf("Unable to read archive: %s", err)
   124  	}
   125  }
   126  
   127  func TestCompressOptions(t *testing.T) {
   128  	const config = `
   129  	{
   130  	    "post-processors": [
   131  	        {
   132  	            "type": "compress",
   133  	            "output": "package.gz",
   134  	            "compression_level": 9
   135  	        }
   136  	    ]
   137  	}
   138  	`
   139  
   140  	artifact := testArchive(t, config)
   141  	defer artifact.Destroy()
   142  
   143  	filename := "package.gz"
   144  	archive, _ := os.Open(filename)
   145  	gzipReader, _ := gzip.NewReader(archive)
   146  	data, _ := ioutil.ReadAll(gzipReader)
   147  
   148  	if string(data) != expectedFileContents {
   149  		t.Errorf("Expected:\n%s\nFound:\n%s\n", expectedFileContents, data)
   150  	}
   151  }
   152  
   153  func TestCompressInterpolation(t *testing.T) {
   154  	const config = `
   155  	{
   156  	    "post-processors": [
   157  	        {
   158  	            "type": "compress",
   159  	            "output": "{{ build_name}}-{{ .BuildName }}-{{.BuilderType}}.gz"
   160  	        }
   161  	    ]
   162  	}
   163  	`
   164  
   165  	artifact := testArchive(t, config)
   166  	defer artifact.Destroy()
   167  
   168  	// You can interpolate using the .BuildName variable or build_name global
   169  	// function. We'll check both.
   170  	filename := "chocolate-vanilla-file.gz"
   171  	archive, err := os.Open(filename)
   172  	if err != nil {
   173  		t.Fatalf("Unable to read %s: %s", filename, err)
   174  	}
   175  
   176  	gzipReader, _ := gzip.NewReader(archive)
   177  	data, _ := ioutil.ReadAll(gzipReader)
   178  
   179  	if string(data) != expectedFileContents {
   180  		t.Errorf("Expected:\n%s\nFound:\n%s\n", expectedFileContents, data)
   181  	}
   182  }
   183  
   184  // Test Helpers
   185  
   186  func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
   187  	// Create fake UI and Cache
   188  	ui := packer.TestUi(t)
   189  	cache := &packer.FileCache{CacheDir: os.TempDir()}
   190  
   191  	// Create config for file builder
   192  	const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
   193  	tpl, err := template.Parse(strings.NewReader(fileConfig))
   194  	if err != nil {
   195  		return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
   196  	}
   197  
   198  	// Prepare the file builder
   199  	builder := file.Builder{}
   200  	warnings, err := builder.Prepare(tpl.Builders["file"].Config)
   201  	if len(warnings) > 0 {
   202  		for _, warn := range warnings {
   203  			return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
   204  		}
   205  	}
   206  	if err != nil {
   207  		return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
   208  	}
   209  
   210  	// Run the file builder
   211  	artifact, err := builder.Run(ui, nil, cache)
   212  	if err != nil {
   213  		return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
   214  	}
   215  
   216  	return ui, artifact, err
   217  }
   218  
   219  func testArchive(t *testing.T, config string) packer.Artifact {
   220  	ui, artifact, err := setup(t)
   221  	if err != nil {
   222  		t.Fatalf("Error bootstrapping test: %s", err)
   223  	}
   224  	if artifact != nil {
   225  		defer artifact.Destroy()
   226  	}
   227  
   228  	tpl, err := template.Parse(strings.NewReader(config))
   229  	if err != nil {
   230  		t.Fatalf("Unable to parse test config: %s", err)
   231  	}
   232  
   233  	compressor := PostProcessor{}
   234  	compressor.Configure(tpl.PostProcessors[0][0].Config)
   235  
   236  	// I get the feeling these should be automatically available somewhere, but
   237  	// some of the post-processors construct this manually.
   238  	compressor.config.ctx.BuildName = "chocolate"
   239  	compressor.config.PackerBuildName = "vanilla"
   240  	compressor.config.PackerBuilderType = "file"
   241  
   242  	artifactOut, _, err := compressor.PostProcess(ui, artifact)
   243  	if err != nil {
   244  		t.Fatalf("Failed to compress artifact: %s", err)
   245  	}
   246  
   247  	return artifactOut
   248  }