diff --git a/pkg/metricstore/metricstore.go b/pkg/metricstore/metricstore.go index 1fcd4ab0..c59b67e5 100644 --- a/pkg/metricstore/metricstore.go +++ b/pkg/metricstore/metricstore.go @@ -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 diff --git a/pkg/metricstore/nodeprovider_test.go b/pkg/metricstore/nodeprovider_test.go new file mode 100644 index 00000000..c8f0e5b0 --- /dev/null +++ b/pkg/metricstore/nodeprovider_test.go @@ -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") + } +}