2023-06-30 13:41:11 +01:00
|
|
|
/*
|
|
|
|
Copyright 2023 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 (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2024-01-22 19:08:04 +02:00
|
|
|
fakek8sclient "k8s.io/client-go/kubernetes/fake"
|
|
|
|
fakenfdclient "sigs.k8s.io/node-feature-discovery/pkg/generated/clientset/versioned/fake"
|
2023-06-30 13:41:11 +01:00
|
|
|
)
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
func newFakeNodeUpdaterPool(nfdMaster *nfdMaster) *nodeUpdaterPool {
|
2023-06-30 13:41:11 +01:00
|
|
|
return &nodeUpdaterPool{
|
|
|
|
nfdMaster: nfdMaster,
|
|
|
|
wg: sync.WaitGroup{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeUpdaterStart(t *testing.T) {
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeMaster := newFakeMaster(nil)
|
|
|
|
nodeUpdaterPool := newFakeNodeUpdaterPool(fakeMaster)
|
2023-06-30 13:41:11 +01:00
|
|
|
|
|
|
|
Convey("When starting the node updater pool", t, func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
nodeUpdaterPool.start(10)
|
|
|
|
q := nodeUpdaterPool.queue
|
2023-06-30 13:41:11 +01:00
|
|
|
Convey("Node updater pool queue properties should change", func() {
|
|
|
|
So(q, ShouldNotBeNil)
|
|
|
|
So(q.ShuttingDown(), ShouldBeFalse)
|
|
|
|
})
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
nodeUpdaterPool.start(10)
|
2023-06-30 13:41:11 +01:00
|
|
|
Convey("Node updater pool queue should not change", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
So(nodeUpdaterPool.queue, ShouldEqual, q)
|
2023-06-30 13:41:11 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeUpdaterStop(t *testing.T) {
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeMaster := newFakeMaster(nil)
|
|
|
|
nodeUpdaterPool := newFakeNodeUpdaterPool(fakeMaster)
|
2023-06-30 13:41:11 +01:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
nodeUpdaterPool.start(10)
|
2023-06-30 13:41:11 +01:00
|
|
|
|
|
|
|
Convey("When stoping the node updater pool", t, func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
nodeUpdaterPool.stop()
|
2023-06-30 13:41:11 +01:00
|
|
|
Convey("Node updater pool queue should be removed", func() {
|
|
|
|
// Wait for the wg.Done()
|
|
|
|
So(func() interface{} {
|
2024-01-22 19:08:04 +02:00
|
|
|
return nodeUpdaterPool.queue.ShuttingDown()
|
2023-06-30 13:41:11 +01:00
|
|
|
}, withTimeout, 2*time.Second, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunNodeUpdater(t *testing.T) {
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeMaster := newFakeMaster(fakek8sclient.NewSimpleClientset())
|
|
|
|
fakeMaster.nfdController = newFakeNfdAPIController(fakenfdclient.NewSimpleClientset())
|
|
|
|
nodeUpdaterPool := newFakeNodeUpdaterPool(fakeMaster)
|
2023-06-30 13:41:11 +01:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
nodeUpdaterPool.start(10)
|
2023-06-30 13:41:11 +01:00
|
|
|
Convey("Queue has no element", t, func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
So(nodeUpdaterPool.queue.Len(), ShouldEqual, 0)
|
2023-06-30 13:41:11 +01:00
|
|
|
})
|
2024-01-22 19:08:04 +02:00
|
|
|
nodeUpdaterPool.queue.Add(testNodeName)
|
2023-06-30 13:41:11 +01:00
|
|
|
Convey("Added element to the queue should be removed", t, func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
So(func() interface{} { return nodeUpdaterPool.queue.Len() },
|
2023-06-30 13:41:11 +01:00
|
|
|
withTimeout, 2*time.Second, ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
}
|