2022-02-14 20:26:57 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-07 18:29:51 +00:00
|
|
|
"github.com/hashicorp/vault/version"
|
2022-02-14 20:26:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestVersionStore_StoreMultipleVaultVersions writes multiple versions of 1.9.0 and verifies that only
|
|
|
|
// the original timestamp is stored.
|
|
|
|
func TestVersionStore_StoreMultipleVaultVersions(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
upgradeTimePlusEpsilon := time.Now().UTC()
|
2022-04-19 18:28:08 +00:00
|
|
|
vaultVersion := &VaultVersion{
|
|
|
|
Version: version.Version,
|
|
|
|
TimestampInstalled: upgradeTimePlusEpsilon.Add(30 * time.Hour),
|
|
|
|
}
|
|
|
|
wasStored, err := c.storeVersionEntry(context.Background(), vaultVersion, false)
|
2022-02-14 20:26:57 +00:00
|
|
|
if err != nil || wasStored {
|
|
|
|
t.Fatalf("vault version was re-stored: %v, err is: %s", wasStored, err.Error())
|
|
|
|
}
|
2022-04-19 18:28:08 +00:00
|
|
|
versionEntry, ok := c.versionHistory[version.Version]
|
2022-02-14 20:26:57 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("no %s version timestamp found", version.Version)
|
|
|
|
}
|
2022-04-19 18:28:08 +00:00
|
|
|
if versionEntry.TimestampInstalled.After(upgradeTimePlusEpsilon) {
|
|
|
|
t.Fatalf("upgrade time for %s is incorrect: got %+v, expected less than %+v", version.Version, versionEntry.TimestampInstalled, upgradeTimePlusEpsilon)
|
2022-02-14 20:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestVersionStore_GetOldestVersion verifies that FindOldestVersionTimestamp finds the oldest
|
|
|
|
// (in time) vault version stored.
|
|
|
|
func TestVersionStore_GetOldestVersion(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
upgradeTimePlusEpsilon := time.Now().UTC()
|
|
|
|
|
|
|
|
// 1.6.2 is stored before 1.6.1, so even though it is a higher number, it should be returned.
|
2022-04-19 18:28:08 +00:00
|
|
|
versionEntries := []VaultVersion{
|
|
|
|
{Version: "1.6.2", TimestampInstalled: upgradeTimePlusEpsilon.Add(-4 * time.Hour)},
|
|
|
|
{Version: "1.6.1", TimestampInstalled: upgradeTimePlusEpsilon.Add(2 * time.Hour)},
|
2022-02-14 20:26:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 23:06:02 +00:00
|
|
|
for _, entry := range versionEntries {
|
2022-04-19 18:28:08 +00:00
|
|
|
_, err := c.storeVersionEntry(context.Background(), &entry, false)
|
2022-02-14 20:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write version entry %#v, err: %s", entry, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
err := c.loadVersionHistory(c.activeContext)
|
2022-02-14 20:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to populate version history cache, err: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
if len(c.versionHistory) != 3 {
|
|
|
|
t.Fatalf("expected 3 entries in timestamps map after refresh, found: %d", len(c.versionHistory))
|
2022-02-14 20:26:57 +00:00
|
|
|
}
|
|
|
|
v, tm, err := c.FindOldestVersionTimestamp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if v != "1.6.2" {
|
|
|
|
t.Fatalf("expected 1.6.2, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 22:16:51 +00:00
|
|
|
// TestVersionStore_GetNewestVersion verifies that FindNewestVersionTimestamp finds the newest
|
|
|
|
// (in time) vault version stored.
|
|
|
|
func TestVersionStore_GetNewestVersion(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
upgradeTimePlusEpsilon := time.Now().UTC()
|
|
|
|
|
|
|
|
// 1.6.1 is stored after 1.6.2, so even though it is a lower number, it should be returned.
|
2022-04-19 18:28:08 +00:00
|
|
|
versionEntries := []VaultVersion{
|
|
|
|
{Version: "1.6.2", TimestampInstalled: upgradeTimePlusEpsilon.Add(-4 * time.Hour)},
|
|
|
|
{Version: "1.6.1", TimestampInstalled: upgradeTimePlusEpsilon.Add(2 * time.Hour)},
|
2022-03-04 22:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range versionEntries {
|
2022-04-19 18:28:08 +00:00
|
|
|
_, err := c.storeVersionEntry(context.Background(), &entry, false)
|
2022-03-04 22:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write version entry %#v, err: %s", entry, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
err := c.loadVersionHistory(c.activeContext)
|
2022-03-04 22:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to populate version history cache, err: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
if len(c.versionHistory) != 3 {
|
|
|
|
t.Fatalf("expected 3 entries in timestamps map after refresh, found: %d", len(c.versionHistory))
|
2022-03-04 22:16:51 +00:00
|
|
|
}
|
|
|
|
v, tm, err := c.FindNewestVersionTimestamp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if v != "1.6.1" {
|
|
|
|
t.Fatalf("expected 1.6.1, found: %s", v)
|
|
|
|
}
|
|
|
|
if tm.Before(upgradeTimePlusEpsilon.Add(1*time.Hour)) || tm.After(upgradeTimePlusEpsilon.Add(3*time.Hour)) {
|
|
|
|
t.Fatalf("incorrect upgrade time logged: %v", tm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 20:26:57 +00:00
|
|
|
func TestVersionStore_SelfHealUTC(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
estLoc, err := time.LoadLocation("EST")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to load location, err: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nowEST := time.Now().In(estLoc)
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
versionEntries := []VaultVersion{
|
|
|
|
{Version: "1.9.0", TimestampInstalled: nowEST.Add(24 * time.Hour)},
|
|
|
|
{Version: "1.9.1", TimestampInstalled: nowEST.Add(48 * time.Hour)},
|
2022-02-14 20:26:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 23:06:02 +00:00
|
|
|
for _, entry := range versionEntries {
|
2022-04-19 18:28:08 +00:00
|
|
|
_, err := c.storeVersionEntry(context.Background(), &entry, false)
|
2022-02-14 20:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write version entry %#v, err: %s", entry, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
err = c.loadVersionHistory(c.activeContext)
|
2022-02-14 20:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to load version timestamps, err: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:28:08 +00:00
|
|
|
for _, entry := range c.versionHistory {
|
|
|
|
if entry.TimestampInstalled.Location() != time.UTC {
|
|
|
|
t.Fatalf("failed to convert %s timestamp %s to UTC", entry.Version, entry.TimestampInstalled)
|
2022-02-14 20:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|