mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2024-12-14 11:57:51 +00:00
api/nfd: use varargs in the NewInstanceFeatures helper
Make usage of this helper function more flexible.
This commit is contained in:
parent
81bdf1dadb
commit
719c5186f6
6 changed files with 15 additions and 20 deletions
|
@ -54,7 +54,7 @@ func NewAttributeFeatures(values map[string]string) AttributeFeatureSet {
|
|||
}
|
||||
|
||||
// NewInstanceFeatures creates a new instance of InstanceFeatureSet.
|
||||
func NewInstanceFeatures(instances []InstanceFeature) InstanceFeatureSet {
|
||||
func NewInstanceFeatures(instances ...InstanceFeature) InstanceFeatureSet {
|
||||
return InstanceFeatureSet{Elements: instances}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,22 +84,20 @@ func TestInstanceFeatureSet(t *testing.T) {
|
|||
f2.MergeInto(&f1)
|
||||
assert.Equal(t, expectedElems, f1.Elements)
|
||||
|
||||
f2 = NewInstanceFeatures([]InstanceFeature{})
|
||||
f2 = NewInstanceFeatures()
|
||||
expectedElems = []InstanceFeature{}
|
||||
f2.MergeInto(&f1)
|
||||
assert.Equal(t, expectedElems, f1.Elements)
|
||||
|
||||
f2 = NewInstanceFeatures([]InstanceFeature{InstanceFeature{}})
|
||||
f2 = NewInstanceFeatures(InstanceFeature{})
|
||||
expectedElems = append(expectedElems, InstanceFeature{})
|
||||
f2.MergeInto(&f1)
|
||||
assert.Equal(t, expectedElems, f1.Elements)
|
||||
|
||||
f2 = NewInstanceFeatures([]InstanceFeature{
|
||||
InstanceFeature{
|
||||
Attributes: map[string]string{
|
||||
"a1": "v1",
|
||||
"a2": "v2",
|
||||
},
|
||||
f2 = NewInstanceFeatures(InstanceFeature{
|
||||
Attributes: map[string]string{
|
||||
"a1": "v1",
|
||||
"a2": "v2",
|
||||
},
|
||||
})
|
||||
expectedElems = append(expectedElems, *NewInstanceFeature(map[string]string{"a1": "v1", "a2": "v2"}))
|
||||
|
@ -139,18 +137,16 @@ func TestFeature(t *testing.T) {
|
|||
f2 = *NewFeatures()
|
||||
f2.Flags["dom.flag"] = NewFlagFeatures("k1", "k2")
|
||||
f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1", "k2": "v2"})
|
||||
f2.Instances["dom.inst"] = NewInstanceFeatures([]InstanceFeature{
|
||||
f2.Instances["dom.inst"] = NewInstanceFeatures(
|
||||
*NewInstanceFeature(map[string]string{"a1": "v1.1", "a2": "v1.2"}),
|
||||
*NewInstanceFeature(map[string]string{"a1": "v2.1", "a2": "v2.2"}),
|
||||
})
|
||||
)
|
||||
f2.MergeInto(&f)
|
||||
assert.Equal(t, f2, f)
|
||||
|
||||
f2.Flags["dom.flag"] = NewFlagFeatures("k3")
|
||||
f2.Attributes["dom.attr"] = NewAttributeFeatures(map[string]string{"k1": "v1.override"})
|
||||
f2.Instances["dom.inst"] = NewInstanceFeatures([]InstanceFeature{
|
||||
*NewInstanceFeature(map[string]string{"a1": "v3.1", "a3": "v3.3"}),
|
||||
})
|
||||
f2.Instances["dom.inst"] = NewInstanceFeatures(*NewInstanceFeature(map[string]string{"a1": "v3.1", "a3": "v3.3"}))
|
||||
f2.MergeInto(&f)
|
||||
expectedFeatures = *NewFeatures()
|
||||
expectedFeatures.Flags["dom.flag"] = FlagFeatureSet{Elements: map[string]Nil{"k1": Nil{}, "k2": Nil{}, "k3": Nil{}}}
|
||||
|
|
|
@ -70,7 +70,7 @@ func TestRule(t *testing.T) {
|
|||
// Test empty feature sets
|
||||
f.Flags["domain-1.kf-1"] = nfdv1alpha1.NewFlagFeatures()
|
||||
f.Attributes["domain-1.vf-1"] = nfdv1alpha1.NewAttributeFeatures(nil)
|
||||
f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures(nil)
|
||||
f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures()
|
||||
|
||||
m, err = Execute(r1, f)
|
||||
assert.Nilf(t, err, "unexpected error: %v", err)
|
||||
|
@ -83,8 +83,7 @@ func TestRule(t *testing.T) {
|
|||
// Test non-empty feature sets
|
||||
f.Flags["domain-1.kf-1"].Elements["key-x"] = nfdv1alpha1.Nil{}
|
||||
f.Attributes["domain-1.vf-1"].Elements["key-1"] = "val-x"
|
||||
f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures([]nfdv1alpha1.InstanceFeature{
|
||||
*nfdv1alpha1.NewInstanceFeature(map[string]string{"attr-1": "val-x"})})
|
||||
f.Instances["domain-1.if-1"] = nfdv1alpha1.NewInstanceFeatures(*nfdv1alpha1.NewInstanceFeature(map[string]string{"attr-1": "val-x"}))
|
||||
|
||||
m, err = Execute(r1, f)
|
||||
assert.Nilf(t, err, "unexpected error: %v", err)
|
||||
|
|
|
@ -126,7 +126,7 @@ func (s *fakeSource) Discover() error {
|
|||
for i, instanceAttributes := range s.config.InstanceFeatures {
|
||||
instances[i] = *nfdv1alpha1.NewInstanceFeature(instanceAttributes)
|
||||
}
|
||||
s.features.Instances[InstanceFeature] = nfdv1alpha1.NewInstanceFeatures(instances)
|
||||
s.features.Instances[InstanceFeature] = nfdv1alpha1.NewInstanceFeatures(instances...)
|
||||
|
||||
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ func (s *pciSource) Discover() error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to detect PCI devices: %s", err.Error())
|
||||
}
|
||||
s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs)
|
||||
s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs...)
|
||||
|
||||
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))
|
||||
|
||||
|
|
|
@ -142,7 +142,7 @@ func (s *usbSource) Discover() error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to detect USB devices: %s", err.Error())
|
||||
}
|
||||
s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs)
|
||||
s.features.Instances[DeviceFeature] = nfdv1alpha1.NewInstanceFeatures(devs...)
|
||||
|
||||
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))
|
||||
|
||||
|
|
Loading…
Reference in a new issue