Files
atlantis/server/events/delete_lock_command_test.go
KevinSnyderCodes 1a8344489f fix: add path to WorkingDir methods (#2180)
* fix: add path to WorkingDir methods

`WorkingDirLocker` and `WorkingDir` are closely related -- the former acquires a lock, which ensures that the latter can safely operate on a given file path.

In #2131 we added `path` to the `WorkingDirLocker` lock key, but neglected to add the same to `WorkingDir`.

This commit adds `path` as an argument to certain `WorkingDir` methods, and includes `path` in the directory that we use to clone the repository for a given project.

Since `path` can include certain special characters such as `/`, we encode `path` as base32 when using it as part of a file path. This should ensure no special characters are used in the filesystem, and that the value can be decoded if desired (unlike hashes such as md5).

Additional changes:

- All calls to changed methods have been updated, including unit tests
- Mocks have been regenerated for `WorkingDir` and `WorkingDirLocker`
- `working_dir_test.go`
  - Commands that operate on filesystem paths have been updated to include the base32 encoded `path` value
  - When running `git init`, we include `-b master` as additional arguments, to ensure that `master` is our default branch (expected by the unit tests)

* Try fixing E2E tests

* Fix DefaultPendingPlanFinder

In addition to iterating over `workspaceDirs`, also iterate over `pathDirs`, and use both `workspace` and `path` to build `repoDir`.

* Fix DefaultPendingPlanFinder unit tests

* Fix DefaultProjectCommandBuilder unit tests

Co-authored-by: Kevin Snyder <kevinsnyder@KevinSnydersMBP.lan>
Co-authored-by: Kevin Snyder <kevinsnyder@ip-192-168-1-188.ec2.internal>
2022-04-08 11:55:35 -07:00

136 lines
4.0 KiB
Go

package events_test
import (
"errors"
"testing"
. "github.com/petergtz/pegomock"
"github.com/runatlantis/atlantis/server/core/db"
lockmocks "github.com/runatlantis/atlantis/server/core/locking/mocks"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
. "github.com/runatlantis/atlantis/testing"
)
func TestDeleteLock_LockerErr(t *testing.T) {
t.Log("If there is an error retrieving the lock, we return the error")
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.Unlock("id")).ThenReturn(nil, errors.New("err"))
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
}
_, err := dlc.DeleteLock("id")
ErrEquals(t, "err", err)
}
func TestDeleteLock_None(t *testing.T) {
t.Log("If there is no lock at that ID we return nil")
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.Unlock("id")).ThenReturn(nil, nil)
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
}
lock, err := dlc.DeleteLock("id")
Ok(t, err)
Assert(t, lock == nil, "lock was not nil")
}
func TestDeleteLock_OldFormat(t *testing.T) {
t.Log("If the lock doesn't have BaseRepo set it is deleted successfully")
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.Unlock("id")).ThenReturn(&models.ProjectLock{}, nil)
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
}
lock, err := dlc.DeleteLock("id")
Ok(t, err)
Assert(t, lock != nil, "lock was nil")
}
func TestDeleteLock_Success(t *testing.T) {
t.Log("Delete lock deletes successfully the working dir")
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.Unlock("id")).ThenReturn(&models.ProjectLock{}, nil)
workingDir := events.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
pull := models.PullRequest{
BaseRepo: models.Repo{FullName: "owner/repo"},
}
When(l.Unlock("id")).ThenReturn(&models.ProjectLock{
Pull: pull,
Workspace: "workspace",
Project: models.Project{
Path: "path",
RepoFullName: "owner/repo",
},
}, nil)
tmp, cleanup := TempDir(t)
defer cleanup()
db, err := db.New(tmp)
Ok(t, err)
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
DB: db,
WorkingDirLocker: workingDirLocker,
WorkingDir: workingDir,
}
lock, err := dlc.DeleteLock("id")
Ok(t, err)
Assert(t, lock != nil, "lock was nil")
workingDir.VerifyWasCalledOnce().DeleteForWorkspace(pull.BaseRepo, pull, "workspace", "path")
}
func TestDeleteLocksByPull_LockerErr(t *testing.T) {
t.Log("If there is an error retrieving the lock, returned a failed status")
repoName := "reponame"
pullNum := 2
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.UnlockByPull(repoName, pullNum)).ThenReturn(nil, errors.New("err"))
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
}
_, err := dlc.DeleteLocksByPull(repoName, pullNum)
ErrEquals(t, "err", err)
}
func TestDeleteLocksByPull_None(t *testing.T) {
t.Log("If there is no lock at that ID there is no error")
repoName := "reponame"
pullNum := 2
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.UnlockByPull(repoName, pullNum)).ThenReturn([]models.ProjectLock{}, nil)
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
}
_, err := dlc.DeleteLocksByPull(repoName, pullNum)
Ok(t, err)
}
func TestDeleteLocksByPull_OldFormat(t *testing.T) {
t.Log("If the lock doesn't have BaseRepo set it is deleted successfully")
repoName := "reponame"
pullNum := 2
RegisterMockTestingT(t)
l := lockmocks.NewMockLocker()
When(l.UnlockByPull(repoName, pullNum)).ThenReturn([]models.ProjectLock{{}}, nil)
dlc := events.DefaultDeleteLockCommand{
Locker: l,
Logger: logging.NewNoopLogger(t),
}
_, err := dlc.DeleteLocksByPull(repoName, pullNum)
Ok(t, err)
}