Skip to content

Commit

Permalink
Update ReadFromHCLBuildBlock to use the hcp_packer_registry.Description
Browse files Browse the repository at this point in the history
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

```
  • Loading branch information
nywilken committed Jan 30, 2023
1 parent 1feff93 commit d880d1b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/hcp/registry/types.bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ func (b *Bucket) Validate() error {
}

// ReadFromHCLBuildBlock reads the information for initialising a Bucket from a HCL2 build block
func (b *Bucket) ReadFromHCLBuildBlock(hcpBlock *hcl2template.BuildBlock) {
func (b *Bucket) ReadFromHCLBuildBlock(build *hcl2template.BuildBlock) {
if b == nil {
return
}
b.Description = hcpBlock.Description

hcp := hcpBlock.HCPPackerRegistry
if hcp == nil {
registryBlock := build.HCPPackerRegistry
if registryBlock == nil {
return
}

b.BucketLabels = hcp.BucketLabels
b.BuildLabels = hcp.BuildLabels
b.Description = registryBlock.Description
b.BucketLabels = registryBlock.BucketLabels
b.BuildLabels = registryBlock.BuildLabels
// If there's already a Slug this was set from env variable.
// In Packer, env variable overrides config values so we keep it that way for consistency.
if b.Slug == "" && hcp.Slug != "" {
b.Slug = hcp.Slug
if b.Slug == "" && registryBlock.Slug != "" {
b.Slug = registryBlock.Slug
}
}

Expand Down
49 changes: 49 additions & 0 deletions internal/hcp/registry/types.bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/packer/hcl2template"
"github.com/hashicorp/packer/internal/hcp/api"
)

Expand Down Expand Up @@ -333,3 +334,51 @@ func TestBucket_PopulateIteration(t *testing.T) {
})
}
}

func TestReadFromHCLBuildBlock(t *testing.T) {
tc := []struct {
desc string
buildBlock *hcl2template.BuildBlock
expectedBucket *Bucket
}{
{
desc: "configure bucket using only hcp_packer_registry block",
buildBlock: &hcl2template.BuildBlock{
HCPPackerRegistry: &hcl2template.HCPPackerRegistryBlock{
Slug: "hcp_packer_registry-block-test",
Description: "description from hcp_packer_registry block",
BucketLabels: map[string]string{
"org": "test",
},
BuildLabels: map[string]string{
"version": "1.7.0",
"based_off": "alpine",
},
},
},
expectedBucket: &Bucket{
Slug: "hcp_packer_registry-block-test",
Description: "description from hcp_packer_registry block",
BucketLabels: map[string]string{
"org": "test",
},
BuildLabels: map[string]string{
"version": "1.7.0",
"based_off": "alpine",
},
},
},
}
for _, tt := range tc {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
bucket := &Bucket{}
bucket.ReadFromHCLBuildBlock(tt.buildBlock)

diff := cmp.Diff(bucket, tt.expectedBucket, cmp.AllowUnexported(Bucket{}))
if diff != "" {
t.Errorf("expected the build to to have contents of hcp_packer_registry block but it does not: %v", diff)
}
})
}
}

0 comments on commit d880d1b

Please sign in to comment.