Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

1.3 KB
Raw Blame
//go:build testing

package systems

import (
	"testing"

	"ganigeorgiev.com/pocketbase/dbx"
	"github.com/henrygd/beszel/internal/entities/container"
	"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)
}