2021-10-14 16:10:59 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-11-03 16:50:12 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/sdk/version"
|
2021-10-14 16:10:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestStoreMultipleVaultVersions writes multiple versions of 1.9.0 and verifies that only
|
|
|
|
// the original timestamp is stored.
|
|
|
|
func TestStoreMultipleVaultVersions(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
upgradeTimePlusEpsilon := time.Now()
|
2021-11-03 16:50:12 +00:00
|
|
|
wasStored, err := c.StoreVersionTimestamp(context.Background(), version.Version, upgradeTimePlusEpsilon.Add(30*time.Hour))
|
2021-10-14 16:10:59 +00:00
|
|
|
if err != nil || wasStored {
|
|
|
|
t.Fatalf("vault version was re-stored: %v, err is: %s", wasStored, err.Error())
|
|
|
|
}
|
2021-11-03 16:50:12 +00:00
|
|
|
upgradeTime, ok := c.VersionTimestamps[version.Version]
|
2021-10-14 16:10:59 +00:00
|
|
|
if !ok {
|
2021-11-03 16:50:12 +00:00
|
|
|
t.Fatalf("no %s version timestamp found", version.Version)
|
2021-10-14 16:10:59 +00:00
|
|
|
}
|
|
|
|
if upgradeTime.After(upgradeTimePlusEpsilon) {
|
2021-11-03 16:50:12 +00:00
|
|
|
t.Fatalf("upgrade time for %s is incorrect: got %+v, expected less than %+v", version.Version, upgradeTime, upgradeTimePlusEpsilon)
|
2021-10-14 16:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGetOldestVersion verifies that FindOldestVersionTimestamp finds the oldest
|
2021-11-03 16:50:12 +00:00
|
|
|
// (in time) vault version stored.
|
2021-10-14 16:10:59 +00:00
|
|
|
func TestGetOldestVersion(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
upgradeTimePlusEpsilon := time.Now()
|
2021-11-03 16:50:12 +00:00
|
|
|
|
2021-10-14 16:10:59 +00:00
|
|
|
c.StoreVersionTimestamp(context.Background(), "1.9.1", upgradeTimePlusEpsilon.Add(-4*time.Hour))
|
|
|
|
c.StoreVersionTimestamp(context.Background(), "1.9.2", upgradeTimePlusEpsilon.Add(2*time.Hour))
|
|
|
|
c.HandleLoadVersionTimestamps(c.activeContext)
|
|
|
|
if len(c.VersionTimestamps) != 3 {
|
|
|
|
t.Fatalf("expected 3 entries in timestamps map after refresh, found: %d", len(c.VersionTimestamps))
|
|
|
|
}
|
|
|
|
v, tm, err := c.FindOldestVersionTimestamp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if v != "1.9.1" {
|
|
|
|
t.Fatalf("expected 1.9.1, found: %s", v)
|
|
|
|
}
|
|
|
|
if tm.Before(upgradeTimePlusEpsilon.Add(-6*time.Hour)) || tm.After(upgradeTimePlusEpsilon.Add(-2*time.Hour)) {
|
|
|
|
t.Fatalf("incorrect upgrade time logged: %v", tm)
|
|
|
|
}
|
|
|
|
}
|