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

Commit

Permalink
test: fix failing unit test
Browse files Browse the repository at this point in the history
fix failing unit test

`newContainer` is refactored due to high cyclomatic complexity.

Signed-off-by: Wei Zhang <[email protected]>
  • Loading branch information
WeiZhang555 committed Feb 8, 2019
1 parent e3d6fd3 commit 78aad02
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
42 changes: 34 additions & 8 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ func (c *Container) SetPid(pid int) error {
return c.storeProcess()
}

func (c *Container) setStateBlockIndex(index int) error {
c.state.BlockIndex = index

err := c.sandbox.storage.storeContainerResource(c.sandbox.id, c.id, stateFileType, c.state)
if err != nil {
return err
}

return nil
}

func (c *Container) setStateFstype(fstype string) error {
c.state.Fstype = fstype

Expand Down Expand Up @@ -627,7 +638,7 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
}

if err := c.Restore(); err != nil &&
!os.IsNotExist(err) && err != ContainerPersistNotExist {
!os.IsNotExist(err) && err != errContainerPersistNotExist {
return nil, err
}

Expand All @@ -643,6 +654,18 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
c.process = process
}

if err = c.createMounts(); err != nil {
return nil, err
}

if err = c.createDevices(contConfig); err != nil {
return nil, err
}

return c, nil
}

func (c *Container) createMounts() error {
mounts, err := c.fetchMounts()
if err == nil {
// restore mounts from disk
Expand All @@ -660,7 +683,7 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err

var stat unix.Stat_t
if err := unix.Stat(m.Source, &stat); err != nil {
return nil, fmt.Errorf("stat %q failed: %v", m.Source, err)
return fmt.Errorf("stat %q failed: %v", m.Source, err)
}

// Check if mount is a block device file. If it is, the block device will be attached to the host
Expand All @@ -674,14 +697,18 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
Minor: int64(unix.Minor(stat.Rdev)),
})
if err != nil {
return nil, fmt.Errorf("device manager failed to create new device for %q: %v", m.Source, err)
return fmt.Errorf("device manager failed to create new device for %q: %v", m.Source, err)
}

c.mounts[i].BlockDeviceID = b.DeviceID()
}
}
}

return nil
}

func (c *Container) createDevices(contConfig ContainerConfig) error {
// Devices will be found in storage after create stage has completed.
// We fetch devices from storage at all other stages.
storedDevices, err := c.fetchDevices()
Expand All @@ -691,9 +718,9 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
// If devices were not found in storage, create Device implementations
// from the configuration. This should happen at create.
for _, info := range contConfig.DeviceInfos {
dev, err := sandbox.devManager.NewDevice(info)
dev, err := c.sandbox.devManager.NewDevice(info)
if err != nil {
return &Container{}, err
return err
}

storedDevices = append(storedDevices, ContainerDevice{
Expand All @@ -704,10 +731,9 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
GID: info.GID,
})
}
c.devices = filterDevices(sandbox, c, storedDevices)
c.devices = filterDevices(c.sandbox, c, storedDevices)
}

return c, nil
return nil
}

// rollbackFailingContainerCreation rolls back important steps that might have
Expand Down
12 changes: 6 additions & 6 deletions virtcontainers/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

var (
SandboxPersistNotExist = errors.New("sandbox doesn't exist in persist data")
ContainerPersistNotExist = errors.New("container doesn't exist in persist data")
errSandboxPersistNotExist = errors.New("sandbox doesn't exist in persist data")
errContainerPersistNotExist = errors.New("container doesn't exist in persist data")
)

func (s *Sandbox) dumpState(ss *persistapi.SandboxState, cs map[string]persistapi.ContainerState) error {
Expand Down Expand Up @@ -122,13 +122,13 @@ func (s *Sandbox) getSbxAndCntStates() (*persistapi.SandboxState, map[string]per
}

if len(cs) == 0 {
return nil, nil, SandboxPersistNotExist
return nil, nil, errSandboxPersistNotExist
}
}
return ss, cs, nil
}

// Restore will restore sandbox ata from persist file on disk
// Restore will restore sandbox data from persist file on disk
func (s *Sandbox) Restore() error {
ss, cs, err := s.getSbxAndCntStates()
if err != nil {
Expand All @@ -143,15 +143,15 @@ func (s *Sandbox) Restore() error {
return nil
}

// RestoreContainer will restore container data from persist file on disk
// Restore will restore container data from persist file on disk
func (c *Container) Restore() error {
_, cs, err := c.sandbox.getSbxAndCntStates()
if err != nil {
return err
}

if _, ok := cs[c.id]; !ok {
return ContainerPersistNotExist
return errContainerPersistNotExist
}

c.state = types.State{
Expand Down

0 comments on commit 78aad02

Please sign in to comment.