|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/arschles/assert" |
| 10 | + "github.com/deis/controller-sdk-go/api" |
| 11 | + "github.com/deis/workflow-cli/pkg/testutil" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAutoscaleList(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + cf, server, err := testutil.NewTestServerAndClient() |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + defer server.Close() |
| 21 | + var b bytes.Buffer |
| 22 | + cmdr := DeisCmd{WOut: &b, ConfigFile: cf} |
| 23 | + |
| 24 | + server.Mux.HandleFunc("/v2/apps/rivendell/settings/", func(w http.ResponseWriter, r *http.Request) { |
| 25 | + testutil.SetHeaders(w) |
| 26 | + fmt.Fprintf(w, `{ |
| 27 | + "owner": "elrond", |
| 28 | + "app": "rivendell", |
| 29 | + "autoscale": {"cmd": {"min": 3, "max": 8, "cpu_percent": 40}}, |
| 30 | + "created": "2014-01-01T00:00:00UTC", |
| 31 | + "updated": "2014-01-01T00:00:00UTC", |
| 32 | + "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" |
| 33 | + }`) |
| 34 | + }) |
| 35 | + |
| 36 | + err = cmdr.AutoscaleList("rivendell") |
| 37 | + assert.NoErr(t, err) |
| 38 | + assert.Equal(t, b.String(), "=== rivendell Autoscale\n\n--- cmd:\nMin Replicas: 3\nMax Replicas: 8\nCPU: 40%\n", "output") |
| 39 | + |
| 40 | + server.Mux.HandleFunc("/v2/apps/mordor/settings/", func(w http.ResponseWriter, r *http.Request) { |
| 41 | + testutil.SetHeaders(w) |
| 42 | + fmt.Fprintf(w, `{ |
| 43 | + "owner": "sauron", |
| 44 | + "app": "mordor", |
| 45 | + "created": "2014-01-01T00:00:00UTC", |
| 46 | + "updated": "2014-01-01T00:00:00UTC", |
| 47 | + "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75" |
| 48 | + }`) |
| 49 | + }) |
| 50 | + b.Reset() |
| 51 | + |
| 52 | + err = cmdr.AutoscaleList("mordor") |
| 53 | + assert.NoErr(t, err) |
| 54 | + assert.Equal(t, b.String(), "=== mordor Autoscale\n\nNo autoscale rules found.\n", "output") |
| 55 | +} |
| 56 | + |
| 57 | +func TestAutoscaleSet(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + cf, server, err := testutil.NewTestServerAndClient() |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + defer server.Close() |
| 64 | + var b bytes.Buffer |
| 65 | + cmdr := DeisCmd{WOut: &b, ConfigFile: cf} |
| 66 | + |
| 67 | + server.Mux.HandleFunc("/v2/apps/lothlorien/settings/", func(w http.ResponseWriter, r *http.Request) { |
| 68 | + testutil.SetHeaders(w) |
| 69 | + data := map[string]*api.Autoscale{ |
| 70 | + "cmd": { |
| 71 | + Min: 3, |
| 72 | + Max: 8, |
| 73 | + CPUPercent: 40, |
| 74 | + }, |
| 75 | + } |
| 76 | + testutil.AssertBody(t, api.AppSettings{Autoscale: data}, r) |
| 77 | + fmt.Fprintf(w, `{}`) |
| 78 | + }) |
| 79 | + |
| 80 | + err = cmdr.AutoscaleSet("lothlorien", "cmd", 3, 8, 40) |
| 81 | + assert.NoErr(t, err) |
| 82 | + assert.Equal(t, testutil.StripProgress(b.String()), "Applying autoscale settings for process type cmd on lothlorien... done\n", "output") |
| 83 | +} |
| 84 | + |
| 85 | +func TestAutoscaleUnset(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + cf, server, err := testutil.NewTestServerAndClient() |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + defer server.Close() |
| 92 | + var b bytes.Buffer |
| 93 | + cmdr := DeisCmd{WOut: &b, ConfigFile: cf} |
| 94 | + |
| 95 | + server.Mux.HandleFunc("/v2/apps/bree/settings/", func(w http.ResponseWriter, r *http.Request) { |
| 96 | + testutil.SetHeaders(w) |
| 97 | + testutil.AssertBody(t, api.AppSettings{Autoscale: map[string]*api.Autoscale{"cmd": nil}}, r) |
| 98 | + fmt.Fprintf(w, `{"autoscale":{"cmd":null}}`) |
| 99 | + }) |
| 100 | + |
| 101 | + err = cmdr.AutoscaleUnset("bree", "cmd") |
| 102 | + assert.NoErr(t, err) |
| 103 | + assert.Equal(t, testutil.StripProgress(b.String()), "Removing autoscale for process type cmd on bree... done\n", "output") |
| 104 | +} |
0 commit comments