Skip to content

Commit

Permalink
add tests for ForceUnlock
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Aug 19, 2022
1 parent 28ec186 commit 4ace2d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tfexec/exit_errors.go
Expand Up @@ -46,6 +46,7 @@ var (
statePlanReadErrRegexp = regexp.MustCompile(
`Terraform couldn't read the given file as a state or plan file.|` +
`Error: Failed to read the given file as a state or plan file`)
lockIdInvalidErrRegexp = regexp.MustCompile(`Failed to unlock state: Lock ID should be numerical value`)
)

func (tf *Terraform) wrapExitError(ctx context.Context, err error, stderr string) error {
Expand Down Expand Up @@ -160,6 +161,8 @@ func (tf *Terraform) wrapExitError(ctx context.Context, err error, stderr string
}
case statePlanReadErrRegexp.MatchString(stderr):
return &ErrStatePlanRead{stderr: stderr}
case lockIdInvalidErrRegexp.MatchString(stderr):
return &ErrLockIdInvalid{stderr: stderr}
}

return fmt.Errorf("%w\n%s", &unwrapper{exitErr, ctxErr}, stderr)
Expand Down Expand Up @@ -256,6 +259,16 @@ func (e *ErrNoConfig) Error() string {
return e.stderr
}

type ErrLockIdInvalid struct {
unwrapper

stderr string
}

func (e *ErrLockIdInvalid) Error() string {
return e.stderr
}

// ErrCLIUsage is returned when the combination of flags or arguments is incorrect.
//
// CLI indicates usage errors in three different ways: either
Expand Down
41 changes: 41 additions & 0 deletions tfexec/force_unlock_test.go
@@ -0,0 +1,41 @@
package tfexec

import (
"context"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

func TestForceUnlockCmd(t *testing.T) {
td := t.TempDir()

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1_1))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

t.Run("defaults", func(t *testing.T) {
forceUnlockCmd := tf.forceUnlockCmd(context.Background(), "12345")

assertCmd(t, []string{
"force-unlock",
"-no-color",
"-force",
}, nil, forceUnlockCmd)
})

t.Run("override all defaults", func(t *testing.T) {
forceUnlockCmd := tf.forceUnlockCmd(context.Background(), "12345", Dir("mydir"))

assertCmd(t, []string{
"force-unlock",
"-no-color",
"-force",
"mydir",
}, nil, forceUnlockCmd)
})
}
16 changes: 16 additions & 0 deletions tfexec/internal/e2etest/force_unlock_test.go
Expand Up @@ -2,6 +2,7 @@ package e2etest

import (
"context"
"errors"
"testing"

"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -40,4 +41,19 @@ func TestForceUnlock(t *testing.T) {
t.Fatalf("error running ForceUnlock: %v", err)
}
})
runTest(t, "invalid lock id", 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)
}
})
}

0 comments on commit 4ace2d4

Please sign in to comment.