mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2026-07-22 06:50:38 +02:00
feat: add isNodeUsed helper for NodeProvider lookups
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -633,6 +633,19 @@ func GetSelectors(ms *MemoryStore, excludeSelectors map[string][]string) [][]str
|
||||
return filteredSelectors
|
||||
}
|
||||
|
||||
// isNodeUsed reports whether cluster/host appears in the used-nodes map
|
||||
// returned by NodeProvider.GetUsedNodes. Host lists are sorted per the
|
||||
// interface contract, so lookup is a binary search. A nil map means no
|
||||
// node is in use.
|
||||
func isNodeUsed(used map[string][]string, cluster, host string) bool {
|
||||
hosts, ok := used[cluster]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
_, found := slices.BinarySearch(hosts, host)
|
||||
return found
|
||||
}
|
||||
|
||||
// GetPaths returns a list of lists (paths) to the specified depth.
|
||||
func (ms *MemoryStore) GetPaths(targetDepth int) [][]string {
|
||||
var results [][]string
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
// All rights reserved. This file is part of cc-backend.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package metricstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// fakeNodeProvider implements NodeProvider for tests.
|
||||
type fakeNodeProvider struct {
|
||||
nodes map[string][]string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeNodeProvider) GetUsedNodes(ts int64) (map[string][]string, error) {
|
||||
return f.nodes, f.err
|
||||
}
|
||||
|
||||
func TestIsNodeUsed(t *testing.T) {
|
||||
used := map[string][]string{"fritz": {"node001", "node003"}}
|
||||
|
||||
cases := []struct {
|
||||
cluster, host string
|
||||
want bool
|
||||
}{
|
||||
{"fritz", "node001", true},
|
||||
{"fritz", "node003", true},
|
||||
{"fritz", "node002", false},
|
||||
{"alex", "node001", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := isNodeUsed(used, c.cluster, c.host); got != c.want {
|
||||
t.Errorf("isNodeUsed(%q, %q) = %v, want %v", c.cluster, c.host, got, c.want)
|
||||
}
|
||||
}
|
||||
|
||||
if isNodeUsed(nil, "fritz", "node001") {
|
||||
t.Error("nil map must report false")
|
||||
}
|
||||
if isNodeUsed(map[string][]string{}, "fritz", "node001") {
|
||||
t.Error("empty map must report false")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user