From 36306e283c11c723a0175eb2d401b07a948a1274 Mon Sep 17 00:00:00 2001 From: Zichang Lin Date: Thu, 11 Oct 2018 16:33:58 +0800 Subject: [PATCH] sandbox/virtcontainers: modify tests relate to memory hotplug. Signed-off-by: Clare Chen Signed-off-by: Zichang Lin --- cli/config/configuration.toml.in | 2 +- cli/create_test.go | 1 - virtcontainers/container.go | 12 ++-- virtcontainers/container_test.go | 94 ++++++++++++++++++++++++++++--- virtcontainers/mock_hypervisor.go | 7 ++- 5 files changed, 96 insertions(+), 20 deletions(-) diff --git a/cli/config/configuration.toml.in b/cli/config/configuration.toml.in index 5ee4e474e5..154ba77c58 100644 --- a/cli/config/configuration.toml.in +++ b/cli/config/configuration.toml.in @@ -75,7 +75,7 @@ default_bridges = @DEFBRIDGES@ # Default memory size in MiB for SB/VM. # If unspecified then it will be set @DEFMEMSZ@ MiB. -#default_memory = @DEFMEMSZ@ +default_memory = @DEFMEMSZ@ # # Default memory slots per SB/VM. # If unspecified then it will be set @DEFMEMSLOTS@. diff --git a/cli/create_test.go b/cli/create_test.go index 930f6832f0..24a6929a63 100644 --- a/cli/create_test.go +++ b/cli/create_test.go @@ -896,7 +896,6 @@ func TestCreateSandboxConfigFail(t *testing.T) { _, err = createSandbox(context.Background(), spec, runtimeConfig, testContainerID, bundlePath, testConsole, true, true) assert.Error(err) - assert.False(vcmock.IsMockError(err)) } func TestCreateCreateSandboxFail(t *testing.T) { diff --git a/virtcontainers/container.go b/virtcontainers/container.go index 51b2965510..d25fce8754 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -993,6 +993,10 @@ func (c *Container) update(resources specs.LinuxResources) error { return err } + if err := c.storeContainer(); err != nil { + return err + } + return c.sandbox.agent.updateContainer(c.sandbox, *c, resources) } @@ -1353,11 +1357,8 @@ func (c *Container) updateResources(oldResources, newResources ContainerResource if err := c.updateVCPUResources(oldResources, &newResources); err != nil { return err } - // Set and save container's config VCPUs field only + // Set container's config VCPUs field only c.config.Resources.VCPUs = newResources.VCPUs - if err := c.storeContainer(); err != nil { - return err - } } // Memory is not updated if memory limit not set @@ -1366,9 +1367,8 @@ func (c *Container) updateResources(oldResources, newResources ContainerResource return err } - // Set and save container's config Mem field only + // Set container's config MemByte field only c.config.Resources.MemByte = newResources.MemByte - return c.storeContainer() } return nil diff --git a/virtcontainers/container_test.go b/virtcontainers/container_test.go index 5344bcb943..a2326ef47d 100644 --- a/virtcontainers/container_test.go +++ b/virtcontainers/container_test.go @@ -326,11 +326,9 @@ func TestContainerAddResources(t *testing.T) { MemByte: memByte, } c.sandbox = &Sandbox{ - hypervisor: &mockHypervisor{ - vCPUs: vCPUs, - }, - agent: &noopAgent{}, - storage: &filesystem{}, + hypervisor: &mockHypervisor{}, + agent: &noopAgent{}, + storage: &filesystem{}, } err = c.addResources() assert.Nil(err) @@ -363,16 +361,94 @@ func TestContainerRemoveResources(t *testing.T) { } c.sandbox = &Sandbox{ - hypervisor: &mockHypervisor{ - vCPUs: vCPUs, - }, - storage: &filesystem{}, + hypervisor: &mockHypervisor{}, + storage: &filesystem{}, } err = c.removeResources() assert.Nil(err) } +func TestContainerUpdateResources(t *testing.T) { + assert := assert.New(t) + + sandbox := &Sandbox{ + hypervisor: &mockHypervisor{}, + agent: &noopAgent{}, + storage: &filesystem{}, + } + + c := &Container{ + sandbox: sandbox, + } + c.config = &ContainerConfig{Annotations: make(map[string]string)} + + // VCPUs is equal to zero + oldResource := ContainerResources{ + VCPUs: 0, + MemByte: 0, + } + + newResource := ContainerResources{ + VCPUs: 0, + MemByte: 104857600, + } + + err := c.updateResources(oldResource, newResource) + assert.Nil(err) + + // MemByte is equal to zero + newResource = ContainerResources{ + VCPUs: 5, + MemByte: 0, + } + + err = c.updateResources(oldResource, newResource) + assert.Nil(err) + + // oldResource is equal to newResource + oldResource = ContainerResources{ + VCPUs: 5, + MemByte: 104857600, + } + + newResource = ContainerResources{ + VCPUs: 5, + MemByte: 104857600, + } + + err = c.updateResources(oldResource, newResource) + assert.Nil(err) + + // memory hotplug and cpu hotplug + oldResource = ContainerResources{ + VCPUs: 5, + MemByte: 104857600, + } + + newResource = ContainerResources{ + VCPUs: 10, + MemByte: 209715200, + } + + err = c.updateResources(oldResource, newResource) + assert.Nil(err) + + // memory hot remove and cpu hot remove + oldResource = ContainerResources{ + VCPUs: 10, + MemByte: 209715200, + } + + newResource = ContainerResources{ + VCPUs: 5, + MemByte: 104857600, + } + + err = c.updateResources(oldResource, newResource) + assert.Nil(err) +} + func TestContainerEnterErrorsOnContainerStates(t *testing.T) { assert := assert.New(t) c := &Container{ diff --git a/virtcontainers/mock_hypervisor.go b/virtcontainers/mock_hypervisor.go index 5952fb7f95..ca9c29d9f0 100644 --- a/virtcontainers/mock_hypervisor.go +++ b/virtcontainers/mock_hypervisor.go @@ -8,7 +8,6 @@ package virtcontainers import "context" type mockHypervisor struct { - vCPUs uint32 } func (m *mockHypervisor) init(ctx context.Context, id string, hypervisorConfig *HypervisorConfig, storage resourceStorage) error { @@ -63,7 +62,7 @@ func (m *mockHypervisor) addDevice(devInfo interface{}, devType deviceType) erro func (m *mockHypervisor) hotplugAddDevice(devInfo interface{}, devType deviceType) (interface{}, error) { switch devType { case cpuDev: - return m.vCPUs, nil + return devInfo.(uint32), nil case memoryDev: memdev := devInfo.(*memoryDevice) return memdev.sizeMB, nil @@ -74,7 +73,9 @@ func (m *mockHypervisor) hotplugAddDevice(devInfo interface{}, devType deviceTyp func (m *mockHypervisor) hotplugRemoveDevice(devInfo interface{}, devType deviceType) (interface{}, error) { switch devType { case cpuDev: - return m.vCPUs, nil + return devInfo.(uint32), nil + case memoryDev: + return 0, nil } return nil, nil }