2019-04-19 03:10:48 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-02 08:41:44 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-04-19 03:10:48 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2019-05-02 08:41:44 -04:00
|
|
|
// NewParallelTestBuilder will return a New ParallelTestBuilder whose first run
|
|
|
|
// will lock until unlockOnce is closed and that will unlock after `runs`
|
|
|
|
// builds
|
|
|
|
func NewParallelTestBuilder(runs int) *ParallelTestBuilder {
|
|
|
|
pb := &ParallelTestBuilder{
|
|
|
|
unlockOnce: make(chan interface{}),
|
|
|
|
}
|
|
|
|
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
|
|
|
once sync.Once
|
|
|
|
unlockOnce chan interface{}
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
2019-04-19 03:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ParallelTestBuilder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ParallelTestBuilder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2019-05-02 08:41:44 -04:00
|
|
|
b.once.Do(func() {
|
|
|
|
ui.Say("locking build")
|
|
|
|
<-b.unlockOnce
|
|
|
|
b.wg.Add(1) // avoid a panic
|
|
|
|
})
|
|
|
|
|
|
|
|
ui.Say("building")
|
2019-04-19 03:10:48 -04:00
|
|
|
b.wg.Done()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// testMetaFile creates a Meta object that includes a file builder
|
|
|
|
func testMetaParallel(t *testing.T, builder *ParallelTestBuilder) Meta {
|
|
|
|
var out, err bytes.Buffer
|
|
|
|
return Meta{
|
|
|
|
CoreConfig: &packer.CoreConfig{
|
|
|
|
Components: packer.ComponentFinder{
|
|
|
|
Builder: func(n string) (packer.Builder, error) {
|
|
|
|
switch n {
|
|
|
|
case "parallel-test":
|
|
|
|
return builder, nil
|
|
|
|
default:
|
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ui: &packer.BasicUi{
|
|
|
|
Writer: &out,
|
|
|
|
ErrorWriter: &err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildParallel(t *testing.T) {
|
2019-05-02 08:41:44 -04:00
|
|
|
// testfile that running 6 builds, with first one locks 'forever', other
|
|
|
|
// builds should go through.
|
|
|
|
b := NewParallelTestBuilder(5)
|
2019-04-19 03:10:48 -04:00
|
|
|
|
|
|
|
c := &BuildCommand{
|
|
|
|
Meta: testMetaParallel(t, b),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2019-05-02 08:41:44 -04:00
|
|
|
fmt.Sprintf("-parallel=2"),
|
2019-04-19 03:10:48 -04:00
|
|
|
filepath.Join(testFixture("parallel"), "template.json"),
|
|
|
|
}
|
|
|
|
|
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 08:41:44 -04:00
|
|
|
b.wg.Wait() // ran 5 times
|
|
|
|
close(b.unlockOnce) // unlock locking one
|
|
|
|
wg.Wait() // wait for termination
|
2019-04-19 03:10:48 -04:00
|
|
|
}
|