2015-03-17 23:16:04 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-08 20:26:13 +00:00
|
|
|
"context"
|
2015-04-02 05:11:48 +00:00
|
|
|
"sync"
|
2015-03-17 23:16:04 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-06-30 19:38:32 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-01-09 00:48:57 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
2015-03-17 23:16:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// mockRollback returns a mock rollback manager
|
|
|
|
func mockRollback(t *testing.T) (*RollbackManager, *NoopBackend) {
|
|
|
|
backend := new(NoopBackend)
|
|
|
|
mounts := new(MountTable)
|
|
|
|
router := NewRouter()
|
2018-08-01 02:45:38 +00:00
|
|
|
core, _, _ := TestCoreUnsealed(t)
|
2015-03-17 23:16:04 +00:00
|
|
|
|
2017-01-07 23:18:22 +00:00
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "logical/")
|
|
|
|
|
2015-03-17 23:16:04 +00:00
|
|
|
mounts.Entries = []*MountEntry{
|
2021-04-08 16:43:39 +00:00
|
|
|
{
|
2018-09-18 03:03:00 +00:00
|
|
|
Path: "foo",
|
|
|
|
NamespaceID: namespace.RootNamespaceID,
|
2018-11-05 16:11:32 +00:00
|
|
|
namespace: namespace.RootNamespace,
|
2015-03-17 23:16:04 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-13 18:40:08 +00:00
|
|
|
meUUID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2018-11-05 16:11:32 +00:00
|
|
|
if err := router.Mount(backend, "foo", &MountEntry{UUID: meUUID, Accessor: "noopaccessor", NamespaceID: namespace.RootNamespaceID, namespace: namespace.RootNamespace}, view); err != nil {
|
2015-03-17 23:16:04 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-11-15 22:32:57 +00:00
|
|
|
mountsFunc := func() []*MountEntry {
|
|
|
|
return mounts.Entries
|
2015-11-11 16:44:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Trace)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2018-08-01 02:45:38 +00:00
|
|
|
rb := NewRollbackManager(context.Background(), logger, mountsFunc, router, core)
|
2015-04-02 05:11:48 +00:00
|
|
|
rb.period = 10 * time.Millisecond
|
|
|
|
return rb, backend
|
2015-03-17 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRollbackManager(t *testing.T) {
|
|
|
|
m, backend := mockRollback(t)
|
|
|
|
if len(backend.Paths) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", backend)
|
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
m.Start()
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-03-17 23:16:04 +00:00
|
|
|
m.Stop()
|
|
|
|
|
|
|
|
count := len(backend.Paths)
|
|
|
|
if count == 0 {
|
|
|
|
t.Fatalf("bad: %#v", backend)
|
|
|
|
}
|
|
|
|
if backend.Paths[0] != "" {
|
|
|
|
t.Fatalf("bad: %#v", backend)
|
|
|
|
}
|
|
|
|
|
2015-04-02 05:11:48 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-03-17 23:16:04 +00:00
|
|
|
|
|
|
|
if count != len(backend.Paths) {
|
|
|
|
t.Fatalf("should stop requests: %#v", backend)
|
|
|
|
}
|
|
|
|
}
|
2015-04-02 05:11:48 +00:00
|
|
|
|
|
|
|
func TestRollbackManager_Join(t *testing.T) {
|
|
|
|
m, backend := mockRollback(t)
|
|
|
|
if len(backend.Paths) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Start()
|
|
|
|
defer m.Stop()
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(3)
|
|
|
|
|
2017-11-06 19:34:27 +00:00
|
|
|
errCh := make(chan error, 3)
|
2015-04-02 05:11:48 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-11-05 16:11:32 +00:00
|
|
|
err := m.Rollback(namespace.RootContext(nil), "foo")
|
2015-04-02 05:11:48 +00:00
|
|
|
if err != nil {
|
2017-11-06 19:34:27 +00:00
|
|
|
errCh <- err
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-11-05 16:11:32 +00:00
|
|
|
err := m.Rollback(namespace.RootContext(nil), "foo")
|
2015-04-02 05:11:48 +00:00
|
|
|
if err != nil {
|
2017-11-06 19:34:27 +00:00
|
|
|
errCh <- err
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-11-05 16:11:32 +00:00
|
|
|
err := m.Rollback(namespace.RootContext(nil), "foo")
|
2015-04-02 05:11:48 +00:00
|
|
|
if err != nil {
|
2017-11-06 19:34:27 +00:00
|
|
|
errCh <- err
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2017-11-06 19:34:27 +00:00
|
|
|
close(errCh)
|
|
|
|
err := <-errCh
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error on rollback:%v", err)
|
|
|
|
}
|
2015-04-02 05:11:48 +00:00
|
|
|
}
|