github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/main_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/containerd/containerd"
    25  	"github.com/containerd/nerdctl/pkg/testutil"
    26  	"gotest.tools/v3/assert"
    27  )
    28  
    29  func TestMain(m *testing.M) {
    30  	testutil.M(m)
    31  }
    32  
    33  // TestUnknownCommand tests https://github.com/containerd/nerdctl/issues/487
    34  func TestUnknownCommand(t *testing.T) {
    35  	t.Parallel()
    36  	base := testutil.NewBase(t)
    37  	base.Cmd("non-existent-command").AssertFail()
    38  	base.Cmd("non-existent-command", "info").AssertFail()
    39  	base.Cmd("system", "non-existent-command").AssertFail()
    40  	base.Cmd("system", "non-existent-command", "info").AssertFail()
    41  	base.Cmd("system").AssertOK() // show help without error
    42  	base.Cmd("system", "info").AssertOutContains("Kernel Version:")
    43  	base.Cmd("info").AssertOutContains("Kernel Version:")
    44  }
    45  
    46  // TestNerdctlConfig validates the configuration precedence [CLI, Env, TOML, Default].
    47  func TestNerdctlConfig(t *testing.T) {
    48  	testutil.DockerIncompatible(t)
    49  	t.Parallel()
    50  	tomlPath := filepath.Join(t.TempDir(), "nerdctl.toml")
    51  	err := os.WriteFile(tomlPath, []byte(`
    52  snapshotter = "dummy-snapshotter-via-toml"
    53  `), 0400)
    54  	assert.NilError(t, err)
    55  	base := testutil.NewBase(t)
    56  
    57  	// [Default]
    58  	base.Cmd("info", "-f", "{{.Driver}}").AssertOutExactly(containerd.DefaultSnapshotter + "\n")
    59  
    60  	// [TOML, Default]
    61  	if len(base.Env) == 0 {
    62  		base.Env = os.Environ()
    63  	}
    64  	base.Env = append(base.Env, "NERDCTL_TOML="+tomlPath)
    65  	base.Cmd("info", "-f", "{{.Driver}}").AssertOutExactly("dummy-snapshotter-via-toml\n")
    66  
    67  	// [CLI, TOML, Default]
    68  	base.Cmd("info", "-f", "{{.Driver}}", "--snapshotter=dummy-snapshotter-via-cli").AssertOutExactly("dummy-snapshotter-via-cli\n")
    69  
    70  	// [Env, TOML, Default]
    71  	base.Env = append(base.Env, "CONTAINERD_SNAPSHOTTER=dummy-snapshotter-via-env")
    72  	base.Cmd("info", "-f", "{{.Driver}}").AssertOutExactly("dummy-snapshotter-via-env\n")
    73  
    74  	// [CLI, Env, TOML, Default]
    75  	base.Cmd("info", "-f", "{{.Driver}}", "--snapshotter=dummy-snapshotter-via-cli").AssertOutExactly("dummy-snapshotter-via-cli\n")
    76  }
    77  
    78  func TestNerdctlConfigBad(t *testing.T) {
    79  	testutil.DockerIncompatible(t)
    80  	t.Parallel()
    81  	tomlPath := filepath.Join(t.TempDir(), "config.toml")
    82  	err := os.WriteFile(tomlPath, []byte(`
    83  # containerd config, not nerdctl config
    84  version = 2
    85  `), 0400)
    86  	assert.NilError(t, err)
    87  	base := testutil.NewBase(t)
    88  	base.Env = append(base.Env, "NERDCTL_TOML="+tomlPath)
    89  	base.Cmd("info").AssertFail()
    90  }