Skip to content

Commit

Permalink
test: fix 'talosctl gen' tests
Browse files Browse the repository at this point in the history
There were weird hacks put into the tests, while each test already runs
in a temporary directory as 'working directory', so no hacks are needed.

Moreover, using fixed `/tmp/...` paths leads to test failures, as CI
runs docker & QEMU tests in parallel conflicting with each other.

Signed-off-by: Andrey Smirnov <[email protected]>
(cherry picked from commit f9639fb)
  • Loading branch information
smira committed Oct 17, 2023
1 parent 69f1ea2 commit b72abb6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
23 changes: 5 additions & 18 deletions internal/integration/cli/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,40 +225,27 @@ func (suite *GenSuite) TestSecrets() {

suite.RunCLI([]string{"gen", "secrets", "--force"}, base.StdoutEmpty())

defer os.Remove("secrets.yaml") //nolint:errcheck

suite.RunCLI([]string{"gen", "secrets", "--output-file", "/tmp/secrets2.yaml"}, base.StdoutEmpty())
suite.Assert().FileExists("/tmp/secrets2.yaml")

defer os.Remove("/tmp/secrets2.yaml") //nolint:errcheck
suite.RunCLI([]string{"gen", "secrets", "--output-file", "secrets2.yaml"}, base.StdoutEmpty())
suite.Assert().FileExists("secrets2.yaml")

suite.RunCLI([]string{"gen", "secrets", "-o", "secrets3.yaml", "--talos-version", "v0.8"}, base.StdoutEmpty())
suite.Assert().FileExists("secrets3.yaml")

defer os.Remove("secrets3.yaml") //nolint:errcheck
}

// TestSecretsWithPKIDirAndToken ...
func (suite *GenSuite) TestSecretsWithPKIDirAndToken() {
path := "/tmp/secrets-with-pki-dir-and-token.yaml"

tempDir := suite.T().TempDir()

dir, err := writeKubernetesPKIFiles(tempDir)
suite.Assert().NoError(err)
path := "secrets-with-pki-dir-and-token.yaml"

defer os.RemoveAll(dir) //nolint:errcheck
suite.Require().NoError(writeKubernetesPKIFiles("k8s-pki/"))

suite.RunCLI([]string{
"gen", "secrets", "--from-kubernetes-pki", dir,
"gen", "secrets", "--from-kubernetes-pki", "k8s-pki/",
"--kubernetes-bootstrap-token", "test-token",
"--output-file", path,
}, base.StdoutEmpty())

suite.Assert().FileExists(path)

defer os.Remove(path) //nolint:errcheck

secretsYaml, err := os.ReadFile(path)
suite.Assert().NoError(err)

Expand Down
40 changes: 19 additions & 21 deletions internal/integration/cli/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,39 @@ var (
pkiEtcdCAKey []byte
)

func writeKubernetesPKIFiles(dir string) (string, error) {
var err error

if err = os.WriteFile(filepath.Join(dir, "ca.crt"), pkiCACrt, 0o777); err != nil {
return "", err
func writeKubernetesPKIFiles(dir string) error {
if err := os.Mkdir(dir, 0o777); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(dir, "ca.key"), pkiCAKey, 0o777); err != nil {
return "", err
if err := os.WriteFile(filepath.Join(dir, "ca.crt"), pkiCACrt, 0o777); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(dir, "front-proxy-ca.crt"), pkiFrontProxyCACrt, 0o777); err != nil {
return "", err
if err := os.WriteFile(filepath.Join(dir, "ca.key"), pkiCAKey, 0o777); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(dir, "front-proxy-ca.key"), pkiFrontProxyCAKey, 0o777); err != nil {
return "", err
if err := os.WriteFile(filepath.Join(dir, "front-proxy-ca.crt"), pkiFrontProxyCACrt, 0o777); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(dir, "sa.key"), pkiSAKey, 0o777); err != nil {
return "", err
if err := os.WriteFile(filepath.Join(dir, "front-proxy-ca.key"), pkiFrontProxyCAKey, 0o777); err != nil {
return err
}

etcdDir := filepath.Join(dir, "etcd")
if err = os.Mkdir(etcdDir, 0o777); err != nil {
return "", err
if err := os.WriteFile(filepath.Join(dir, "sa.key"), pkiSAKey, 0o777); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(etcdDir, "ca.crt"), pkiEtcdCACrt, 0o777); err != nil {
return "", err
etcdDir := filepath.Join(dir, "etcd")
if err := os.Mkdir(etcdDir, 0o777); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(etcdDir, "ca.key"), pkiEtcdCAKey, 0o777); err != nil {
return "", err
if err := os.WriteFile(filepath.Join(etcdDir, "ca.crt"), pkiEtcdCACrt, 0o777); err != nil {
return err
}

return dir, nil
return os.WriteFile(filepath.Join(etcdDir, "ca.key"), pkiEtcdCAKey, 0o777)
}

0 comments on commit b72abb6

Please sign in to comment.