Files
cozystack/cmd/gpu-node-agent/main_test.go
Aleksei Sviridkin 0cb44dc919 feat(gpu-node-agent): add the agent command, image and chart
The inventory has to run next to kubelet: a DaemonSet reading the
host's sysfs and kubelet's pod-resources socket, both mounted
read-only. Connecting to the socket needs root, so the agent runs as
root but with no capabilities and no privilege escalation.

The manager's cached client would watch every pod in the cluster, so
the agent uses a direct client and lists only its own node's pods.
RBAC cannot name the caller's node, so the role is cluster-wide; it
holds only the verbs the agent uses, and the DaemonSet selects the
nodes node-feature-discovery marks as carrying an NVIDIA device, so
the token does not sit on nodes with nothing to report.

The image is in the root build list so that every build path stamps
its reference; the committed tag is replaced by the first build.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-21 06:33:45 +03:00

71 lines
2.1 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"testing"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
gpuv1alpha1 "github.com/cozystack/cozystack/api/gpu/v1alpha1"
)
func TestSchemeRegistersWrittenKinds(t *testing.T) {
for _, obj := range []runtime.Object{&gpuv1alpha1.GPUNode{}, &corev1.Node{}, &corev1.Pod{}} {
if _, _, err := scheme.ObjectKinds(obj); err != nil {
t.Errorf("%T: %v", obj, err)
}
}
}
func TestParseFlags(t *testing.T) {
tests := []struct {
name string
args []string
env string
wantErr bool
want options
}{
{
name: "node name from the downward API",
env: "gpu-node-1",
want: options{nodeName: "gpu-node-1", sysfs: "/sys", socket: "/var/lib/kubelet/pod-resources/kubelet.sock", interval: 30 * time.Second, probeAddr: ":8081"},
},
{
name: "flags override",
args: []string{"--node-name=n2", "--sysfs=/host/sys", "--interval=1m"},
want: options{nodeName: "n2", sysfs: "/host/sys", socket: "/var/lib/kubelet/pod-resources/kubelet.sock", interval: time.Minute, probeAddr: ":8081"},
},
{name: "no node name", wantErr: true},
{name: "interval below a second", env: "n", args: []string{"--interval=100ms"}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("NODE_NAME", tt.env)
got, err := parseFlags(tt.args)
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && got != tt.want {
t.Errorf("options = %+v, want %+v", got, tt.want)
}
})
}
}