17
17
package main
18
18
19
19
import (
20
- "os"
21
- "path/filepath"
20
+ "errors"
22
21
"testing"
23
22
24
- "gotest.tools/v3/assert"
25
-
26
23
"github.com/containerd/containerd/v2/defaults"
27
24
28
25
"github.com/containerd/nerdctl/v2/pkg/testutil"
26
+ "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
27
+ "github.com/containerd/nerdctl/v2/pkg/testutil/test"
29
28
)
30
29
31
30
func TestMain (m * testing.M ) {
@@ -34,56 +33,100 @@ func TestMain(m *testing.M) {
34
33
35
34
// TestUnknownCommand tests https://github.com/containerd/nerdctl/issues/487
36
35
func TestUnknownCommand (t * testing.T ) {
37
- t .Parallel ()
38
- base := testutil .NewBase (t )
39
- base .Cmd ("non-existent-command" ).AssertFail ()
40
- base .Cmd ("non-existent-command" , "info" ).AssertFail ()
41
- base .Cmd ("system" , "non-existent-command" ).AssertFail ()
42
- base .Cmd ("system" , "non-existent-command" , "info" ).AssertFail ()
43
- base .Cmd ("system" ).AssertOK () // show help without error
44
- base .Cmd ("system" , "info" ).AssertOutContains ("Kernel Version:" )
45
- base .Cmd ("info" ).AssertOutContains ("Kernel Version:" )
36
+ nerdtest .Setup ()
37
+
38
+ var unknownSubCommand = errors .New ("unknown subcommand" )
39
+
40
+ testGroup := & test.Group {
41
+ {
42
+ Description : "non-existent-command" ,
43
+ Command : test .RunCommand ("non-existent-command" ),
44
+ Expected : test .Expects (1 , []error {unknownSubCommand }, nil ),
45
+ },
46
+ {
47
+ Description : "non-existent-command info" ,
48
+ Command : test .RunCommand ("non-existent-command" , "info" ),
49
+ Expected : test .Expects (1 , []error {unknownSubCommand }, nil ),
50
+ },
51
+ {
52
+ Description : "system non-existent-command" ,
53
+ Command : test .RunCommand ("system" , "non-existent-command" ),
54
+ Expected : test .Expects (1 , []error {unknownSubCommand }, nil ),
55
+ },
56
+ {
57
+ Description : "system non-existent-command info" ,
58
+ Command : test .RunCommand ("system" , "non-existent-command" , "info" ),
59
+ Expected : test .Expects (1 , []error {unknownSubCommand }, nil ),
60
+ },
61
+ {
62
+ Description : "system" ,
63
+ Command : test .RunCommand ("system" ),
64
+ Expected : test .Expects (0 , nil , nil ),
65
+ },
66
+ {
67
+ Description : "system info" ,
68
+ Command : test .RunCommand ("system" , "info" ),
69
+ Expected : test .Expects (0 , nil , test .Contains ("Kernel Version:" )),
70
+ },
71
+ {
72
+ Description : "info" ,
73
+ Command : test .RunCommand ("info" ),
74
+ Expected : test .Expects (0 , nil , test .Contains ("Kernel Version:" )),
75
+ },
76
+ }
77
+
78
+ testGroup .Run (t )
46
79
}
47
80
48
- // TestNerdctlConfig validates the configuration precedence [CLI, Env, TOML, Default].
81
+ // TestNerdctlConfig validates the configuration precedence [CLI, Env, TOML, Default] and broken config rejection
49
82
func TestNerdctlConfig (t * testing.T ) {
50
- testutil .DockerIncompatible (t )
51
- t .Parallel ()
52
- tomlPath := filepath .Join (t .TempDir (), "nerdctl.toml" )
53
- err := os .WriteFile (tomlPath , []byte (`
54
- snapshotter = "dummy-snapshotter-via-toml"
55
- ` ), 0400 )
56
- assert .NilError (t , err )
57
- base := testutil .NewBase (t )
58
-
59
- // [Default]
60
- base .Cmd ("info" , "-f" , "{{.Driver}}" ).AssertOutExactly (defaults .DefaultSnapshotter + "\n " )
61
-
62
- // [TOML, Default]
63
- base .Env = append (base .Env , "NERDCTL_TOML=" + tomlPath )
64
- base .Cmd ("info" , "-f" , "{{.Driver}}" ).AssertOutExactly ("dummy-snapshotter-via-toml\n " )
65
-
66
- // [CLI, TOML, Default]
67
- base .Cmd ("info" , "-f" , "{{.Driver}}" , "--snapshotter=dummy-snapshotter-via-cli" ).AssertOutExactly ("dummy-snapshotter-via-cli\n " )
68
-
69
- // [Env, TOML, Default]
70
- base .Env = append (base .Env , "CONTAINERD_SNAPSHOTTER=dummy-snapshotter-via-env" )
71
- base .Cmd ("info" , "-f" , "{{.Driver}}" ).AssertOutExactly ("dummy-snapshotter-via-env\n " )
72
-
73
- // [CLI, Env, TOML, Default]
74
- base .Cmd ("info" , "-f" , "{{.Driver}}" , "--snapshotter=dummy-snapshotter-via-cli" ).AssertOutExactly ("dummy-snapshotter-via-cli\n " )
75
- }
76
-
77
- func TestNerdctlConfigBad (t * testing.T ) {
78
- testutil .DockerIncompatible (t )
79
- t .Parallel ()
80
- tomlPath := filepath .Join (t .TempDir (), "config.toml" )
81
- err := os .WriteFile (tomlPath , []byte (`
82
- # containerd config, not nerdctl config
83
- version = 2
84
- ` ), 0400 )
85
- assert .NilError (t , err )
86
- base := testutil .NewBase (t )
87
- base .Env = append (base .Env , "NERDCTL_TOML=" + tomlPath )
88
- base .Cmd ("info" ).AssertFail ()
83
+ nerdtest .Setup ()
84
+
85
+ tc := & test.Case {
86
+ Description : "Nerdctl configuration" ,
87
+ // Docker does not support nerdctl.toml obviously
88
+ Require : test .Not (nerdtest .Docker ),
89
+ SubTests : []* test.Case {
90
+ {
91
+ Description : "Default" ,
92
+ Command : test .RunCommand ("info" , "-f" , "{{.Driver}}" ),
93
+ Expected : test .Expects (0 , nil , test .Equals (defaults .DefaultSnapshotter + "\n " )),
94
+ },
95
+ {
96
+ Description : "TOML > Default" ,
97
+ Command : test .RunCommand ("info" , "-f" , "{{.Driver}}" ),
98
+ Expected : test .Expects (0 , nil , test .Equals ("dummy-snapshotter-via-toml\n " )),
99
+ Data : test .WithConfig (nerdtest .NerdctlToml , `snapshotter = "dummy-snapshotter-via-toml"` ),
100
+ },
101
+ {
102
+ Description : "Cli > TOML > Default" ,
103
+ Command : test .RunCommand ("info" , "-f" , "{{.Driver}}" , "--snapshotter=dummy-snapshotter-via-cli" ),
104
+ Expected : test .Expects (0 , nil , test .Equals ("dummy-snapshotter-via-cli\n " )),
105
+ Data : test .WithConfig (nerdtest .NerdctlToml , `snapshotter = "dummy-snapshotter-via-toml"` ),
106
+ },
107
+ {
108
+ Description : "Env > TOML > Default" ,
109
+ Command : test .RunCommand ("info" , "-f" , "{{.Driver}}" ),
110
+ Env : map [string ]string {"CONTAINERD_SNAPSHOTTER" : "dummy-snapshotter-via-env" },
111
+ Expected : test .Expects (0 , nil , test .Equals ("dummy-snapshotter-via-env\n " )),
112
+ Data : test .WithConfig (nerdtest .NerdctlToml , `snapshotter = "dummy-snapshotter-via-toml"` ),
113
+ },
114
+ {
115
+ Description : "Cli > Env > TOML > Default" ,
116
+ Command : test .RunCommand ("info" , "-f" , "{{.Driver}}" , "--snapshotter=dummy-snapshotter-via-cli" ),
117
+ Env : map [string ]string {"CONTAINERD_SNAPSHOTTER" : "dummy-snapshotter-via-env" },
118
+ Expected : test .Expects (0 , nil , test .Equals ("dummy-snapshotter-via-cli\n " )),
119
+ Data : test .WithConfig (nerdtest .NerdctlToml , `snapshotter = "dummy-snapshotter-via-toml"` ),
120
+ },
121
+ {
122
+ Description : "Broken config" ,
123
+ Command : test .RunCommand ("info" ),
124
+ Expected : test .Expects (1 , []error {errors .New ("failed to load nerdctl config" )}, nil ),
125
+ Data : test .WithConfig (nerdtest .NerdctlToml , `# containerd config, not nerdctl config
126
+ version = 2` ),
127
+ },
128
+ },
129
+ }
130
+
131
+ tc .Run (t )
89
132
}
0 commit comments