github.com/aspring/packer@v0.8.1-0.20150629211158-9db281ac0f89/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/mitchellh/packer/builder/file"
    12  	"github.com/mitchellh/packer/packer"
    13  	"github.com/mitchellh/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  // Test Helpers
   154  
   155  func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
   156  	// Create fake UI and Cache
   157  	ui := packer.TestUi(t)
   158  	cache := &packer.FileCache{CacheDir: os.TempDir()}
   159  
   160  	// Create config for file builder
   161  	const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
   162  	tpl, err := template.Parse(strings.NewReader(fileConfig))
   163  	if err != nil {
   164  		return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
   165  	}
   166  
   167  	// Prepare the file builder
   168  	builder := file.Builder{}
   169  	warnings, err := builder.Prepare(tpl.Builders["file"].Config)
   170  	if len(warnings) > 0 {
   171  		for _, warn := range warnings {
   172  			return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
   173  		}
   174  	}
   175  	if err != nil {
   176  		return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
   177  	}
   178  
   179  	// Run the file builder
   180  	artifact, err := builder.Run(ui, nil, cache)
   181  	if err != nil {
   182  		return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
   183  	}
   184  
   185  	return ui, artifact, err
   186  }
   187  
   188  func testArchive(t *testing.T, config string) packer.Artifact {
   189  	ui, artifact, err := setup(t)
   190  	if err != nil {
   191  		t.Fatalf("Error bootstrapping test: %s", err)
   192  	}
   193  	if artifact != nil {
   194  		defer artifact.Destroy()
   195  	}
   196  
   197  	tpl, err := template.Parse(strings.NewReader(config))
   198  	if err != nil {
   199  		t.Fatalf("Unable to parse test config: %s", err)
   200  	}
   201  
   202  	compressor := PostProcessor{}
   203  	compressor.Configure(tpl.PostProcessors[0][0].Config)
   204  	artifactOut, _, err := compressor.PostProcess(ui, artifact)
   205  	if err != nil {
   206  		t.Fatalf("Failed to compress artifact: %s", err)
   207  	}
   208  
   209  	return artifactOut
   210  }