/
force_unlock_test.go
59 lines (50 loc) · 1.68 KB
/
force_unlock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package e2etest
import (
"context"
"errors"
"testing"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-exec/tfexec"
)
// LockID set in the test fixture
const inmemLockID = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
var forceUnlockDirArgMaxVersion = version.Must(version.NewVersion("0.15.0"))
func TestForceUnlock(t *testing.T) {
runTest(t, "inmem_backend_locked", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init: %v", err)
}
err = tf.ForceUnlock(context.Background(), inmemLockID)
if err != nil {
t.Fatalf("error running ForceUnlock: %v", err)
}
})
runTest(t, "inmem_backend_locked", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
if tfv.GreaterThanOrEqual(forceUnlockDirArgMaxVersion) {
t.Skip("legacy positional path argument deprecated in favor of global -chdir flag")
}
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init: %v", err)
}
err = tf.ForceUnlock(context.Background(), inmemLockID, tfexec.Dir(tf.WorkingDir()))
if err != nil {
t.Fatalf("error running ForceUnlock: %v", err)
}
})
runTest(t, "inmem_backend_locked", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init: %v", err)
}
err = tf.ForceUnlock(context.Background(), "badlockid")
if err == nil {
t.Fatalf("expected error when running ForceUnlock with invalid lock id")
}
var foErr *tfexec.ErrLockIdInvalid
if !errors.As(err, &foErr) {
t.Fatalf("expected ErrLockIdInvalid, %T returned: %s", err, err)
}
})
}