Skip to content

Commit 3bb3702

Browse files
committed
Update ReadFromHCLBuildBlock to use the hcp_packer_registry.Description
In packer v1.8.5, the bucket's description was not properly set in the bucket object we use for HCP, therefore all the buckets created by Packer did not have their description updated. Before the change ``` --- FAIL: TestReadFromHCLBuildBlock (0.00s) --- FAIL: TestReadFromHCLBuildBlock/configure_bucket_using_only_hcp_packer_registry_block (0.00s) types.bucket_test.go:380: expected the build to to have contents of hcp_packer_registry block but it does not: &registry.Bucket{ Slug: "hcp_packer_registry-block-test", - Description: "", + Description: "description from hcp_packer_registry block", Destination: "", BucketLabels: {"org": "test"}, ... // 5 identical fields } FAIL FAIL github.com/hashicorp/packer/internal/hcp/registry 1.072s FAIL ``` After Change ``` ~> go test ./... ? github.com/hashicorp/packer/internal/hcp/api [no test files] ok github.com/hashicorp/packer/internal/hcp/env (cached) ok github.com/hashicorp/packer/internal/hcp/registry 1.130s ```
1 parent 1feff93 commit 3bb3702

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

internal/hcp/registry/types.bucket.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ func (b *Bucket) ReadFromHCLBuildBlock(hcpBlock *hcl2template.BuildBlock) {
6868
if b == nil {
6969
return
7070
}
71-
b.Description = hcpBlock.Description
7271

7372
hcp := hcpBlock.HCPPackerRegistry
7473
if hcp == nil {
7574
return
7675
}
7776

77+
b.Description = hcp.Description
7878
b.BucketLabels = hcp.BucketLabels
7979
b.BuildLabels = hcp.BuildLabels
8080
// If there's already a Slug this was set from env variable.

internal/hcp/registry/types.bucket_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
9+
"github.com/hashicorp/packer/hcl2template"
910
"github.com/hashicorp/packer/internal/hcp/api"
1011
)
1112

@@ -333,3 +334,51 @@ func TestBucket_PopulateIteration(t *testing.T) {
333334
})
334335
}
335336
}
337+
338+
func TestReadFromHCLBuildBlock(t *testing.T) {
339+
tc := []struct {
340+
desc string
341+
buildBlock *hcl2template.BuildBlock
342+
expectedBucket *Bucket
343+
}{
344+
{
345+
desc: "configure bucket using only hcp_packer_registry block",
346+
buildBlock: &hcl2template.BuildBlock{
347+
HCPPackerRegistry: &hcl2template.HCPPackerRegistryBlock{
348+
Slug: "hcp_packer_registry-block-test",
349+
Description: "description from hcp_packer_registry block",
350+
BucketLabels: map[string]string{
351+
"org": "test",
352+
},
353+
BuildLabels: map[string]string{
354+
"version": "1.7.0",
355+
"based_off": "alpine",
356+
},
357+
},
358+
},
359+
expectedBucket: &Bucket{
360+
Slug: "hcp_packer_registry-block-test",
361+
Description: "description from hcp_packer_registry block",
362+
BucketLabels: map[string]string{
363+
"org": "test",
364+
},
365+
BuildLabels: map[string]string{
366+
"version": "1.7.0",
367+
"based_off": "alpine",
368+
},
369+
},
370+
},
371+
}
372+
for _, tt := range tc {
373+
tt := tt
374+
t.Run(tt.desc, func(t *testing.T) {
375+
bucket := &Bucket{}
376+
bucket.ReadFromHCLBuildBlock(tt.buildBlock)
377+
378+
diff := cmp.Diff(bucket, tt.expectedBucket, cmp.AllowUnexported(Bucket{}))
379+
if diff != "" {
380+
t.Errorf("expected the build to to have contents of hcp_packer_registry block but it does not: %v", diff)
381+
}
382+
})
383+
}
384+
}

0 commit comments

Comments
 (0)