github.phpd.cn/hashicorp/packer@v1.3.2/post-processor/checksum/post-processor_test.go (about)

     1  package checksum
     2  
     3  import (
     4  	"bytes"
     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  const expectedFileContents = "Hello world!"
    17  
    18  func TestChecksumSHA1(t *testing.T) {
    19  	const config = `
    20  	{
    21  	    "post-processors": [
    22  	        {
    23  	            "type": "checksum",
    24  	            "checksum_types": ["sha1"],
    25  	            "output": "sha1sums"
    26  	        }
    27  	    ]
    28  	}
    29  	`
    30  	artifact := testChecksum(t, config)
    31  	defer artifact.Destroy()
    32  
    33  	f, err := os.Open("sha1sums")
    34  	if err != nil {
    35  		t.Errorf("Unable to read checksum file: %s", err)
    36  	}
    37  	if buf, _ := ioutil.ReadAll(f); !bytes.Equal(buf, []byte("d3486ae9136e7856bc42212385ea797094475802\tpackage.txt\n")) {
    38  		t.Errorf("Failed to compute checksum: %s\n%s", buf, "d3486ae9136e7856bc42212385ea797094475802 package.txt")
    39  	}
    40  
    41  	defer f.Close()
    42  }
    43  
    44  // Test Helpers
    45  
    46  func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
    47  	// Create fake UI and Cache
    48  	ui := packer.TestUi(t)
    49  	cache := &packer.FileCache{CacheDir: os.TempDir()}
    50  
    51  	// Create config for file builder
    52  	const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
    53  	tpl, err := template.Parse(strings.NewReader(fileConfig))
    54  	if err != nil {
    55  		return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
    56  	}
    57  
    58  	// Prepare the file builder
    59  	builder := file.Builder{}
    60  	warnings, err := builder.Prepare(tpl.Builders["file"].Config)
    61  	if len(warnings) > 0 {
    62  		for _, warn := range warnings {
    63  			return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
    64  		}
    65  	}
    66  	if err != nil {
    67  		return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
    68  	}
    69  
    70  	// Run the file builder
    71  	artifact, err := builder.Run(ui, nil, cache)
    72  	if err != nil {
    73  		return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
    74  	}
    75  
    76  	return ui, artifact, err
    77  }
    78  
    79  func testChecksum(t *testing.T, config string) packer.Artifact {
    80  	ui, artifact, err := setup(t)
    81  	if err != nil {
    82  		t.Fatalf("Error bootstrapping test: %s", err)
    83  	}
    84  	if artifact != nil {
    85  		defer artifact.Destroy()
    86  	}
    87  
    88  	tpl, err := template.Parse(strings.NewReader(config))
    89  	if err != nil {
    90  		t.Fatalf("Unable to parse test config: %s", err)
    91  	}
    92  
    93  	checksum := PostProcessor{}
    94  	checksum.Configure(tpl.PostProcessors[0][0].Config)
    95  
    96  	// I get the feeling these should be automatically available somewhere, but
    97  	// some of the post-processors construct this manually.
    98  	checksum.config.ctx.BuildName = "chocolate"
    99  	checksum.config.PackerBuildName = "vanilla"
   100  	checksum.config.PackerBuilderType = "file"
   101  
   102  	artifactOut, _, err := checksum.PostProcess(ui, artifact)
   103  	if err != nil {
   104  		t.Fatalf("Failed to checksum artifact: %s", err)
   105  	}
   106  
   107  	return artifactOut
   108  }