2019-02-08 19:43:54 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package nfdmaster
|
|
|
|
|
|
|
|
import (
|
2019-05-07 09:41:20 +00:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2019-02-08 19:43:54 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-13 14:14:27 +00:00
|
|
|
"github.com/smartystreets/assertions"
|
2019-02-08 19:43:54 +00:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2020-03-05 14:40:55 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2019-02-08 19:43:54 +00:00
|
|
|
"github.com/vektra/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
api "k8s.io/api/core/v1"
|
2020-03-05 14:40:55 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2019-02-12 13:31:59 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-02-08 19:43:54 +00:00
|
|
|
k8sclient "k8s.io/client-go/kubernetes"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/apihelper"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/labeler"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
2020-08-13 14:14:27 +00:00
|
|
|
"sigs.k8s.io/yaml"
|
2019-02-08 19:43:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mockNodeName = "mock-node"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
nodeName = mockNodeName
|
|
|
|
}
|
|
|
|
|
2019-02-12 13:31:59 +00:00
|
|
|
func newMockNode() *api.Node {
|
|
|
|
n := api.Node{}
|
2020-03-05 14:40:55 +00:00
|
|
|
n.Name = mockNodeName
|
2019-02-12 13:31:59 +00:00
|
|
|
n.Labels = map[string]string{}
|
|
|
|
n.Annotations = map[string]string{}
|
2020-03-05 14:40:55 +00:00
|
|
|
n.Status.Capacity = api.ResourceList{}
|
2019-02-12 13:31:59 +00:00
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:43:54 +00:00
|
|
|
func TestUpdateNodeFeatures(t *testing.T) {
|
|
|
|
Convey("When I update the node using fake client", t, func() {
|
2020-08-12 18:28:21 +00:00
|
|
|
fakeFeatureLabels := map[string]string{LabelNs + "/source-feature.1": "1", LabelNs + "/source-feature.2": "2", LabelNs + "/source-feature.3": "val3"}
|
2020-08-13 14:14:27 +00:00
|
|
|
fakeAnnotations := map[string]string{"my-annotation": "my-val"}
|
2020-08-12 18:28:21 +00:00
|
|
|
fakeExtResources := ExtendedResources{LabelNs + "/source-feature.1": "", LabelNs + "/source-feature.2": ""}
|
2020-08-13 14:14:27 +00:00
|
|
|
|
2019-02-08 19:43:54 +00:00
|
|
|
fakeFeatureLabelNames := make([]string, 0, len(fakeFeatureLabels))
|
2020-05-19 13:18:29 +00:00
|
|
|
for k := range fakeFeatureLabels {
|
2020-08-13 14:14:27 +00:00
|
|
|
fakeFeatureLabelNames = append(fakeFeatureLabelNames, strings.TrimPrefix(k, LabelNs+"/"))
|
2019-02-08 19:43:54 +00:00
|
|
|
}
|
2019-05-08 08:08:14 +00:00
|
|
|
sort.Strings(fakeFeatureLabelNames)
|
2020-08-13 14:14:27 +00:00
|
|
|
|
|
|
|
fakeExtResourceNames := make([]string, 0, len(fakeExtResources))
|
|
|
|
for k := range fakeExtResources {
|
|
|
|
fakeExtResourceNames = append(fakeExtResourceNames, strings.TrimPrefix(k, LabelNs+"/"))
|
|
|
|
}
|
|
|
|
sort.Strings(fakeExtResourceNames)
|
|
|
|
|
2020-08-12 18:28:21 +00:00
|
|
|
fakeAnnotations[AnnotationNs+"/feature-labels"] = strings.Join(fakeFeatureLabelNames, ",")
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
mockAPIHelper := new(apihelper.MockAPIHelpers)
|
|
|
|
mockClient := &k8sclient.Clientset{}
|
2019-02-12 13:31:59 +00:00
|
|
|
// Mock node with old features
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockNode.Labels[LabelNs+"/old-feature"] = "old-value"
|
|
|
|
mockNode.Annotations[AnnotationNs+"/feature-labels"] = "old-feature"
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("When I successfully update the node with feature labels", func() {
|
2020-08-13 14:14:27 +00:00
|
|
|
metadataPatches := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("replace", "/metadata/annotations", AnnotationNs+"/feature-labels", strings.Join(fakeFeatureLabelNames, ",")),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", "my-annotation", "my-val"),
|
|
|
|
apihelper.NewJsonPatch("remove", "/metadata/labels", LabelNs+"/old-feature", ""),
|
|
|
|
}
|
|
|
|
for k, v := range fakeFeatureLabels {
|
|
|
|
metadataPatches = append(metadataPatches, apihelper.NewJsonPatch("add", "/metadata/labels", k, v))
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:43:54 +00:00
|
|
|
mockAPIHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockAPIHelper.On("GetNode", mockClient, mockNodeName).Return(mockNode, nil).Once()
|
2020-08-13 14:14:27 +00:00
|
|
|
mockAPIHelper.On("PatchNode", mockClient, mockNodeName, mock.MatchedBy(jsonPatchMatcher(metadataPatches))).Return(nil)
|
|
|
|
mockAPIHelper.On("PatchNodeStatus", mockClient, mockNodeName, mock.Anything).Return(nil).Twice()
|
2020-03-05 14:40:55 +00:00
|
|
|
err := updateNodeFeatures(mockAPIHelper, mockNodeName, fakeFeatureLabels, fakeAnnotations, fakeExtResources)
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("Error is nil", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When I fail to update the node with feature labels", func() {
|
|
|
|
expectedError := errors.New("fake error")
|
|
|
|
mockAPIHelper.On("GetClient").Return(nil, expectedError)
|
2020-03-05 14:40:55 +00:00
|
|
|
err := updateNodeFeatures(mockAPIHelper, mockNodeName, fakeFeatureLabels, fakeAnnotations, fakeExtResources)
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("Error is produced", func() {
|
|
|
|
So(err, ShouldEqual, expectedError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When I fail to get a mock client while updating feature labels", func() {
|
|
|
|
expectedError := errors.New("fake error")
|
|
|
|
mockAPIHelper.On("GetClient").Return(nil, expectedError)
|
2020-03-05 14:40:55 +00:00
|
|
|
err := updateNodeFeatures(mockAPIHelper, mockNodeName, fakeFeatureLabels, fakeAnnotations, fakeExtResources)
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("Error is produced", func() {
|
|
|
|
So(err, ShouldEqual, expectedError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When I fail to get a mock node while updating feature labels", func() {
|
|
|
|
expectedError := errors.New("fake error")
|
|
|
|
mockAPIHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockAPIHelper.On("GetNode", mockClient, mockNodeName).Return(nil, expectedError).Once()
|
2020-03-05 14:40:55 +00:00
|
|
|
err := updateNodeFeatures(mockAPIHelper, mockNodeName, fakeFeatureLabels, fakeAnnotations, fakeExtResources)
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("Error is produced", func() {
|
|
|
|
So(err, ShouldEqual, expectedError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When I fail to update a mock node while updating feature labels", func() {
|
|
|
|
expectedError := errors.New("fake error")
|
|
|
|
mockAPIHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockAPIHelper.On("GetNode", mockClient, mockNodeName).Return(mockNode, nil).Once()
|
2020-08-13 14:14:27 +00:00
|
|
|
mockAPIHelper.On("PatchNode", mockClient, mockNodeName, mock.Anything).Return(expectedError).Once()
|
2020-03-05 14:40:55 +00:00
|
|
|
err := updateNodeFeatures(mockAPIHelper, mockNodeName, fakeFeatureLabels, fakeAnnotations, fakeExtResources)
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("Error is produced", func() {
|
|
|
|
So(err, ShouldEqual, expectedError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateMasterNode(t *testing.T) {
|
|
|
|
Convey("When updating the nfd-master node", t, func() {
|
|
|
|
mockHelper := &apihelper.MockAPIHelpers{}
|
|
|
|
mockClient := &k8sclient.Clientset{}
|
2019-02-12 13:31:59 +00:00
|
|
|
mockNode := newMockNode()
|
2019-02-08 19:43:54 +00:00
|
|
|
Convey("When update operation succeeds", func() {
|
2020-08-13 14:14:27 +00:00
|
|
|
expectedPatches := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", AnnotationNs+"/master.version", version.Get())}
|
2019-02-08 19:43:54 +00:00
|
|
|
mockHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockHelper.On("GetNode", mockClient, mockNodeName).Return(mockNode, nil)
|
2020-08-13 14:14:27 +00:00
|
|
|
mockHelper.On("PatchNode", mockClient, mockNodeName, mock.MatchedBy(jsonPatchMatcher(expectedPatches))).Return(nil)
|
2019-02-08 19:43:54 +00:00
|
|
|
err := updateMasterNode(mockHelper)
|
|
|
|
Convey("No error should be returned", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
mockErr := errors.New("mock-error")
|
|
|
|
Convey("When getting API client fails", func() {
|
|
|
|
mockHelper.On("GetClient").Return(mockClient, mockErr)
|
|
|
|
err := updateMasterNode(mockHelper)
|
|
|
|
Convey("An error should be returned", func() {
|
|
|
|
So(err, ShouldEqual, mockErr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When getting API node object fails", func() {
|
|
|
|
mockHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockHelper.On("GetNode", mockClient, mockNodeName).Return(mockNode, mockErr)
|
|
|
|
err := updateMasterNode(mockHelper)
|
|
|
|
Convey("An error should be returned", func() {
|
|
|
|
So(err, ShouldEqual, mockErr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When updating node object fails", func() {
|
|
|
|
mockHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockHelper.On("GetNode", mockClient, mockNodeName).Return(mockNode, nil)
|
2020-08-13 14:14:27 +00:00
|
|
|
mockHelper.On("PatchNode", mockClient, mockNodeName, mock.Anything).Return(mockErr)
|
2019-02-08 19:43:54 +00:00
|
|
|
err := updateMasterNode(mockHelper)
|
|
|
|
Convey("An error should be returned", func() {
|
|
|
|
So(err, ShouldEqual, mockErr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-05 14:40:55 +00:00
|
|
|
func TestAddingExtResources(t *testing.T) {
|
|
|
|
Convey("When adding extended resources", t, func() {
|
|
|
|
Convey("When there are no matching labels", func() {
|
|
|
|
mockNode := newMockNode()
|
|
|
|
mockResourceLabels := ExtendedResources{}
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldEqual, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When there are matching labels", func() {
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockResourceLabels := ExtendedResources{LabelNs + "/feature-1": "1", LabelNs + "feature-2": "2"}
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldBeGreaterThan, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When the resource already exists", func() {
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-1")] = *resource.NewQuantity(1, resource.BinarySI)
|
|
|
|
mockResourceLabels := ExtendedResources{LabelNs + "/feature-1": "1"}
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldEqual, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When the resource already exists but its capacity has changed", func() {
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-1")] = *resource.NewQuantity(2, resource.BinarySI)
|
|
|
|
mockResourceLabels := ExtendedResources{LabelNs + "/feature-1": "1"}
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldBeGreaterThan, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovingExtResources(t *testing.T) {
|
|
|
|
Convey("When removing extended resources", t, func() {
|
|
|
|
Convey("When none are removed", func() {
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockResourceLabels := ExtendedResources{LabelNs + "/feature-1": "1", LabelNs + "/feature-2": "2"}
|
|
|
|
mockNode.Annotations[AnnotationNs+"/extended-resources"] = "feature-1,feature-2"
|
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-1")] = *resource.NewQuantity(1, resource.BinarySI)
|
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-2")] = *resource.NewQuantity(2, resource.BinarySI)
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldEqual, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
Convey("When the related label is gone", func() {
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockResourceLabels := ExtendedResources{LabelNs + "/feature-4": "", LabelNs + "/feature-2": "2"}
|
|
|
|
mockNode.Annotations[AnnotationNs+"/extended-resources"] = "feature-4,feature-2"
|
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-4")] = *resource.NewQuantity(4, resource.BinarySI)
|
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-2")] = *resource.NewQuantity(2, resource.BinarySI)
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldBeGreaterThan, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
Convey("When the extended resource is no longer wanted", func() {
|
|
|
|
mockNode := newMockNode()
|
2020-08-12 18:28:21 +00:00
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-1")] = *resource.NewQuantity(1, resource.BinarySI)
|
|
|
|
mockNode.Status.Capacity[api.ResourceName(LabelNs+"/feature-2")] = *resource.NewQuantity(2, resource.BinarySI)
|
|
|
|
mockResourceLabels := ExtendedResources{LabelNs + "/feature-2": "2"}
|
|
|
|
mockNode.Annotations[AnnotationNs+"/extended-resources"] = "feature-1,feature-2"
|
2020-08-13 14:14:27 +00:00
|
|
|
patches := createExtendedResourcePatches(mockNode, mockResourceLabels)
|
|
|
|
So(len(patches), ShouldBeGreaterThan, 0)
|
2020-03-05 14:40:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:43:54 +00:00
|
|
|
func TestSetLabels(t *testing.T) {
|
|
|
|
Convey("When servicing SetLabels request", t, func() {
|
|
|
|
const workerName = "mock-worker"
|
|
|
|
const workerVer = "0.1-test"
|
|
|
|
mockHelper := &apihelper.MockAPIHelpers{}
|
|
|
|
mockClient := &k8sclient.Clientset{}
|
2019-02-12 13:31:59 +00:00
|
|
|
mockNode := newMockNode()
|
2019-05-07 09:41:20 +00:00
|
|
|
mockServer := labelerServer{args: Args{LabelWhiteList: regexp.MustCompile("")}, apiHelper: mockHelper}
|
2019-02-08 19:43:54 +00:00
|
|
|
mockCtx := context.Background()
|
2020-08-12 18:28:21 +00:00
|
|
|
// In the gRPC request the label names may omit the default ns
|
2019-05-07 09:41:20 +00:00
|
|
|
mockLabels := map[string]string{"feature-1": "val-1", "feature-2": "val-2", "feature-3": "val-3"}
|
|
|
|
mockReq := &labeler.SetLabelsRequest{NodeName: workerName, NfdVersion: workerVer, Labels: mockLabels}
|
|
|
|
|
|
|
|
mockLabelNames := make([]string, 0, len(mockLabels))
|
|
|
|
for k := range mockLabels {
|
|
|
|
mockLabelNames = append(mockLabelNames, k)
|
|
|
|
}
|
|
|
|
sort.Strings(mockLabelNames)
|
2019-02-08 19:43:54 +00:00
|
|
|
|
|
|
|
Convey("When node update succeeds", func() {
|
2020-08-13 14:14:27 +00:00
|
|
|
expectedPatches := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", workerVersionAnnotation, workerVer),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", featureLabelAnnotation, strings.Join(mockLabelNames, ",")),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", extendedResourceAnnotation, ""),
|
|
|
|
}
|
|
|
|
for k, v := range mockLabels {
|
|
|
|
expectedPatches = append(expectedPatches, apihelper.NewJsonPatch("add", "/metadata/labels", LabelNs+"/"+k, v))
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:43:54 +00:00
|
|
|
mockHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockHelper.On("GetNode", mockClient, workerName).Return(mockNode, nil)
|
2020-08-13 14:14:27 +00:00
|
|
|
mockHelper.On("PatchNode", mockClient, mockNodeName, mock.MatchedBy(jsonPatchMatcher(expectedPatches))).Return(nil)
|
|
|
|
mockHelper.On("PatchNodeStatus", mockClient, mockNodeName, mock.Anything).Return(nil)
|
2019-02-08 19:43:54 +00:00
|
|
|
_, err := mockServer.SetLabels(mockCtx, mockReq)
|
|
|
|
Convey("No error should be returned", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2019-05-07 09:41:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When --label-whitelist is specified", func() {
|
2020-08-13 14:14:27 +00:00
|
|
|
expectedPatches := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", workerVersionAnnotation, workerVer),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", featureLabelAnnotation, "feature-2"),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", extendedResourceAnnotation, ""),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/labels", LabelNs+"/feature-2", mockLabels["feature-2"]),
|
|
|
|
}
|
|
|
|
|
2019-05-07 09:41:20 +00:00
|
|
|
mockServer.args.LabelWhiteList = regexp.MustCompile("^f.*2$")
|
|
|
|
mockHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockHelper.On("GetNode", mockClient, workerName).Return(mockNode, nil)
|
2020-08-13 14:14:27 +00:00
|
|
|
mockHelper.On("PatchNode", mockClient, mockNodeName, mock.MatchedBy(jsonPatchMatcher(expectedPatches))).Return(nil)
|
|
|
|
mockHelper.On("PatchNodeStatus", mockClient, mockNodeName, mock.Anything).Return(nil)
|
2019-05-07 09:41:20 +00:00
|
|
|
_, err := mockServer.SetLabels(mockCtx, mockReq)
|
|
|
|
Convey("Error is nil", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
2019-02-08 19:43:54 +00:00
|
|
|
})
|
|
|
|
|
2019-04-05 22:31:40 +00:00
|
|
|
Convey("When --extra-label-ns is specified", func() {
|
2020-08-12 18:28:21 +00:00
|
|
|
// In the gRPC request the label names may omit the default ns
|
2019-04-05 22:31:40 +00:00
|
|
|
mockLabels := map[string]string{"feature-1": "val-1",
|
|
|
|
"valid.ns/feature-2": "val-2",
|
|
|
|
"invalid.ns/feature-3": "val-3"}
|
2020-08-13 14:14:27 +00:00
|
|
|
expectedPatches := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", workerVersionAnnotation, workerVer),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", featureLabelAnnotation, "feature-1,valid.ns/feature-2"),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/annotations", extendedResourceAnnotation, ""),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/labels", LabelNs+"/feature-1", mockLabels["feature-1"]),
|
|
|
|
apihelper.NewJsonPatch("add", "/metadata/labels", "valid.ns/feature-2", mockLabels["valid.ns/feature-2"]),
|
|
|
|
}
|
|
|
|
|
|
|
|
mockServer.args.ExtraLabelNs = map[string]struct{}{"valid.ns": struct{}{}}
|
|
|
|
mockHelper.On("GetClient").Return(mockClient, nil)
|
|
|
|
mockHelper.On("GetNode", mockClient, workerName).Return(mockNode, nil)
|
|
|
|
mockHelper.On("PatchNode", mockClient, mockNodeName, mock.MatchedBy(jsonPatchMatcher(expectedPatches))).Return(nil)
|
|
|
|
mockHelper.On("PatchNodeStatus", mockClient, mockNodeName, mock.Anything).Return(nil)
|
2019-04-05 22:31:40 +00:00
|
|
|
mockReq := &labeler.SetLabelsRequest{NodeName: workerName, NfdVersion: workerVer, Labels: mockLabels}
|
|
|
|
_, err := mockServer.SetLabels(mockCtx, mockReq)
|
|
|
|
Convey("Error is nil", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-02-08 19:43:54 +00:00
|
|
|
mockErr := errors.New("mock-error")
|
|
|
|
Convey("When node update fails", func() {
|
|
|
|
mockHelper.On("GetClient").Return(mockClient, mockErr)
|
|
|
|
_, err := mockServer.SetLabels(mockCtx, mockReq)
|
|
|
|
Convey("An error should be returned", func() {
|
|
|
|
So(err, ShouldEqual, mockErr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
mockServer.args.NoPublish = true
|
|
|
|
Convey("With '--no-publish'", func() {
|
|
|
|
_, err := mockServer.SetLabels(mockCtx, mockReq)
|
|
|
|
Convey("Operation should succeed", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-02-12 13:31:59 +00:00
|
|
|
|
2020-08-13 14:14:27 +00:00
|
|
|
func TestCreatePatches(t *testing.T) {
|
|
|
|
Convey("When creating JSON patches", t, func() {
|
|
|
|
existingItems := map[string]string{"key-1": "val-1", "key-2": "val-2", "key-3": "val-3"}
|
|
|
|
jsonPath := "/root"
|
2019-02-12 13:31:59 +00:00
|
|
|
|
2020-08-13 14:14:27 +00:00
|
|
|
Convey("When when there are neither itmes to remoe nor to add or update", func() {
|
|
|
|
p := createPatches([]string{"foo", "bar"}, existingItems, map[string]string{}, jsonPath)
|
|
|
|
So(len(p), ShouldEqual, 0)
|
|
|
|
})
|
2019-02-12 13:31:59 +00:00
|
|
|
|
2020-08-13 14:14:27 +00:00
|
|
|
Convey("When when there are itmes to remoe but none to add or update", func() {
|
|
|
|
p := createPatches([]string{"key-2", "key-3", "foo"}, existingItems, map[string]string{}, jsonPath)
|
|
|
|
expected := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("remove", jsonPath, "key-2", ""),
|
|
|
|
apihelper.NewJsonPatch("remove", jsonPath, "key-3", ""),
|
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
2019-02-12 13:31:59 +00:00
|
|
|
})
|
|
|
|
|
2020-08-13 14:14:27 +00:00
|
|
|
Convey("When when there are no itmes to remove but new items to add", func() {
|
|
|
|
newItems := map[string]string{"new-key": "new-val", "key-1": "new-1"}
|
|
|
|
p := createPatches([]string{"key-1"}, existingItems, newItems, jsonPath)
|
|
|
|
expected := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("add", jsonPath, "new-key", newItems["new-key"]),
|
|
|
|
apihelper.NewJsonPatch("replace", jsonPath, "key-1", newItems["key-1"]),
|
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When when there are items to remove add and update", func() {
|
|
|
|
newItems := map[string]string{"new-key": "new-val", "key-2": "new-2", "key-4": "val-4"}
|
|
|
|
p := createPatches([]string{"key-1", "key-2", "key-3", "foo"}, existingItems, newItems, jsonPath)
|
|
|
|
expected := []apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("add", jsonPath, "new-key", newItems["new-key"]),
|
|
|
|
apihelper.NewJsonPatch("add", jsonPath, "key-4", newItems["key-4"]),
|
|
|
|
apihelper.NewJsonPatch("replace", jsonPath, "key-2", newItems["key-2"]),
|
|
|
|
apihelper.NewJsonPatch("remove", jsonPath, "key-1", ""),
|
|
|
|
apihelper.NewJsonPatch("remove", jsonPath, "key-3", ""),
|
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
2019-02-12 13:31:59 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveLabelsWithPrefix(t *testing.T) {
|
|
|
|
Convey("When removing labels", t, func() {
|
|
|
|
n := &api.Node{
|
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"single-label": "123",
|
|
|
|
"multiple_A": "a",
|
|
|
|
"multiple_B": "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("a unique label should be removed", func() {
|
2020-08-13 14:14:27 +00:00
|
|
|
p := removeLabelsWithPrefix(n, "single")
|
|
|
|
So(p, ShouldResemble, []apihelper.JsonPatch{apihelper.NewJsonPatch("remove", "/metadata/labels", "single-label", "")})
|
2019-02-12 13:31:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("a non-unique search string should remove all matching keys", func() {
|
2020-08-13 14:14:27 +00:00
|
|
|
p := removeLabelsWithPrefix(n, "multiple")
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches([]apihelper.JsonPatch{
|
|
|
|
apihelper.NewJsonPatch("remove", "/metadata/labels", "multiple_A", ""),
|
|
|
|
apihelper.NewJsonPatch("remove", "/metadata/labels", "multiple_B", ""),
|
|
|
|
}))
|
2019-02-12 13:31:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("a search string with no matches should not alter labels", func() {
|
|
|
|
removeLabelsWithPrefix(n, "unique")
|
|
|
|
So(n.Labels, ShouldContainKey, "single-label")
|
|
|
|
So(n.Labels, ShouldContainKey, "multiple_A")
|
|
|
|
So(n.Labels, ShouldContainKey, "multiple_B")
|
|
|
|
So(len(n.Labels), ShouldEqual, 3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-08-13 14:14:27 +00:00
|
|
|
|
|
|
|
func jsonPatchMatcher(expected []apihelper.JsonPatch) func([]apihelper.JsonPatch) bool {
|
|
|
|
return func(actual []apihelper.JsonPatch) bool {
|
|
|
|
// We don't care about modifying the original slices
|
|
|
|
ok, msg := assertions.So(sortJsonPatches(actual), ShouldResemble, sortJsonPatches(expected))
|
|
|
|
if !ok {
|
|
|
|
// We parse the cryptic string message for better readability
|
|
|
|
var f assertions.FailureView
|
|
|
|
if err := yaml.Unmarshal([]byte(msg), &f); err == nil {
|
|
|
|
Printf("%s\n", f.Message)
|
|
|
|
} else {
|
|
|
|
Printf("%s\n", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortJsonPatches(p []apihelper.JsonPatch) []apihelper.JsonPatch {
|
|
|
|
sort.Slice(p, func(i, j int) bool { return p[i].Path < p[j].Path })
|
|
|
|
return p
|
|
|
|
}
|