SPB Git

Repository Public

live_test.go 4.0 KB 124 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/agent/collect/live_test.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package collect4344import (45	"context"46	"encoding/json"47	"testing"48	"time"4950	"clustr.spboucher.ai/mcc/internal/protocol"51)5253// TestLiveRound measures the machine running the tests (no synthetic data)54// and checks the invariants every round must satisfy.55func TestLiveRound(t *testing.T) {56	if testing.Short() {57		t.Skip("live collection skipped in -short")58	}59	c := New()60	ctx := context.Background()61	now := time.Now()62	r1 := c.Collect(ctx, now)63	for _, s := range r1.Samples {64		m, _ := protocol.MetricByName(s.M)65		if m.Kind == "counter_rate" {66			t.Errorf("first round must not report rate %s (no previous counter)", s.M)67		}68		if s.M == "cpu.usage" {69			t.Errorf("first round must not report cpu.usage")70		}71	}72	time.Sleep(1100 * time.Millisecond)73	// Force every group to be due again.74	c.last = map[string]time.Time{}75	r2 := c.Collect(ctx, time.Now())76	seen := map[string]bool{}77	for _, s := range r2.Samples {78		m, ok := protocol.MetricByName(s.M)79		if !ok {80			t.Fatalf("collector produced uncatalogued metric %q", s.M)81		}82		if m.Labeled != (s.L != "") {83			t.Errorf("%s: label presence does not match the catalog", s.M)84		}85		if s.V != s.V || s.V < 0 {86			t.Errorf("%s{%s} = %v", s.M, s.L, s.V)87		}88		seen[s.M] = true89	}90	avail := c.Availability()91	for _, m := range protocol.Catalog {92		_, declared := avail[m.Name]93		if seen[m.Name] && declared {94			t.Errorf("%s is both measured and declared %s", m.Name, avail[m.Name])95		}96		if !seen[m.Name] && !declared {97			t.Errorf("%s is neither measured nor declared in availability", m.Name)98		}99	}100	if avail["thermal.temp"] != protocol.AvailUnsupported || avail["power.draw"] != protocol.AvailUnsupported {101		t.Errorf("thermal.temp and power.draw must be unsupported, got %v", avail)102	}103	if len(r2.Volumes) == 0 || r2.Volumes[0].Mount != "/" {104		t.Errorf("expected the root volume first, got %+v", r2.Volumes)105	}106	if len(r2.Processes) == 0 || len(r2.Processes) > protocol.MaxProcesses {107		t.Errorf("process table size %d", len(r2.Processes))108	}109	inv := c.Inventory(ctx)110	if inv.HardwareUUID == "" || inv.CPUCores <= 0 || inv.MemBytes <= 0 {111		t.Errorf("inventory incomplete: %+v", inv)112	}113	if testing.Verbose() {114		b, _ := json.Marshal(r2.Samples)115		t.Logf("samples: %s", b)116		b, _ = json.Marshal(avail)117		t.Logf("availability: %s", b)118		b, _ = json.Marshal(inv)119		t.Logf("inventory: %s", b)120		b, _ = json.Marshal(r2.Processes[:min(3, len(r2.Processes))])121		t.Logf("top: %s", b)122	}123}124
Files main
Menu

Theme