Files
Packer-Cn/post-processor/checksum/post-processor_test.go
T
Adrien Delorme 9f82b75e57 Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 12:11:58 +01:00

108 lines
2.6 KiB
Go

package checksum
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/hashicorp/packer/builder/file"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template"
)
const expectedFileContents = "Hello world!"
func TestChecksumSHA1(t *testing.T) {
const config = `
{
"post-processors": [
{
"type": "checksum",
"checksum_types": ["sha1"],
"output": "sha1sums"
}
]
}
`
artifact := testChecksum(t, config)
defer artifact.Destroy()
f, err := os.Open("sha1sums")
if err != nil {
t.Errorf("Unable to read checksum file: %s", err)
}
if buf, _ := ioutil.ReadAll(f); !bytes.Equal(buf, []byte("d3486ae9136e7856bc42212385ea797094475802\tpackage.txt\n")) {
t.Errorf("Failed to compute checksum: %s\n%s", buf, "d3486ae9136e7856bc42212385ea797094475802 package.txt")
}
defer f.Close()
}
// Test Helpers
func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
// Create fake UI and Cache
ui := packer.TestUi(t)
// Create config for file builder
const fileConfig = `{"builders":[{"type":"file","target":"package.txt","content":"Hello world!"}]}`
tpl, err := template.Parse(strings.NewReader(fileConfig))
if err != nil {
return nil, nil, fmt.Errorf("Unable to parse setup configuration: %s", err)
}
// Prepare the file builder
builder := file.Builder{}
warnings, err := builder.Prepare(tpl.Builders["file"].Config)
if len(warnings) > 0 {
for _, warn := range warnings {
return nil, nil, fmt.Errorf("Configuration warning: %s", warn)
}
}
if err != nil {
return nil, nil, fmt.Errorf("Invalid configuration: %s", err)
}
// Run the file builder
artifact, err := builder.Run(ui, nil)
if err != nil {
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err)
}
return ui, artifact, err
}
func testChecksum(t *testing.T, config string) packer.Artifact {
ui, artifact, err := setup(t)
if err != nil {
t.Fatalf("Error bootstrapping test: %s", err)
}
if artifact != nil {
defer artifact.Destroy()
}
tpl, err := template.Parse(strings.NewReader(config))
if err != nil {
t.Fatalf("Unable to parse test config: %s", err)
}
checksum := PostProcessor{}
checksum.Configure(tpl.PostProcessors[0][0].Config)
// I get the feeling these should be automatically available somewhere, but
// some of the post-processors construct this manually.
checksum.config.ctx.BuildName = "chocolate"
checksum.config.PackerBuildName = "vanilla"
checksum.config.PackerBuilderType = "file"
artifactOut, _, err := checksum.PostProcess(ui, artifact)
if err != nil {
t.Fatalf("Failed to checksum artifact: %s", err)
}
return artifactOut
}