go-latestver/internal/database/store_test.go

132 lines
3.8 KiB
Go
Raw Normal View History

2021-11-22 02:39:25 +00:00
package database
import (
"testing"
"time"
)
const sqlliteMemoryDSN = "file::memory:?cache=shared"
func Test_CatalogMetaStorage(t *testing.T) {
ttp := func(v time.Time) *time.Time { return &v }
dbc, err := NewClient("sqlite3", sqlliteMemoryDSN)
if err != nil {
t.Fatalf("unable to create database client: %s", err)
}
var (
ce = CatalogEntry{Name: "testapp", Tag: "latest"}
cm = CatalogMeta{CatalogName: ce.Name, CatalogTag: ce.Tag, CurrentVersion: "1.0.0", LastChecked: ttp(time.Now())}
)
// Empty fetch
fetchedCM, err := dbc.Catalog.GetMeta(&ce)
if err != nil {
t.Fatalf("unable to retrieve catalog meta: %s", err)
}
for name, check := range map[string]bool{
"name match": fetchedCM.CatalogName == ce.Name,
"tag match": fetchedCM.CatalogTag == ce.Tag,
"version empty": fetchedCM.CurrentVersion == "",
"date nil": fetchedCM.LastChecked == nil,
} {
if !check {
t.Errorf("check failed: %s", name)
}
}
// Initial set
if err = dbc.Catalog.PutMeta(&cm); err != nil {
t.Fatalf("unable to store catalog meta: %s", err)
}
fetchedCM, err = dbc.Catalog.GetMeta(&ce)
if err != nil {
t.Fatalf("unable to retrieve catalog meta: %s", err)
}
for name, check := range map[string]bool{
"name match": fetchedCM.CatalogName == ce.Name,
"tag match": fetchedCM.CatalogTag == ce.Tag,
"version match": fetchedCM.CurrentVersion == cm.CurrentVersion,
"date match": fetchedCM.LastChecked.Equal(*cm.LastChecked),
} {
if !check {
t.Errorf("check failed: %s", name)
}
}
// Update
cm.LastChecked = ttp(time.Now().Add(time.Hour)) // Compensate test running quite fast
cm.CurrentVersion = "1.1.0"
if err = dbc.Catalog.PutMeta(&cm); err != nil {
t.Fatalf("unable to update catalog meta: %s", err)
}
fetchedCM, err = dbc.Catalog.GetMeta(&ce)
if err != nil {
t.Fatalf("unable to retrieve catalog meta: %s", err)
}
for name, check := range map[string]bool{
"name match": fetchedCM.CatalogName == ce.Name,
"tag match": fetchedCM.CatalogTag == ce.Tag,
"version match": fetchedCM.CurrentVersion == cm.CurrentVersion,
"date match": fetchedCM.LastChecked.Equal(*cm.LastChecked),
} {
if !check {
t.Errorf("check failed: %s", name)
}
}
}
func Test_LogStorage(t *testing.T) {
dbc, err := NewClient("sqlite3", sqlliteMemoryDSN)
if err != nil {
t.Fatalf("unable to create database client: %s", err)
}
var (
ce = CatalogEntry{Name: "testapp", Tag: "latest"}
rt = time.Now()
)
for _, le := range []LogEntry{
{CatalogName: ce.Name, CatalogTag: ce.Tag, Timestamp: rt.Add(-3 * time.Hour), VersionFrom: "1.0.0", VersionTo: "1.1.0"},
{CatalogName: ce.Name, CatalogTag: ce.Tag, Timestamp: rt.Add(-1 * time.Hour), VersionFrom: "1.2.0", VersionTo: "1.3.0"},
{CatalogName: ce.Name, CatalogTag: ce.Tag, Timestamp: rt.Add(-2 * time.Hour), VersionFrom: "1.1.0", VersionTo: "1.2.0"},
{CatalogName: "anotherapp", CatalogTag: ce.Tag, Timestamp: rt.Add(-2 * time.Hour), VersionFrom: "5.2.0", VersionTo: "5.2.1"},
{CatalogName: "anotherapp", CatalogTag: ce.Tag, Timestamp: rt.Add(-1 * time.Hour), VersionFrom: "5.2.1", VersionTo: "6.0.0"},
} {
Add PR testing, fix linter errors Squashed commit of the following: commit 2a83adf6c54d6abcf6762760fd38f2505511f545 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:34:49 2021 +0100 Lint: Fix copylocks errors Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 418f85d504203a6968329e280ecd9cf7d2365373 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:31:38 2021 +0100 Lint: Fix gosec warnings Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 1a977875740be3c40884aa0985578721ceb4ae37 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:28:02 2021 +0100 Lint: Disable gomnd for certain cases Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 5e81cf79ba7256b321442530715a2b53de0a18e1 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:26:01 2021 +0100 Lint: fix ineffassign errors Signed-off-by: Knut Ahlers <knut@ahlers.me> commit cb14fae2dad985368e1f05d62f8e778817d01c6f Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:23:42 2021 +0100 Lint: Fix revive linter errors Signed-off-by: Knut Ahlers <knut@ahlers.me> commit b3390b8dff9b939caa4e3821a48dd848af0bfba4 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:21:35 2021 +0100 Lint: Remove unrequired dereference Signed-off-by: Knut Ahlers <knut@ahlers.me> commit f9052e6aa530c5b5017249fc6c31bdbb94252760 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:20:43 2021 +0100 Lint: Remove deadcode Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 72b88adaa25a3bb5a7af21da7ed12f08cae36573 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 02:52:27 2021 +0100 Add PR-testing Signed-off-by: Knut Ahlers <knut@ahlers.me> Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-12-01 02:38:52 +00:00
//#nosec G601 // Acceptable for test usage
2021-11-22 02:39:25 +00:00
if err = dbc.Logs.Add(&le); err != nil {
t.Fatalf("unable to add log entry: %s", err)
}
}
logs, err := dbc.Logs.ListForCatalogEntry(&ce, 100, 0)
if err != nil {
t.Fatalf("unable to fetch log entries for entry: %s", err)
}
if c := len(logs); c != 3 {
t.Errorf("got unexpected number of logs for entry: %d != 3", c)
}
if !logs[2].Timestamp.Before(logs[1].Timestamp) || !logs[1].Timestamp.Before(logs[0].Timestamp) {
t.Error("log entries are not sorted descending")
}
logs, err = dbc.Logs.List(100, 0)
if err != nil {
t.Fatalf("unable to fetch log entries: %s", err)
}
if c := len(logs); c != 5 {
t.Errorf("got unexpected number of logs: %d != 5", c)
}
}