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

Templating for generating random Ports #441

Merged
merged 1 commit into from
Nov 9, 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
16 changes: 14 additions & 2 deletions cmd/amp/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"regexp"
"strings"
"strconv"
"testing"
"text/template"
"time"
Expand Down Expand Up @@ -119,13 +120,15 @@ func runTestSpec(t *testing.T, test *TestSpec) (err error) {
startTime := time.Now().UnixNano() / 1000000
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can simplify your time code. Just use time.now() and later check elapsed (eg, elapsed := time.Since(startTime)). I believe the units you're using for the timeout are seconds (needs a comment!), so multiply the timeout by time.Second so you can compare against elapsed.

Copy link
Contributor

Choose a reason for hiding this comment

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

See Go Playground for a simple example.

Copy link
Contributor

Choose a reason for hiding this comment

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

timeout and delay are defined in milliseconds.


for i = -1; i < cmdSpec.Retry; i++ {
err = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be a good spot for a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Neha is putting up comments in her next PR.


cmdString := generateCmdString(&cmdSpec)
tmplOutput, tmplErr := performTemplating(strings.Join(cmdString, " "), cache)
if tmplErr != nil {
err = fmt.Errorf("Executing templating failed: %s", tmplErr)
Copy link
Contributor

@subfuzion subfuzion Nov 9, 2016

Choose a reason for hiding this comment

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

If there is an error with templating, we don't want to keep retrying (which makes setting err to nil unnecessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can make the templating error return the same as the timeout err statement, however, the reason the err = nil is there is for the regex err. If there is a miss matched output and the command is allowed to retry, it should retry that number of times.

t.Log(err)
}
tmplString := strings.Fields(tmplOutput)
tmplString = strings.Fields(tmplOutput)

t.Logf("Running: %s", strings.Join(tmplString, " "))
cmdOutput, cmdErr := exec.Command(tmplString[0], tmplString[1:]...).CombinedOutput()
Expand All @@ -148,7 +151,7 @@ func runTestSpec(t *testing.T, test *TestSpec) (err error) {
t.Log("This command :", tmplString, "has re-run", i, "times.")
}
}
return
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding err to return statement is not necessary (see here).

}

func generateCmdString(cmdSpec *CommandSpec) (cmdString []string) {
Expand Down Expand Up @@ -208,9 +211,18 @@ func performTemplating(s string, cache map[string]string) (output string, err er
cache[in] = out
return out
}
p := func(in string, min, max int) string {
if val, ok := cache[in]; ok {
return val
}
out := strconv.Itoa(rand.Intn(max - min) + min)
cache[in] = out
return out
}
var doc bytes.Buffer
var fm = template.FuncMap{
"uniq": func(in string) string { return f(in) },
"port": func(in string, min, max int) string { return p(in, min, max) },
}
err = t.Execute(&doc, fm)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/amp/cli/lookup/regex-lookup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ logs-metadata: (timestamp:"[A-Z0-9\.\-:\"]+\s+time_id:"[A-Z0-9\.\-:\"]+\s+servic
logs-numbered: ([a-zA-Z0-9\.\-:\"\s\_\t\n\$&%?/\[\]\%\>\<\,\;]*){10}
docker-service-list: ID\s+NAME\s+REPLICAS\s+IMAGE\s+COMMAND\s*\n([a-zA-Z0-9\s\.\-:/]*){1,}
service-id: ([a-z0-9]){25}
service-curl: ((.)|(\s))*(pong)((.)|(\s))*
stack-list: NAME\s+ID\s+STATE\s*-+\s*\n+([a-z0-9A-Z\s]*){1,}
stack-id: ([a-z0-9]){64}
stack-unavailable: No stack is available
Expand Down
18 changes: 10 additions & 8 deletions cmd/amp/cli/test_samples/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- appcelerator/pinger
options:
- "--name {{call .uniq `pinger`}}"
- "-p www:90:3000"
- "-p www:{{call .port `pinger1` 1100 1200}}:3000"
expectation: service-id

- service-list:
Expand All @@ -13,13 +13,15 @@
options:
expectation: docker-service-list

# Commented out until Delay and APIcall (Blocking) implemented.
# - service-curl:
# cmd: curl
# args:
# - localhost:90/ping
# options:
# expectation: ((.)|(\s))*(pong)((.)|(\s))*
# Should use API call as a form of blocking.
- service-curl:
cmd: curl
args:
- localhost:{{call .port `pinger1` 1100 1200}}/ping
options:
expectation: service-curl
retry: 3
delay: 2500

- service-remove:
cmd: amp service rm
Expand Down