11package validation
22
33import (
4- "fmt"
54 "io/ioutil"
65 "os"
76 "os/exec"
87 "path/filepath"
98 "testing"
109
1110 "github.com/mrunalp/fileutils"
12- "github.com/opencontainers/runtime-tools/generate "
11+ rspecs "github.com/opencontainers/runtime-spec/specs-go "
1312 "github.com/satori/go.uuid"
13+ "github.com/stretchr/testify/assert"
14+
15+ "github.com/opencontainers/runtime-tools/generate"
16+ "github.com/opencontainers/runtime-tools/validate"
1417)
1518
1619var (
@@ -24,81 +27,106 @@ func init() {
2427 }
2528}
2629
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" )
3033 if err != nil {
31- return err
34+ return "" , err
3235 }
3336
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 ()
3640 if err != nil {
37- return err
41+ os .RemoveAll (bundleDir )
42+ return "" , err
3843 }
39- defer os .RemoveAll (tmpDir )
4044
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 {
4458 return err
4559 }
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 )
5061 if err != nil {
51- fmt . Println ( string ( output ) )
62+ os . RemoveAll ( bundleDir )
5263 return err
5364 }
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 )
5767 if err != nil {
5868 return err
5969 }
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" ))
6371 if err != nil {
6472 return err
6573 }
6674
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 {
7678 return err
7779 }
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 ()
8781}
8882
8983func TestValidateBasic (t * testing.T ) {
9084 g := getDefaultGenerator ()
9185
92- if err := runtimeValidate (runtime , g ); err != nil {
93- t .Errorf ("%s failed validation: %v" , runtime , err )
94- }
86+ assert .Nil (t , runtimeInsideValidate (g ))
9587}
9688
9789func TestValidateSysctls (t * testing.T ) {
9890 g := getDefaultGenerator ()
9991 g .AddLinuxSysctl ("net.ipv4.ip_forward" , "1" )
10092
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 , validate .NewError (validate .CreateWithID , "'Create' MUST generate an error if the ID is not provided" , rspecs .Version )},
118+ {containerID , true , validate .NewError (validate .CreateNewContainer , "'Create' MUST create a new container" , rspecs .Version )},
119+ {containerID , false , validate .NewError (validate .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+ }
103131 }
104132}
0 commit comments