Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
grpc: add unit test for onlineResources function
Browse files Browse the repository at this point in the history
Unit test for `onlineResources` function.

fixes #387

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Jun 4, 2019
1 parent eba24ce commit ca2f724
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ca2f724

Please sign in to comment.