Skip to content

Commit

Permalink
server: fix unit tests
Browse files Browse the repository at this point in the history
Move the creation of the mocked resourcePool out of BeforeEach because
it is used in a goroutine (the one that calls server.Stop()) and so
having a unique reference would cause race-conditions.

Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz committed Nov 26, 2020
1 parent fedc278 commit 61dad28
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions pkg/resources/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var _ = Describe("Server", func() {

if shouldRunServer {
if shouldEnablePluginWatch {
rp.On("StoreDeviceInfoFile", "fakeprefix").Return(nil)
rp.On("CleanDeviceInfoFile", "fakeprefix").Return(nil)
rs.Start()
} else {
os.MkdirAll(pluginapi.DevicePluginPath, 0755)
Expand Down Expand Up @@ -121,7 +123,6 @@ var _ = Describe("Server", func() {
// integration-like test for the resource server (positive cases)
var (
fakeConf *types.ResourceConfig
rp mocks.ResourcePool
fs *utils.FakeFilesystem
)
BeforeEach(func() {
Expand All @@ -133,26 +134,29 @@ var _ = Describe("Server", func() {
ResourceName: "fake",
Selectors: &selectors,
}
rp = mocks.ResourcePool{}
rp.On("GetConfig").Return(fakeConf).
On("GetResourceName").Return("fake.com").
On("DiscoverDevices").Return(nil).
On("GetDevices").Return(map[string]*pluginapi.Device{}).
On("Probe").Return(true)
fs = &utils.FakeFilesystem{}
})
Context("starting, restarting and stopping the resource server", func() {
It("should not fail and messages should be received on the channels", func(done Done) {
defer fs.Use()()
// Use faked dir as socket dir
types.DeprecatedSockDir = fs.RootDir
rp := mocks.ResourcePool{}
rp.On("GetConfig").Return(fakeConf).
On("GetResourceName").Return("fake.com").
On("DiscoverDevices").Return(nil).
On("GetDevices").Return(map[string]*pluginapi.Device{}).
On("Probe").Return(true).
On("StoreDeviceInfoFile", "fake").Return(nil).
On("CleanDeviceInfoFile", "fake").Return(nil)

// Create ResourceServer with plugin watch mode disabled
rs := NewResourceServer("fake", "fake", false, &rp).(*resourceServer)

registrationServer := createFakeRegistrationServer(fs.RootDir,
"fake_fake.com.fake", false, false)
os.MkdirAll(pluginapi.DevicePluginPath, 0755)

registrationServer.start()
defer registrationServer.stop()

Expand All @@ -164,6 +168,7 @@ var _ = Describe("Server", func() {
Eventually(rs.termSignal, time.Second*10).Should(Receive())

go func() {
rp.On("CleanDeviceInfoFile", "fake").Return(nil)
err := rs.Stop()
Expect(err).NotTo(HaveOccurred())
}()
Expand All @@ -177,6 +182,14 @@ var _ = Describe("Server", func() {
// Use faked dir as socket dir
types.SockDir = fs.RootDir

rp := mocks.ResourcePool{}
rp.On("GetConfig").Return(fakeConf).
On("GetResourceName").Return("fake.com").
On("DiscoverDevices").Return(nil).
On("GetDevices").Return(map[string]*pluginapi.Device{}).
On("Probe").Return(true).
On("StoreDeviceInfoFile", "fake").Return(nil).
On("CleanDeviceInfoFile", "fake").Return(nil)
// Create ResourceServer with plugin watch mode enabled
rs := NewResourceServer("fake", "fake", true, &rp).(*resourceServer)

Expand All @@ -189,6 +202,7 @@ var _ = Describe("Server", func() {
Expect(err).NotTo(HaveOccurred())

go func() {
rp.On("CleanDeviceInfoFile", "fake").Return(nil)
err := rs.Stop()
Expect(err).NotTo(HaveOccurred())
}()
Expand All @@ -202,9 +216,17 @@ var _ = Describe("Server", func() {
defer fs.Use()()
// Use faked dir as socket dir
types.DeprecatedSockDir = fs.RootDir
rp := mocks.ResourcePool{}
rp.On("GetConfig").Return(fakeConf).
On("GetResourceName").Return("fake.com").
On("DiscoverDevices").Return(nil).
On("GetDevices").Return(map[string]*pluginapi.Device{}).
On("Probe").Return(true).
On("StoreDeviceInfoFile", "fake").Return(nil).
On("CleanDeviceInfoFile", "fake").Return(nil)

// Create ResourceServer with plugin watch mode disabled
rs := NewResourceServer("fake.com", "fake", false, &rp).(*resourceServer)
rs := NewResourceServer("fake", "fake", false, &rp).(*resourceServer)

registrationServer := createFakeRegistrationServer(fs.RootDir,
"fake_fake.com.fake", false, false)
Expand Down

0 comments on commit 61dad28

Please sign in to comment.