1
1
package validation
2
2
3
3
import (
4
- "fmt"
5
4
"io/ioutil"
6
5
"os"
7
6
"os/exec"
8
7
"path/filepath"
9
8
"testing"
10
9
11
10
"github.com/mrunalp/fileutils"
12
- "github.com/opencontainers/runtime-tools/generate "
11
+ rspecs "github.com/opencontainers/runtime-spec/specs-go "
13
12
"github.com/satori/go.uuid"
13
+ "github.com/stretchr/testify/assert"
14
+
15
+ rerr "github.com/opencontainers/runtime-tools/error"
16
+ "github.com/opencontainers/runtime-tools/generate"
14
17
)
15
18
16
19
var (
@@ -24,81 +27,106 @@ func init() {
24
27
}
25
28
}
26
29
27
- func runtimeValidate ( runtime string , g * generate. Generator ) error {
28
- // Find the runtime binary in the PATH
29
- runtimePath , err := exec . LookPath ( runtime )
30
+ func prepareBundle () ( string , error ) {
31
+ // Setup a temporary test directory
32
+ bundleDir , err := ioutil . TempDir ( "" , "ocitest" )
30
33
if err != nil {
31
- return err
34
+ return "" , err
32
35
}
33
36
34
- // Setup a temporary test directory
35
- tmpDir , err := ioutil .TempDir ("" , "ocitest" )
37
+ // Untar the root fs
38
+ untarCmd := exec .Command ("tar" , "-xf" , "../rootfs.tar.gz" , "-C" , bundleDir )
39
+ _ , err = untarCmd .CombinedOutput ()
36
40
if err != nil {
37
- return err
41
+ os .RemoveAll (bundleDir )
42
+ return "" , err
38
43
}
39
- defer os .RemoveAll (tmpDir )
40
44
41
- // Create bundle directory for the test container
42
- bundleDir := tmpDir + "/busybox"
43
- if err := os .MkdirAll (bundleDir , 0755 ); err != nil {
45
+ return bundleDir , nil
46
+ }
47
+
48
+ func getDefaultGenerator () * generate.Generator {
49
+ g := generate .New ()
50
+ g .SetRootPath ("." )
51
+ g .SetProcessArgs ([]string {"/runtimetest" })
52
+ return & g
53
+ }
54
+
55
+ func runtimeInsideValidate (g * generate.Generator ) error {
56
+ bundleDir , err := prepareBundle ()
57
+ if err != nil {
44
58
return err
45
59
}
46
-
47
- // Untar the root fs
48
- untarCmd := exec .Command ("tar" , "-xf" , "../rootfs.tar.gz" , "-C" , bundleDir )
49
- output , err := untarCmd .CombinedOutput ()
60
+ r , err := NewRuntime (runtime , bundleDir )
50
61
if err != nil {
51
- fmt . Println ( string ( output ) )
62
+ os . RemoveAll ( bundleDir )
52
63
return err
53
64
}
54
-
55
- // Copy the runtimetest binary to the rootfs
56
- err = fileutils .CopyFile ("../runtimetest" , filepath .Join (bundleDir , "runtimetest" ))
65
+ defer r .Clean (true )
66
+ err = r .SetConfig (g )
57
67
if err != nil {
58
68
return err
59
69
}
60
-
61
- // Generate test configuration
62
- err = g .SaveToFile (filepath .Join (bundleDir , "config.json" ), generate.ExportOptions {})
70
+ err = fileutils .CopyFile ("../runtimetest" , filepath .Join (r .BundleDir , "runtimetest" ))
63
71
if err != nil {
64
72
return err
65
73
}
66
74
67
- // TODO: Use a library to split run into create/start
68
- // Launch the OCI runtime
69
- containerID := uuid .NewV4 ()
70
- runtimeCmd := exec .Command (runtimePath , "run" , containerID .String ())
71
- runtimeCmd .Dir = bundleDir
72
- runtimeCmd .Stdin = os .Stdin
73
- runtimeCmd .Stdout = os .Stdout
74
- runtimeCmd .Stderr = os .Stderr
75
- if err = runtimeCmd .Run (); err != nil {
75
+ r .SetID (uuid .NewV4 ().String ())
76
+ err = r .Create ()
77
+ if err != nil {
76
78
return err
77
79
}
78
-
79
- return nil
80
- }
81
-
82
- func getDefaultGenerator () * generate.Generator {
83
- g := generate .New ()
84
- g .SetRootPath ("." )
85
- g .SetProcessArgs ([]string {"/runtimetest" })
86
- return & g
80
+ return r .Start ()
87
81
}
88
82
89
83
func TestValidateBasic (t * testing.T ) {
90
84
g := getDefaultGenerator ()
91
85
92
- if err := runtimeValidate (runtime , g ); err != nil {
93
- t .Errorf ("%s failed validation: %v" , runtime , err )
94
- }
86
+ assert .Nil (t , runtimeInsideValidate (g ))
95
87
}
96
88
97
89
func TestValidateSysctls (t * testing.T ) {
98
90
g := getDefaultGenerator ()
99
91
g .AddLinuxSysctl ("net.ipv4.ip_forward" , "1" )
100
92
101
- if err := runtimeValidate (runtime , g ); err != nil {
102
- t .Errorf ("%s failed validation: %v" , runtime , err )
93
+ assert .Nil (t , runtimeInsideValidate (g ))
94
+ }
95
+
96
+ func TestValidateCreate (t * testing.T ) {
97
+ g := generate .New ()
98
+ g .SetRootPath ("." )
99
+ g .SetProcessArgs ([]string {"ls" })
100
+
101
+ bundleDir , err := prepareBundle ()
102
+ assert .Nil (t , err )
103
+
104
+ r , err := NewRuntime (runtime , bundleDir )
105
+ assert .Nil (t , err )
106
+ defer r .Clean (true )
107
+
108
+ err = r .SetConfig (& g )
109
+ assert .Nil (t , err )
110
+
111
+ containerID := uuid .NewV4 ().String ()
112
+ cases := []struct {
113
+ id string
114
+ errExpected bool
115
+ err error
116
+ }{
117
+ {"" , false , rerr .NewError (rerr .CreateWithID , "'Create' MUST generate an error if the ID is not provided" , rspecs .Version )},
118
+ {containerID , true , rerr .NewError (rerr .CreateNewContainer , "'Create' MUST create a new container" , rspecs .Version )},
119
+ {containerID , false , rerr .NewError (rerr .CreateWithUniqueID , "'Create' MUST generate an error if the ID provided is not unique" , rspecs .Version )},
120
+ }
121
+
122
+ for _ , c := range cases {
123
+ r .SetID (c .id )
124
+ err := r .Create ()
125
+ assert .Equal (t , c .errExpected , err == nil , c .err .Error ())
126
+
127
+ if err == nil {
128
+ state , _ := r .State ()
129
+ assert .Equal (t , c .id , state .ID , c .err .Error ())
130
+ }
103
131
}
104
132
}
0 commit comments