a73x

internal/server/api/allocation_api_test.go

Ref:   Size: 1.0 KiB   History

package api

import (
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// TestHostResponseIncludesAllocated verifies the host wire shape carries
// server-computed allocation summed from the host's live VMs.
func TestHostResponseIncludesAllocated(t *testing.T) {
	ts, _, _, _, _ := newServer(t)
	out := enroll(t, ts)
	hostID := out["host_id"]

	mk := func(vcpus, mem, disk int) {
		resp := do(t, "POST", ts.URL+"/api/v1/vms", testPAT, map[string]any{
			"host_id": hostID, "vcpus": vcpus, "mem_mb": mem, "disk_gb": disk,
		})
		require.Equal(t, http.StatusCreated, resp.StatusCode)
	}
	mk(2, 2048, 10)
	mk(1, 1024, 5)

	hosts := decodeJSONKeys(t, do(t, "GET", ts.URL+"/api/v1/hosts", testPAT, nil))
	require.Len(t, hosts, 1)
	alloc, ok := hosts[0]["allocated"].(map[string]any)
	require.True(t, ok, "host response must contain allocated object")
	assert.Equal(t, float64(3), alloc["vcpus"])
	assert.Equal(t, float64(3072), alloc["mem_mb"])
	assert.Equal(t, float64(15), alloc["disk_gb"])
}