Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 926ab56

Browse files
crosbymichaelvmarmol
authored andcommitted
Add testing for linux factory Load
Signed-off-by: Michael Crosby <[email protected]>
1 parent 7760faa commit 926ab56

File tree

9 files changed

+255
-73
lines changed

9 files changed

+255
-73
lines changed

container.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ type ContainerInfo interface {
99

1010
// Returns the current run state of the container.
1111
//
12-
// Errors:
12+
// errors:
1313
// ContainerDestroyed - Container no longer exists,
14-
// SystemError - System error.
15-
RunState() (*RunState, Error)
14+
// Systemerror - System error.
15+
RunState() (*RunState, error)
1616

1717
// Returns the current config of the container.
1818
Config() *Config
1919

2020
// Returns the PIDs inside this container. The PIDs are in the namespace of the calling process.
2121
//
22-
// Errors:
22+
// errors:
2323
// ContainerDestroyed - Container no longer exists,
24-
// SystemError - System error.
24+
// Systemerror - System error.
2525
//
2626
// Some of the returned PIDs may no longer refer to processes in the Container, unless
2727
// the Container state is PAUSED in which case every PID in the slice is valid.
28-
Processes() ([]int, Error)
28+
Processes() ([]int, error)
2929

3030
// Returns statistics for the container.
3131
//
32-
// Errors:
32+
// errors:
3333
// ContainerDestroyed - Container no longer exists,
34-
// SystemError - System error.
35-
Stats() (*ContainerStats, Error)
34+
// Systemerror - System error.
35+
Stats() (*ContainerStats, error)
3636
}
3737

3838
// A libcontainer container object.
@@ -45,60 +45,60 @@ type Container interface {
4545

4646
// Start a process inside the container. Returns the PID of the new process (in the caller process's namespace) and a channel that will return the exit status of the process whenever it dies.
4747
//
48-
// Errors:
48+
// errors:
4949
// ContainerDestroyed - Container no longer exists,
5050
// ConfigInvalid - config is invalid,
5151
// ContainerPaused - Container is paused,
52-
// SystemError - System error.
53-
StartProcess(config *ProcessConfig) (pid int, err Error)
52+
// Systemerror - System error.
53+
StartProcess(config *ProcessConfig) (pid int, err error)
5454

5555
// Destroys the container after killing all running processes.
5656
//
5757
// Any event registrations are removed before the container is destroyed.
5858
// No error is returned if the container is already destroyed.
5959
//
60-
// Errors:
61-
// SystemError - System error.
62-
Destroy() Error
60+
// errors:
61+
// Systemerror - System error.
62+
Destroy() error
6363

6464
// If the Container state is RUNNING or PAUSING, sets the Container state to PAUSING and pauses
6565
// the execution of any user processes. Asynchronously, when the container finished being paused the
6666
// state is changed to PAUSED.
6767
// If the Container state is PAUSED, do nothing.
6868
//
69-
// Errors:
69+
// errors:
7070
// ContainerDestroyed - Container no longer exists,
71-
// SystemError - System error.
72-
Pause() Error
71+
// Systemerror - System error.
72+
Pause() error
7373

7474
// If the Container state is PAUSED, resumes the execution of any user processes in the
7575
// Container before setting the Container state to RUNNING.
7676
// If the Container state is RUNNING, do nothing.
7777
//
78-
// Errors:
78+
// errors:
7979
// ContainerDestroyed - Container no longer exists,
80-
// SystemError - System error.
81-
Resume() Error
80+
// Systemerror - System error.
81+
Resume() error
8282

8383
// Signal sends the specified signal to a process owned by the container.
8484
//
85-
// Errors:
85+
// errors:
8686
// ContainerDestroyed - Container no longer exists,
8787
// ContainerPaused - Container is paused,
88-
// SystemError - System error.
89-
Signal(pid, signal int) Error
88+
// Systemerror - System error.
89+
Signal(pid, signal int) error
9090

9191
// Wait waits for the init process of the conatiner to die and returns it's exit status.
9292
//
93-
// Errors:
93+
// errors:
9494
// ContainerDestroyed - Container no longer exists,
95-
// SystemError - System error.
96-
Wait() (exitStatus int, err Error)
95+
// Systemerror - System error.
96+
Wait() (exitStatus int, err error)
9797

9898
// WaitProcess waits on a process owned by the container.
9999
//
100-
// Errors:
100+
// errors:
101101
// ContainerDestroyed - Container no longer exists,
102-
// SystemError - System error.
103-
WaitProcess(pid int) (exitStatus int, err Error)
102+
// Systemerror - System error.
103+
WaitProcess(pid int) (exitStatus int, err error)
104104
}

