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