feat: implement nixos-exporter
Prometheus exporter for NixOS-specific metrics including: - Generation collector: count, current, booted, age, config mismatch - Flake collector: input age, input info, revision behind Includes NixOS module, flake packaging, and documentation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
151
collector/flake_test.go
Normal file
151
collector/flake_test.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRevisionPattern(t *testing.T) {
|
||||
tests := []struct {
|
||||
version string
|
||||
wantRev string
|
||||
}{
|
||||
{"25.11.20260203.e576e3c", "e576e3c"},
|
||||
{"1994-294a625", "294a625"},
|
||||
{"25.05.20250101.abcdef1234567890", "abcdef1234567890"},
|
||||
{"no-revision-here", ""},
|
||||
{"", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.version, func(t *testing.T) {
|
||||
matches := revisionPattern.FindStringSubmatch(tt.version)
|
||||
var got string
|
||||
if matches != nil {
|
||||
got = matches[1]
|
||||
}
|
||||
if got != tt.wantRev {
|
||||
t.Errorf("revisionPattern.FindStringSubmatch(%q) = %q, want %q", tt.version, got, tt.wantRev)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCurrentSystemRevision(t *testing.T) {
|
||||
// Skip if not on NixOS
|
||||
if _, err := os.Stat(nixosVersionPath); os.IsNotExist(err) {
|
||||
t.Skip("not running on NixOS")
|
||||
}
|
||||
|
||||
rev, err := getCurrentSystemRevision()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Just check it returns something reasonable
|
||||
t.Logf("current system revision: %s", rev)
|
||||
}
|
||||
|
||||
func TestGetCurrentSystemRevisionFromFile(t *testing.T) {
|
||||
// Create a temp file to simulate /run/current-system/nixos-version
|
||||
dir := t.TempDir()
|
||||
versionPath := filepath.Join(dir, "nixos-version")
|
||||
|
||||
tests := []struct {
|
||||
content string
|
||||
wantRev string
|
||||
}{
|
||||
{"25.11.20260203.e576e3c\n", "e576e3c"},
|
||||
{"1994-294a625\n", "294a625"},
|
||||
{"25.05.20250101.abcdef1234567890\n", "abcdef1"},
|
||||
{"no-hash", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.content, func(t *testing.T) {
|
||||
if err := os.WriteFile(versionPath, []byte(tt.content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// We can't easily test the actual function without modifying the constant,
|
||||
// so we test the pattern extraction logic directly
|
||||
version := tt.content
|
||||
if len(version) > 0 && version[len(version)-1] == '\n' {
|
||||
version = version[:len(version)-1]
|
||||
}
|
||||
|
||||
matches := revisionPattern.FindStringSubmatch(version)
|
||||
var rev string
|
||||
if matches != nil {
|
||||
rev = matches[1]
|
||||
if len(rev) > 7 {
|
||||
rev = rev[:7]
|
||||
}
|
||||
}
|
||||
|
||||
if rev != tt.wantRev {
|
||||
t.Errorf("got revision %q, want %q", rev, tt.wantRev)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlakeLocksUnmarshal(t *testing.T) {
|
||||
jsonData := `{
|
||||
"revision": "abc1234567890",
|
||||
"locks": {
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1700000000,
|
||||
"rev": "def4567890123",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"home-manager": {
|
||||
"locked": {
|
||||
"lastModified": 1699000000,
|
||||
"rev": "ghi7890123456",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs",
|
||||
"home-manager": "home-manager"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root"
|
||||
}
|
||||
}`
|
||||
|
||||
var data flakeMetadata
|
||||
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if data.Revision != "abc1234567890" {
|
||||
t.Errorf("expected revision abc1234567890, got %s", data.Revision)
|
||||
}
|
||||
|
||||
if len(data.Locks.Nodes) != 3 {
|
||||
t.Errorf("expected 3 nodes, got %d", len(data.Locks.Nodes))
|
||||
}
|
||||
|
||||
nixpkgs := data.Locks.Nodes["nixpkgs"]
|
||||
if nixpkgs.Locked == nil {
|
||||
t.Fatal("expected nixpkgs to have locked info")
|
||||
}
|
||||
if nixpkgs.Locked.LastModified != 1700000000 {
|
||||
t.Errorf("expected lastModified 1700000000, got %d", nixpkgs.Locked.LastModified)
|
||||
}
|
||||
if nixpkgs.Locked.Rev != "def4567890123" {
|
||||
t.Errorf("expected rev def4567890123, got %s", nixpkgs.Locked.Rev)
|
||||
}
|
||||
if nixpkgs.Locked.Type != "github" {
|
||||
t.Errorf("expected type github, got %s", nixpkgs.Locked.Type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user