packer/command/build_cancellation_test.go
hashicorp-copywrite[bot] 19055df3ec
[COMPLIANCE] License changes (#12568)
* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-10 15:53:29 -07:00

92 lines
2.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"context"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestBuildCommand_RunContext_CtxCancel(t *testing.T) {
tests := []struct {
name string
args []string
parallelPassingTests int
expected int
}{
{"cancel 1 pending build - parallel=true",
[]string{"-parallel-builds=10", filepath.Join(testFixture("parallel"), "1lock-5wg.json")},
5,
1,
},
{"cancel in the middle with 2 pending builds - parallel=true",
[]string{"-parallel-builds=10", filepath.Join(testFixture("parallel"), "2lock-4wg.json")},
4,
1,
},
{"cancel 1 locked build - debug - parallel=true",
[]string{"-parallel-builds=10", "-debug=true", filepath.Join(testFixture("parallel"), "1lock.json")},
0,
1,
},
{"cancel 2 locked builds - debug - parallel=true",
[]string{"-parallel-builds=10", "-debug=true", filepath.Join(testFixture("parallel"), "2lock.json")},
0,
1,
},
{"cancel 1 locked build - debug - parallel=false",
[]string{"-parallel-builds=1", "-debug=true", filepath.Join(testFixture("parallel"), "1lock.json")},
0,
1,
},
{"cancel 2 locked builds - debug - parallel=false",
[]string{"-parallel-builds=1", "-debug=true", filepath.Join(testFixture("parallel"), "2lock.json")},
0,
1,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
b := NewParallelTestBuilder(tt.parallelPassingTests)
locked := &LockedBuilder{unlock: make(chan interface{})}
c := &BuildCommand{
Meta: testMetaParallel(t, b, locked),
}
ctx, cancelCtx := context.WithCancel(context.Background())
codeC := make(chan int)
go func() {
defer close(codeC)
cfg, ret := c.ParseArgs(tt.args)
if ret != 0 {
t.Error("ParseArgs failed.")
return
}
codeC <- c.RunContext(ctx, cfg)
}()
t.Logf("waiting for passing tests if any")
b.wg.Wait() // ran `tt.parallelPassingTests` times
t.Logf("cancelling context")
cancelCtx()
select {
case code := <-codeC:
if code != tt.expected {
t.Logf("wrong code: %s", cmp.Diff(code, tt.expected))
fatalCommand(t, c.Meta)
}
case <-time.After(15 * time.Second):
t.Fatal("deadlock")
}
})
}
}