Skip to content

Commit

Permalink
chore: refactor CLI tests
Browse files Browse the repository at this point in the history
Use testing.T.TempDir.
Add support for `talosctl --endpoints`.

Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
  • Loading branch information
AlekSi authored and talos-bot committed Jun 23, 2021
1 parent 0fd9ea2 commit 3c1b321
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 68 deletions.
14 changes: 6 additions & 8 deletions internal/integration/base/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ package base

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -73,10 +71,7 @@ func (cliSuite *CLISuite) RandomDiscoveredNode(types ...machine.Type) string {

func (cliSuite *CLISuite) discoverKubectl() cluster.Info {
// pull down kubeconfig into temporary directory
tempDir, err := ioutil.TempDir("", "talos")
cliSuite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := cliSuite.T().TempDir()

// rely on `nodes:` being set in talosconfig
cliSuite.RunCLI([]string{"kubeconfig", tempDir}, StdoutEmpty())
Expand All @@ -96,15 +91,18 @@ func (cliSuite *CLISuite) discoverKubectl() cluster.Info {
}

func (cliSuite *CLISuite) buildCLICmd(args []string) *exec.Cmd {
// TODO: add support for calling `talosctl config endpoint` before running talosctl
if cliSuite.Endpoint != "" {
args = append([]string{"--endpoints", cliSuite.Endpoint}, args...)
}

args = append([]string{"--talosconfig", cliSuite.TalosConfig}, args...)

return exec.Command(cliSuite.TalosctlPath, args...)
}

// RunCLI runs talosctl binary with the options provided.
func (cliSuite *CLISuite) RunCLI(args []string, options ...RunOption) (stdout string) {
return Run(&cliSuite.Suite, cliSuite.buildCLICmd(args), options...)
return run(&cliSuite.Suite, cliSuite.buildCLICmd(args), options...)
}

// RunAndWaitForMatch retries command until output matches.
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/base/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ func runAndWait(suite *suite.Suite, cmd *exec.Cmd) (stdoutBuf, stderrBuf *bytes.
return &stdout, &stderr, err
}

// Run executes command, asserts on its exit status/output, and returns stdout.
// run executes command, asserts on its exit status/output, and returns stdout.
//
//nolint:gocyclo,nakedret
func Run(suite *suite.Suite, cmd *exec.Cmd, options ...RunOption) (stdout string) {
func run(suite *suite.Suite, cmd *exec.Cmd, options ...RunOption) (stdout string) {
var opts runOptions

for _, o := range options {
Expand Down
7 changes: 1 addition & 6 deletions internal/integration/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
package cli

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"

Expand All @@ -34,10 +32,7 @@ func (suite *TalosconfigSuite) TestList() {

// TestMerge checks how talosctl config merge.
func (suite *TalosconfigSuite) TestMerge() {
tempDir, err := ioutil.TempDir("", "talos")
defer os.RemoveAll(tempDir) //nolint:errcheck

suite.Require().NoError(err)
tempDir := suite.T().TempDir()

suite.RunCLI([]string{"gen", "config", "-o", tempDir, "foo", "https://192.168.0.1:6443"})

Expand Down
8 changes: 2 additions & 6 deletions internal/integration/cli/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package cli

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -27,15 +26,12 @@ func (suite *CopySuite) SuiteName() string {

// TestSuccess runs comand with success.
func (suite *CopySuite) TestSuccess() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := suite.T().TempDir()

suite.RunCLI([]string{"copy", "--nodes", suite.RandomDiscoveredNode(), "/etc/os-release", tempDir},
base.StdoutEmpty())

_, err = os.Stat(filepath.Join(tempDir, "os-release"))
_, err := os.Stat(filepath.Join(tempDir, "os-release"))
suite.Require().NoError(err)
}

Expand Down
9 changes: 1 addition & 8 deletions internal/integration/cli/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
package cli

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"

Expand Down Expand Up @@ -46,12 +44,7 @@ func (suite *EtcdSuite) TestForfeitLeadership() {

// TestSnapshot tests etcd snapshot (backup).
func (suite *EtcdSuite) TestSnapshot() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer func() {
suite.Assert().NoError(os.RemoveAll(tempDir))
}()
tempDir := suite.T().TempDir()

dbPath := filepath.Join(tempDir, "snapshot.db")

Expand Down
10 changes: 2 additions & 8 deletions internal/integration/cli/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cli

import (
"encoding/json"
"io/ioutil"
"os"
"regexp"

Expand All @@ -31,10 +30,9 @@ func (suite *GenSuite) SuiteName() string {

// SetupTest ...
func (suite *GenSuite) SetupTest() {
var err error
suite.tmpDir, err = ioutil.TempDir("", "talos")
suite.Require().NoError(err)
suite.tmpDir = suite.T().TempDir()

var err error
suite.savedCwd, err = os.Getwd()
suite.Require().NoError(err)

Expand All @@ -46,10 +44,6 @@ func (suite *GenSuite) TearDownTest() {
if suite.savedCwd != "" {
suite.Require().NoError(os.Chdir(suite.savedCwd))
}

if suite.tmpDir != "" {
suite.Require().NoError(os.RemoveAll(suite.tmpDir))
}
}

// TestCA ...
Expand Down
28 changes: 6 additions & 22 deletions internal/integration/cli/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package cli

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -30,27 +29,21 @@ func (suite *KubeconfigSuite) SuiteName() string {

// TestDirectory generates kubeconfig in specified directory.
func (suite *KubeconfigSuite) TestDirectory() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := suite.T().TempDir()

suite.RunCLI([]string{"kubeconfig", "--merge=false", "--nodes", suite.RandomDiscoveredNode(machine.TypeControlPlane), tempDir},
base.StdoutEmpty())

path := filepath.Join(tempDir, "kubeconfig")
suite.Require().FileExists(path)

_, err = clientcmd.LoadFromFile(path)
_, err := clientcmd.LoadFromFile(path)
suite.Require().NoError(err)
}

// TestCwd generates kubeconfig in cwd.
func (suite *KubeconfigSuite) TestCwd() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := suite.T().TempDir()

savedCwd, err := os.Getwd()
suite.Require().NoError(err)
Expand All @@ -67,10 +60,7 @@ func (suite *KubeconfigSuite) TestCwd() {

// TestCustomName generates kubeconfig with custom name.
func (suite *KubeconfigSuite) TestCustomName() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := suite.T().TempDir()

suite.RunCLI([]string{"kubeconfig", "--merge=false", "--nodes", suite.RandomDiscoveredNode(machine.TypeControlPlane), filepath.Join(tempDir, "k8sconfig")},
base.StdoutEmpty())
Expand All @@ -89,10 +79,7 @@ func (suite *KubeconfigSuite) TestMultiNodeFail() {

// TestMergeRename tests merge config into existing kubeconfig with default rename conflict resolution.
func (suite *KubeconfigSuite) TestMergeRename() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := suite.T().TempDir()

path := filepath.Join(tempDir, "config")

Expand All @@ -108,10 +95,7 @@ func (suite *KubeconfigSuite) TestMergeRename() {

// TestMergeOverwrite test merge config into existing kubeconfig with overwrite conflict resolution.
func (suite *KubeconfigSuite) TestMergeOverwrite() {
tempDir, err := ioutil.TempDir("", "talos")
suite.Require().NoError(err)

defer os.RemoveAll(tempDir) //nolint:errcheck
tempDir := suite.T().TempDir()

path := filepath.Join(tempDir, "config")

Expand Down
10 changes: 2 additions & 8 deletions internal/integration/cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cli

import (
"fmt"
"io/ioutil"
"os"

"github.com/talos-systems/talos/internal/integration/base"
Expand All @@ -29,10 +28,9 @@ func (suite *ValidateSuite) SuiteName() string {

// SetupTest ...
func (suite *ValidateSuite) SetupTest() {
var err error
suite.tmpDir, err = ioutil.TempDir("", "talos")
suite.Require().NoError(err)
suite.tmpDir = suite.T().TempDir()

var err error
suite.savedCwd, err = os.Getwd()
suite.Require().NoError(err)

Expand All @@ -44,10 +42,6 @@ func (suite *ValidateSuite) TearDownTest() {
if suite.savedCwd != "" {
suite.Require().NoError(os.Chdir(suite.savedCwd))
}

if suite.tmpDir != "" {
suite.Require().NoError(os.RemoveAll(suite.tmpDir))
}
}

// TestValidate generates config and validates it for all the modes.
Expand Down

0 comments on commit 3c1b321

Please sign in to comment.