Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial CLI command tests #305

Merged
merged 1 commit into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions cmd/amp/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cli_test

import (
"github.com/appcelerator/amp/api/server"
"gopkg.in/yaml.v2"
"io/ioutil"
"os/exec"
"path"
"strings"
"testing"
"time"
)

type TestSpec struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't export test types in the cli package. Better yet, rename this package to cli_test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test types removed.

fileName string
contents []byte
}

type commandSpec struct {
Cmd string `yaml:"cmd"`
Args []string `yaml:"args"`
Options []string `yaml:"options"`
}

var (
testDir = "./test_samples"
)

func TestMain(t *testing.T) {
_, conn := server.StartTestServer()
t.Log(conn)
}

func TestCmds(t *testing.T) {
tests := loadFiles(t)
for _, test := range tests {
parse(t, test)
}
}

func loadFiles(t *testing.T) []*TestSpec {
tests := []*TestSpec{}
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Error(err)
return nil
}
for _, f := range files {
name := f.Name()
t.Log("Loading file:", name)
contents, err := ioutil.ReadFile(path.Join(testDir, name))
if err != nil {
t.Errorf("unable to load test sample: %s. Error: %v", name, err)
}
testSpec := &TestSpec{
fileName: name,
contents: contents,
}
tests = append(tests, testSpec)
}
return tests
}

func parse(t *testing.T, test *TestSpec) {
commandMap, err := parseCommandMap(test.contents)
if err != nil {
t.Error(err)
return
}
for _, spec := range commandMap {
cmdSplit := strings.Fields(spec.Cmd)
optionsSplit := []string{}
for _, val := range spec.Options {
optionsSplit = append(optionsSplit, strings.Fields(val)...)
}
commandFinal := append(cmdSplit, spec.Args...)
commandFinal = append(commandFinal, optionsSplit...)
t.Log(commandFinal, "Command passed.")
runCmd(t, commandFinal)
}
}

func runCmd(t *testing.T, cmdString []string) {
t.Log(cmdString, "Running...")
for i := 0; i < 10; i++ {
t.Log(cmdString, " Iteration:", i+1)
cmd := exec.Command(cmdString[0], cmdString[1:]...)
result, err := cmd.CombinedOutput()
if err != nil && i >= 9 {
t.Log(cmdString, " Error:", err)
t.Log(cmdString, " Command has failed, exiting.")
t.Fail()
} else if err != nil {
t.Log(cmdString, " Error:", err)
t.Log(cmdString, " Command failed, retrying.")
time.Sleep(1 * time.Second)
} else {
t.Log(cmdString, " Command result:\n", string(result))
break
}
}
}

func parseCommandMap(b []byte) (out map[string]commandSpec, err error) {
err = yaml.Unmarshal(b, &out)
return
}
7 changes: 7 additions & 0 deletions cmd/amp/cli/test_samples/01-create-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create-service:
cmd: amp service create
args:
- appcelerator/pinger
options:
- --name pinger
- -p www:90:3000
4 changes: 4 additions & 0 deletions cmd/amp/cli/test_samples/02-list-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
list-service:
cmd: docker service ls
args:
options:
5 changes: 5 additions & 0 deletions cmd/amp/cli/test_samples/03-curl-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl-service:
cmd: curl
args:
- localhost:90/ping
options:
9 changes: 9 additions & 0 deletions cmd/amp/cli/test_samples/04-remove-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
remove-service:
cmd: amp service rm
args:
- pinger
options:
list-service:
cmd: docker service ls
args:
options: