packer-cn/command/build_parallel_test.go

174 lines
4.3 KiB
Go
Raw Normal View History

2019-04-19 03:10:48 -04:00
package command
import (
"bytes"
"context"
"fmt"
"path/filepath"
"sync"
"testing"
"github.com/hashicorp/hcl/v2/hcldec"
2019-05-02 08:41:44 -04:00
"golang.org/x/sync/errgroup"
"github.com/hashicorp/packer/builder/file"
2019-04-19 03:10:48 -04:00
"github.com/hashicorp/packer/packer"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/provisioner/sleep"
2019-04-19 03:10:48 -04:00
)
2019-05-02 09:04:02 -04:00
// NewParallelTestBuilder will return a New ParallelTestBuilder that will
// unlock after `runs` builds
2019-05-02 08:41:44 -04:00
func NewParallelTestBuilder(runs int) *ParallelTestBuilder {
2019-05-02 09:04:02 -04:00
pb := &ParallelTestBuilder{}
2019-05-02 08:41:44 -04:00
pb.wg.Add(runs)
return pb
}
// The ParallelTestBuilder's first run will lock
2019-04-19 03:10:48 -04:00
type ParallelTestBuilder struct {
2019-05-02 08:41:44 -04:00
wg sync.WaitGroup
2019-04-19 03:10:48 -04:00
}
func (b *ParallelTestBuilder) ConfigSpec() hcldec.ObjectSpec { return nil }
func (b *ParallelTestBuilder) Prepare(raws ...interface{}) ([]string, []string, error) {
return nil, nil, nil
}
2019-04-19 03:10:48 -04:00
func (b *ParallelTestBuilder) Run(ctx context.Context, ui packersdk.Ui, hook packer.Hook) (packersdk.Artifact, error) {
2019-05-02 08:41:44 -04:00
ui.Say("building")
2019-04-19 03:10:48 -04:00
b.wg.Done()
return nil, nil
}
2019-05-02 09:04:02 -04:00
// LockedBuilder wont run until unlock is called
type LockedBuilder struct{ unlock chan interface{} }
func (b *LockedBuilder) ConfigSpec() hcldec.ObjectSpec { return nil }
2020-03-17 07:05:37 -04:00
func (b *LockedBuilder) Prepare(raws ...interface{}) ([]string, []string, error) {
return nil, nil, nil
}
2019-05-02 09:04:02 -04:00
func (b *LockedBuilder) Run(ctx context.Context, ui packersdk.Ui, hook packer.Hook) (packersdk.Artifact, error) {
2019-05-02 09:04:02 -04:00
ui.Say("locking build")
select {
case <-b.unlock:
case <-ctx.Done():
return nil, ctx.Err()
}
return nil, nil
}
2019-04-19 03:10:48 -04:00
// testMetaFile creates a Meta object that includes a file builder
2019-05-02 09:04:02 -04:00
func testMetaParallel(t *testing.T, builder *ParallelTestBuilder, locked *LockedBuilder) Meta {
2019-04-19 03:10:48 -04:00
var out, err bytes.Buffer
return Meta{
CoreConfig: &packer.CoreConfig{
Components: packer.ComponentFinder{
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
BuilderStore: packer.MapOfBuilder{
"parallel-test": func() (packer.Builder, error) { return builder, nil },
"file": func() (packer.Builder, error) { return &file.Builder{}, nil },
"lock": func() (packer.Builder, error) { return locked, nil },
2019-04-19 03:10:48 -04:00
},
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
ProvisionerStore: packer.MapOfProvisioner{
"sleep": func() (packer.Provisioner, error) { return &sleep.Provisioner{}, nil },
},
2019-04-19 03:10:48 -04:00
},
},
Ui: &packer.BasicUi{
Writer: &out,
ErrorWriter: &err,
},
}
}
2019-05-02 09:04:02 -04:00
func TestBuildParallel_1(t *testing.T) {
// testfile has 6 builds, with first one locks 'forever', other builds
// should go through.
2019-05-02 08:41:44 -04:00
b := NewParallelTestBuilder(5)
2019-05-02 09:04:02 -04:00
locked := &LockedBuilder{unlock: make(chan interface{})}
2019-04-19 03:10:48 -04:00
c := &BuildCommand{
2019-05-02 09:04:02 -04:00
Meta: testMetaParallel(t, b, locked),
2019-04-19 03:10:48 -04:00
}
args := []string{
2020-05-08 11:46:33 -04:00
fmt.Sprintf("-parallel-builds=10"),
filepath.Join(testFixture("parallel"), "1lock-5wg.json"),
2019-04-19 03:10:48 -04:00
}
2019-05-02 08:41:44 -04:00
wg := errgroup.Group{}
wg.Go(func() error {
2019-04-19 03:10:48 -04:00
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
2019-05-02 08:41:44 -04:00
return nil
})
2019-04-19 03:10:48 -04:00
2019-05-02 09:04:02 -04:00
b.wg.Wait() // ran 5 times
close(locked.unlock) // unlock locking one
wg.Wait() // wait for termination
2019-04-19 03:10:48 -04:00
}
func TestBuildParallel_2(t *testing.T) {
// testfile has 6 builds, 2 of them lock 'forever', other builds
// should go through.
b := NewParallelTestBuilder(4)
locked := &LockedBuilder{unlock: make(chan interface{})}
c := &BuildCommand{
Meta: testMetaParallel(t, b, locked),
}
args := []string{
fmt.Sprintf("-parallel-builds=3"),
2019-05-06 09:47:53 -04:00
filepath.Join(testFixture("parallel"), "2lock-4wg.json"),
}
wg := errgroup.Group{}
wg.Go(func() error {
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
return nil
})
b.wg.Wait() // ran 4 times
close(locked.unlock) // unlock locking one
wg.Wait() // wait for termination
}
func TestBuildParallel_Timeout(t *testing.T) {
// testfile has 6 builds, 1 of them locks 'forever', one locks and times
// out other builds should go through.
b := NewParallelTestBuilder(4)
locked := &LockedBuilder{unlock: make(chan interface{})}
c := &BuildCommand{
Meta: testMetaParallel(t, b, locked),
}
args := []string{
fmt.Sprintf("-parallel-builds=3"),
filepath.Join(testFixture("parallel"), "2lock-timeout.json"),
}
wg := errgroup.Group{}
wg.Go(func() error {
if code := c.Run(args); code == 0 {
fatalCommand(t, c.Meta)
}
return nil
})
b.wg.Wait() // ran 4 times
close(locked.unlock) // unlock locking one
wg.Wait() // wait for termination
}