Skip to content

Commit

Permalink
add hcp_packer_registry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss committed Jul 28, 2021
1 parent 7056b2e commit 4d55796
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 36 deletions.
8 changes: 7 additions & 1 deletion hcl2template/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer/builder/null"
. "github.com/hashicorp/packer/hcl2template/internal"
"github.com/hashicorp/packer/internal/packer_registry"
"github.com/hashicorp/packer/packer"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -354,10 +354,16 @@ var cmpOpts = []cmp.Option{
packer.CoreBuildProvisioner{},
packer.CoreBuildPostProcessor{},
null.Builder{},
packer_registry.Bucket{},
packer_registry.Iteration{},
packer.RegistryBuilder{},
),
cmpopts.IgnoreFields(PackerConfig{},
"Cwd", // Cwd will change for every os type
),
cmpopts.IgnoreFields(packer_registry.Iteration{},
"Fingerprint", // Fingerprint will change everytime
),
cmpopts.IgnoreFields(VariableAssignment{},
"Expr", // its an interface
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// starts resources to provision them.
build {
name = "bucket-slug"

hcp_packer_registry {
description = <<EOT
Some description
Expand All @@ -12,7 +14,14 @@ Some description
sources = [
"source.virtualbox-iso.ubuntu-1204",
]

source "source.amazon-ebs.ubuntu-1604" {
name = "aws-ubuntu-16.04"
}
}

source "virtualbox-iso" "ubuntu-1204" {
}

source "amazon-ebs" "ubuntu-1604" {
}
11 changes: 11 additions & 0 deletions hcl2template/testdata/hcp_par/duplicate.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// starts resources to provision them.
build {
name = "bucket-slug"
hcp_packer_registry {
description = ""
labels = {
"foo" = "bar"
}
}
hcp_packer_registry {}
}
12 changes: 12 additions & 0 deletions hcl2template/testdata/hcp_par/empty.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// starts resources to provision them.
build {
name = "bucket-slug"
hcp_packer_registry {}

sources = [
"source.virtualbox-iso.ubuntu-1204",
]
}

source "virtualbox-iso" "ubuntu-1204" {
}
7 changes: 7 additions & 0 deletions hcl2template/testdata/hcp_par/invalid.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// starts resources to provision them.
build {
name = "bucket-slug"
hcp_packer_registry {
labels = ""
}
}
11 changes: 11 additions & 0 deletions hcl2template/testdata/hcp_par/long-description.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// starts resources to provision them.
build {
name = "bucket-slug"
hcp_packer_registry {
description = <<EOT
This is a super super super super super super super super super super super super super super super super super super
super super super super super super super super super super super super super super super super super super super super
super super super long description
EOT
}
}
8 changes: 8 additions & 0 deletions hcl2template/types.build.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block, cfg *PackerConfig) (*BuildB
if moreDiags.HasErrors() {
continue
}
if build.HCPPackerRegistry != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Only one " + buildHCPPackerRegistryLabel + " is allowed"),
Subject: block.DefRange.Ptr(),
})
continue
}
build.HCPPackerRegistry = hcpPackerRegistry
case sourceLabel:
ref, moreDiags := p.decodeBuildSource(block)
Expand Down
10 changes: 10 additions & 0 deletions hcl2template/types.build.hcp_packer_registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hcl2template

import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
packerregistry "github.com/hashicorp/packer/internal/packer_registry"
Expand Down Expand Up @@ -35,6 +36,15 @@ func (p *Parser) decodeHCPRegistry(block *hcl.Block) (*HCPPackerRegistryBlock, h
return nil, diags
}

if len(b.Description) > 255 {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf(buildHCPPackerRegistryLabel +".description should have a maximum length of 255 characters"),
Subject: block.DefRange.Ptr(),
})
return nil, diags
}

par.Description = b.Description
par.Labels = b.Labels

Expand Down
212 changes: 212 additions & 0 deletions hcl2template/types.build.hcp_packer_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package hcl2template

import (
"path/filepath"
"testing"

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/internal/packer_registry"
"github.com/hashicorp/packer/packer"
)

