mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 17:07:47 +02:00
feat(agent): Add docker image update available flag (#2211)
Co-authored-by: henrygd <hank@henrygd.me>
This commit is contained in:
46
internal/hub/systems/container_records_test.go
Normal file
46
internal/hub/systems/container_records_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
//go:build testing
|
||||
|
||||
package systems
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/henrygd/beszel/internal/entities/container"
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCreateContainerRecordsPersistsImageUpdateAvailability(t *testing.T) {
|
||||
_, app := newTestSystemWithHub(t)
|
||||
|
||||
const (
|
||||
systemID = "system123"
|
||||
containerID = "abcdef123456"
|
||||
image = "nginx:latest"
|
||||
)
|
||||
|
||||
data := &container.Stats{
|
||||
Id: containerID,
|
||||
Name: "web",
|
||||
Image: image,
|
||||
UpdateAvailable: true,
|
||||
}
|
||||
require.NoError(t, createContainerRecords(app, []*container.Stats{data}, systemID))
|
||||
|
||||
var record struct {
|
||||
Image string `db:"image"`
|
||||
UpdateAvailable bool `db:"updatable"`
|
||||
}
|
||||
require.NoError(t, app.DB().Select("image", "updatable").From("containers").
|
||||
Where(dbx.HashExp{"id": containerID}).One(&record))
|
||||
assert.Equal(t, image, record.Image)
|
||||
assert.True(t, record.UpdateAvailable)
|
||||
|
||||
data.UpdateAvailable = false
|
||||
require.NoError(t, createContainerRecords(app, []*container.Stats{data}, systemID))
|
||||
require.NoError(t, app.DB().Select("image", "updatable").From("containers").
|
||||
Where(dbx.HashExp{"id": containerID}).One(&record))
|
||||
assert.Equal(t, image, record.Image)
|
||||
assert.False(t, record.UpdateAvailable)
|
||||
}
|
||||
@@ -376,7 +376,7 @@ func createContainerRecords(app core.App, data []*container.Stats, systemId stri
|
||||
valueStrings := make([]string, 0, len(data))
|
||||
for i, container := range data {
|
||||
suffix := fmt.Sprintf("%d", i)
|
||||
valueStrings = append(valueStrings, fmt.Sprintf("({:id%[1]s}, {:system}, {:name%[1]s}, {:image%[1]s}, {:ports%[1]s}, {:status%[1]s}, {:health%[1]s}, {:cpu%[1]s}, {:memory%[1]s}, {:net%[1]s}, {:updated})", suffix))
|
||||
valueStrings = append(valueStrings, fmt.Sprintf("({:id%[1]s}, {:system}, {:name%[1]s}, {:image%[1]s}, {:ports%[1]s}, {:status%[1]s}, {:health%[1]s}, {:cpu%[1]s}, {:memory%[1]s}, {:net%[1]s}, {:updateAvailable%[1]s}, {:updated})", suffix))
|
||||
params["id"+suffix] = container.Id
|
||||
params["name"+suffix] = container.Name
|
||||
params["image"+suffix] = container.Image
|
||||
@@ -390,9 +390,10 @@ func createContainerRecords(app core.App, data []*container.Stats, systemId stri
|
||||
netBytes = uint64((container.NetworkSent + container.NetworkRecv) * 1024 * 1024)
|
||||
}
|
||||
params["net"+suffix] = netBytes
|
||||
params["updateAvailable"+suffix] = container.UpdateAvailable
|
||||
}
|
||||
queryString := fmt.Sprintf(
|
||||
"INSERT INTO containers (id, system, name, image, ports, status, health, cpu, memory, net, updated) VALUES %s ON CONFLICT(id) DO UPDATE SET system = excluded.system, name = excluded.name, image = excluded.image, ports = excluded.ports, status = excluded.status, health = excluded.health, cpu = excluded.cpu, memory = excluded.memory, net = excluded.net, updated = excluded.updated",
|
||||
"INSERT INTO containers (id, system, name, image, ports, status, health, cpu, memory, net, updatable, updated) VALUES %s ON CONFLICT(id) DO UPDATE SET system = excluded.system, name = excluded.name, image = excluded.image, ports = excluded.ports, status = excluded.status, health = excluded.health, cpu = excluded.cpu, memory = excluded.memory, net = excluded.net, updatable = excluded.updatable, updated = excluded.updated",
|
||||
strings.Join(valueStrings, ","),
|
||||
)
|
||||
_, err := app.DB().NewQuery(queryString).Bind(params).Execute()
|
||||
|
||||
Reference in New Issue
Block a user