mirror of https://github.com/knative/client.git
test: use `T.TempDir` to create temporary test directory (#1660)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* docs: update CHANGELOG.adoc
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
ff097e9d8d
commit
736c7c24ef
|
|
@ -36,6 +36,10 @@
|
||||||
| Added subpath functionality to --mount flag
|
| Added subpath functionality to --mount flag
|
||||||
| https://github.com/knative/client/pull/1655[#1655]
|
| https://github.com/knative/client/pull/1655[#1655]
|
||||||
|
|
||||||
|
| 🐣
|
||||||
|
| Use `T.TempDir` to create temporary test directory
|
||||||
|
| https://github.com/knative/client/pull/1660[#1660]
|
||||||
|
|
||||||
|===
|
|===
|
||||||
## v1.3.0 (2022-03-08)
|
## v1.3.0 (2022-03-08)
|
||||||
[cols="1,10,3", options="header", width="100%"]
|
[cols="1,10,3", options="header", width="100%"]
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -703,8 +702,6 @@ func TestResourceNameCompletionFuncRevision(t *testing.T) {
|
||||||
|
|
||||||
func TestResourceNameCompletionFuncGitOps(t *testing.T) {
|
func TestResourceNameCompletionFuncGitOps(t *testing.T) {
|
||||||
tempDir := setupTempDir(t)
|
tempDir := setupTempDir(t)
|
||||||
assert.Assert(t, tempDir != "")
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
completionFunc := ResourceNameCompletionFunc(knParams)
|
completionFunc := ResourceNameCompletionFunc(knParams)
|
||||||
|
|
||||||
|
|
@ -1594,11 +1591,10 @@ func getResourceCommandWithTestSubcommand(resource string, addNamespace, addSubc
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupTempDir(t *testing.T) string {
|
func setupTempDir(t *testing.T) string {
|
||||||
tempDir, err := ioutil.TempDir("", "test-dir")
|
tempDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
svcPath := path.Join(tempDir, "test-ns", "ksvc")
|
svcPath := path.Join(tempDir, "test-ns", "ksvc")
|
||||||
err = os.MkdirAll(svcPath, 0700)
|
err := os.MkdirAll(svcPath, 0700)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
for i, testSvc := range []servingv1.Service{testSvc1, testSvc2, testSvc3} {
|
for i, testSvc := range []servingv1.Service{testSvc1, testSvc2, testSvc3} {
|
||||||
|
|
|
||||||
|
|
@ -139,13 +139,11 @@ func TestGetNamespaceAllNamespacesNotDefined(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetNamespaceFallback(t *testing.T) {
|
func TestGetNamespaceFallback(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-unit-tests")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
t.Run("MockConfig", func(t *testing.T) {
|
t.Run("MockConfig", func(t *testing.T) {
|
||||||
tempFile := filepath.Join(tempDir, "mock")
|
tempFile := filepath.Join(tempDir, "mock")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
|
err := ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
kp := &KnParams{KubeCfgPath: tempFile}
|
kp := &KnParams{KubeCfgPath: tempFile}
|
||||||
|
|
@ -163,7 +161,7 @@ func TestGetNamespaceFallback(t *testing.T) {
|
||||||
|
|
||||||
t.Run("EmptyConfig", func(t *testing.T) {
|
t.Run("EmptyConfig", func(t *testing.T) {
|
||||||
tempFile := filepath.Join(tempDir, "empty")
|
tempFile := filepath.Join(tempDir, "empty")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
|
err := ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
kp := &KnParams{KubeCfgPath: tempFile}
|
kp := &KnParams{KubeCfgPath: tempFile}
|
||||||
|
|
@ -190,14 +188,12 @@ func TestGetNamespaceFallback(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCurrentNamespace(t *testing.T) {
|
func TestCurrentNamespace(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-unit-tests")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
t.Run("EmptyConfig", func(t *testing.T) {
|
t.Run("EmptyConfig", func(t *testing.T) {
|
||||||
// Invalid kubeconfig
|
// Invalid kubeconfig
|
||||||
tempFile := filepath.Join(tempDir, "empty")
|
tempFile := filepath.Join(tempDir, "empty")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
|
err := ioutil.WriteFile(tempFile, []byte(""), test.FileModeReadWrite)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
kp := &KnParams{KubeCfgPath: tempFile}
|
kp := &KnParams{KubeCfgPath: tempFile}
|
||||||
|
|
@ -234,7 +230,7 @@ func TestCurrentNamespace(t *testing.T) {
|
||||||
t.Run("MockConfig", func(t *testing.T) {
|
t.Run("MockConfig", func(t *testing.T) {
|
||||||
// Fallback to "default" namespace from mock kubeconfig
|
// Fallback to "default" namespace from mock kubeconfig
|
||||||
tempFile := filepath.Join(tempDir, "mock")
|
tempFile := filepath.Join(tempDir, "mock")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
|
err := ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
kp := &KnParams{KubeCfgPath: tempFile}
|
kp := &KnParams{KubeCfgPath: tempFile}
|
||||||
actual, err := kp.CurrentNamespace()
|
actual, err := kp.CurrentNamespace()
|
||||||
|
|
|
||||||
|
|
@ -133,8 +133,7 @@ func TestPluginListExtendingBuiltinCommandGroup(t *testing.T) {
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
func prepareTestSetup(t *testing.T, args ...interface{}) (string, func()) {
|
func prepareTestSetup(t *testing.T, args ...interface{}) (string, func()) {
|
||||||
tmpPathDir, err := ioutil.TempDir("", "plugin_list")
|
tmpPathDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
// Prepare configuration to for our test
|
// Prepare configuration to for our test
|
||||||
oldConfig := config.GlobalConfig
|
oldConfig := config.GlobalConfig
|
||||||
|
|
@ -151,7 +150,6 @@ func prepareTestSetup(t *testing.T, args ...interface{}) (string, func()) {
|
||||||
|
|
||||||
return tmpPathDir, func() {
|
return tmpPathDir, func() {
|
||||||
config.GlobalConfig = oldConfig
|
config.GlobalConfig = oldConfig
|
||||||
os.RemoveAll(tmpPathDir)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -894,9 +894,7 @@ func TestServiceCreateFromFile(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testWithServiceFiles(t *testing.T, testFunction func(t *testing.T, file string)) {
|
func testWithServiceFiles(t *testing.T, testFunction func(t *testing.T, file string)) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
for _, d := range []struct {
|
for _, d := range []struct {
|
||||||
filename string
|
filename string
|
||||||
|
|
@ -910,7 +908,7 @@ func testWithServiceFiles(t *testing.T, testFunction func(t *testing.T, file str
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
tempFile := filepath.Join(tempDir, d.filename)
|
tempFile := filepath.Join(tempDir, d.filename)
|
||||||
err = ioutil.WriteFile(tempFile, []byte(d.content), os.FileMode(0666))
|
err := ioutil.WriteFile(tempFile, []byte(d.content), os.FileMode(0666))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
testFunction(t, tempFile)
|
testFunction(t, tempFile)
|
||||||
}
|
}
|
||||||
|
|
@ -941,14 +939,12 @@ func TestServiceCreateFileError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceCreateInvalidDataJSON(t *testing.T) {
|
func TestServiceCreateInvalidDataJSON(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
tempFile := filepath.Join(tempDir, "invalid.json")
|
tempFile := filepath.Join(tempDir, "invalid.json")
|
||||||
|
|
||||||
// Double curly bracket at the beginning of file
|
// Double curly bracket at the beginning of file
|
||||||
invalidData := strings.Replace(serviceJSON, "{\n", "{{\n", 1)
|
invalidData := strings.Replace(serviceJSON, "{\n", "{{\n", 1)
|
||||||
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
|
err := ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--filename", tempFile}, false)
|
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--filename", tempFile}, false)
|
||||||
assert.Assert(t, util.ContainsAll(err.Error(), "invalid", "character", "'{'", "beginning"))
|
assert.Assert(t, util.ContainsAll(err.Error(), "invalid", "character", "'{'", "beginning"))
|
||||||
|
|
@ -969,14 +965,12 @@ func TestServiceCreateInvalidDataJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceCreateInvalidDataYAML(t *testing.T) {
|
func TestServiceCreateInvalidDataYAML(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
tempFile := filepath.Join(tempDir, "invalid.yaml")
|
tempFile := filepath.Join(tempDir, "invalid.yaml")
|
||||||
|
|
||||||
// Remove dash
|
// Remove dash
|
||||||
invalidData := strings.Replace(serviceYAML, "- image", "image", 1)
|
invalidData := strings.Replace(serviceYAML, "- image", "image", 1)
|
||||||
err = ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
|
err := ioutil.WriteFile(tempFile, []byte(invalidData), os.FileMode(0666))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--filename", tempFile}, false)
|
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--filename", tempFile}, false)
|
||||||
assert.Assert(t, util.ContainsAll(err.Error(), "mapping", "values", "not", "allowed"))
|
assert.Assert(t, util.ContainsAll(err.Error(), "mapping", "values", "not", "allowed"))
|
||||||
|
|
@ -997,12 +991,10 @@ func TestServiceCreateInvalidDataYAML(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
|
func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
tempFile := filepath.Join(tempDir, "service.yaml")
|
tempFile := filepath.Join(tempDir, "service.yaml")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(serviceYAML), os.FileMode(0666))
|
err := ioutil.WriteFile(tempFile, []byte(serviceYAML), os.FileMode(0666))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
// Merge env vars
|
// Merge env vars
|
||||||
expectedEnvVars := map[string]string{
|
expectedEnvVars := map[string]string{
|
||||||
|
|
@ -1077,12 +1069,10 @@ func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceCreateFromYAMLWithOverrideError(t *testing.T) {
|
func TestServiceCreateFromYAMLWithOverrideError(t *testing.T) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
tempFile := filepath.Join(tempDir, "service.yaml")
|
tempFile := filepath.Join(tempDir, "service.yaml")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(serviceYAML), os.FileMode(0666))
|
err := ioutil.WriteFile(tempFile, []byte(serviceYAML), os.FileMode(0666))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
_, _, _, err = fakeServiceCreate([]string{
|
_, _, _, err = fakeServiceCreate([]string{
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,8 @@ func TestServiceImportFilenameError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceImportExistError(t *testing.T) {
|
func TestServiceImportExistError(t *testing.T) {
|
||||||
file, err := generateFile([]byte(exportYAML))
|
file, err := generateFile(t, []byte(exportYAML))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(filepath.Dir(file))
|
|
||||||
|
|
||||||
client := knclient.NewMockKnServiceClient(t)
|
client := knclient.NewMockKnServiceClient(t)
|
||||||
r := client.Recorder()
|
r := client.Recorder()
|
||||||
|
|
@ -60,9 +59,8 @@ func TestServiceImportExistError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceImport(t *testing.T) {
|
func TestServiceImport(t *testing.T) {
|
||||||
file, err := generateFile([]byte(exportYAML))
|
file, err := generateFile(t, []byte(exportYAML))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(filepath.Dir(file))
|
|
||||||
|
|
||||||
client := knclient.NewMockKnServiceClient(t)
|
client := knclient.NewMockKnServiceClient(t)
|
||||||
r := client.Recorder()
|
r := client.Recorder()
|
||||||
|
|
@ -80,9 +78,8 @@ func TestServiceImport(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceImportNoWait(t *testing.T) {
|
func TestServiceImportNoWait(t *testing.T) {
|
||||||
file, err := generateFile([]byte(exportYAML))
|
file, err := generateFile(t, []byte(exportYAML))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(filepath.Dir(file))
|
|
||||||
|
|
||||||
client := knclient.NewMockKnServiceClient(t)
|
client := knclient.NewMockKnServiceClient(t)
|
||||||
r := client.Recorder()
|
r := client.Recorder()
|
||||||
|
|
@ -98,9 +95,8 @@ func TestServiceImportNoWait(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServiceImportWitRevisions(t *testing.T) {
|
func TestServiceImportWitRevisions(t *testing.T) {
|
||||||
file, err := generateFile([]byte(exportWithRevisionsYAML))
|
file, err := generateFile(t, []byte(exportWithRevisionsYAML))
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
defer os.RemoveAll(filepath.Dir(file))
|
|
||||||
|
|
||||||
client := knclient.NewMockKnServiceClient(t)
|
client := knclient.NewMockKnServiceClient(t)
|
||||||
r := client.Recorder()
|
r := client.Recorder()
|
||||||
|
|
@ -120,14 +116,11 @@ func TestServiceImportWitRevisions(t *testing.T) {
|
||||||
r.Validate()
|
r.Validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateFile(fileContent []byte) (string, error) {
|
func generateFile(t *testing.T, fileContent []byte) (string, error) {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
tempFile := filepath.Join(tempDir, "import.yaml")
|
tempFile := filepath.Join(tempDir, "import.yaml")
|
||||||
if err = ioutil.WriteFile(tempFile, fileContent, os.FileMode(0666)); err != nil {
|
if err := ioutil.WriteFile(tempFile, fileContent, os.FileMode(0666)); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return tempFile, nil
|
return tempFile, nil
|
||||||
|
|
|
||||||
|
|
@ -121,11 +121,9 @@ type typeTestCase struct {
|
||||||
func TestGetClientConfig(t *testing.T) {
|
func TestGetClientConfig(t *testing.T) {
|
||||||
multiConfigs := fmt.Sprintf("%s%s%s", "/testing/assets/kube-config-01.yml", string(os.PathListSeparator), "/testing/assets/kube-config-02.yml")
|
multiConfigs := fmt.Sprintf("%s%s%s", "/testing/assets/kube-config-01.yml", string(os.PathListSeparator), "/testing/assets/kube-config-02.yml")
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "kn-unit-tests")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
tempFile := filepath.Join(tempDir, "mock")
|
tempFile := filepath.Join(tempDir, "mock")
|
||||||
err = ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
|
err := ioutil.WriteFile(tempFile, []byte(BASIC_KUBECONFIG), test.FileModeReadWrite)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
for _, tc := range []typeTestCase{
|
for _, tc := range []typeTestCase{
|
||||||
|
|
|
||||||
|
|
@ -109,8 +109,7 @@ sink:
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupConfig(t *testing.T, configContent string) (string, func()) {
|
func setupConfig(t *testing.T, configContent string) (string, func()) {
|
||||||
tmpDir, err := ioutil.TempDir("", "configContent")
|
tmpDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
// Avoid to be fooled by the things in the the real homedir
|
// Avoid to be fooled by the things in the the real homedir
|
||||||
oldHome := os.Getenv("HOME")
|
oldHome := os.Getenv("HOME")
|
||||||
|
|
@ -124,7 +123,7 @@ func setupConfig(t *testing.T, configContent string) (string, func()) {
|
||||||
if configContent != "" {
|
if configContent != "" {
|
||||||
cfgFile = filepath.Join(tmpDir, "config.yaml")
|
cfgFile = filepath.Join(tmpDir, "config.yaml")
|
||||||
os.Args = []string{"kn", "--config", cfgFile}
|
os.Args = []string{"kn", "--config", cfgFile}
|
||||||
err = ioutil.WriteFile(cfgFile, []byte(configContent), 0644)
|
err := ioutil.WriteFile(cfgFile, []byte(configContent), 0644)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,7 +137,6 @@ func setupConfig(t *testing.T, configContent string) (string, func()) {
|
||||||
|
|
||||||
return cfgFile, func() {
|
return cfgFile, func() {
|
||||||
// Cleanup everything
|
// Cleanup everything
|
||||||
os.RemoveAll(tmpDir)
|
|
||||||
os.Setenv("HOME", oldHome)
|
os.Setenv("HOME", oldHome)
|
||||||
os.Args = backupArgs
|
os.Args = backupArgs
|
||||||
bootstrapDefaults = initDefaults()
|
bootstrapDefaults = initDefaults()
|
||||||
|
|
|
||||||
|
|
@ -879,9 +879,7 @@ containers:
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Equal(t, len(fromFile.Containers), 2)
|
assert.Equal(t, len(fromFile.Containers), 2)
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
fileName := filepath.Join(tempDir, "container.yaml")
|
fileName := filepath.Join(tempDir, "container.yaml")
|
||||||
ioutil.WriteFile(fileName, []byte(rawInput), test.FileModeReadWrite)
|
ioutil.WriteFile(fileName, []byte(rawInput), test.FileModeReadWrite)
|
||||||
fromFile, err = decodeContainersFromFile(fileName)
|
fromFile, err = decodeContainersFromFile(fileName)
|
||||||
|
|
|
||||||
|
|
@ -244,8 +244,7 @@ containers:
|
||||||
"Input:file",
|
"Input:file",
|
||||||
rawInput,
|
rawInput,
|
||||||
func(data string) string {
|
func(data string) string {
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
fileName := filepath.Join(tempDir, "container.yaml")
|
fileName := filepath.Join(tempDir, "container.yaml")
|
||||||
ioutil.WriteFile(fileName, []byte(data), test.FileModeReadWrite)
|
ioutil.WriteFile(fileName, []byte(data), test.FileModeReadWrite)
|
||||||
return fileName
|
return fileName
|
||||||
|
|
|
||||||
|
|
@ -357,8 +357,7 @@ func setup(t *testing.T) testContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupWithPathLookup(t *testing.T, lookupInPath bool) testContext {
|
func setupWithPathLookup(t *testing.T, lookupInPath bool) testContext {
|
||||||
tmpPathDir, err := ioutil.TempDir("", "plugin_list")
|
tmpPathDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
return testContext{
|
return testContext{
|
||||||
pluginsDir: tmpPathDir,
|
pluginsDir: tmpPathDir,
|
||||||
pluginManager: NewManager(tmpPathDir, lookupInPath),
|
pluginManager: NewManager(tmpPathDir, lookupInPath),
|
||||||
|
|
@ -388,13 +387,11 @@ func executePlugin(plugin Plugin, args []string) (string, error) {
|
||||||
|
|
||||||
// Prepare a directory and set the path to this directory
|
// Prepare a directory and set the path to this directory
|
||||||
func preparePathDirectory(t *testing.T) (string, func()) {
|
func preparePathDirectory(t *testing.T) (string, func()) {
|
||||||
tmpPathDir, err := ioutil.TempDir("", "plugin_path")
|
tmpPathDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
oldPath := os.Getenv("PATH")
|
oldPath := os.Getenv("PATH")
|
||||||
os.Setenv("PATH", fmt.Sprintf("%s%c%s", tmpPathDir, os.PathListSeparator, "fast-forward-this-year-plz"))
|
os.Setenv("PATH", fmt.Sprintf("%s%c%s", tmpPathDir, os.PathListSeparator, "fast-forward-this-year-plz"))
|
||||||
return tmpPathDir, func() {
|
return tmpPathDir, func() {
|
||||||
os.RemoveAll(tmpPathDir)
|
|
||||||
os.Setenv("PATH", oldPath)
|
os.Setenv("PATH", oldPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,6 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -35,12 +33,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGitOpsOperations(t *testing.T) {
|
func TestGitOpsOperations(t *testing.T) {
|
||||||
c1TempDir, err := ioutil.TempDir("", "kn-files-cluster1")
|
c1TempDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
c2TempDir := t.TempDir()
|
||||||
c2TempDir, err := ioutil.TempDir("", "kn-files-cluster2")
|
|
||||||
assert.NilError(t, err)
|
|
||||||
defer os.RemoveAll(c1TempDir)
|
|
||||||
defer os.RemoveAll(c2TempDir)
|
|
||||||
// create clients
|
// create clients
|
||||||
fooclient := NewKnServingGitOpsClient("foo-ns", c1TempDir)
|
fooclient := NewKnServingGitOpsClient("foo-ns", c1TempDir)
|
||||||
bazclient := NewKnServingGitOpsClient("baz-ns", c1TempDir)
|
bazclient := NewKnServingGitOpsClient("baz-ns", c1TempDir)
|
||||||
|
|
@ -141,9 +136,7 @@ func TestGitOpsOperations(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitOpsSingleFile(t *testing.T) {
|
func TestGitOpsSingleFile(t *testing.T) {
|
||||||
tmpDir, err := ioutil.TempDir("", "singlefile")
|
tmpDir := t.TempDir()
|
||||||
assert.NilError(t, err)
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
// create clients
|
// create clients
|
||||||
fooclient := NewKnServingGitOpsClient("", filepath.Join(tmpDir, "test.yaml"))
|
fooclient := NewKnServingGitOpsClient("", filepath.Join(tmpDir, "test.yaml"))
|
||||||
barclient := NewKnServingGitOpsClient("", filepath.Join(tmpDir, "test.yml"))
|
barclient := NewKnServingGitOpsClient("", filepath.Join(tmpDir, "test.yml"))
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@
|
||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
|
@ -42,12 +40,9 @@ type channelTypeAliasTestConfig struct {
|
||||||
knConfigPath string
|
knConfigPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *channelTypeAliasTestConfig) setup() error {
|
func (tc *channelTypeAliasTestConfig) setup(t *testing.T) error {
|
||||||
var err error
|
var err error
|
||||||
tc.knConfigDir, err = ioutil.TempDir("", "kn-channel-config")
|
tc.knConfigDir = t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
tc.knConfigPath, err = test.CreateFile("config.yaml", knChannelTypesConfigContent, tc.knConfigDir, test.FileModeReadWrite)
|
tc.knConfigPath, err = test.CreateFile("config.yaml", knChannelTypesConfigContent, tc.knConfigDir, test.FileModeReadWrite)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -55,10 +50,6 @@ func (tc *channelTypeAliasTestConfig) setup() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *channelTypeAliasTestConfig) teardown() {
|
|
||||||
os.RemoveAll(tc.knConfigDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannels(t *testing.T) {
|
func TestChannels(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
it, err := test.NewKnTest()
|
it, err := test.NewKnTest()
|
||||||
|
|
@ -71,8 +62,7 @@ func TestChannels(t *testing.T) {
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
tc := channelTypeAliasTestConfig{}
|
tc := channelTypeAliasTestConfig{}
|
||||||
assert.NilError(t, tc.setup())
|
assert.NilError(t, tc.setup(t))
|
||||||
defer tc.teardown()
|
|
||||||
|
|
||||||
t.Log("Create a channel with default messaging layer settings")
|
t.Log("Create a channel with default messaging layer settings")
|
||||||
test.ChannelCreate(r, "c0")
|
test.ChannelCreate(r, "c0")
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -47,9 +45,7 @@ func TestMultiContainers(t *testing.T) {
|
||||||
r := test.NewKnRunResultCollector(t, it)
|
r := test.NewKnRunResultCollector(t, it)
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
t.Log("Creating a multicontainer service from file")
|
t.Log("Creating a multicontainer service from file")
|
||||||
container := addContainer(r, "sidecar", pkgtest.ImagePath("sidecarcontainer"))
|
container := addContainer(r, "sidecar", pkgtest.ImagePath("sidecarcontainer"))
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
@ -57,12 +56,9 @@ type pluginTestConfig struct {
|
||||||
knConfigPath, knPluginPath, knPluginPath2 string
|
knConfigPath, knPluginPath, knPluginPath2 string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pc *pluginTestConfig) setup() error {
|
func (pc *pluginTestConfig) setup(t *testing.T) error {
|
||||||
var err error
|
var err error
|
||||||
pc.knConfigDir, err = ioutil.TempDir("", "kn-config")
|
pc.knConfigDir = t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
pc.knPluginsDir = filepath.Join(pc.knConfigDir, "plugins")
|
pc.knPluginsDir = filepath.Join(pc.knConfigDir, "plugins")
|
||||||
err = os.MkdirAll(pc.knPluginsDir, test.FileModeExecutable)
|
err = os.MkdirAll(pc.knPluginsDir, test.FileModeExecutable)
|
||||||
|
|
@ -106,15 +102,11 @@ func (pc *pluginTestConfig) setup() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pc *pluginTestConfig) teardown() {
|
|
||||||
os.RemoveAll(pc.knConfigDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPluginWithoutLookup(t *testing.T) {
|
func TestPluginWithoutLookup(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
pc, oldPath := setupPluginTestConfigWithNewPath(t)
|
pc, oldPath := setupPluginTestConfigWithNewPath(t)
|
||||||
defer tearDownWithPath(pc, oldPath)
|
defer tearDownWithPath(oldPath)
|
||||||
|
|
||||||
it, err := test.NewKnTest()
|
it, err := test.NewKnTest()
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
@ -133,8 +125,7 @@ func TestPluginWithoutLookup(t *testing.T) {
|
||||||
|
|
||||||
func TestPluginInHelpMessage(t *testing.T) {
|
func TestPluginInHelpMessage(t *testing.T) {
|
||||||
pc := pluginTestConfig{}
|
pc := pluginTestConfig{}
|
||||||
assert.NilError(t, pc.setup())
|
assert.NilError(t, pc.setup(t))
|
||||||
defer pc.teardown()
|
|
||||||
|
|
||||||
result := test.Kn{}.Run("--plugins-dir", pc.knPluginsDir, "--help")
|
result := test.Kn{}.Run("--plugins-dir", pc.knPluginsDir, "--help")
|
||||||
assert.NilError(t, result.Error)
|
assert.NilError(t, result.Error)
|
||||||
|
|
@ -153,8 +144,7 @@ func TestPluginWithLookup(t *testing.T) {
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
pc := pluginTestConfig{}
|
pc := pluginTestConfig{}
|
||||||
assert.NilError(t, pc.setup())
|
assert.NilError(t, pc.setup(t))
|
||||||
defer pc.teardown()
|
|
||||||
|
|
||||||
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir)}
|
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir)}
|
||||||
|
|
||||||
|
|
@ -172,7 +162,7 @@ func TestListPluginInPath(t *testing.T) {
|
||||||
r := test.NewKnRunResultCollector(t, it)
|
r := test.NewKnRunResultCollector(t, it)
|
||||||
|
|
||||||
pc, oldPath := setupPluginTestConfigWithNewPath(t)
|
pc, oldPath := setupPluginTestConfigWithNewPath(t)
|
||||||
defer tearDownWithPath(pc, oldPath)
|
defer tearDownWithPath(oldPath)
|
||||||
|
|
||||||
t.Log("list plugin in $PATH")
|
t.Log("list plugin in $PATH")
|
||||||
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir)}
|
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir)}
|
||||||
|
|
@ -189,7 +179,7 @@ func TestExecutePluginInPath(t *testing.T) {
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
pc, oldPath := setupPluginTestConfigWithNewPath(t)
|
pc, oldPath := setupPluginTestConfigWithNewPath(t)
|
||||||
defer tearDownWithPath(pc, oldPath)
|
defer tearDownWithPath(oldPath)
|
||||||
|
|
||||||
t.Log("execute plugin in $PATH")
|
t.Log("execute plugin in $PATH")
|
||||||
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir)}
|
knFlags := []string{fmt.Sprintf("--plugins-dir=%s", pc.knPluginsDir)}
|
||||||
|
|
@ -204,7 +194,7 @@ func TestExecutePluginInPathWithError(t *testing.T) {
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
pc := pluginTestConfig{}
|
pc := pluginTestConfig{}
|
||||||
assert.NilError(t, pc.setup())
|
assert.NilError(t, pc.setup(t))
|
||||||
oldPath := os.Getenv("PATH")
|
oldPath := os.Getenv("PATH")
|
||||||
|
|
||||||
t.Log("execute plugin in $PATH that returns error")
|
t.Log("execute plugin in $PATH that returns error")
|
||||||
|
|
@ -214,21 +204,20 @@ func TestExecutePluginInPathWithError(t *testing.T) {
|
||||||
_, err = test.CreateFile(pluginBin3, pluginBinErr, pluginsDir, test.FileModeExecutable)
|
_, err = test.CreateFile(pluginBin3, pluginBinErr, pluginsDir, test.FileModeExecutable)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.NilError(t, os.Setenv("PATH", fmt.Sprintf("%s%s%s", oldPath, delim, pluginsDir)))
|
assert.NilError(t, os.Setenv("PATH", fmt.Sprintf("%s%s%s", oldPath, delim, pluginsDir)))
|
||||||
defer tearDownWithPath(pc, oldPath)
|
defer tearDownWithPath(oldPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
func setupPluginTestConfigWithNewPath(t *testing.T) (pluginTestConfig, string) {
|
func setupPluginTestConfigWithNewPath(t *testing.T) (pluginTestConfig, string) {
|
||||||
pc := pluginTestConfig{}
|
pc := pluginTestConfig{}
|
||||||
assert.NilError(t, pc.setup())
|
assert.NilError(t, pc.setup(t))
|
||||||
oldPath := os.Getenv("PATH")
|
oldPath := os.Getenv("PATH")
|
||||||
assert.NilError(t, os.Setenv("PATH", fmt.Sprintf("%s%s%s", oldPath, delim, pc.knPluginsDir2)))
|
assert.NilError(t, os.Setenv("PATH", fmt.Sprintf("%s%s%s", oldPath, delim, pc.knPluginsDir2)))
|
||||||
return pc, oldPath
|
return pc, oldPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func tearDownWithPath(pc pluginTestConfig, oldPath string) {
|
func tearDownWithPath(oldPath string) {
|
||||||
os.Setenv("PATH", oldPath)
|
os.Setenv("PATH", oldPath)
|
||||||
pc.teardown()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listPlugin(r *test.KnRunResultCollector, knFlags []string, expectedPlugins []string, unexpectedPlugins []string) {
|
func listPlugin(r *test.KnRunResultCollector, knFlags []string, expectedPlugins []string, unexpectedPlugins []string) {
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,6 @@ package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -86,9 +84,7 @@ func TestServiceCreateFromFile(t *testing.T) {
|
||||||
r := test.NewKnRunResultCollector(t, it)
|
r := test.NewKnRunResultCollector(t, it)
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
test.CreateFile("foo.json", fmt.Sprintf(ServiceJSON, pkgtest.ImagePath("helloworld")), tempDir, test.FileModeReadWrite)
|
test.CreateFile("foo.json", fmt.Sprintf(ServiceJSON, pkgtest.ImagePath("helloworld")), tempDir, test.FileModeReadWrite)
|
||||||
test.CreateFile("foo.yaml", fmt.Sprintf(ServiceYAML, pkgtest.ImagePath("helloworld")), tempDir, test.FileModeReadWrite)
|
test.CreateFile("foo.yaml", fmt.Sprintf(ServiceYAML, pkgtest.ImagePath("helloworld")), tempDir, test.FileModeReadWrite)
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -39,9 +38,7 @@ func TestServiceImport(t *testing.T) {
|
||||||
r := test.NewKnRunResultCollector(t, it)
|
r := test.NewKnRunResultCollector(t, it)
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "kn-file")
|
tempDir := t.TempDir()
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
t.Log("import service foo with revision")
|
t.Log("import service foo with revision")
|
||||||
testFile := filepath.Join(tempDir, "foo-with-revisions")
|
testFile := filepath.Join(tempDir, "foo-with-revisions")
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@
|
||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
|
@ -41,12 +39,9 @@ type sinkprefixTestConfig struct {
|
||||||
knConfigPath string
|
knConfigPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *sinkprefixTestConfig) setup() error {
|
func (tc *sinkprefixTestConfig) setup(t *testing.T) error {
|
||||||
var err error
|
var err error
|
||||||
tc.knConfigDir, err = ioutil.TempDir("", "kn1-config")
|
tc.knConfigDir = t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
tc.knConfigPath, err = test.CreateFile("config.yaml", KnConfigContent, tc.knConfigDir, test.FileModeReadWrite)
|
tc.knConfigPath, err = test.CreateFile("config.yaml", KnConfigContent, tc.knConfigDir, test.FileModeReadWrite)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -54,10 +49,6 @@ func (tc *sinkprefixTestConfig) setup() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *sinkprefixTestConfig) teardown() {
|
|
||||||
os.RemoveAll(tc.knConfigDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSinkPrefixConfig(t *testing.T) {
|
func TestSinkPrefixConfig(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
it, err := test.NewKnTest()
|
it, err := test.NewKnTest()
|
||||||
|
|
@ -70,8 +61,7 @@ func TestSinkPrefixConfig(t *testing.T) {
|
||||||
defer r.DumpIfFailed()
|
defer r.DumpIfFailed()
|
||||||
|
|
||||||
tc := sinkprefixTestConfig{}
|
tc := sinkprefixTestConfig{}
|
||||||
assert.NilError(t, tc.setup())
|
assert.NilError(t, tc.setup(t))
|
||||||
defer tc.teardown()
|
|
||||||
|
|
||||||
t.Log("Creating a testservice")
|
t.Log("Creating a testservice")
|
||||||
test.ServiceCreate(r, "testsvc0")
|
test.ServiceCreate(r, "testsvc0")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue