Merge branch 'feature-39'
All checks were successful
Documentation / publish (push) Successful in 1m53s
All checks were successful
Documentation / publish (push) Successful in 1m53s
This commit is contained in:
commit
1b37bd6b4b
34 changed files with 1389 additions and 11 deletions
|
|
@ -33,7 +33,7 @@ jobs:
|
|||
echo "Building for ${BINARI}/${GOOS}/${GOARCH} (release: ${RELEASE_CIBLE})"
|
||||
go env GOOS GOARCH
|
||||
mkdir -p dist/
|
||||
go build -o dist/${BINARI}_${GOOS}_${GOARCH} ./cmd/${BINARI}
|
||||
go build -ldflags "-X main.version=${RELEASE_CIBLE}" -o dist/${BINARI}_${GOOS}_${GOARCH} ./cmd/${BINARI}
|
||||
echo "artifact pour ${RELEASE_CIBLE} ${BINARI} ${GOOS} ${GOARCH}" > dist/${BINARI}-${GOOS}-${GOARCH}.txt
|
||||
ls -l ./dist
|
||||
- name: Upload artifact
|
||||
|
|
|
|||
138
api/agent.yaml
138
api/agent.yaml
|
|
@ -489,6 +489,63 @@ paths:
|
|||
"500":
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
# ── Host ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/register:
|
||||
get:
|
||||
summary: Facts of the host that do not change while it runs
|
||||
operationId: getRegister
|
||||
description: >-
|
||||
Read once when the agent starts and kept in memory. Will also be sent to the broker of the
|
||||
orchestrator at startup.
|
||||
responses:
|
||||
"200":
|
||||
description: Host facts
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/HostRegister"
|
||||
"405":
|
||||
description: Method other than GET
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"503":
|
||||
description: Host facts could not be read when the agent started (the error says why)
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
|
||||
/ping:
|
||||
get:
|
||||
summary: Current state of the host and of the agent
|
||||
operationId: getPing
|
||||
description: >-
|
||||
Last reading, taken when the agent starts then every times.host_state_interval seconds and
|
||||
kept in memory; collected_at gives its age. boot_time and agent_start_time tell a host
|
||||
reboot from an agent restart; boot_time plus uptime_seconds give the clock of the host.
|
||||
responses:
|
||||
"200":
|
||||
description: Last host state
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/HostPing"
|
||||
"405":
|
||||
description: Method other than GET
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"503":
|
||||
description: No reading has succeeded yet
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
|
||||
components:
|
||||
|
||||
parameters:
|
||||
|
|
@ -932,6 +989,87 @@ components:
|
|||
type: string
|
||||
example: "203.0.113.1"
|
||||
|
||||
HostRegister:
|
||||
type: object
|
||||
properties:
|
||||
hostname:
|
||||
type: string
|
||||
example: hv1
|
||||
cpu:
|
||||
type: object
|
||||
properties:
|
||||
vendor:
|
||||
type: string
|
||||
example: AuthenticAMD
|
||||
model:
|
||||
type: string
|
||||
example: AMD EPYC 7272 12-Core Processor
|
||||
sockets:
|
||||
type: integer
|
||||
example: 1
|
||||
cores:
|
||||
type: integer
|
||||
description: Physical cores, all sockets together
|
||||
example: 12
|
||||
threads:
|
||||
type: integer
|
||||
description: Logical processors, all sockets together
|
||||
example: 24
|
||||
memory_bytes:
|
||||
type: integer
|
||||
format: int64
|
||||
example: 134217728000
|
||||
os:
|
||||
type: object
|
||||
description: From /etc/os-release
|
||||
properties:
|
||||
id:
|
||||
type: string
|
||||
example: debian
|
||||
version_id:
|
||||
type: string
|
||||
example: "12"
|
||||
pretty_name:
|
||||
type: string
|
||||
example: Debian GNU/Linux 12 (bookworm)
|
||||
kernel:
|
||||
type: string
|
||||
example: 6.1.0-26-amd64
|
||||
|
||||
HostPing:
|
||||
type: object
|
||||
properties:
|
||||
agent_version:
|
||||
type: string
|
||||
description: Release tag, commit for a lab build, undefined when not set at build time
|
||||
example: 0.3.0
|
||||
collected_at:
|
||||
type: integer
|
||||
format: int64
|
||||
description: Unix time (seconds) of the reading
|
||||
example: 1793520853
|
||||
boot_time:
|
||||
type: integer
|
||||
format: int64
|
||||
description: Unix time (seconds) the host booted
|
||||
example: 1793499123
|
||||
agent_start_time:
|
||||
type: integer
|
||||
format: int64
|
||||
description: Unix time (seconds) the agent started
|
||||
example: 1793499190
|
||||
uptime_seconds:
|
||||
type: number
|
||||
example: 21730.4
|
||||
load:
|
||||
type: array
|
||||
description: Load average over 1, 5 and 15 minutes
|
||||
minItems: 3
|
||||
maxItems: 3
|
||||
items:
|
||||
type: number
|
||||
example: [0.12, 0.3, 0.25]
|
||||
|
||||
Error:
|
||||
type: object
|
||||
properties:
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||
"git.g3e.fr/syonad/two/internal/frr"
|
||||
"git.g3e.fr/syonad/two/internal/host"
|
||||
"git.g3e.fr/syonad/two/internal/migration"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent"
|
||||
|
|
@ -28,6 +29,8 @@ import (
|
|||
|
||||
const shutdownTimeout = 20 * time.Second
|
||||
|
||||
var version = "undefined"
|
||||
|
||||
func main() {
|
||||
confFile := flag.String("config", "/etc/two/agent.yml", "config file path")
|
||||
flag.Parse()
|
||||
|
|
@ -35,6 +38,7 @@ func main() {
|
|||
}
|
||||
|
||||
func run(confFile string) int {
|
||||
start := time.Now()
|
||||
cfg, err := configuration.LoadConfig(confFile)
|
||||
if err != nil {
|
||||
slog.Error("failed to load config", "error", err)
|
||||
|
|
@ -96,6 +100,7 @@ func run(confFile string) int {
|
|||
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
||||
|
||||
log.Info("starting agent",
|
||||
"version", version,
|
||||
"api", apiAddr,
|
||||
"prometheus", promAddr,
|
||||
"workers", cfg.Worker.Count,
|
||||
|
|
@ -108,7 +113,10 @@ func run(confFile string) int {
|
|||
|
||||
d := dispatcher.New(q, db, cfg, log.With(slog.String("component", "dispatcher")))
|
||||
|
||||
apiSrv := agentapi.New(d, db, log.With(slog.String("component", "api")), apiAddr)
|
||||
hostInfo := host.NewCollector(os.DirFS("/"), version, start, cfg.Times.HostState(), log.With(slog.String("component", "host")))
|
||||
go hostInfo.Run(ctx)
|
||||
|
||||
apiSrv := agentapi.New(d, db, log.With(slog.String("component", "api")), apiAddr, hostInfo)
|
||||
go apiSrv.Start()
|
||||
|
||||
promSrv := promserver.New(promAddr, registry)
|
||||
|
|
|
|||
|
|
@ -35,6 +35,8 @@ times:
|
|||
timeout_vms_start: 300
|
||||
# Interval between each state check
|
||||
poll_interval: 2
|
||||
# Interval between two readings of the host state served by GET /ping
|
||||
host_state_interval: 10
|
||||
|
||||
# Bridge interface used when the requested iface_type is not found in interfaces
|
||||
default_interface: br-000000
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ Paquets
|
|||
``/24`` et ``/32``
|
||||
* - ``internal/frr``
|
||||
- génération des commandes FRR, seul point d'appel à ``vtysh``
|
||||
* - ``internal/host``
|
||||
- connaissance de l'hôte : faits lus au démarrage (``/register``), état mesuré périodiquement en mémoire (``/ping``)
|
||||
* - ``internal/sysctl``
|
||||
- écriture sous ``/proc/sys``, dans un netns si besoin
|
||||
* - ``internal/netns``
|
||||
|
|
|
|||
|
|
@ -320,6 +320,10 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
- machine d'états — 409 pendant creating, suppression depuis error, sockets QEMU d'une VM en error retirés, dépendances VM → subnet → VPC, suppression d'un subnet en attente de ses VM, arrêt d'une VM juste démarrée par renvoi de system_powerdown, recréation sous le même nom
|
||||
- hv1
|
||||
- #41, #84, #79
|
||||
* - ``13-hote``
|
||||
- connaissance de l'hôte — /register (hostname, CPU, RAM, OS, noyau) lu au démarrage, /ping (version, démarrages de l'hôte et de l'agent, uptime, load) mesuré périodiquement, lecture seule, redémarrage de l'agent distingué de celui de l'hôte
|
||||
- hv1
|
||||
- #39
|
||||
* - ``21-dhcp-two``
|
||||
- DHCP par le serveur intégré (backend two) — bail, server-id, renouvellement, option 121, un serveur par subnet, démarrages simultanés
|
||||
- hv1
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@ Exécution des commandes
|
|||
timeout_subnet_start: 300
|
||||
timeout_vms_start: 300
|
||||
poll_interval: 2
|
||||
host_state_interval: 10
|
||||
|
||||
``worker.count`` est le nombre de goroutines qui exécutent les commandes ; ``buffer_size`` le
|
||||
nombre de commandes en attente au-delà duquel ``Dispatch`` bloque.
|
||||
|
|
@ -101,6 +102,9 @@ La section ``times`` règle, en secondes, les attentes des commandes asynchrones
|
|||
création d'une VM : attente des subnets de ses interfaces ``running``.
|
||||
``poll_interval``
|
||||
pas de toutes ces attentes.
|
||||
``host_state_interval``
|
||||
intervalle entre deux mesures de l'état de l'hôte servi par ``GET /ping`` (voir
|
||||
:doc:`observabilite`).
|
||||
|
||||
Chaque valeur doit être strictement positive, et ``next_vms_shutdown`` inférieur à
|
||||
``timeout_vms_stop`` ; sinon l'agent refuse de démarrer. Les attentes s'enchaînent — une VM,
|
||||
|
|
|
|||
|
|
@ -27,6 +27,31 @@ Les états sont ceux du :doc:`cycle de vie </concepts/cycle-de-vie>`. Une valeur
|
|||
durable sur ``error`` est l'alerte la plus utile à poser ; une valeur durable sur ``creating``
|
||||
ou ``deleting`` signale une opération qui n'aboutit pas.
|
||||
|
||||
Connaissance de l'hôte
|
||||
----------------------
|
||||
|
||||
Deux routes de l'API de l'agent, en lecture seule (voir :doc:`api-agent/reference`) :
|
||||
|
||||
``GET /register``
|
||||
ce qui ne change pas tant que l'hôte tourne : hostname, CPU (fabricant, modèle, sockets,
|
||||
cœurs, threads), RAM totale, OS (``/etc/os-release``) et noyau. Lu une fois au démarrage de
|
||||
l'agent ; si la lecture échoue, l'agent démarre quand même, journalise ``host facts
|
||||
unavailable`` et la route répond 503 avec l'erreur.
|
||||
``GET /ping``
|
||||
l'état courant : version de l'agent, démarrage de l'hôte (``boot_time``) et de l'agent
|
||||
(``agent_start_time``), uptime de l'hôte et load sur 1, 5 et 15 minutes. Mesuré au démarrage
|
||||
puis toutes les ``times.host_state_interval`` secondes (10 par défaut), gardé en mémoire ;
|
||||
``collected_at`` donne l'heure de la mesure. 503 tant qu'aucune mesure n'a réussi ; une mesure
|
||||
en échec est journalisée (``host state collection failed``) et la précédente reste servie.
|
||||
|
||||
Les dates sont des timestamps Unix en secondes. Un ``boot_time`` qui change signale un
|
||||
redémarrage de l'hôte, un ``agent_start_time`` seul un redémarrage de l'agent ;
|
||||
``boot_time`` + ``uptime_seconds`` donne l'horloge de l'hôte au moment de la mesure.
|
||||
|
||||
La version de l'agent est injectée au build : le tag pour une release, le commit court
|
||||
(suffixé ``-dirty`` si l'arbre avait des modifications) pour les binaires de test du lab,
|
||||
``undefined`` sinon. Elle figure aussi dans la ligne ``starting agent`` du journal.
|
||||
|
||||
Watchdog
|
||||
--------
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ func newTestServerWith(t *testing.T, cfg *configuration.Config) (*Server, *badge
|
|||
q := worker.New(100)
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
d := dispatcher.New(q, db, cfg, logger)
|
||||
return New(d, db, logger, "127.0.0.1:0"), db
|
||||
return New(d, db, logger, "127.0.0.1:0", &fakeHost{}), db
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
|||
56
internal/api/agent/host.go
Normal file
56
internal/api/agent/host.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package agentapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/host"
|
||||
)
|
||||
|
||||
type HostInfo interface {
|
||||
Register() (host.Facts, error)
|
||||
Ping() (host.Ping, bool)
|
||||
}
|
||||
|
||||
func (s *Server) RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
f, err := s.host.Register()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "host facts unavailable: " + err.Error()})
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(HostRegister{
|
||||
Hostname: f.Hostname,
|
||||
CPU: HostCPU{Vendor: f.CPU.Vendor, Model: f.CPU.Model, Sockets: f.CPU.Sockets, Cores: f.CPU.Cores, Threads: f.CPU.Threads},
|
||||
MemoryBytes: f.MemoryBytes,
|
||||
OS: HostOS{ID: f.OS.ID, VersionID: f.OS.VersionID, PrettyName: f.OS.PrettyName},
|
||||
Kernel: f.Kernel,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) PingHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
p, ok := s.host.Ping()
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "no host state collected yet"})
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(HostPing{
|
||||
AgentVersion: p.AgentVersion,
|
||||
CollectedAt: p.CollectedAt,
|
||||
BootTime: p.BootTime,
|
||||
AgentStartTime: p.AgentStartTime,
|
||||
UptimeSeconds: p.UptimeSeconds,
|
||||
Load: p.Load,
|
||||
})
|
||||
}
|
||||
103
internal/api/agent/host_test.go
Normal file
103
internal/api/agent/host_test.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package agentapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/host"
|
||||
)
|
||||
|
||||
type fakeHost struct {
|
||||
facts host.Facts
|
||||
factsErr error
|
||||
ping *host.Ping
|
||||
}
|
||||
|
||||
func (f *fakeHost) Register() (host.Facts, error) { return f.facts, f.factsErr }
|
||||
|
||||
func (f *fakeHost) Ping() (host.Ping, bool) {
|
||||
if f.ping == nil {
|
||||
return host.Ping{}, false
|
||||
}
|
||||
return *f.ping, true
|
||||
}
|
||||
|
||||
func serveHost(t *testing.T, h *fakeHost, method, path string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
s, _ := newTestServer(t)
|
||||
s.host = h
|
||||
w := httptest.NewRecorder()
|
||||
s.srv.Handler.ServeHTTP(w, httptest.NewRequest(method, path, nil))
|
||||
return w
|
||||
}
|
||||
|
||||
func TestRegister_ServesTheFacts(t *testing.T) {
|
||||
h := &fakeHost{facts: host.Facts{
|
||||
Hostname: "hv1",
|
||||
CPU: host.CPU{Vendor: "AuthenticAMD", Model: "AMD EPYC 7272 12-Core Processor", Sockets: 1, Cores: 12, Threads: 24},
|
||||
MemoryBytes: 134217728000,
|
||||
OS: host.OS{ID: "debian", VersionID: "12", PrettyName: "Debian GNU/Linux 12 (bookworm)"},
|
||||
Kernel: "6.1.0-26-amd64",
|
||||
}}
|
||||
w := serveHost(t, h, http.MethodGet, "/register")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("attendu 200, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
want := `{"hostname":"hv1","cpu":{"vendor":"AuthenticAMD","model":"AMD EPYC 7272 12-Core Processor","sockets":1,"cores":12,"threads":24},"memory_bytes":134217728000,"os":{"id":"debian","version_id":"12","pretty_name":"Debian GNU/Linux 12 (bookworm)"},"kernel":"6.1.0-26-amd64"}`
|
||||
if got := strings.TrimSpace(w.Body.String()); got != want {
|
||||
t.Errorf("corps :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegister_UnavailableFacts(t *testing.T) {
|
||||
w := serveHost(t, &fakeHost{factsErr: errors.New("open proc/cpuinfo: file does not exist")}, http.MethodGet, "/register")
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("attendu 503, obtenu %d", w.Code)
|
||||
}
|
||||
var e ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&e); err != nil || e.Error != "host facts unavailable: open proc/cpuinfo: file does not exist" {
|
||||
t.Errorf("erreur %q, %v", e.Error, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPing_ServesTheLastState(t *testing.T) {
|
||||
h := &fakeHost{ping: &host.Ping{
|
||||
State: host.State{BootTime: 1793499123, UptimeSeconds: 21730.4, Load: [3]float64{0.12, 0.3, 0.25}},
|
||||
AgentVersion: "0.3.0",
|
||||
AgentStartTime: 1793499190,
|
||||
CollectedAt: 1793520853,
|
||||
}}
|
||||
w := serveHost(t, h, http.MethodGet, "/ping")
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("attendu 200, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
want := `{"agent_version":"0.3.0","collected_at":1793520853,"boot_time":1793499123,"agent_start_time":1793499190,"uptime_seconds":21730.4,"load":[0.12,0.3,0.25]}`
|
||||
if got := strings.TrimSpace(w.Body.String()); got != want {
|
||||
t.Errorf("corps :\n%s\nattendu :\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPing_BeforeTheFirstCollection(t *testing.T) {
|
||||
w := serveHost(t, &fakeHost{}, http.MethodGet, "/ping")
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("attendu 503, obtenu %d", w.Code)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "no host state collected yet") {
|
||||
t.Errorf("corps %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostRoutes_ReadOnly(t *testing.T) {
|
||||
h := &fakeHost{ping: &host.Ping{}}
|
||||
for _, path := range []string{"/register", "/ping"} {
|
||||
for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodDelete} {
|
||||
if w := serveHost(t, h, method, path); w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("%s %s : attendu 405, obtenu %d", method, path, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,3 +123,34 @@ type FloatingIP struct {
|
|||
IP string `json:"ip,omitempty"`
|
||||
Gateway string `json:"gateway,omitempty"`
|
||||
}
|
||||
|
||||
type HostCPU struct {
|
||||
Vendor string `json:"vendor"`
|
||||
Model string `json:"model"`
|
||||
Sockets int `json:"sockets"`
|
||||
Cores int `json:"cores"`
|
||||
Threads int `json:"threads"`
|
||||
}
|
||||
|
||||
type HostOS struct {
|
||||
ID string `json:"id"`
|
||||
VersionID string `json:"version_id"`
|
||||
PrettyName string `json:"pretty_name"`
|
||||
}
|
||||
|
||||
type HostRegister struct {
|
||||
Hostname string `json:"hostname"`
|
||||
CPU HostCPU `json:"cpu"`
|
||||
MemoryBytes uint64 `json:"memory_bytes"`
|
||||
OS HostOS `json:"os"`
|
||||
Kernel string `json:"kernel"`
|
||||
}
|
||||
|
||||
type HostPing struct {
|
||||
AgentVersion string `json:"agent_version"`
|
||||
CollectedAt int64 `json:"collected_at"`
|
||||
BootTime int64 `json:"boot_time"`
|
||||
AgentStartTime int64 `json:"agent_start_time"`
|
||||
UptimeSeconds float64 `json:"uptime_seconds"`
|
||||
Load [3]float64 `json:"load"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,11 +17,12 @@ type Server struct {
|
|||
dispatcher *dispatcher.Dispatcher
|
||||
db *badger.DB
|
||||
logger *slog.Logger
|
||||
host HostInfo
|
||||
srv *http.Server
|
||||
}
|
||||
|
||||
func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address string) *Server {
|
||||
s := &Server{dispatcher: d, db: db, logger: logger}
|
||||
func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address string, h HostInfo) *Server {
|
||||
s := &Server{dispatcher: d, db: db, logger: logger, host: h}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/vpcs", s.VpcsHandler)
|
||||
mux.HandleFunc("/vpcs/", s.VpcByNameHandler)
|
||||
|
|
@ -33,6 +34,8 @@ func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address s
|
|||
mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler)
|
||||
mux.HandleFunc("/floating-ips", s.FloatingIPsHandler)
|
||||
mux.HandleFunc("/floating-ips/", s.FloatingIPByNameHandler)
|
||||
mux.HandleFunc("/register", s.RegisterHandler)
|
||||
mux.HandleFunc("/ping", s.PingHandler)
|
||||
s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)}
|
||||
return s
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,6 +78,7 @@ func LoadConfig(path string) (*Config, error) {
|
|||
v.SetDefault("times.timeout_subnet_start", TimeoutSubnetStart)
|
||||
v.SetDefault("times.timeout_vms_start", TimeoutVMsStart)
|
||||
v.SetDefault("times.poll_interval", PollInterval)
|
||||
v.SetDefault("times.host_state_interval", HostStateInterval)
|
||||
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
||||
v.SetDefault("dhcp.backend", BackendTwo)
|
||||
v.SetDefault("hostsvc.dns_upstream", []string{})
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ const (
|
|||
TimeoutSubnetStart = 300
|
||||
TimeoutVMsStart = 300
|
||||
PollInterval = 2
|
||||
HostStateInterval = 10
|
||||
)
|
||||
|
||||
type Times struct {
|
||||
|
|
@ -23,6 +24,7 @@ type Times struct {
|
|||
TimeoutSubnetStart int `mapstructure:"timeout_subnet_start"`
|
||||
TimeoutVMsStart int `mapstructure:"timeout_vms_start"`
|
||||
PollInterval int `mapstructure:"poll_interval"`
|
||||
HostStateInterval int `mapstructure:"host_state_interval"`
|
||||
}
|
||||
|
||||
func seconds(value, fallback int) time.Duration {
|
||||
|
|
@ -39,6 +41,7 @@ func (t Times) VPCStop() time.Duration { return seconds(t.TimeoutVPCStop, Ti
|
|||
func (t Times) SubnetStart() time.Duration { return seconds(t.TimeoutSubnetStart, TimeoutSubnetStart) }
|
||||
func (t Times) VMsStart() time.Duration { return seconds(t.TimeoutVMsStart, TimeoutVMsStart) }
|
||||
func (t Times) Poll() time.Duration { return seconds(t.PollInterval, PollInterval) }
|
||||
func (t Times) HostState() time.Duration { return seconds(t.HostStateInterval, HostStateInterval) }
|
||||
|
||||
func (t Times) Validate() error {
|
||||
for _, f := range []struct {
|
||||
|
|
@ -52,6 +55,7 @@ func (t Times) Validate() error {
|
|||
{"timeout_subnet_start", t.TimeoutSubnetStart},
|
||||
{"timeout_vms_start", t.TimeoutVMsStart},
|
||||
{"poll_interval", t.PollInterval},
|
||||
{"host_state_interval", t.HostStateInterval},
|
||||
} {
|
||||
if f.value <= 0 {
|
||||
return fmt.Errorf("times.%s %d must be a positive number of seconds", f.key, f.value)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ func TestLoadConfig_TimesDefauts(t *testing.T) {
|
|||
TimeoutSubnetStart: 300,
|
||||
TimeoutVMsStart: 300,
|
||||
PollInterval: 2,
|
||||
HostStateInterval: 10,
|
||||
}
|
||||
if cfg.Times != want {
|
||||
t.Errorf("times par défaut : %+v, attendu %+v", cfg.Times, want)
|
||||
|
|
@ -26,11 +27,11 @@ func TestLoadConfig_TimesDefauts(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLoadConfig_TimesValeursExplicites(t *testing.T) {
|
||||
cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n"))
|
||||
cfg, err := LoadConfig(writeYAML(t, "times:\n timeout_vms_stop: 600\n next_vms_shutdown: 10\n timeout_subnet_stop: 700\n timeout_vpc_stop: 800\n timeout_subnet_start: 50\n timeout_vms_start: 60\n poll_interval: 1\n host_state_interval: 5\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig a échoué : %v", err)
|
||||
}
|
||||
want := Times{600, 10, 700, 800, 50, 60, 1}
|
||||
want := Times{600, 10, 700, 800, 50, 60, 1, 5}
|
||||
if cfg.Times != want {
|
||||
t.Errorf("times : %+v, attendu %+v", cfg.Times, want)
|
||||
}
|
||||
|
|
@ -55,6 +56,7 @@ func TestLoadConfig_TimesInvalidesRefuses(t *testing.T) {
|
|||
{"times:\n timeout_subnet_start: 0\n", "times.timeout_subnet_start 0"},
|
||||
{"times:\n timeout_vms_start: 0\n", "times.timeout_vms_start 0"},
|
||||
{"times:\n poll_interval: 0\n", "times.poll_interval 0"},
|
||||
{"times:\n host_state_interval: 0\n", "times.host_state_interval 0"},
|
||||
{"times:\n next_vms_shutdown: 300\n", "times.next_vms_shutdown 300 must be below times.timeout_vms_stop 300"},
|
||||
} {
|
||||
if _, err := LoadConfig(writeYAML(t, c.yaml)); err == nil || !strings.Contains(err.Error(), c.msg) {
|
||||
|
|
@ -79,14 +81,15 @@ func TestTimesDurees(t *testing.T) {
|
|||
{"SubnetStart", zero.SubnetStart(), 300 * time.Second},
|
||||
{"VMsStart", zero.VMsStart(), 300 * time.Second},
|
||||
{"Poll", zero.Poll(), 2 * time.Second},
|
||||
{"HostState", zero.HostState(), 10 * time.Second},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if c.got != c.want {
|
||||
t.Errorf("Times{}.%s() = %v, attendu %v", c.name, c.got, c.want)
|
||||
}
|
||||
}
|
||||
set := Times{11, 12, 13, 14, 15, 16, 17}
|
||||
got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll()}
|
||||
set := Times{11, 12, 13, 14, 15, 16, 17, 18}
|
||||
got := []time.Duration{set.VMsStop(), set.VMsShutdown(), set.SubnetStop(), set.VPCStop(), set.SubnetStart(), set.VMsStart(), set.Poll(), set.HostState()}
|
||||
for i, d := range got {
|
||||
if want := time.Duration(11+i) * time.Second; d != want {
|
||||
t.Errorf("durée %d = %v, attendu %v", i, d, want)
|
||||
|
|
|
|||
78
internal/host/collector.go
Normal file
78
internal/host/collector.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ping struct {
|
||||
State
|
||||
AgentVersion string
|
||||
AgentStartTime int64
|
||||
CollectedAt int64
|
||||
}
|
||||
|
||||
type Collector struct {
|
||||
fsys fs.FS
|
||||
version string
|
||||
start time.Time
|
||||
interval time.Duration
|
||||
logger *slog.Logger
|
||||
now func() time.Time
|
||||
facts Facts
|
||||
factsErr error
|
||||
|
||||
mu sync.RWMutex
|
||||
last *Ping
|
||||
}
|
||||
|
||||
func NewCollector(fsys fs.FS, version string, start time.Time, interval time.Duration, logger *slog.Logger) *Collector {
|
||||
c := &Collector{fsys: fsys, version: version, start: start, interval: interval, logger: logger, now: time.Now}
|
||||
c.facts, c.factsErr = ReadFacts(fsys)
|
||||
if c.factsErr != nil {
|
||||
logger.Error("host facts unavailable", "error", c.factsErr)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Collector) Register() (Facts, error) {
|
||||
return c.facts, c.factsErr
|
||||
}
|
||||
|
||||
func (c *Collector) Ping() (Ping, bool) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
if c.last == nil {
|
||||
return Ping{}, false
|
||||
}
|
||||
return *c.last, true
|
||||
}
|
||||
|
||||
func (c *Collector) Run(ctx context.Context) {
|
||||
ticker := time.NewTicker(c.interval)
|
||||
defer ticker.Stop()
|
||||
c.collect()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
c.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collect() {
|
||||
s, err := ReadState(c.fsys)
|
||||
if err != nil {
|
||||
c.logger.Error("host state collection failed", "error", err)
|
||||
return
|
||||
}
|
||||
p := Ping{State: s, AgentVersion: c.version, AgentStartTime: c.start.Unix(), CollectedAt: c.now().Unix()}
|
||||
c.mu.Lock()
|
||||
c.last = &p
|
||||
c.mu.Unlock()
|
||||
}
|
||||
102
internal/host/collector_test.go
Normal file
102
internal/host/collector_test.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func discard() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
||||
|
||||
func TestCollector_RegisterServesTheFactsReadAtCreation(t *testing.T) {
|
||||
fsys := debian12()
|
||||
c := NewCollector(fsys, "0.3.0", time.Unix(1792210950, 0), time.Hour, discard())
|
||||
fsys["proc/sys/kernel/hostname"].Data = []byte("autre\n")
|
||||
f, err := c.Register()
|
||||
if err != nil || f.Hostname != "hv1" {
|
||||
t.Errorf("Register = %q, %v ; hv1 lu à la création attendu", f.Hostname, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_RegisterKeepsTheReadError(t *testing.T) {
|
||||
fsys := debian12()
|
||||
delete(fsys, "proc/cpuinfo")
|
||||
var logs strings.Builder
|
||||
c := NewCollector(fsys, "0.3.0", time.Unix(0, 0), time.Hour, slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
if _, err := c.Register(); err == nil {
|
||||
t.Error("Register sans cpuinfo : aucune erreur")
|
||||
}
|
||||
if !strings.Contains(logs.String(), "host facts unavailable") {
|
||||
t.Errorf("erreur non journalisée : %q", logs.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_PingBeforeAnyCollection(t *testing.T) {
|
||||
c := NewCollector(debian12(), "0.3.0", time.Unix(0, 0), time.Hour, discard())
|
||||
if _, ok := c.Ping(); ok {
|
||||
t.Error("Ping avant toute mesure : instantané rendu")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_PingServesTheLastCollection(t *testing.T) {
|
||||
c := NewCollector(debian12(), "0.3.0", time.Unix(1792210950, 0), time.Hour, discard())
|
||||
c.now = func() time.Time { return time.Unix(1793454325, 0) }
|
||||
c.collect()
|
||||
got, ok := c.Ping()
|
||||
want := Ping{
|
||||
State: State{BootTime: 1792210880, UptimeSeconds: 1243445.41, Load: [3]float64{0.08, 0.03, 0.04}},
|
||||
AgentVersion: "0.3.0",
|
||||
AgentStartTime: 1792210950,
|
||||
CollectedAt: 1793454325,
|
||||
}
|
||||
if !ok || got != want {
|
||||
t.Errorf("Ping = %+v, %v ; %+v attendu", got, ok, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_FailedCollectionKeepsThePreviousOne(t *testing.T) {
|
||||
fsys := debian12()
|
||||
var logs strings.Builder
|
||||
c := NewCollector(fsys, "0.3.0", time.Unix(0, 0), time.Hour, slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
c.now = func() time.Time { return time.Unix(100, 0) }
|
||||
c.collect()
|
||||
delete(fsys, "proc/loadavg")
|
||||
c.now = func() time.Time { return time.Unix(200, 0) }
|
||||
c.collect()
|
||||
got, ok := c.Ping()
|
||||
if !ok || got.CollectedAt != 100 {
|
||||
t.Errorf("Ping = %+v, %v ; mesure de 100 gardée attendue", got, ok)
|
||||
}
|
||||
if !strings.Contains(logs.String(), "host state collection failed") {
|
||||
t.Errorf("échec non journalisé : %q", logs.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollector_RunCollectsAtOnceAndStopsWithTheContext(t *testing.T) {
|
||||
c := NewCollector(debian12(), "0.3.0", time.Unix(0, 0), time.Hour, discard())
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
c.Run(ctx)
|
||||
close(done)
|
||||
}()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
if _, ok := c.Ping(); ok {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("aucune mesure au démarrage de Run")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("Run ne s'arrête pas à l'annulation du contexte")
|
||||
}
|
||||
}
|
||||
225
internal/host/host.go
Normal file
225
internal/host/host.go
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CPU struct {
|
||||
Vendor string
|
||||
Model string
|
||||
Sockets int
|
||||
Cores int
|
||||
Threads int
|
||||
}
|
||||
|
||||
type OS struct {
|
||||
ID string
|
||||
VersionID string
|
||||
PrettyName string
|
||||
}
|
||||
|
||||
type Facts struct {
|
||||
Hostname string
|
||||
CPU CPU
|
||||
MemoryBytes uint64
|
||||
OS OS
|
||||
Kernel string
|
||||
}
|
||||
|
||||
type State struct {
|
||||
BootTime int64
|
||||
UptimeSeconds float64
|
||||
Load [3]float64
|
||||
}
|
||||
|
||||
func ReadFacts(fsys fs.FS) (Facts, error) {
|
||||
var f Facts
|
||||
var err error
|
||||
if f.Hostname, err = readLine(fsys, "proc/sys/kernel/hostname"); err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
if f.Kernel, err = readLine(fsys, "proc/sys/kernel/osrelease"); err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
data, err := fs.ReadFile(fsys, "proc/cpuinfo")
|
||||
if err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
if f.CPU, err = parseCPUInfo(string(data)); err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
data, err = fs.ReadFile(fsys, "proc/meminfo")
|
||||
if err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
if f.MemoryBytes, err = parseMemTotal(string(data)); err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
data, err = fs.ReadFile(fsys, "etc/os-release")
|
||||
if err != nil {
|
||||
return Facts{}, err
|
||||
}
|
||||
f.OS = parseOSRelease(string(data))
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func ReadState(fsys fs.FS) (State, error) {
|
||||
var s State
|
||||
data, err := fs.ReadFile(fsys, "proc/stat")
|
||||
if err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
if s.BootTime, err = parseBootTime(string(data)); err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
line, err := readLine(fsys, "proc/uptime")
|
||||
if err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
if s.UptimeSeconds, err = parseUptime(line); err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
if line, err = readLine(fsys, "proc/loadavg"); err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
if s.Load, err = parseLoadAvg(line); err != nil {
|
||||
return State{}, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func readLine(fsys fs.FS, name string) (string, error) {
|
||||
data, err := fs.ReadFile(fsys, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
line := strings.TrimSpace(string(data))
|
||||
if line == "" {
|
||||
return "", fmt.Errorf("%s: empty", name)
|
||||
}
|
||||
return line, nil
|
||||
}
|
||||
|
||||
func parseCPUInfo(data string) (CPU, error) {
|
||||
var cpu CPU
|
||||
sockets := map[string]bool{}
|
||||
cores := map[string]bool{}
|
||||
var physical string
|
||||
scanner := bufio.NewScanner(strings.NewReader(data))
|
||||
for scanner.Scan() {
|
||||
key, value, ok := strings.Cut(scanner.Text(), ":")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
value = strings.TrimSpace(value)
|
||||
switch strings.TrimSpace(key) {
|
||||
case "processor":
|
||||
cpu.Threads++
|
||||
physical = ""
|
||||
case "vendor_id":
|
||||
cpu.Vendor = value
|
||||
case "model name":
|
||||
cpu.Model = value
|
||||
case "physical id":
|
||||
physical = value
|
||||
sockets[value] = true
|
||||
case "core id":
|
||||
if physical == "" {
|
||||
return CPU{}, fmt.Errorf("cpuinfo: core id %s without physical id", value)
|
||||
}
|
||||
cores[physical+"/"+value] = true
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return CPU{}, fmt.Errorf("cpuinfo: %w", err)
|
||||
}
|
||||
if cpu.Threads == 0 || len(sockets) == 0 || len(cores) == 0 {
|
||||
return CPU{}, fmt.Errorf("cpuinfo: %d processors, %d physical ids, %d core ids", cpu.Threads, len(sockets), len(cores))
|
||||
}
|
||||
cpu.Sockets = len(sockets)
|
||||
cpu.Cores = len(cores)
|
||||
return cpu, nil
|
||||
}
|
||||
|
||||
func parseMemTotal(data string) (uint64, error) {
|
||||
for _, line := range strings.Split(data, "\n") {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 3 || fields[0] != "MemTotal:" || fields[2] != "kB" {
|
||||
continue
|
||||
}
|
||||
kb, err := strconv.ParseUint(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("meminfo: MemTotal: %w", err)
|
||||
}
|
||||
return kb * 1024, nil
|
||||
}
|
||||
return 0, fmt.Errorf("meminfo: no MemTotal in kB")
|
||||
}
|
||||
|
||||
func parseOSRelease(data string) OS {
|
||||
var rel OS
|
||||
for _, line := range strings.Split(data, "\n") {
|
||||
key, value, ok := strings.Cut(strings.TrimSpace(line), "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if unquoted, err := strconv.Unquote(value); err == nil {
|
||||
value = unquoted
|
||||
}
|
||||
switch key {
|
||||
case "ID":
|
||||
rel.ID = value
|
||||
case "VERSION_ID":
|
||||
rel.VersionID = value
|
||||
case "PRETTY_NAME":
|
||||
rel.PrettyName = value
|
||||
}
|
||||
}
|
||||
return rel
|
||||
}
|
||||
|
||||
func parseBootTime(data string) (int64, error) {
|
||||
for _, line := range strings.Split(data, "\n") {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) == 2 && fields[0] == "btime" {
|
||||
t, err := strconv.ParseInt(fields[1], 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("stat: btime: %w", err)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("stat: no btime")
|
||||
}
|
||||
|
||||
func parseUptime(line string) (float64, error) {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
return 0, fmt.Errorf("uptime: %q", line)
|
||||
}
|
||||
up, err := strconv.ParseFloat(fields[0], 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("uptime: %w", err)
|
||||
}
|
||||
return up, nil
|
||||
}
|
||||
|
||||
func parseLoadAvg(line string) ([3]float64, error) {
|
||||
var load [3]float64
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 5 {
|
||||
return load, fmt.Errorf("loadavg: %q", line)
|
||||
}
|
||||
for i := range load {
|
||||
v, err := strconv.ParseFloat(fields[i], 64)
|
||||
if err != nil {
|
||||
return load, fmt.Errorf("loadavg: %w", err)
|
||||
}
|
||||
load[i] = v
|
||||
}
|
||||
return load, nil
|
||||
}
|
||||
128
internal/host/host_test.go
Normal file
128
internal/host/host_test.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func debian12() fstest.MapFS {
|
||||
fsys := fstest.MapFS{}
|
||||
for _, name := range []string{
|
||||
"proc/cpuinfo", "proc/meminfo", "proc/stat", "proc/uptime", "proc/loadavg",
|
||||
"proc/sys/kernel/osrelease", "proc/sys/kernel/hostname", "etc/os-release",
|
||||
} {
|
||||
data, err := os.ReadFile("testdata/debian12/" + name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fsys[name] = &fstest.MapFile{Data: data}
|
||||
}
|
||||
return fsys
|
||||
}
|
||||
|
||||
func TestReadFacts_Debian12(t *testing.T) {
|
||||
got, err := ReadFacts(debian12())
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFacts: %v", err)
|
||||
}
|
||||
want := Facts{
|
||||
Hostname: "hv1",
|
||||
CPU: CPU{Vendor: "AuthenticAMD", Model: "QEMU Virtual CPU version 2.5+", Sockets: 1, Cores: 2, Threads: 2},
|
||||
MemoryBytes: 2010432 * 1024,
|
||||
OS: OS{ID: "debian", VersionID: "12", PrettyName: "Debian GNU/Linux 12 (bookworm)"},
|
||||
Kernel: "6.8.0-50-generic",
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("faits %+v, %+v attendus", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadState_Debian12(t *testing.T) {
|
||||
got, err := ReadState(debian12())
|
||||
if err != nil {
|
||||
t.Fatalf("ReadState: %v", err)
|
||||
}
|
||||
want := State{BootTime: 1792210880, UptimeSeconds: 1243445.41, Load: [3]float64{0.08, 0.03, 0.04}}
|
||||
if got != want {
|
||||
t.Errorf("état %+v, %+v attendu", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCPUInfo_SocketsCoresAndThreadsAreDistinct(t *testing.T) {
|
||||
data, err := os.ReadFile("testdata/cpuinfo-2x2x2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := parseCPUInfo(string(data))
|
||||
if err != nil {
|
||||
t.Fatalf("parseCPUInfo: %v", err)
|
||||
}
|
||||
if got.Sockets != 2 || got.Cores != 4 || got.Threads != 8 {
|
||||
t.Errorf("%d sockets, %d cœurs, %d threads ; 2, 4 et 8 attendus", got.Sockets, got.Cores, got.Threads)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCPUInfo_RefusesMissingTopology(t *testing.T) {
|
||||
data, err := os.ReadFile("testdata/debian12/proc/cpuinfo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for label, input := range map[string]string{
|
||||
"vide": "",
|
||||
"sans physical id": strings.ReplaceAll(string(data), "physical id\t: 0\n", ""),
|
||||
"sans core id": strings.ReplaceAll(strings.ReplaceAll(string(data), "core id\t\t: 0\n", ""), "core id\t\t: 1\n", ""),
|
||||
} {
|
||||
if _, err := parseCPUInfo(input); err == nil {
|
||||
t.Errorf("%s : accepté", label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFacts_FailsOnAMissingFile(t *testing.T) {
|
||||
for _, name := range []string{"proc/cpuinfo", "proc/meminfo", "etc/os-release", "proc/sys/kernel/osrelease", "proc/sys/kernel/hostname"} {
|
||||
fsys := debian12()
|
||||
delete(fsys, name)
|
||||
if _, err := ReadFacts(fsys); err == nil || !strings.Contains(err.Error(), name) {
|
||||
t.Errorf("sans %s : erreur %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadState_FailsOnAMissingFile(t *testing.T) {
|
||||
for _, name := range []string{"proc/stat", "proc/uptime", "proc/loadavg"} {
|
||||
fsys := debian12()
|
||||
delete(fsys, name)
|
||||
if _, err := ReadState(fsys); err == nil || !strings.Contains(err.Error(), name) {
|
||||
t.Errorf("sans %s : erreur %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOSRelease_Rocky10Quoted(t *testing.T) {
|
||||
data, err := os.ReadFile("testdata/rocky10/etc/os-release")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := parseOSRelease(string(data))
|
||||
want := OS{ID: "rocky", VersionID: "10.2", PrettyName: "Rocky Linux 10.2 (Red Quartz)"}
|
||||
if got != want {
|
||||
t.Errorf("%+v, %+v attendu", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsers_RefuseMalformedInput(t *testing.T) {
|
||||
if _, err := parseMemTotal("MemFree: 12 kB\n"); err == nil {
|
||||
t.Error("meminfo sans MemTotal accepté")
|
||||
}
|
||||
if _, err := parseBootTime("cpu 1 2 3\n"); err == nil {
|
||||
t.Error("stat sans btime accepté")
|
||||
}
|
||||
if _, err := parseUptime("12.5"); err == nil {
|
||||
t.Error("uptime à un champ accepté")
|
||||
}
|
||||
if _, err := parseLoadAvg("0.1 0.2 x 1/2 3"); err == nil {
|
||||
t.Error("loadavg invalide accepté")
|
||||
}
|
||||
}
|
||||
224
internal/host/testdata/cpuinfo-2x2x2
vendored
Normal file
224
internal/host/testdata/cpuinfo-2x2x2
vendored
Normal file
|
|
@ -0,0 +1,224 @@
|
|||
processor : 0
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 0
|
||||
siblings : 4
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 1
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 0
|
||||
siblings : 4
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 2
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 1
|
||||
siblings : 4
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 3
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 1
|
||||
siblings : 4
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 4
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 0
|
||||
siblings : 4
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 5
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 0
|
||||
siblings : 4
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 6
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 1
|
||||
siblings : 4
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 7
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 1
|
||||
siblings : 4
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
9
internal/host/testdata/debian12/etc/os-release
vendored
Normal file
9
internal/host/testdata/debian12/etc/os-release
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="12"
|
||||
VERSION="12 (bookworm)"
|
||||
VERSION_CODENAME=bookworm
|
||||
ID=debian
|
||||
HOME_URL="https://www.debian.org/"
|
||||
SUPPORT_URL="https://www.debian.org/support"
|
||||
BUG_REPORT_URL="https://bugs.debian.org/"
|
||||
56
internal/host/testdata/debian12/proc/cpuinfo
vendored
Normal file
56
internal/host/testdata/debian12/proc/cpuinfo
vendored
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
processor : 0
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 0
|
||||
siblings : 2
|
||||
core id : 0
|
||||
cpu cores : 2
|
||||
apicid : 0
|
||||
initial apicid : 0
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 1999.99
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
processor : 1
|
||||
vendor_id : AuthenticAMD
|
||||
cpu family : 15
|
||||
model : 107
|
||||
model name : QEMU Virtual CPU version 2.5+
|
||||
stepping : 1
|
||||
microcode : 0x1000065
|
||||
cpu MHz : 999.997
|
||||
cache size : 512 KB
|
||||
physical id : 0
|
||||
siblings : 2
|
||||
core id : 1
|
||||
cpu cores : 2
|
||||
apicid : 1
|
||||
initial apicid : 1
|
||||
fpu : yes
|
||||
fpu_exception : yes
|
||||
cpuid level : 13
|
||||
wp : yes
|
||||
flags : fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl cpuid extd_apicid pni cx16 hypervisor lahf_lm cmp_legacy svm 3dnowprefetch vmmcall
|
||||
bugs : fxsave_leak sysret_ss_attrs null_seg swapgs_fence amd_e400 spectre_v1 spectre_v2
|
||||
bogomips : 2503.44
|
||||
TLB size : 1024 4K pages
|
||||
clflush size : 64
|
||||
cache_alignment : 64
|
||||
address sizes : 40 bits physical, 48 bits virtual
|
||||
power management:
|
||||
|
||||
1
internal/host/testdata/debian12/proc/loadavg
vendored
Normal file
1
internal/host/testdata/debian12/proc/loadavg
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
0.08 0.03 0.04 1/144 6
|
||||
54
internal/host/testdata/debian12/proc/meminfo
vendored
Normal file
54
internal/host/testdata/debian12/proc/meminfo
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
MemTotal: 2010432 kB
|
||||
MemFree: 690340 kB
|
||||
MemAvailable: 1620016 kB
|
||||
Buffers: 177720 kB
|
||||
Cached: 837308 kB
|
||||
SwapCached: 0 kB
|
||||
Active: 409508 kB
|
||||
Inactive: 681136 kB
|
||||
Active(anon): 81680 kB
|
||||
Inactive(anon): 72 kB
|
||||
Active(file): 327828 kB
|
||||
Inactive(file): 681064 kB
|
||||
Unevictable: 11868 kB
|
||||
Mlocked: 0 kB
|
||||
SwapTotal: 0 kB
|
||||
SwapFree: 0 kB
|
||||
Zswap: 0 kB
|
||||
Zswapped: 0 kB
|
||||
Dirty: 568 kB
|
||||
Writeback: 0 kB
|
||||
AnonPages: 80808 kB
|
||||
Mapped: 166636 kB
|
||||
Shmem: 4936 kB
|
||||
KReclaimable: 105056 kB
|
||||
Slab: 159000 kB
|
||||
SReclaimable: 105056 kB
|
||||
SUnreclaim: 53944 kB
|
||||
KernelStack: 2320 kB
|
||||
PageTables: 2928 kB
|
||||
SecPageTables: 0 kB
|
||||
NFS_Unstable: 0 kB
|
||||
Bounce: 0 kB
|
||||
WritebackTmp: 0 kB
|
||||
CommitLimit: 1005216 kB
|
||||
Committed_AS: 496972 kB
|
||||
VmallocTotal: 34359738367 kB
|
||||
VmallocUsed: 17080 kB
|
||||
VmallocChunk: 0 kB
|
||||
Percpu: 1192 kB
|
||||
HardwareCorrupted: 0 kB
|
||||
AnonHugePages: 0 kB
|
||||
ShmemHugePages: 0 kB
|
||||
ShmemPmdMapped: 0 kB
|
||||
FileHugePages: 0 kB
|
||||
FilePmdMapped: 0 kB
|
||||
Unaccepted: 0 kB
|
||||
HugePages_Total: 0
|
||||
HugePages_Free: 0
|
||||
HugePages_Rsvd: 0
|
||||
HugePages_Surp: 0
|
||||
Hugepagesize: 2048 kB
|
||||
Hugetlb: 0 kB
|
||||
DirectMap4k: 112268 kB
|
||||
DirectMap2M: 1980416 kB
|
||||
10
internal/host/testdata/debian12/proc/stat
vendored
Normal file
10
internal/host/testdata/debian12/proc/stat
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
cpu 2014808 0 3797047 237290637 7608 0 66047 0 0 0
|
||||
cpu0 997273 0 1861281 118761074 3753 0 27566 0 0 0
|
||||
cpu1 1017534 0 1935766 118529563 3854 0 38481 0 0 0
|
||||
intr 191922858 235 9 0 0 1122 0 0 0 0 0 0 0 144 0 0 0 0 0 0 0 0 0 0 0 0 6 0 3 0 274341 277139 0 0 0 281571 324044 0 1603949 1543300 0 20236 0 1770807 0 21 0 0 0 0 0 0 0 0 0 5953816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
||||
ctxt 212551065
|
||||
btime 1792210880
|
||||
processes 2962640
|
||||
procs_running 1
|
||||
procs_blocked 0
|
||||
softirq 141699889 5 32984936 1057 2776040 0 0 74 54302609 3 51635165
|
||||
1
internal/host/testdata/debian12/proc/sys/kernel/hostname
vendored
Normal file
1
internal/host/testdata/debian12/proc/sys/kernel/hostname
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
hv1
|
||||
1
internal/host/testdata/debian12/proc/sys/kernel/osrelease
vendored
Normal file
1
internal/host/testdata/debian12/proc/sys/kernel/osrelease
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
6.8.0-50-generic
|
||||
1
internal/host/testdata/debian12/proc/uptime
vendored
Normal file
1
internal/host/testdata/debian12/proc/uptime
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
1243445.41 2372906.39
|
||||
20
internal/host/testdata/rocky10/etc/os-release
vendored
Normal file
20
internal/host/testdata/rocky10/etc/os-release
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
NAME="Rocky Linux"
|
||||
VERSION="10.2 (Red Quartz)"
|
||||
RELEASE_TYPE="stable"
|
||||
ID="rocky"
|
||||
ID_LIKE="rhel centos fedora"
|
||||
VERSION_ID="10.2"
|
||||
PLATFORM_ID="platform:el10"
|
||||
PRETTY_NAME="Rocky Linux 10.2 (Red Quartz)"
|
||||
ANSI_COLOR="0;32"
|
||||
LOGO="fedora-logo-icon"
|
||||
CPE_NAME="cpe:/o:rocky:rocky:10::baseos"
|
||||
HOME_URL="https://rockylinux.org/"
|
||||
VENDOR_NAME="RESF"
|
||||
VENDOR_URL="https://resf.org/"
|
||||
BUG_REPORT_URL="https://bugs.rockylinux.org/"
|
||||
SUPPORT_END="2035-05-31"
|
||||
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-10"
|
||||
ROCKY_SUPPORT_PRODUCT_VERSION="10.2"
|
||||
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION="10.2"
|
||||
|
|
@ -113,6 +113,16 @@
|
|||
- Une IP flottante est refusée (409) sur une interface d'un subnet `public_ip`. Le watchdog
|
||||
contrôle les subnets `public_ip` comme les subnets `bridge`
|
||||
|
||||
**Connaissance de l'hôte (#39)**
|
||||
|
||||
- `GET /register` : hostname, CPU (fabricant, modèle, sockets, cœurs, threads), RAM totale, OS
|
||||
et noyau, lus au démarrage de l'agent (503 si la lecture a échoué)
|
||||
- `GET /ping` : version de l'agent, démarrages de l'hôte et de l'agent (timestamps Unix),
|
||||
uptime de l'hôte et load, mesurés toutes les `times.host_state_interval` secondes (10 par
|
||||
défaut) et gardés en mémoire, avec l'heure de la mesure (`collected_at`)
|
||||
- **L'agent connaît sa version** : injectée au build (tag de la release), affichée dans
|
||||
`/ping` et dans la ligne `starting agent` du journal
|
||||
|
||||
## Correctifs
|
||||
|
||||
- **Chaque VRF de VPC porte une route `unreachable default` (#81).** Une recherche qui échouait
|
||||
|
|
|
|||
|
|
@ -30,11 +30,24 @@ clean () {
|
|||
done
|
||||
}
|
||||
|
||||
build_version () {
|
||||
local commit changes
|
||||
commit=$(git -C "${REPO_DIR}" rev-parse --short HEAD) || return 1
|
||||
changes=$(git -C "${REPO_DIR}" status --porcelain) || return 1
|
||||
if [ -n "${changes}" ]; then
|
||||
echo "${commit}-dirty"
|
||||
else
|
||||
echo "${commit}"
|
||||
fi
|
||||
}
|
||||
|
||||
build () {
|
||||
local binary unit script
|
||||
local binary unit script version
|
||||
version=$(build_version) || return 1
|
||||
echo "== version ${version}" >&2
|
||||
for binary in ${BINARIES}; do
|
||||
echo "== ${binary}" >&2
|
||||
(cd "${REPO_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "${BIN_DIR}/${binary}" "./cmd/${binary}") || return 1
|
||||
(cd "${REPO_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=${version}" -o "${BIN_DIR}/${binary}" "./cmd/${binary}") || return 1
|
||||
done
|
||||
for unit in ${UNITS}; do
|
||||
cp "${REPO_DIR}/systemd/${unit}" "${BIN_DIR}/${unit}" || return 1
|
||||
|
|
|
|||
|
|
@ -826,6 +826,23 @@ PY
|
|||
teardown
|
||||
}
|
||||
|
||||
test_build_version_is_the_commit_dirty_when_the_tree_changed () {
|
||||
setup "build_version : commit court, suffixé -dirty si l'arbre a des modifications, échec hors dépôt"
|
||||
local repo="${WORK}/repo" commit
|
||||
git init -q "${repo}" && printf 'a\n' > "${repo}/f" && git -C "${repo}" add f \
|
||||
&& git -C "${repo}" -c user.name=t -c user.email=t@t commit -q -m t || fail "dépôt de test"
|
||||
commit=$(git -C "${repo}" rev-parse --short HEAD)
|
||||
build_version_in () { bash -c ". '${DIR}/build.sh'; REPO_DIR='${1}'; build_version" 2>&1; }
|
||||
[[ $(build_version_in "${repo}") == "${commit}" ]] || fail "arbre propre : $(build_version_in "${repo}")"
|
||||
printf 'b\n' > "${repo}/f"
|
||||
[[ $(build_version_in "${repo}") == "${commit}-dirty" ]] || fail "fichier modifié : $(build_version_in "${repo}")"
|
||||
git -C "${repo}" checkout -q f && printf 'n\n' > "${repo}/nouveau"
|
||||
[[ $(build_version_in "${repo}") == "${commit}-dirty" ]] || fail "fichier non suivi : $(build_version_in "${repo}")"
|
||||
mkdir "${WORK}/hors-depot"
|
||||
build_version_in "${WORK}/hors-depot" >/dev/null && fail "hors dépôt : réussite"
|
||||
teardown
|
||||
}
|
||||
|
||||
for T in $(declare -F | awk '{print $3}' | grep '^test_'); do
|
||||
"${T}"
|
||||
done
|
||||
|
|
|
|||
44
test/e2e/suite/13-hote.sh
Normal file
44
test/e2e/suite/13-hote.sh
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# fonctionnalité: connaissance de l'hôte — /register (hostname, CPU, RAM, OS, noyau) lu au démarrage, /ping (version, démarrages de l'hôte et de l'agent, uptime, load) mesuré périodiquement, lecture seule, redémarrage de l'agent distingué de celui de l'hôte
|
||||
# hôtes: hv1
|
||||
# tickets: #39
|
||||
|
||||
on hv1 <<'NODE'
|
||||
reg () { api_field register "${1}"; }
|
||||
ping_field () { api_field ping "${1}"; }
|
||||
lscpu_field () { lscpu | awk -F: -v k="${1}" '$1 == k {gsub(/ /, "", $2); print $2}'; }
|
||||
os_field () { (. /etc/os-release && eval "echo \"\${${1}}\""); }
|
||||
near () { awk -v a="${1}" -v b="${2}" -v d="${3}" 'BEGIN { exit !((a - b) <= d && (b - a) <= d) }' || { echo "${1} et ${2} à plus de ${3} s"; return 1; }; }
|
||||
ping_ready () { local i; for i in $(seq 1 30); do [ "$(api_code GET ping)" = 200 ] && return 0; sleep 1; done; return 1; }
|
||||
|
||||
check "/register : 200" expect_code 200 api_code GET register
|
||||
check "/register : hostname = hostname" test "$(reg hostname)" = "$(hostname)"
|
||||
check "/register : threads = nproc" test "$(reg cpu.threads)" = "$(nproc --all)"
|
||||
check "/register : sockets = lscpu" test "$(reg cpu.sockets)" = "$(lscpu_field 'Socket(s)')"
|
||||
check "/register : cœurs = sockets × cœurs par socket (lscpu)" test "$(reg cpu.cores)" = "$(( $(lscpu_field 'Socket(s)') * $(lscpu_field 'Core(s) per socket') ))"
|
||||
check "/register : modèle = lscpu" test "$(reg cpu.model)" = "$(lscpu | sed -n 's/^Model name: *//p')"
|
||||
check "/register : fabricant = lscpu" test "$(reg cpu.vendor)" = "$(lscpu | sed -n 's/^Vendor ID: *//p')"
|
||||
check "/register : RAM = MemTotal" test "$(reg memory_bytes)" = "$(awk '/^MemTotal:/ {printf "%.0f\n", $2 * 1024}' /proc/meminfo)"
|
||||
check "/register : noyau = uname -r" test "$(reg kernel)" = "$(uname -r)"
|
||||
check "/register : OS = os-release (ID, VERSION_ID, PRETTY_NAME)" test "$(reg os.id)|$(reg os.version_id)|$(reg os.pretty_name)" = "$(os_field ID)|$(os_field VERSION_ID)|$(os_field PRETTY_NAME)"
|
||||
|
||||
check "/ping : 200" expect_code 200 api_code GET ping
|
||||
VERSION=$(ping_field agent_version)
|
||||
info "version de l'agent : ${VERSION}"
|
||||
check "/ping : version injectée au build (commit ou tag, pas undefined)" sh -c "echo '${VERSION}' | grep -Eq '^([0-9a-f]{7,}(-dirty)?|[0-9]+\.[0-9]+\.[0-9]+.*)$'"
|
||||
check "/ping : boot_time = btime" test "$(ping_field boot_time)" = "$(awk '/^btime/ {print $2}' /proc/stat)"
|
||||
check "/ping : boot_time + uptime_seconds = collected_at (horloge de l'hôte)" near "$(awk -v b="$(ping_field boot_time)" -v u="$(ping_field uptime_seconds)" 'BEGIN { printf "%d", b + u }')" "$(ping_field collected_at)" 2
|
||||
check "/ping : mesure de moins de 15 s" near "$(ping_field collected_at)" "$(date +%s)" 15
|
||||
check "/ping : trois valeurs de load" test "$(api_field ping 'load | length')" = 3
|
||||
check "/ping : agent_start_time = démarrage de l'unité agent" near "$(ping_field agent_start_time)" "$(date -d "$(systemctl show agent -p ActiveEnterTimestamp --value)" +%s)" 2
|
||||
for path in register ping; do
|
||||
check "POST /${path} : 405" expect_code 405 api_code POST "${path}"
|
||||
done
|
||||
|
||||
BOOT=$(ping_field boot_time)
|
||||
START=$(ping_field agent_start_time)
|
||||
sleep 1
|
||||
check "redémarrage de l'agent" systemctl restart agent
|
||||
check "/ping de nouveau servi après le redémarrage" ping_ready
|
||||
check "après redémarrage : boot_time inchangé" test "$(ping_field boot_time)" = "${BOOT}"
|
||||
check "après redémarrage : agent_start_time plus récent" test "$(ping_field agent_start_time)" -gt "${START}"
|
||||
NODE
|
||||
Loading…
Add table
Add a link
Reference in a new issue