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

Commit

Permalink
sandbox/virtcontainers: modify tests relate to memory hotplug.
Browse files Browse the repository at this point in the history
Signed-off-by: Clare Chen <[email protected]>
Signed-off-by: Zichang Lin <[email protected]>
  • Loading branch information
linzichang authored and cedriccchen committed Oct 18, 2018
1 parent 14f480a commit 36306e2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cli/config/configuration.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@.
Expand Down
1 change: 0 additions & 1 deletion cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
94 changes: 85 additions & 9 deletions virtcontainers/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions virtcontainers/mock_hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 36306e2

Please sign in to comment.