Skip to content

Commit

Permalink
test(cmd): fix tests.
Browse files Browse the repository at this point in the history
fix tests for timeouts command
  • Loading branch information
notmaxx committed Jun 19, 2018
1 parent 9e7e80f commit f948c83
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type Commander interface {
LimitsList(string) error
LimitsSet(string, []string, string) error
LimitsUnset(string, []string, string) error
TimeoutsList(string) error
TimeoutsSet(string, []string) error
TimeoutsUnset(string, []string) error
MaintenanceInfo(string) error
MaintenanceEnable(string) error
MaintenanceDisable(string) error
Expand Down
10 changes: 5 additions & 5 deletions cmd/timeouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/notmaxx/controller-sdk-go/config"
)

// LimitsList lists an app's timeouts.
// TimeoutsList lists an app's timeouts.
func (d *DeisCmd) TimeoutsList(appID string) error {
s, appID, err := load(d.ConfigFile, appID)

Expand All @@ -23,7 +23,7 @@ func (d *DeisCmd) TimeoutsList(appID string) error {
return err
}

d.Printf("=== %s Timeouts (sec)\n\n", appID)
d.Printf("=== %s Timeouts (sec)\n", appID)

if len(config.Timeout) == 0 {
d.Println("default (30 sec) or controlled by env KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS")
Expand All @@ -40,7 +40,7 @@ func (d *DeisCmd) TimeoutsList(appID string) error {
return nil
}

// LimitsSet sets an app's timeouts.
// TimeoutsSet sets an app's timeouts.
func (d *DeisCmd) TimeoutsSet(appID string, timeouts []string) error {
s, appID, err := load(d.ConfigFile, appID)

Expand Down Expand Up @@ -72,7 +72,7 @@ func (d *DeisCmd) TimeoutsSet(appID string, timeouts []string) error {
return d.TimeoutsList(appID)
}

// LimitsUnset removes an app's timeouts.
// TimeoutsUnset removes an app's timeouts.
func (d *DeisCmd) TimeoutsUnset(appID string, timeouts []string) error {
s, appID, err := load(d.ConfigFile, appID)

Expand Down Expand Up @@ -123,7 +123,7 @@ func parseTimeouts(timeouts []string) (map[string]interface{}, error) {
}

func parseTimeout(timeout string) (string, string, error) {
regex := regexp.MustCompile("^[0-9]*$")
regex := regexp.MustCompile("^([a-z0-9]+(?:-[a-z0-9]+)*)=([0-9]+)$")

if !regex.MatchString(timeout) {
return "", "", fmt.Errorf(`%s doesn't fit format type=#
Expand Down
60 changes: 28 additions & 32 deletions cmd/timeouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Examples: web=30 worker=300`
{"=1", "", "", true, "=1" + errorHint},
{"web=", "", "", true, "web=" + errorHint},
{"1=", "", "", true, "1=" + errorHint},
{"web=ABCD", "", "", true, "web=G" + errorHint},
{"web=ABCD", "", "", true, "web=ABCD" + errorHint},
}

for _, check := range cases {
Expand All @@ -57,7 +57,7 @@ func TestTimeoutTags(t *testing.T) {

cases := []parseTimeoutsCase{
{[]string{"web=10", "worker=20"}, map[string]interface{}{"web": "10", "worker": "20"}, false, ""},
{[]string{"foo=", "web=1G"}, nil, true, `foo= doesn't fit format type=#
{[]string{"foo=", "web=10"}, nil, true, `foo= doesn't fit format type=#
Examples: web=30 worker=300`},
}

Expand Down Expand Up @@ -105,10 +105,9 @@ func TestTimeoutsList(t *testing.T) {

err = cmdr.TimeoutsList("enterprise")
assert.NoErr(t, err)
assert.Equal(t, b.String(), `=== enterprise Timeouts
web 10
worker 20
assert.Equal(t, b.String(), `=== enterprise Timeouts (sec)
web 10
worker 20
`, "output")

server.Mux.HandleFunc("/v2/apps/franklin/config/", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -130,9 +129,8 @@ worker 20

err = cmdr.TimeoutsList("franklin")
assert.NoErr(t, err)
assert.Equal(t, b.String(), `=== franklin Timeouts
assert.Equal(t, b.String(), `=== franklin Timeouts (sec)
default (30 sec) or controlled by env KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS
`, "output")
}

Expand All @@ -148,7 +146,7 @@ func TestTimeoutsSet(t *testing.T) {
testutil.SetHeaders(w)
if r.Method == "POST" {
testutil.AssertBody(t, api.Config{
CPU: map[string]interface{}{
Timeout: map[string]interface{}{
"web": "10",
},
}, r)
Expand All @@ -161,7 +159,7 @@ func TestTimeoutsSet(t *testing.T) {
"memory": {},
"cpu": {},
"termination_grace_period": {
"web": 10
"web": "10"
},
"tags": {},
"registry": {},
Expand All @@ -179,16 +177,16 @@ func TestTimeoutsSet(t *testing.T) {

assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
=== foo Timeouts
=== foo Timeouts (sec)
web 10
`, "output")

server.Mux.HandleFunc("/v2/apps/franklin/config/", func(w http.ResponseWriter, r *http.Request) {
testutil.SetHeaders(w)
if r.Method == "POST" {
testutil.AssertBody(t, api.Config{
Memory: map[string]interface{}{
"web": 10,
Timeout: map[string]interface{}{
"web": "10",
},
}, r)
}
Expand All @@ -200,8 +198,8 @@ func TestTimeoutsSet(t *testing.T) {
"memory": {},
"cpu": {},
"termination_grace_period": {
"web": 10
}
"web": "10"
},
"tags": {},
"registry": {},
"created": "2014-01-01T00:00:00UTC",
Expand All @@ -216,20 +214,19 @@ func TestTimeoutsSet(t *testing.T) {

assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
=== franklin Timeouts
=== franklin Timeouts (sec)
web 10
`, "output")

// with requests/timeout parameter
server.Mux.HandleFunc("/v2/apps/jim/config/", func(w http.ResponseWriter, r *http.Request) {
testutil.SetHeaders(w)
if r.Method == "POST" {
testutil.AssertBody(t, api.Config{
Memory: map[string]interface{}{
"web": 10,
"worker": 100,
"db": 300,
Timeout: map[string]interface{}{
"web": "10",
"worker": "100",
"db": "300",
},
}, r)
}
Expand All @@ -238,12 +235,12 @@ web 10
"owner": "foo",
"app": "jim",
"values": {},
"memory": {}
"memory": {},
"cpu": {},
"termination_grace_period": {
"web": 10,
"worker": 100,
"db": 300
"web": "10",
"worker": "100",
"db": "300"
},
"tags": {},
"registry": {},
Expand All @@ -259,11 +256,10 @@ web 10

assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
=== jim Timeouts
=== jim Timeouts (sec)
db 300
web 10
worker 100
`, "output")

}
Expand All @@ -280,7 +276,7 @@ func TestTimeoutsUnset(t *testing.T) {
testutil.SetHeaders(w)
if r.Method == "POST" {
testutil.AssertBody(t, api.Config{
Memory: map[string]interface{}{
Timeout: map[string]interface{}{
"web": nil,
},
}, r)
Expand Down Expand Up @@ -311,15 +307,15 @@ func TestTimeoutsUnset(t *testing.T) {

assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
=== foo Timeouts
=== foo Timeouts (sec)
web 10
`, "output")

server.Mux.HandleFunc("/v2/apps/franklin/config/", func(w http.ResponseWriter, r *http.Request) {
testutil.SetHeaders(w)
if r.Method == "POST" {
testutil.AssertBody(t, api.Config{
CPU: map[string]interface{}{
Timeout: map[string]interface{}{
"web": nil,
},
}, r)
Expand Down Expand Up @@ -348,7 +344,7 @@ web 10

assert.Equal(t, testutil.StripProgress(b.String()), `Applying timeouts... done
=== franklin Timeouts
=== franklin Timeouts (sec)
web 10
`, "output")
}
4 changes: 2 additions & 2 deletions parser/timeouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func (d FakeDeisCmd) TimeoutsList(string) error {
return errors.New("timeouts:list")
}

func (d FakeDeisCmd) TimeoutsSet(string, []string, string) error {
func (d FakeDeisCmd) TimeoutsSet(string, []string) error {
return errors.New("timeouts:set")
}

func (d FakeDeisCmd) TimeoutsUnset(string, []string, string) error {
func (d FakeDeisCmd) TimeoutsUnset(string, []string) error {
return errors.New("timeouts:unset")
}

Expand Down

0 comments on commit f948c83

Please sign in to comment.