1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-31 04:04:51 +00:00
node-feature-discovery/main_test.go
Cody Roseborough 88c15c2358 Fixes : Deletes non-discovered nfd labels
Adds RemoveLabels() to remove all labels from a node that contains the
supplied search string. Uses this to remove node-feature-discovery
labels prior to re-applying newly discovered labels.

This behavior applies to consecutive runs of node-feature-discovery and
whatever restrictions are placed on it. For example if a whitelist is
specified that changes the labels that are discovered, any labels that
do not match the whitelist will be removed. Adds a small note to the
readme to point out this fact.

Adds tests to main_test.go to test the behavior of AddLabels and
RemoveLabels.
2016-11-02 14:40:17 -07:00

167 lines
5.2 KiB
Go

package main
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/vektra/errors"
"k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
func TestDiscoveryWithMockSources(t *testing.T) {
Convey("When I discover features from fake source and update the node using fake client", t, func() {
mockFeatureSource := new(MockFeatureSource)
fakeFeatureSourceName := string("testSource")
fakeFeatures := []string{"testfeature1", "testfeature2", "testfeature3"}
fakeFeatureLabels := Labels{}
for _, f := range fakeFeatures {
fakeFeatureLabels[fmt.Sprintf("%s-testSource-%s", prefix, f)] = "true"
}
fakeFeatureSource := FeatureSource(mockFeatureSource)
Convey("When I successfully get the labels from the mock source", func() {
mockFeatureSource.On("Name").Return(fakeFeatureSourceName)
mockFeatureSource.On("Discover").Return(fakeFeatures, nil)
returnedLabels, err := getFeatureLabels(fakeFeatureSource)
Convey("Proper label is returned", func() {
So(returnedLabels, ShouldResemble, fakeFeatureLabels)
})
Convey("Error is nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When I fail to get the labels from the mock source", func() {
expectedError := errors.New("fake error")
mockFeatureSource.On("Discover").Return(nil, expectedError)
returnedLabels, err := getFeatureLabels(fakeFeatureSource)
Convey("No label is returned", func() {
So(returnedLabels, ShouldBeNil)
})
Convey("Error is produced", func() {
So(err, ShouldEqual, expectedError)
})
})
mockAPIHelper := new(MockAPIHelpers)
testHelper := APIHelpers(mockAPIHelper)
var mockClient *client.Client
var mockNode *api.Node
Convey("When I successfully advertise feature labels to a node", func() {
mockAPIHelper.On("GetClient").Return(mockClient, nil)
mockAPIHelper.On("GetNode", mockClient).Return(mockNode, nil).Once()
mockAPIHelper.On("AddLabels", mockNode, fakeFeatureLabels).Return().Once()
mockAPIHelper.On("RemoveLabels", mockNode, prefix).Return().Once()
mockAPIHelper.On("UpdateNode", mockClient, mockNode).Return(nil).Once()
err := advertiseFeatureLabels(testHelper, fakeFeatureLabels)
Convey("Error is nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When I fail to get a mock client while advertising feature labels", func() {
expectedError := errors.New("fake error")
mockAPIHelper.On("GetClient").Return(nil, expectedError)
err := advertiseFeatureLabels(testHelper, fakeFeatureLabels)
Convey("Error is produced", func() {
So(err, ShouldEqual, expectedError)
})
})
Convey("When I fail to get a mock node while advertising feature labels", func() {
expectedError := errors.New("fake error")
mockAPIHelper.On("GetClient").Return(mockClient, nil)
mockAPIHelper.On("GetNode", mockClient).Return(nil, expectedError).Once()
err := advertiseFeatureLabels(testHelper, fakeFeatureLabels)
Convey("Error is produced", func() {
So(err, ShouldEqual, expectedError)
})
})
Convey("When I fail to update a mock node while advertising feature labels", func() {
expectedError := errors.New("fake error")
mockAPIHelper.On("GetClient").Return(mockClient, nil)
mockAPIHelper.On("GetNode", mockClient).Return(mockNode, nil).Once()
mockAPIHelper.On("RemoveLabels", mockNode, prefix).Return().Once()
mockAPIHelper.On("AddLabels", mockNode, fakeFeatureLabels).Return().Once()
mockAPIHelper.On("UpdateNode", mockClient, mockNode).Return(expectedError).Once()
err := advertiseFeatureLabels(testHelper, fakeFeatureLabels)
Convey("Error is produced", func() {
So(err, ShouldEqual, expectedError)
})
})
})
}
func TestAddLabels(t *testing.T) {
Convey("When adding labels", t, func() {
helper := k8sHelpers{}
labels := Labels{}
n := &api.Node{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{},
},
}
Convey("If no labels are passed", func() {
helper.AddLabels(n, labels)
Convey("None should be added", func() {
So(len(n.Labels), ShouldEqual, 0)
})
})
Convey("They should be added to the node.Labels", func() {
test1 := prefix + ".test1"
labels[test1] = "true"
helper.AddLabels(n, labels)
So(n.Labels, ShouldContainKey, test1)
})
})
}
func TestRemoveLabels(t *testing.T) {
Convey("When removing labels", t, func() {
helper := k8sHelpers{}
n := &api.Node{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"single": "123",
"multiple_A": "a",
"multiple_B": "b",
},
},
}
Convey("a unique label should be removed", func() {
helper.RemoveLabels(n, "single")
So(len(n.Labels), ShouldEqual, 2)
So(n.Labels, ShouldNotContainKey, "single")
})
Convey("a non-unique search string should remove all matching keys", func() {
helper.RemoveLabels(n, "multiple")
So(len(n.Labels), ShouldEqual, 1)
So(n.Labels, ShouldNotContainKey, "multiple_A")
So(n.Labels, ShouldNotContainKey, "multiple_B")
})
Convey("a search string with no matches should not alter labels", func() {
helper.RemoveLabels(n, "unique")
So(n.Labels, ShouldContainKey, "single")
So(n.Labels, ShouldContainKey, "multiple_A")
So(n.Labels, ShouldContainKey, "multiple_B")
So(len(n.Labels), ShouldEqual, 3)
})
})
}