File: step_set_generated_data_test.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (51 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package docker

import (
	"context"
	"testing"

	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	"github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata"
)

func TestStepSetGeneratedData_Run(t *testing.T) {
	state := testState(t)
	step := new(StepSetGeneratedData)
	step.GeneratedData = &packerbuilderdata.GeneratedData{State: state}
	driver := state.Get("driver").(*MockDriver)
	driver.Sha256Result = "80B3BB1B1696E73A9B19DEEF92F664F8979F948DF348088B61F9A3477655AF64"
	state.Put("image_id", "12345")

	if action := step.Run(context.TODO(), state); action != multistep.ActionContinue {
		t.Fatalf("Should not halt")
	}
	if !driver.Sha256Called {
		t.Fatalf("driver.SHA256 should be called")
	}
	if driver.Sha256Id != "12345" {
		t.Fatalf("driver.SHA256 got wrong image it: %s", driver.Sha256Id)
	}
	genData := state.Get("generated_data").(map[string]interface{})
	imgSha256 := genData["ImageSha256"].(string)
	if imgSha256 != driver.Sha256Result {
		t.Fatalf("Expected ImageSha256 to be %s but was %s", driver.Sha256Result, imgSha256)
	}

	// Image ID not implement
	state = testState(t)
	step.GeneratedData = &packerbuilderdata.GeneratedData{State: state}
	driver = state.Get("driver").(*MockDriver)
	notImplementedMsg := "ERR_IMAGE_SHA256_NOT_FOUND"

	if action := step.Run(context.TODO(), state); action != multistep.ActionContinue {
		t.Fatalf("Should not halt")
	}
	if driver.Sha256Called {
		t.Fatalf("driver.SHA256 should not be called")
	}
	genData = state.Get("generated_data").(map[string]interface{})
	imgSha256 = genData["ImageSha256"].(string)
	if imgSha256 != notImplementedMsg {
		t.Fatalf("Expected ImageSha256 to be %s but was %s", notImplementedMsg, imgSha256)
	}
}