a73x

internal/agent/syncclient/capacity_test.go

Ref:   Size: 1.5 KiB   History

package syncclient

import (
	"testing"

	"github.com/a73x/eitri/internal/pb"
	"github.com/stretchr/testify/assert"
)

func TestClampCapacityUnsetIsPassthrough(t *testing.T) {
	// caps unset (0) must reproduce today's behaviour exactly: advertise the
	// raw machine totals.
	raw := &pb.Capacity{Vcpus: 16, MemMb: 32000, DiskGb: 500}
	got := clampCapacity(raw, 0, 0, 0)
	assert.Equal(t, int64(16), got.GetVcpus())
	assert.Equal(t, int64(32000), got.GetMemMb())
	assert.Equal(t, int64(500), got.GetDiskGb())
}

func TestClampCapacityReducesToCap(t *testing.T) {
	raw := &pb.Capacity{Vcpus: 16, MemMb: 32000, DiskGb: 500}
	got := clampCapacity(raw, 8, 16000, 200)
	assert.Equal(t, int64(8), got.GetVcpus())
	assert.Equal(t, int64(16000), got.GetMemMb())
	assert.Equal(t, int64(200), got.GetDiskGb())
}

func TestClampCapacityNeverInflates(t *testing.T) {
	// A cap above the machine's real total is a no-op — an agent cannot
	// advertise more than it has.
	raw := &pb.Capacity{Vcpus: 4, MemMb: 8000, DiskGb: 100}
	got := clampCapacity(raw, 64, 128000, 2000)
	assert.Equal(t, int64(4), got.GetVcpus())
	assert.Equal(t, int64(8000), got.GetMemMb())
	assert.Equal(t, int64(100), got.GetDiskGb())
}

func TestClampCapacityDimensionsIndependent(t *testing.T) {
	// Capping memory must not touch vcpus or disk.
	raw := &pb.Capacity{Vcpus: 16, MemMb: 32000, DiskGb: 500}
	got := clampCapacity(raw, 0, 16000, 0)
	assert.Equal(t, int64(16), got.GetVcpus())
	assert.Equal(t, int64(16000), got.GetMemMb())
	assert.Equal(t, int64(500), got.GetDiskGb())
}