mirror of https://github.com/hashicorp/packer
Update CreateIntialBuildForIteration to initialize each build its own map (#11574)
* Rename mock bucket test file * Add failing tests for reproducing #11573 ``` === RUN TestBucket_CreateInitialBuildForIteration 2022/02/16 16:22:21 [TRACE] creating initial build for component happycloud.image --- PASS: TestBucket_CreateInitialBuildForIteration (0.00s) === RUN TestBucket_UpdateLabelsForBuild 2022/02/16 16:22:21 [TRACE] creating initial build for component happycloud.image types.bucket_test.go:87: expected the initial build to have an additional build label but thee is no diff: "" --- FAIL: TestBucket_UpdateLabelsForBuild (0.00s) === RUN TestBucket_UpdateLabelsForBuild_withMultipleBuilds 2022/02/16 16:22:21 [TRACE] creating initial build for component happycloud.image 2022/02/16 16:22:21 [TRACE] creating initial build for component happycloud.image2 types.bucket_test.go:125: Comparing component build labels: map[based_off:alpine source_image:another-happycloud-image version:1.7.0] against global build labels: map[based_off:alpine source_image:another-happycloud-image version:1.7.0] types.bucket_test.go:128: expected the initial build to have an additional build label but they are equal types.bucket_test.go:125: Comparing component build labels: map[based_off:alpine source_image:another-happycloud-image version:1.7.0] against global build labels: map[based_off:alpine source_image:another-happycloud-image version:1.7.0] types.bucket_test.go:128: expected the initial build to have an additional build label but they are equal --- FAIL: TestBucket_UpdateLabelsForBuild_withMultipleBuilds (0.00s) FAIL FAIL github.com/hashicorp/packer/internal/registry 0.646s ``` * Update CreateIntialBuildForIteration to initialize each build with a new map Previously upon creating the initial build the same map, which was initialized for the build_labels argument was being shared across all build images. This was causing an issue with labels being backed by the same map for all builds. This change ensures that all builds get their own map with any global build labels copied over during the initial creation. Closes #11573 Passing tests with changes on branch ``` RUN TestBucket_CreateInitialBuildForIteration 2022/02/16 16:37:40 [TRACE] creating initial build for component happycloud.image --- PASS: TestBucket_CreateInitialBuildForIteration (0.00s) === RUN TestBucket_UpdateLabelsForBuild 2022/02/16 16:37:40 [TRACE] creating initial build for component happycloud.image --- PASS: TestBucket_UpdateLabelsForBuild (0.00s) === RUN TestBucket_UpdateLabelsForBuild_withMultipleBuilds 2022/02/16 16:37:40 [TRACE] creating initial build for component happycloud.image 2022/02/16 16:37:40 [TRACE] creating initial build for component happycloud.image2 types.bucket_test.go:125: Comparing component build labels: map[based_off:alpine source_image:another-happycloud-image version:1.7.0] against global build labels: map[based_off:alpine version:1.7.0] types.bucket_test.go:125: Comparing component build labels: map[based_off:alpine source_image:the-original-happycloud-image version:1.7.0] against global build labels: map[based_off:alpine version:1.7.0] --- PASS: TestBucket_UpdateLabelsForBuild_withMultipleBuilds (0.00s) ``` * Handle errors from bucket methods * Update test cases Initialize maps for bucket when calling NewBucketWithIterationpull/11586/head
parent
df5699c10d
commit
119a825296
@ -0,0 +1,385 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/hcp-sdk-go/clients/cloud-packer-service/preview/2021-04-30/models"
|
||||
)
|
||||
|
||||
func TestInitialize_NewBucketNewIteration(t *testing.T) {
|
||||
//nolint:errcheck
|
||||
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
|
||||
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
|
||||
mockService := NewMockPackerClientService()
|
||||
|
||||
b := &Bucket{
|
||||
Slug: "TestBucket",
|
||||
client: &Client{
|
||||
Packer: mockService,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
b.Iteration, err = NewIteration(IterationOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
b.Iteration.expectedBuilds = append(b.Iteration.expectedBuilds, "happycloud.image")
|
||||
|
||||
err = b.Initialize(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
if !mockService.CreateBucketCalled {
|
||||
t.Errorf("expected a call to CreateBucket but it didn't happen")
|
||||
}
|
||||
|
||||
if !mockService.CreateIterationCalled {
|
||||
t.Errorf("expected a call to CreateIteration but it didn't happen")
|
||||
}
|
||||
|
||||
if mockService.CreateBuildCalled {
|
||||
t.Errorf("Didn't expect a call to CreateBuild")
|
||||
}
|
||||
|
||||
if b.Iteration.ID != "iteration-id" {
|
||||
t.Errorf("expected an iteration to created but it didn't")
|
||||
}
|
||||
|
||||
err = b.PopulateIteration(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
if !mockService.CreateBuildCalled {
|
||||
t.Errorf("Expected a call to CreateBuild but it didn't happen")
|
||||
}
|
||||
|
||||
if _, ok := b.Iteration.builds.Load("happycloud.image"); !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInitialize_ExistingBucketNewIteration(t *testing.T) {
|
||||
//nolint:errcheck
|
||||
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
|
||||
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
|
||||
mockService := NewMockPackerClientService()
|
||||
mockService.BucketAlreadyExist = true
|
||||
|
||||
b := &Bucket{
|
||||
Slug: "TestBucket",
|
||||
client: &Client{
|
||||
Packer: mockService,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
b.Iteration, err = NewIteration(IterationOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
b.Iteration.expectedBuilds = append(b.Iteration.expectedBuilds, "happycloud.image")
|
||||
|
||||
err = b.Initialize(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
if !mockService.UpdateBucketCalled {
|
||||
t.Errorf("expected call to UpdateBucket but it didn't happen")
|
||||
}
|
||||
|
||||
if !mockService.CreateIterationCalled {
|
||||
t.Errorf("expected a call to CreateIteration but it didn't happen")
|
||||
}
|
||||
|
||||
if mockService.CreateBuildCalled {
|
||||
t.Errorf("Didn't expect a call to CreateBuild")
|
||||
}
|
||||
|
||||
if b.Iteration.ID != "iteration-id" {
|
||||
t.Errorf("expected an iteration to created but it didn't")
|
||||
}
|
||||
|
||||
err = b.PopulateIteration(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
if !mockService.CreateBuildCalled {
|
||||
t.Errorf("Expected a call to CreateBuild but it didn't happen")
|
||||
}
|
||||
|
||||
if _, ok := b.Iteration.builds.Load("happycloud.image"); !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInitialize_ExistingBucketExistingIteration(t *testing.T) {
|
||||
//nolint:errcheck
|
||||
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
|
||||
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
|
||||
mockService := NewMockPackerClientService()
|
||||
mockService.BucketAlreadyExist = true
|
||||
mockService.IterationAlreadyExist = true
|
||||
|
||||
b := &Bucket{
|
||||
Slug: "TestBucket",
|
||||
client: &Client{
|
||||
Packer: mockService,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
b.Iteration, err = NewIteration(IterationOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
b.Iteration.expectedBuilds = append(b.Iteration.expectedBuilds, "happycloud.image")
|
||||
mockService.ExistingBuilds = append(mockService.ExistingBuilds, "happycloud.image")
|
||||
|
||||
err = b.Initialize(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
err = b.PopulateIteration(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
if mockService.CreateBucketCalled {
|
||||
t.Errorf("unexpected call to CreateBucket")
|
||||
}
|
||||
|
||||
if !mockService.UpdateBucketCalled {
|
||||
t.Errorf("expected call to UpdateBucket but it didn't happen")
|
||||
}
|
||||
|
||||
if mockService.CreateIterationCalled {
|
||||
t.Errorf("unexpected a call to CreateIteration")
|
||||
}
|
||||
|
||||
if !mockService.GetIterationCalled {
|
||||
t.Errorf("expected a call to GetIteration but it didn't happen")
|
||||
}
|
||||
|
||||
if mockService.CreateBuildCalled {
|
||||
t.Errorf("unexpected a call to CreateBuild")
|
||||
}
|
||||
|
||||
if b.Iteration.ID != "iteration-id" {
|
||||
t.Errorf("expected an iteration to created but it didn't")
|
||||
}
|
||||
|
||||
err = b.PopulateIteration(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
loadedBuild, ok := b.Iteration.builds.Load("happycloud.image")
|
||||
if !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
existingBuild, ok := loadedBuild.(*Build)
|
||||
if !ok {
|
||||
t.Errorf("expected the existing build loaded from an existing bucket to be valid")
|
||||
}
|
||||
|
||||
if existingBuild.Status != models.HashicorpCloudPackerBuildStatusUNSET {
|
||||
t.Errorf("expected the existing build to be in the default state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitialize_ExistingBucketCompleteIteration(t *testing.T) {
|
||||
//nolint:errcheck
|
||||
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
|
||||
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
|
||||
mockService := NewMockPackerClientService()
|
||||
mockService.BucketAlreadyExist = true
|
||||
mockService.IterationAlreadyExist = true
|
||||
mockService.IterationCompleted = true
|
||||
mockService.BuildAlreadyDone = true
|
||||
|
||||
b := &Bucket{
|
||||
Slug: "TestBucket",
|
||||
client: &Client{
|
||||
Packer: mockService,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
b.Iteration, err = NewIteration(IterationOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
b.Iteration.expectedBuilds = append(b.Iteration.expectedBuilds, "happycloud.image")
|
||||
mockService.ExistingBuilds = append(mockService.ExistingBuilds, "happycloud.image")
|
||||
|
||||
err = b.Initialize(context.TODO())
|
||||
if err == nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
if mockService.CreateIterationCalled {
|
||||
t.Errorf("unexpected call to CreateIteration")
|
||||
}
|
||||
|
||||
if !mockService.GetIterationCalled {
|
||||
t.Errorf("expected a call to GetIteration but it didn't happen")
|
||||
}
|
||||
|
||||
if mockService.CreateBuildCalled {
|
||||
t.Errorf("unexpected call to CreateBuild")
|
||||
}
|
||||
|
||||
if b.Iteration.ID != "iteration-id" {
|
||||
t.Errorf("expected an iteration to be returned but it wasn't")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBuildStatus(t *testing.T) {
|
||||
//nolint:errcheck
|
||||
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
|
||||
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
|
||||
mockService := NewMockPackerClientService()
|
||||
mockService.BucketAlreadyExist = true
|
||||
mockService.IterationAlreadyExist = true
|
||||
|
||||
b := &Bucket{
|
||||
Slug: "TestBucket",
|
||||
client: &Client{
|
||||
Packer: mockService,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
b.Iteration, err = NewIteration(IterationOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
b.Iteration.expectedBuilds = append(b.Iteration.expectedBuilds, "happycloud.image")
|
||||
mockService.ExistingBuilds = append(mockService.ExistingBuilds, "happycloud.image")
|
||||
|
||||
err = b.Initialize(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
err = b.PopulateIteration(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
loadedBuild, ok := b.Iteration.builds.Load("happycloud.image")
|
||||
if !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
existingBuild, ok := loadedBuild.(*Build)
|
||||
if !ok {
|
||||
t.Errorf("expected the existing build loaded from an existing bucket to be valid")
|
||||
}
|
||||
|
||||
if existingBuild.Status != models.HashicorpCloudPackerBuildStatusUNSET {
|
||||
t.Errorf("expected the existing build to be in the default state")
|
||||
}
|
||||
|
||||
err = b.UpdateBuildStatus(context.TODO(), "happycloud.image", models.HashicorpCloudPackerBuildStatusRUNNING)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure for PublishBuildStatus: %v", err)
|
||||
}
|
||||
|
||||
reloadedBuild, ok := b.Iteration.builds.Load("happycloud.image")
|
||||
if !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
existingBuild, ok = reloadedBuild.(*Build)
|
||||
if !ok {
|
||||
t.Errorf("expected the existing build loaded from an existing bucket to be valid")
|
||||
}
|
||||
|
||||
if existingBuild.Status != models.HashicorpCloudPackerBuildStatusRUNNING {
|
||||
t.Errorf("expected the existing build to be in the running state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBuildStatus_DONENoImages(t *testing.T) {
|
||||
//nolint:errcheck
|
||||
os.Setenv("HCP_PACKER_BUILD_FINGEPRINT", "testnumber")
|
||||
defer os.Unsetenv("HCP_PACKER_BUILD_FINGERPRINT")
|
||||
mockService := NewMockPackerClientService()
|
||||
mockService.BucketAlreadyExist = true
|
||||
mockService.IterationAlreadyExist = true
|
||||
|
||||
b := &Bucket{
|
||||
Slug: "TestBucket",
|
||||
client: &Client{
|
||||
Packer: mockService,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
b.Iteration, err = NewIteration(IterationOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
b.Iteration.expectedBuilds = append(b.Iteration.expectedBuilds, "happycloud.image")
|
||||
mockService.ExistingBuilds = append(mockService.ExistingBuilds, "happycloud.image")
|
||||
|
||||
err = b.Initialize(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
err = b.PopulateIteration(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected failure: %v", err)
|
||||
}
|
||||
|
||||
loadedBuild, ok := b.Iteration.builds.Load("happycloud.image")
|
||||
if !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
existingBuild, ok := loadedBuild.(*Build)
|
||||
if !ok {
|
||||
t.Errorf("expected the existing build loaded from an existing bucket to be valid")
|
||||
}
|
||||
|
||||
if existingBuild.Status != models.HashicorpCloudPackerBuildStatusUNSET {
|
||||
t.Errorf("expected the existing build to be in the default state")
|
||||
}
|
||||
|
||||
//nolint:errcheck
|
||||
b.UpdateBuildStatus(context.TODO(), "happycloud.image", models.HashicorpCloudPackerBuildStatusRUNNING)
|
||||
|
||||
err = b.UpdateBuildStatus(context.TODO(), "happycloud.image", models.HashicorpCloudPackerBuildStatusDONE)
|
||||
if err == nil {
|
||||
t.Errorf("expected failure for PublishBuildStatus when setting status to DONE with no images")
|
||||
}
|
||||
|
||||
reloadedBuild, ok := b.Iteration.builds.Load("happycloud.image")
|
||||
if !ok {
|
||||
t.Errorf("expected a basic build entry to be created but it didn't")
|
||||
}
|
||||
|
||||
existingBuild, ok = reloadedBuild.(*Build)
|
||||
if !ok {
|
||||
t.Errorf("expected the existing build loaded from an existing bucket to be valid")
|
||||
}
|
||||
|
||||
if existingBuild.Status != models.HashicorpCloudPackerBuildStatusRUNNING {
|
||||
t.Errorf("expected the existing build to be in the running state")
|
||||
}
|
||||
}
|
||||
|
||||
//func (b *Bucket) PublishBuildStatus(ctx context.Context, name string, status models.HashicorpCloudPackerBuildStatus) error {}
|
||||
Loading…
Reference in new issue