From ca2f724437948311ae0b5ac043a16c4a00f65770 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Mon, 3 Jun 2019 15:49:12 -0500 Subject: [PATCH] grpc: add unit test for onlineResources function Unit test for `onlineResources` function. fixes #387 Signed-off-by: Julio Montes --- grpc_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/grpc_test.go b/grpc_test.go index ccb3551e67..51d26baee5 100644 --- a/grpc_test.go +++ b/grpc_test.go @@ -993,3 +993,61 @@ func TestIsSignalHandled(t *testing.T) { handled = isSignalHandled(pid, signum) assert.True(handled) } + +func TestOnlineResources(t *testing.T) { + assert := assert.New(t) + + cpusDir, err := ioutil.TempDir("", "cpu") + assert.NoError(err) + defer os.RemoveAll(cpusDir) + + resource := onlineResource{ + sysfsOnlinePath: cpusDir, + regexpPattern: cpuRegexpPattern, + } + + // cold plug CPU + cpu0Path := filepath.Join(cpusDir, "cpu0") + err = os.Mkdir(cpu0Path, 0755) + assert.NoError(err) + + // readonly CPU + cpu1Path := filepath.Join(cpusDir, "cpu1") + err = os.Mkdir(cpu1Path, 0755) + assert.NoError(err) + f, err := os.Create(filepath.Join(cpu1Path, "online")) + assert.NoError(err) + _, err = f.Write([]byte("0")) + assert.NoError(err) + assert.NoError(f.Close()) + err = os.Chmod(f.Name(), 0400) + assert.NoError(err) + err = os.Chmod(cpu1Path, 0400) + assert.NoError(err) + + // Hot plug CPU + cpu2Path := filepath.Join(cpusDir, "cpu2") + err = os.Mkdir(cpu2Path, 0755) + assert.NoError(err) + f, err = os.Create(filepath.Join(cpu2Path, "online")) + assert.NoError(err) + _, err = f.Write([]byte("0")) + assert.NoError(err) + assert.NoError(f.Close()) + + // nothing related to CPUs + argbPath := filepath.Join(cpusDir, "argb") + err = os.Mkdir(argbPath, 0755) + assert.NoError(err) + + expectedCpus := int32(1) + r, err := onlineResources(resource, expectedCpus) + assert.NoError(err) + assert.Equal(uint32(expectedCpus), r) + + // Error: path doesn't exist + resource.sysfsOnlinePath = "/abc/123/rgb/:)" + r, err = onlineResources(resource, expectedCpus) + assert.Error(err) + assert.Equal(uint32(0), r) +}