From 61dad2871fb3279fd0d276ac82e6aeb3d5442859 Mon Sep 17 00:00:00 2001 From: Adrian Moreno Date: Mon, 23 Nov 2020 13:31:44 +0100 Subject: [PATCH] server: fix unit tests 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 --- pkg/resources/server_test.go | 38 ++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/pkg/resources/server_test.go b/pkg/resources/server_test.go index df2d4e228..3f2140768 100644 --- a/pkg/resources/server_test.go +++ b/pkg/resources/server_test.go @@ -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) @@ -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() { @@ -133,12 +134,6 @@ 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() { @@ -146,6 +141,14 @@ 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", "fake", false, &rp).(*resourceServer) @@ -153,6 +156,7 @@ var _ = Describe("Server", func() { registrationServer := createFakeRegistrationServer(fs.RootDir, "fake_fake.com.fake", false, false) os.MkdirAll(pluginapi.DevicePluginPath, 0755) + registrationServer.start() defer registrationServer.stop() @@ -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()) }() @@ -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) @@ -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()) }() @@ -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)