Files
nmon/testdata/gen/main.go

82 lines
2.2 KiB
Go

// Command gen writes the synthetic fixtures into testdata/snmprec: one
// <profile>.snmprec per shipped profile (generated from the profile's own
// metric list), the behaviour fixtures and their <name>.agent.yaml sidecars.
// Run it from the repository root: go run ./testdata/gen
//
// With -expect it also writes a <name>.expect.yaml, for fixtures that have
// none, from a profiletest run against the fixture (-force replaces existing
// ones). Those are DRAFTS: review each one before committing (the PDU counts
// and series lists are the contract a profile change must keep).
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/v10networks/nmon/internal/fixturegen"
"github.com/v10networks/nmon/internal/fixtures"
"github.com/v10networks/nmon/internal/profile"
"github.com/v10networks/nmon/internal/simagent"
)
func main() {
out := flag.String("out", "testdata/snmprec", "output directory")
expect := flag.Bool("expect", false, "also rewrite draft <name>.expect.yaml files by running profiletest against each fixture")
force := flag.Bool("force", false,
"with -expect: overwrite expect.yaml files that already exist (they are reviewed; this discards the review)")
flag.Parse()
if err := run(*out); err != nil {
fmt.Fprintln(os.Stderr, "gen:", err)
os.Exit(1)
}
if *expect {
if err := fixtures.WriteDrafts(context.Background(), *out, *force); err != nil {
fmt.Fprintln(os.Stderr, "gen -expect:", err)
os.Exit(1)
}
}
}
func run(dir string) error {
set, err := profile.Load("")
if err != nil {
return err
}
all, err := fixturegen.All(set)
if err != nil {
return err
}
for _, f := range all {
for _, r := range f.Recordings {
if err := write(filepath.Join(dir, r.Stem+".snmprec"), r.Rec); err != nil {
return err
}
}
y, err := f.AgentYAML()
if err != nil {
return err
}
if y != nil {
if err := os.WriteFile(filepath.Join(dir, f.Name+".agent.yaml"), y, 0o644); err != nil {
return err
}
}
}
return nil
}
func write(path string, rec *simagent.Recording) error {
f, err := os.Create(path)
if err != nil {
return err
}
if err := fixturegen.WriteSnmprec(f, rec); err != nil {
_ = f.Close() // best-effort: the write error is the one to report
return err
}
return f.Close()
}