85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
package kubectl
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"path/filepath"
|
|
)
|
|
|
|
var TEST_DEST_DIR = "../wrk_kubectl"
|
|
var TEST_SRC_DIR = filepath.Join("../../test", "kubectl")
|
|
var TEST_BIN_DIR = filepath.Join("../../test", "bin")
|
|
|
|
func TestMain(m *testing.M) {
|
|
folderPath := TEST_DEST_DIR
|
|
|
|
os.RemoveAll(folderPath)
|
|
os.MkdirAll(folderPath, os.ModePerm)
|
|
|
|
// call flag.Parse() here if TestMain uses flags
|
|
exitCode := m.Run()
|
|
|
|
os.RemoveAll(folderPath)
|
|
os.Exit(exitCode)
|
|
}
|
|
|
|
// Mock
|
|
|
|
type MockCommandExecutor struct {
|
|
// Used to stub the return of the Output method
|
|
// Could add other properties depending on testing needs
|
|
output string
|
|
}
|
|
|
|
// Implements the commandExecutor interface
|
|
func (m *MockCommandExecutor) Output() ([]byte, error) {
|
|
return []byte(m.output), nil
|
|
}
|
|
|
|
func (m *MockCommandExecutor) CombinedOutput() ([]byte, error) {
|
|
return []byte(m.output), nil
|
|
}
|
|
|
|
//
|
|
|
|
func getCmdKubectl(mock bool, output string) (KubectlCommand) {
|
|
if mock == true {
|
|
|
|
mock := func(name string, args ...string) commandExecutor {
|
|
return &MockCommandExecutor{output: output}
|
|
}
|
|
|
|
cmd := KubectlCommand{Bin: "mock", Exec: mock}
|
|
return cmd
|
|
} else {
|
|
bin := filepath.Join(TEST_BIN_DIR, "kubectl")
|
|
os.Chmod(bin, 0700)
|
|
|
|
cmd := KubectlCommand{Bin: bin}
|
|
cmd.New()
|
|
return cmd
|
|
}
|
|
}
|
|
|
|
func getCmdsKubectl(mock bool, outputs map[string]string) (KubectlCommand) {
|
|
if mock == true {
|
|
|
|
mock := func(name string, args ...string) commandExecutor {
|
|
cmd := strings.TrimSuffix(strings.Join(args," "), " ")
|
|
output := outputs[cmd]
|
|
return &MockCommandExecutor{output: output}
|
|
}
|
|
|
|
cmd := KubectlCommand{Bin: "mock", Exec: mock}
|
|
return cmd
|
|
} else {
|
|
bin := filepath.Join(TEST_BIN_DIR, "Kubectl")
|
|
os.Chmod(bin, 0700)
|
|
|
|
cmd := KubectlCommand{Bin: bin}
|
|
cmd.New()
|
|
return cmd
|
|
}
|
|
}
|