From 0cb5f22ba185d1de2c55fd2eb8f8cf56781e564f Mon Sep 17 00:00:00 2001 From: Penny Zheng Date: Wed, 23 Jan 2019 17:38:19 +0800 Subject: [PATCH] agent: refine unit test for func GetGuestDetails since we add extra functionality in func GetGuestDetails for memory hotplug via probe interface, we need to refine its related unit test. Fixes: #442 Signed-off-by: Penny Zheng --- grpc_test.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/grpc_test.go b/grpc_test.go index b82d02dd07..0cec1bb41a 100644 --- a/grpc_test.go +++ b/grpc_test.go @@ -664,12 +664,19 @@ func TestGetGuestDetails(t *testing.T) { } req := &pb.GuestDetailsRequest{ - MemBlockSize: true, + MemBlockSize: true, + MemHotplugProbe: true, } // sysfsMemoryBlockSizePath exist with error format file, err := ioutil.TempFile("", "test") assert.NoError(err) + + oldsysfsMemoryBlockSizePath := sysfsMemoryBlockSizePath + defer func() { + sysfsMemoryBlockSizePath = oldsysfsMemoryBlockSizePath + }() + sysfsMemoryBlockSizePath = file.Name() // empty _, err = a.GetGuestDetails(context.TODO(), req) @@ -699,6 +706,26 @@ func TestGetGuestDetails(t *testing.T) { resp, err = a.GetGuestDetails(context.TODO(), req) assert.NoError(err) assert.Equal(resp.MemBlockSizeBytes, uint64(0)) + + // sysfsMemoryHotplugProbePath exist + probeFile, err := ioutil.TempFile("", "probe") + assert.NoError(err) + + oldSysfsMemoryHotplugProbePath := sysfsMemoryHotplugProbePath + defer func() { + sysfsMemoryHotplugProbePath = oldSysfsMemoryHotplugProbePath + }() + + sysfsMemoryHotplugProbePath = probeFile.Name() + resp, err = a.GetGuestDetails(context.TODO(), req) + assert.NoError(err) + assert.Equal(resp.SupportMemHotplugProbe, true) + + // sysfsMemoryHotplugProbePath does not exist + os.Remove(sysfsMemoryHotplugProbePath) + resp, err = a.GetGuestDetails(context.TODO(), req) + assert.NoError(err) + assert.Equal(resp.SupportMemHotplugProbe, false) } func TestGetAgentDetails(t *testing.T) {