test
This commit is contained in:
24
src/kubectl/main_test.go
Normal file
24
src/kubectl/main_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package kubectl
|
||||
|
||||
import (
|
||||
"os"
|
||||
"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)
|
||||
}
|
||||
@@ -6,25 +6,25 @@ import (
|
||||
)
|
||||
|
||||
type toolClientVersion struct {
|
||||
GitVersion string `json:"gitVersion"`
|
||||
GitVersion string `json:"gitVersion"`
|
||||
}
|
||||
|
||||
type toolVersion struct {
|
||||
ClientVersion toolClientVersion `json:"clientVersion"`
|
||||
ClientVersion toolClientVersion `json:"clientVersion"`
|
||||
}
|
||||
|
||||
func Version(path string) (string, error) {
|
||||
|
||||
cmd := exec.Command(path, "version", "-o", "json", "--client=true")
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
cmd := exec.Command(path, "version", "-o", "json", "--client=true")
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var objmap toolVersion
|
||||
var objmap toolVersion
|
||||
|
||||
json.Unmarshal(stdout, &objmap)
|
||||
json.Unmarshal(stdout, &objmap)
|
||||
res := objmap.ClientVersion.GitVersion
|
||||
|
||||
return res, nil
|
||||
|
||||
22
src/kubectl/version_test.go
Normal file
22
src/kubectl/version_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package kubectl
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestKubectlVersion(t *testing.T){
|
||||
|
||||
bin := filepath.Join(TEST_BIN_DIR, "kubectl")
|
||||
os.Chmod(bin, 0700)
|
||||
assert.FileExists(t, bin, "TestKubectlVersion error")
|
||||
|
||||
version, err := Version(bin)
|
||||
|
||||
assert.Nilf(t, err, "error message %s", bin)
|
||||
assert.Equal(t, "v1.30.3", version, "TestKubectlVersion error")
|
||||
}
|
||||
Reference in New Issue
Block a user