- Use `nixos-version --json` command instead of reading files directly - Add nixpkgs_rev and nixos_version labels to nixos_flake_info metric - Show "unknown" for current_rev when system.configurationRevision not set - Document configurationRevision setup in README Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
package collector
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os/exec"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetNixosVersionInfo(t *testing.T) {
|
|
// Skip if nixos-version command is not available
|
|
if _, err := exec.LookPath("nixos-version"); err != nil {
|
|
t.Skip("nixos-version command not available")
|
|
}
|
|
|
|
info, err := getNixosVersionInfo()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Logf("NixOS version info: configurationRevision=%s, nixosVersion=%s, nixpkgsRevision=%s",
|
|
info.ConfigurationRevision, info.NixosVersion, info.NixpkgsRevision)
|
|
}
|
|
|
|
func TestNixosVersionInfoUnmarshal(t *testing.T) {
|
|
jsonData := `{
|
|
"configurationRevision": "138aeb9e450890efd7ca3be6159b1eb22256c2e3",
|
|
"nixosVersion": "25.11.20260203.e576e3c",
|
|
"nixpkgsRevision": "e576e3c9cf9bad747afcddd9e34f51d18c855b4e"
|
|
}`
|
|
|
|
var info nixosVersionInfo
|
|
if err := json.Unmarshal([]byte(jsonData), &info); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if info.ConfigurationRevision != "138aeb9e450890efd7ca3be6159b1eb22256c2e3" {
|
|
t.Errorf("expected configurationRevision 138aeb9e450890efd7ca3be6159b1eb22256c2e3, got %s", info.ConfigurationRevision)
|
|
}
|
|
if info.NixosVersion != "25.11.20260203.e576e3c" {
|
|
t.Errorf("expected nixosVersion 25.11.20260203.e576e3c, got %s", info.NixosVersion)
|
|
}
|
|
if info.NixpkgsRevision != "e576e3c9cf9bad747afcddd9e34f51d18c855b4e" {
|
|
t.Errorf("expected nixpkgsRevision e576e3c9cf9bad747afcddd9e34f51d18c855b4e, got %s", info.NixpkgsRevision)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|