a73x

internal/server/syncsvc/narration_test.go

Ref:   Size: 2.8 KiB   History

package syncsvc

import (
	"testing"

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

// TestApplyReportCarriesWhatAHostIsDoing pins the inbound leg of the narration:
// what a host says it is doing about a VM lands in the registry beside the
// phase, where the VM API reads it.
func TestApplyReportCarriesWhatAHostIsDoing(t *testing.T) {
	f := setup(t)

	f.svc.applyReport(f.host.ID, &pb.Report{
		Vms: []*pb.VMStatus{{
			VmId: "vm1", PowerState: "stopped", Phase: "creating",
			StatusDetail: "downloading image 1.2/3.7 GiB",
		}},
	})

	got, ok := f.reg.Get(f.host.ID)
	require.True(t, ok)
	require.Len(t, got.Report.VMs, 1)
	assert.Equal(t, "downloading image 1.2/3.7 GiB", got.Report.VMs[0].StatusDetail)
}

// TestApplyReportFromAnAgentThatSaysNothing is the fielded-agent case: an agent
// that predates status_detail sends none, and the field is absent rather than
// wrong. Everything about the VM must read exactly as it did before the field
// existed.
func TestApplyReportFromAnAgentThatSaysNothing(t *testing.T) {
	f := setup(t)

	f.svc.applyReport(f.host.ID, &pb.Report{
		Vms: []*pb.VMStatus{{VmId: "vm1", PowerState: "stopped", Phase: "creating"}},
	})

	got, ok := f.reg.Get(f.host.ID)
	require.True(t, ok)
	require.Len(t, got.Report.VMs, 1)
	assert.Equal(t, "creating", got.Report.VMs[0].Phase)
	assert.Empty(t, got.Report.VMs[0].StatusDetail, "an agent that says nothing must not be quoted")
}

// TestApplyReportCountsExposureSessions pins the counters' inbound leg, gauge
// and totals together.
func TestApplyReportCountsExposureSessions(t *testing.T) {
	f := setup(t)

	f.svc.applyReport(f.host.ID, &pb.Report{
		Exposures: []*pb.ExposureStatus{{
			Id: "e1", State: "active",
			Sessions: &pb.ExposureSessions{Active: 7, Refused: 12, Dropped: 3},
		}},
	})

	got, ok := f.reg.Get(f.host.ID)
	require.True(t, ok)
	require.Len(t, got.Report.Exposures, 1)
	require.NotNil(t, got.Report.Exposures[0].Sessions)
	assert.Equal(t, registry.ExposureSessions{Active: 7, Refused: 12, Dropped: 3},
		*got.Report.Exposures[0].Sessions)
}

// TestApplyReportFromAnAgentThatCountsNothing is the other half of the fielded-
// agent case, and the one where zero would be a lie: an agent that reports no
// counters leaves nil, NOT a zeroed struct. "Nobody said" and "nothing has
// happened" are different answers, and only the second one is a fact.
func TestApplyReportFromAnAgentThatCountsNothing(t *testing.T) {
	f := setup(t)

	f.svc.applyReport(f.host.ID, &pb.Report{
		Exposures: []*pb.ExposureStatus{{Id: "e1", State: "active"}},
	})

	got, ok := f.reg.Get(f.host.ID)
	require.True(t, ok)
	require.Len(t, got.Report.Exposures, 1)
	assert.Equal(t, "active", got.Report.Exposures[0].State)
	assert.Nil(t, got.Report.Exposures[0].Sessions, "an uncounted port must not report zeros")
}