Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/nfd: use varargs in the NewInstanceFeatures helper #1669

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/nfd/v1alpha1/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down
22 changes: 9 additions & 13 deletions api/nfd/v1alpha1/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
Expand Down Expand Up @@ -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{}}}
Expand Down
5 changes: 2 additions & 3 deletions pkg/apis/nfd/nodefeaturerule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion source/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion source/pci/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion source/usb/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
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...)

Check warning on line 145 in source/usb/usb.go

View check run for this annotation

Codecov / codecov/patch

source/usb/usb.go#L145

Added line #L145 was not covered by tests

klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))

Expand Down