error.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package libcontainer
22

3+
import "io"
4+
35
// API error code type.
46
type ErrorCode int
57

@@ -10,7 +12,7 @@ const (
1012
InvalidIdFormat
1113

1214
// Container errors
13-
ContainerDestroyed
15+
ContainerNotExists
1416
ContainerPaused
1517

1618
// Common errors
@@ -24,14 +26,14 @@ func (c ErrorCode) String() string {
2426
return "Id already in use"
2527
case InvalidIdFormat:
2628
return "Invalid format"
27-
case ContainerDestroyed:
28-
return "Container destroyed"
2929
case ContainerPaused:
3030
return "Container paused"
3131
case ConfigInvalid:
3232
return "Invalid configuration"
3333
case SystemError:
34-
return "System Error"
34+
return "System error"
35+
case ContainerNotExists:
36+
return "Container does not exist"
3537
default:
3638
return "Unknown error"
3739
}
@@ -44,7 +46,7 @@ type Error interface {
4446
// Returns a verbose string including the error message
4547
// and a representation of the stack trace suitable for
4648
// printing.
47-
Detail() string
49+
Detail(w io.Writer) error
4850

4951
// Returns the error code for this error.
5052
Code() ErrorCode

error_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package libcontainer
2+
3+
import "testing"
4+
5+
func TestErrorCode(t *testing.T) {
6+
codes := map[ErrorCode]string{
7+
IdInUse: "Id already in use",
8+
InvalidIdFormat: "Invalid format",
9+
ContainerPaused: "Container paused",
10+
ConfigInvalid: "Invalid configuration",
11+
SystemError: "System error",
12+
ContainerNotExists: "Container does not exist",
13+
}
14+
15+
for code, expected := range codes {
16+
if actual := code.String(); actual != expected {
17+
t.Fatalf("expected string %q but received %q", expected, actual)
18+
}
19+
}
20+
}

factory.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ type Factory interface {
1010
//
1111
// Returns the new container with a running process.
1212
//
13-
// Errors:
13+
// errors:
1414
// IdInUse - id is already in use by a container
1515
// InvalidIdFormat - id has incorrect format
1616
// ConfigInvalid - config is invalid
17-
// SystemError - System error
17+
// Systemerror - System error
1818
//
1919
// On error, any partially created container parts are cleaned up (the operation is atomic).
20-
Create(id string, config *Config) (Container, Error)
20+
Create(id string, config *Config) (Container, error)
2121

2222
// Load takes an ID for an existing container and returns the container information
2323
// from the state. This presents a read only view of the container.
2424
//
25-
// Errors:
25+
// errors:
2626
// Path does not exist
2727
// Container is stopped
2828
// System error
29-
Load(id string) (ContainerInfo, Error)
29+
Load(id string) (ContainerInfo, error)
3030
}

generic_error.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
package libcontainer
22

33
import (
4-
"bytes"
54
"fmt"
6-
"runtime"
5+
"io"
6+
"text/template"
77
"time"
8+
9+
"github.com/docker/libcontainer/stacktrace"
810
)
911

10-
var newLine = []byte("\n")
12+
var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}}
13+
Code: {{.ECode}}
14+
Message: {{.Err.Error}}
15+
Frames:{{range $i, $frame := .Stack.Frames}}
16+
---
17+
{{$i}}: {{$frame.Function}}
18+
Package: {{$frame.Package}}
19+
File: {{$frame.File}}{{end}}
20+
`))
1121

1222
func newGenericError(err error, c ErrorCode) Error {
1323
return &GenericError{
14-
timestamp: time.Now(),
15-
err: err,
16-
code: c,
17-
stack: captureStackTrace(2),
24+
Timestamp: time.Now(),
25+
Err: err,
26+
ECode: c,
27+
Stack: stacktrace.Capture(2),
1828
}
1929
}
2030

21-
func captureStackTrace(skip int) string {
22-
buf := make([]byte, 4096)
23-
buf = buf[:runtime.Stack(buf, true)]
24-
25-
lines := bytes.Split(buf, newLine)
26-
return string(bytes.Join(lines[skip:], newLine))
27-
}
28-
2931
type GenericError struct {
30-
timestamp time.Time
31-
code ErrorCode
32-
err error
33-
stack string
32+
Timestamp time.Time
33+
ECode ErrorCode
34+
Err error
35+
Stack stacktrace.Stacktrace
3436
}
3537

3638
func (e *GenericError) Error() string {
37-
return fmt.Sprintf("[%d] %s: %s", e.code, e.code, e.err)
39+
return fmt.Sprintf("[%d] %s: %s", e.ECode, e.ECode, e.Err)
3840
}
3941

4042
func (e *GenericError) Code() ErrorCode {
41-
return e.code
43+
return e.ECode
4244
}
4345

44-
func (e *GenericError) Detail() string {
45-
return fmt.Sprintf("[%d] %s\n%s", e.code, e.err, e.stack)
46+
func (e *GenericError) Detail(w io.Writer) error {
47+
return errorTemplate.Execute(w, e)
4648
}

generic_error_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package libcontainer
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"testing"
7+
)
8+
9+
func TestErrorDetail(t *testing.T) {
10+
err := newGenericError(fmt.Errorf("test error"), SystemError)
11+
if derr := err.Detail(ioutil.Discard); derr != nil {
12+
t.Fatal(derr)
13+
}
14+
}

linux_container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func (c *linuxContainer) Config() *Config {
2323
return c.config
2424
}
2525

26-
func (c *linuxContainer) RunState() (*RunState, Error) {
26+
func (c *linuxContainer) RunState() (*RunState, error) {
2727
panic("not implemented")
2828
}
2929

30-
func (c *linuxContainer) Processes() ([]int, Error) {
30+
func (c *linuxContainer) Processes() ([]int, error) {
3131
var (
3232
err error
3333
pids []int
@@ -44,7 +44,7 @@ func (c *linuxContainer) Processes() ([]int, Error) {
4444
return pids, nil
4545
}
4646

47-
func (c *linuxContainer) Stats() (*ContainerStats, Error) {
47+
func (c *linuxContainer) Stats() (*ContainerStats, error) {
4848
var (
4949
err error
5050
stats = &ContainerStats{}

linux_factory.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
)
1515

1616
// New returns a linux based container factory based in the root directory.
17-
func New(root string) (Factory, Error) {
17+
func New(root string) (Factory, error) {
1818
if err := os.MkdirAll(root, 0700); err != nil {
1919
return nil, newGenericError(err, SystemError)
2020
}
@@ -30,11 +30,11 @@ type linuxFactory struct {
3030
root string
3131
}
3232

33-
func (l *linuxFactory) Create(id string, config *Config) (Container, Error) {
33+
func (l *linuxFactory) Create(id string, config *Config) (Container, error) {
3434
panic("not implemented")
3535
}
3636

37-
func (l *linuxFactory) Load(id string) (ContainerInfo, Error) {
37+
func (l *linuxFactory) Load(id string) (ContainerInfo, error) {
3838
containerRoot := filepath.Join(l.root, id)
3939
config, err := l.loadContainerConfig(containerRoot)
4040
if err != nil {
@@ -54,11 +54,11 @@ func (l *linuxFactory) Load(id string) (ContainerInfo, Error) {
5454
}, nil
5555
}
5656

57-
func (l *linuxFactory) loadContainerConfig(root string) (*Config, Error) {
57+
func (l *linuxFactory) loadContainerConfig(root string) (*Config, error) {
5858
f, err := os.Open(filepath.Join(root, configFilename))
5959
if err != nil {
6060
if os.IsNotExist(err) {
61-
return nil, newGenericError(err, ContainerDestroyed)
61+
return nil, newGenericError(err, ContainerNotExists)
6262
}
6363
return nil, newGenericError(err, SystemError)
6464
}
@@ -71,11 +71,11 @@ func (l *linuxFactory) loadContainerConfig(root string) (*Config, Error) {
7171
return config, nil
7272
}
7373

74-
func (l *linuxFactory) loadContainerState(root string) (*State, Error) {
74+
func (l *linuxFactory) loadContainerState(root string) (*State, error) {
7575
f, err := os.Open(filepath.Join(root, stateFilename))
7676
if err != nil {
7777
if os.IsNotExist(err) {
78-
return nil, newGenericError(err, ContainerDestroyed)
78+
return nil, newGenericError(err, ContainerNotExists)
7979
}
8080
return nil, newGenericError(err, SystemError)
8181
}

0 commit comments

Comments
 (0)