forked from kunalkushwaha/ctr-powertest
/
testContainerBasic.go
138 lines (113 loc) · 3.02 KB
/
testContainerBasic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package testcase
import (
"context"
"fmt"
"github.com/kunalkushwaha/ctr-powertest/libocispec"
"github.com/kunalkushwaha/ctr-powertest/libruntime"
log "github.com/sirupsen/logrus"
)
type BasicContainerTest struct {
Runtime libruntime.Runtime
}
func (t *BasicContainerTest) RunTestCases(ctx context.Context, testcases, args []string) error {
log.Info("Running tests on ", t.Runtime.Version(ctx))
if err := t.TestPullContainerImage(ctx, testImage); err != nil {
return err
}
if err := t.TestCreateContainers(ctx, "test", testImage); err != nil {
return err
}
if err := t.TestCreateRunningContainers(ctx, testContainerName, testImage); err != nil {
return err
}
if err := t.TestCreateRunningNWaitContainers(ctx, testContainerName, testImage); err != nil {
return err
}
return nil
}
func (t *BasicContainerTest) TestPullContainerImage(ctx context.Context, imageName string) error {
log.Info("TestPullContainerImage..")
// Pull image from remote repo.
_, err := t.Runtime.Pull(ctx, imageName)
if err != nil {
return err
}
// Pull image in already present in locally.
_, err = t.Runtime.Pull(ctx, imageName)
if err != nil {
return err
}
//TODO: Cleanup function.
log.Info("OK..")
return nil
}
func (t *BasicContainerTest) TestCreateContainers(ctx context.Context, containerName, imageName string) error {
//TODO :
// Test with tty container,
// Test without tty container
// Test background container.
log.Info("TestCreateContainers..")
ctr, err := t.Runtime.Create(ctx, containerName, imageName, nil)
if err != nil {
return err
}
err = t.Runtime.Delete(ctx, ctr)
if err != nil {
return err
}
log.Info("OK..")
return nil
}
func (t *BasicContainerTest) TestCreateRunningContainers(ctx context.Context, containerName, imageName string) error {
log.Info("TestCreateRunningContainers..")
statusC, ctr, err := t.Runtime.Run(ctx, containerName, imageName, nil)
if err != nil {
return err
}
err = t.Runtime.Stop(ctx, ctr)
if err != nil {
return fmt.Errorf("Container Stop: %v", err)
}
waitForContainerEvent(statusC)
err = t.Runtime.Delete(ctx, ctr)
if err != nil {
return fmt.Errorf("Container Delete: %v", err)
}
log.Info("OK..")
return nil
}
func (t *BasicContainerTest) TestCreateRunningNWaitContainers(ctx context.Context, containerName, imageName string) error {
log.Info("TestCreateRunningNWaitContainers..")
specs, err := libocispec.GenerateSpec(libocispec.WithProcessArgs("sleep", "5s"))
if err != nil {
return err
}
// Create -> Runnable -> Wait -> Start -> Stop -> Listen -> Delete
ctr, err := t.Runtime.Create(ctx, containerName, imageName, specs)
if err != nil {
return err
}
err = t.Runtime.Runnable(ctx, ctr)
if err != nil {
return err
}
statusC, err := t.Runtime.Wait(ctx, ctr)
if err != nil {
return err
}
err = t.Runtime.Start(ctx, ctr)
if err != nil {
return err
}
waitForContainerEvent(statusC)
err = t.Runtime.Stop(ctx, ctr)
if err != nil {
return err
}
err = t.Runtime.Delete(ctx, ctr)
if err != nil {
return err
}
log.Info("OK..")
return nil
}