func Test_ParseHCPPackerRegistryBlock(t *testing.T) {
defaultParser := getBasicParser()

tests := []parseTest{
{"complete working build with hcp_packer_registry block",
defaultParser,
parseTestArgs{"testdata/hcp_par/complete.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "hcp_par"),
Sources: map[SourceRef]SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},
refAWSEBSUbuntu1604: {Type: "amazon-ebs", Name: "ubuntu-1604"},
},
Builds: Builds{
&BuildBlock{
Name: "bucket-slug",
HCPPackerRegistry: &HCPPackerRegistryBlock{
Description: "Some description\n",
Labels: map[string]string{"foo": "bar"},
},
Sources: []SourceUseBlock{
{
SourceRef: refVBIsoUbuntu1204,
},
{
SourceRef: SourceRef{Type: "amazon-ebs", Name: "ubuntu-1604"},
LocalName: "aws-ubuntu-16.04",
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
BuildName: "bucket-slug",
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: &packer.RegistryBuilder{
Name: "virtualbox-iso.ubuntu-1204",
Builder: emptyMockBuilder,
ArtifactMetadataPublisher: &packer_registry.Bucket{
Slug: "bucket-slug",
Description: "Some description\n",
Labels: map[string]string{"foo": "bar"},
Iteration: &packer_registry.Iteration{
Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored
},
},
},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{
{
{
PostProcessor: &packer.RegistryPostProcessor{
BuilderType: "virtualbox-iso.ubuntu-1204",
ArtifactMetadataPublisher: &packer_registry.Bucket{
Slug: "bucket-slug",
Description: "Some description\n",
Labels: map[string]string{"foo": "bar"},
Iteration: &packer_registry.Iteration{
Fingerprint: "ignored-fingerprint",
},
},
},
},
},
},
},
&packer.CoreBuild{
BuildName: "bucket-slug",
Type: "amazon-ebs.aws-ubuntu-16.04",
Prepared: true,
Builder: &packer.RegistryBuilder{
Name: "amazon-ebs.aws-ubuntu-16.04",
Builder: emptyMockBuilder,
ArtifactMetadataPublisher: &packer_registry.Bucket{
Slug: "bucket-slug",
Description: "Some description\n",
Labels: map[string]string{"foo": "bar"},
Iteration: &packer_registry.Iteration{
Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored
},
},
},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{
{
{
PostProcessor: &packer.RegistryPostProcessor{
BuilderType: "amazon-ebs.aws-ubuntu-16.04",
ArtifactMetadataPublisher: &packer_registry.Bucket{
Slug: "bucket-slug",
Description: "Some description\n",
Labels: map[string]string{"foo": "bar"},
Iteration: &packer_registry.Iteration{
Fingerprint: "ignored-fingerprint",
},
},
},
},
},
},
},
},
false,
},
{"duplicate hcp_packer_registry blocks",
defaultParser,
parseTestArgs{"testdata/hcp_par/duplicate.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "hcp_par"),
},
true, true,
nil,
false,
},
{"empty hcp_packer_registry block",
defaultParser,
parseTestArgs{"testdata/hcp_par/empty.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "hcp_par"),
Sources: map[SourceRef]SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},
},
Builds: Builds{
&BuildBlock{
Name: "bucket-slug",
HCPPackerRegistry: &HCPPackerRegistryBlock{},
Sources: []SourceUseBlock{
{
SourceRef: refVBIsoUbuntu1204,
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
BuildName: "bucket-slug",
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: &packer.RegistryBuilder{
Name: "virtualbox-iso.ubuntu-1204",
Builder: emptyMockBuilder,
ArtifactMetadataPublisher: &packer_registry.Bucket{
Slug: "bucket-slug",
Iteration: &packer_registry.Iteration{
Fingerprint: "ignored-fingerprint", // this will be different everytime so it's ignored
},
},
},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{
{
{
PostProcessor: &packer.RegistryPostProcessor{
BuilderType: "virtualbox-iso.ubuntu-1204",
ArtifactMetadataPublisher: &packer_registry.Bucket{
Slug: "bucket-slug",
Iteration: &packer_registry.Iteration{
Fingerprint: "ignored-fingerprint",
},
},
},
},
},
},
},
},
false,
},
{"invalid hcp_packer_registry config",
defaultParser,
parseTestArgs{"testdata/hcp_par/invalid.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "hcp_par"),
},
true, true,
nil,
false,
},
{"long hcp_packer_registry.description",
defaultParser,
parseTestArgs{"testdata/hcp_par/long-description.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "hcp_par"),
},
true, true,
nil,
false,
},
}
testParse(t, tests)
}
35 changes: 0 additions & 35 deletions hcl2template/types.build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,41 +421,6 @@ func TestParse_build(t *testing.T) {
},
false,
},
{"hcp packer registry build",
defaultParser,
parseTestArgs{"testdata/build/hcp_packer_registry.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "build"),
Sources: map[SourceRef]SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},
},
Builds: Builds{
&BuildBlock{
HCPPackerRegistry: &HCPPackerRegistryBlock{
Description: "Some description\n",
Labels: map[string]string{"foo": "bar"},
},
Sources: []SourceUseBlock{
{
SourceRef: refVBIsoUbuntu1204,
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "virtualbox-iso.ubuntu-1204",
Prepared: true,
Builder: emptyMockBuilder,
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
},
},
false,
},
}
testParse(t, tests)
}

0 comments on commit 4d55796

Please sign in to comment.