From c1fad03035f4b8604bf54a9a1dddc0a22386b99a Mon Sep 17 00:00:00 2001 From: David Dollar Date: Tue, 5 Nov 2019 14:33:10 -0500 Subject: [PATCH] update tests --- .github/workflows/{push.yml => release.yml} | 2 +- .github/workflows/test.yml | 13 ++ .gitignore | 1 + pkg/api/api_test.go | 3 +- pkg/api/auth_test.go | 3 +- pkg/cli/rack_test.go | 114 ------------- pkg/cli/start.go | 60 +++---- pkg/cli/start_test.go | 180 ++++++-------------- pkg/start/gen2_test.go | 4 +- provider/k8s/app_test.go | 9 +- provider/k8s/testdata/app-locked.yml | 1 - provider/k8s/testdata/app-params.yml | 1 - provider/k8s/testdata/app.yml | 1 - 13 files changed, 102 insertions(+), 290 deletions(-) rename .github/workflows/{push.yml => release.yml} (97%) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/push.yml b/.github/workflows/release.yml similarity index 97% rename from .github/workflows/push.yml rename to .github/workflows/release.yml index 32b3744..0d582e9 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: push +name: release on: push: branches: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..49516b6 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,13 @@ +name: release +on: + push: + branches: ["*"] +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v1 + - name: test + run: make test + diff --git a/.gitignore b/.gitignore index 3fa8c86..598837a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .terraform +coverage.txt diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index d8b16de..6052e41 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -6,9 +6,9 @@ import ( "net/http/httptest" "testing" - "github.com/convox/logger" "github.com/convox/convox/pkg/api" "github.com/convox/convox/pkg/structs" + "github.com/convox/logger" "github.com/convox/stdsdk" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -17,6 +17,7 @@ import ( func testServer(t *testing.T, fn func(*stdsdk.Client, *structs.MockProvider)) { p := &structs.MockProvider{} p.On("Initialize", mock.Anything).Return(nil) + p.On("Start").Return(nil) p.On("WithContext", mock.Anything).Return(p).Maybe() s := api.NewWithProvider(p) diff --git a/pkg/api/auth_test.go b/pkg/api/auth_test.go index 616288f..080f5be 100644 --- a/pkg/api/auth_test.go +++ b/pkg/api/auth_test.go @@ -5,10 +5,10 @@ import ( "net/url" "testing" - "github.com/convox/logger" "github.com/convox/convox/pkg/api" "github.com/convox/convox/pkg/structs" "github.com/convox/convox/sdk" + "github.com/convox/logger" "github.com/convox/stdsdk" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -17,6 +17,7 @@ import ( func TestAuthentication(t *testing.T) { p := &structs.MockProvider{} p.On("Initialize", mock.Anything).Return(nil) + p.On("Start").Return(nil) s := api.NewWithProvider(p) s.Logger = logger.Discard diff --git a/pkg/cli/rack_test.go b/pkg/cli/rack_test.go index 5310e1e..7cb1f33 100644 --- a/pkg/cli/rack_test.go +++ b/pkg/cli/rack_test.go @@ -2,12 +2,6 @@ package cli_test import ( "fmt" - "io" - "io/ioutil" - "net/http" - "net/http/httptest" - "net/url" - "path/filepath" "testing" "time" @@ -15,8 +9,6 @@ import ( mocksdk "github.com/convox/convox/pkg/mock/sdk" "github.com/convox/convox/pkg/options" "github.com/convox/convox/pkg/structs" - "github.com/convox/convox/provider" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -71,65 +63,6 @@ func TestRackInternal(t *testing.T) { }) } -func TestRackInstall(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - require.Equal(t, "/auth", r.URL.Path) - user, pass, _ := r.BasicAuth() - require.Equal(t, "convox", user) - require.Equal(t, "password", pass) - })) - - tsu, err := url.Parse(ts.URL) - require.NoError(t, err) - - opts := structs.SystemInstallOptions{ - Name: options.String("foo"), - Parameters: map[string]string{}, - Version: options.String("bar"), - } - provider.Mock.On("SystemInstall", mock.Anything, opts).Once().Return(fmt.Sprintf("https://convox:password@%s", tsu.Host), nil).Run(func(args mock.Arguments) { - w := args.Get(0).(io.Writer) - fmt.Fprintf(w, "line1\n") - fmt.Fprintf(w, "line2\n") - }) - - res, err := testExecute(e, "rack install test -n foo -v bar", nil) - require.NoError(t, err) - require.Equal(t, 0, res.Code) - res.RequireStderr(t, []string{""}) - res.RequireStdout(t, []string{ - "line1", - "line2", - }) - - data, err := ioutil.ReadFile(filepath.Join(e.Settings, "auth")) - require.NoError(t, err) - require.Equal(t, fmt.Sprintf("{\n \"%s\": \"password\"\n}", tsu.Host), string(data)) - - data, err = ioutil.ReadFile(filepath.Join(e.Settings, "host")) - require.NoError(t, err) - require.Equal(t, tsu.Host, string(data)) - }) -} - -func TestRackInstallError(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - opts := structs.SystemInstallOptions{ - Name: options.String("foo"), - Parameters: map[string]string{}, - Version: options.String("bar"), - } - provider.Mock.On("SystemInstall", mock.Anything, opts).Return("", fmt.Errorf("err1")) - - res, err := testExecute(e, "rack install test -n foo -v bar", nil) - require.NoError(t, err) - require.Equal(t, 1, res.Code) - res.RequireStderr(t, []string{"ERROR: err1"}) - res.RequireStdout(t, []string{""}) - }) -} - func TestRackLogs(t *testing.T) { testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { i.On("SystemLogs", structs.LogsOptions{Prefix: options.Bool(true)}).Return(testLogs(fxLogs()), nil) @@ -376,53 +309,6 @@ func TestRackScaleUpdateError(t *testing.T) { }) } -func TestRackUninstall(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - opts := structs.SystemUninstallOptions{ - Force: options.Bool(true), - } - provider.Mock.On("SystemUninstall", "foo", mock.Anything, opts).Once().Return(nil).Run(func(args mock.Arguments) { - w := args.Get(1).(io.Writer) - fmt.Fprintf(w, "line1\n") - fmt.Fprintf(w, "line2\n") - }) - - res, err := testExecute(e, "rack uninstall test foo --force", nil) - require.NoError(t, err) - require.Equal(t, 0, res.Code) - res.RequireStderr(t, []string{""}) - res.RequireStdout(t, []string{ - "line1", - "line2", - }) - }) -} - -func TestRackUninstallError(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - opts := structs.SystemUninstallOptions{ - Force: options.Bool(true), - } - provider.Mock.On("SystemUninstall", "foo", mock.Anything, opts).Return(fmt.Errorf("err1")) - - res, err := testExecute(e, "rack uninstall test foo --force", nil) - require.NoError(t, err) - require.Equal(t, 1, res.Code) - res.RequireStderr(t, []string{"ERROR: err1"}) - res.RequireStdout(t, []string{""}) - }) -} - -func TestRackUninstallWithoutForce(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - res, err := testExecute(e, "rack uninstall test foo", nil) - require.NoError(t, err) - require.Equal(t, 1, res.Code) - res.RequireStderr(t, []string{"ERROR: must use --force for non-interactive uninstall"}) - res.RequireStdout(t, []string{""}) - }) -} - func TestRackUpdate(t *testing.T) { testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { i.On("SystemUpdate", structs.SystemUpdateOptions{Version: options.String("version1")}).Return(nil) diff --git a/pkg/cli/start.go b/pkg/cli/start.go index 8370475..6d87060 100644 --- a/pkg/cli/start.go +++ b/pkg/cli/start.go @@ -6,10 +6,8 @@ import ( "os" "os/signal" "path/filepath" - "strings" "github.com/convox/convox/pkg/start" - "github.com/convox/convox/pkg/structs" "github.com/convox/convox/sdk" "github.com/convox/stdcli" ) @@ -39,50 +37,44 @@ func Start(rack sdk.Interface, c *stdcli.Context) error { return fmt.Errorf("gen1 is no longer supported") } - var p structs.Provider + // var p structs.Provider - if rack != nil { - p = rack - // s, err := rack.SystemGet() - // if err != nil { - // return err - // } - // if s.Provider == "local" || s.Provider == "kaws" { - // p = rack - // } - } + // if rack != nil { + // fmt.Printf("rack: %+v\n", rack) + // p = rack + // } - if p == nil { - if !localRackRunning(c) { - return fmt.Errorf("local rack not found, try `sudo convox rack install local`") - } + // if p == nil { + // if !localRackRunning(c) { + // return fmt.Errorf("local rack not found, try `sudo convox rack install local`") + // } - r, err := matchRack(c, "local/") - if err != nil { - if strings.HasPrefix(err.Error(), "ambiguous rack name") { - return fmt.Errorf("multiple local racks detected, use `convox switch` to select one") - } - return err - } + // r, err := matchRack(c, "local/") + // if err != nil { + // if strings.HasPrefix(err.Error(), "ambiguous rack name") { + // return fmt.Errorf("multiple local racks detected, use `convox switch` to select one") + // } + // return err + // } - cl, err := sdk.New(fmt.Sprintf("https://rack.%s", strings.TrimPrefix(r.Name, "local/"))) - if err != nil { - return err - } + // cl, err := sdk.New(fmt.Sprintf("https://rack.%s", strings.TrimPrefix(r.Name, "local/"))) + // if err != nil { + // return err + // } - p = cl - } + // p = cl + // } - if p == nil { - return fmt.Errorf("could not find local rack") - } + // if p == nil { + // return fmt.Errorf("could not find local rack") + // } opts := start.Options2{ App: app(c), Build: !c.Bool("no-build"), Cache: !c.Bool("no-cache"), Manifest: c.String("manifest"), - Provider: p, + Provider: rack, Sync: !c.Bool("no-sync"), } diff --git a/pkg/cli/start_test.go b/pkg/cli/start_test.go index f88dd13..aea06c3 100644 --- a/pkg/cli/start_test.go +++ b/pkg/cli/start_test.go @@ -9,81 +9,10 @@ import ( mockstart "github.com/convox/convox/pkg/mock/start" mockstdcli "github.com/convox/convox/pkg/mock/stdcli" "github.com/convox/convox/pkg/start" - "github.com/convox/convox/sdk" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) -func TestStart1(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - ms := &mockstart.Interface{} - cli.Starter = ms - - opts := start.Options1{ - App: "app1", - Build: true, - Cache: true, - Sync: true, - } - - ms.On("Start1", mock.Anything, opts).Return(nil) - - res, err := testExecute(e, "start -g 1 -a app1", nil) - require.NoError(t, err) - require.Equal(t, 0, res.Code) - res.RequireStderr(t, []string{""}) - res.RequireStdout(t, []string{""}) - }) -} - -func TestStart1Error(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - ms := &mockstart.Interface{} - cli.Starter = ms - - opts := start.Options1{ - App: "app1", - Build: true, - Cache: true, - Sync: true, - } - - ms.On("Start1", mock.Anything, opts).Return(fmt.Errorf("err1")) - - res, err := testExecute(e, "start -g 1 -a app1", nil) - require.NoError(t, err) - require.Equal(t, 1, res.Code) - res.RequireStderr(t, []string{"ERROR: err1"}) - res.RequireStdout(t, []string{""}) - }) -} - -func TestStart1Options(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - ms := &mockstart.Interface{} - cli.Starter = ms - - opts := start.Options1{ - App: "app1", - Build: false, - Cache: false, - Command: []string{"bin/command", "args"}, - Manifest: "manifest1", - Service: "service1", - Shift: 3000, - Sync: false, - } - - ms.On("Start1", mock.Anything, opts).Return(nil) - - res, err := testExecute(e, "start -g 1 -a app1 -m manifest1 --no-build --no-cache --no-sync -s 3000 service1 bin/command args", nil) - require.NoError(t, err) - require.Equal(t, 0, res.Code) - res.RequireStderr(t, []string{""}) - res.RequireStdout(t, []string{""}) - }) -} - func TestStart2(t *testing.T) { testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { me := &mockstdcli.Executor{} @@ -103,8 +32,6 @@ func TestStart2(t *testing.T) { ms.On("Start2", mock.Anything, mock.Anything, opts).Return(nil) - i.On("SystemGet").Return(fxSystemLocal, nil) - res, err := testExecute(e, "start -g 2 -a app1", nil) require.NoError(t, err) require.Equal(t, 0, res.Code) @@ -132,8 +59,6 @@ func TestStart2Error(t *testing.T) { ms.On("Start2", mock.Anything, mock.Anything, opts).Return(fmt.Errorf("err1")) - i.On("SystemGet").Return(fxSystemLocal, nil) - res, err := testExecute(e, "start -g 2 -a app1", nil) require.NoError(t, err) require.Equal(t, 1, res.Code) @@ -163,8 +88,6 @@ func TestStart2Options(t *testing.T) { ms.On("Start2", mock.Anything, mock.Anything, opts).Return(nil) - i.On("SystemGet").Return(fxSystemLocal(), nil) - res, err := testExecute(e, "start -g 2 -a app1 -m manifest1 --no-build --no-cache --no-sync service1 service2", nil) require.NoError(t, err) require.Equal(t, 0, res.Code) @@ -173,64 +96,61 @@ func TestStart2Options(t *testing.T) { }) } -func TestStart2Remote(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - me := &mockstdcli.Executor{} - me.On("Execute", "kubectl", "get", "ns", "--selector=system=convox,type=rack", "--output=name").Return([]byte("namespace/dev"), nil) - e.Executor = me +// func TestStart2Remote(t *testing.T) { +// testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { +// me := &mockstdcli.Executor{} +// me.On("Execute", "kubectl", "get", "ns", "--selector=system=convox,type=rack", "--output=name").Return([]byte("namespace/dev"), nil) +// e.Executor = me - ms := &mockstart.Interface{} - cli.Starter = ms +// ms := &mockstart.Interface{} +// cli.Starter = ms - ms.On("Start2", mock.Anything, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { - opts := args.Get(2).(start.Options2) - require.Equal(t, "app1", opts.App) - require.Equal(t, true, opts.Build) - require.Equal(t, true, opts.Cache) - require.Equal(t, true, opts.Sync) - p := opts.Provider.(*sdk.Client) - require.Equal(t, "https", p.Client.Endpoint.Scheme) - require.Equal(t, "rack.dev", p.Client.Endpoint.Host) - }) +// ms.On("Start2", mock.Anything, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { +// opts := args.Get(2).(start.Options2) +// require.Equal(t, "app1", opts.App) +// require.Equal(t, true, opts.Build) +// require.Equal(t, true, opts.Cache) +// require.Equal(t, true, opts.Sync) +// fmt.Printf("opts.Provider: %+v\n", opts.Provider) +// p := opts.Provider.(*sdk.Client) +// require.Equal(t, "https", p.Client.Endpoint.Scheme) +// require.Equal(t, "rack.dev", p.Client.Endpoint.Host) +// }) - i.On("SystemGet").Return(fxSystem(), nil) +// res, err := testExecute(e, "start -g 2 -a app1", nil) +// require.NoError(t, err) +// require.Equal(t, 0, res.Code) +// res.RequireStderr(t, []string{""}) +// res.RequireStdout(t, []string{""}) +// }) +// } - res, err := testExecute(e, "start -g 2 -a app1", nil) - require.NoError(t, err) - require.Equal(t, 0, res.Code) - res.RequireStderr(t, []string{""}) - res.RequireStdout(t, []string{""}) - }) -} +// func TestStart2RemoteMultiple(t *testing.T) { +// testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { +// me := &mockstdcli.Executor{} +// me.On("Execute", "kubectl", "get", "ns", "--selector=system=convox,type=rack", "--output=name").Return([]byte("namespace/dev\nnamespace/dev2\n"), nil) +// e.Executor = me -func TestStart2RemoteMultiple(t *testing.T) { - testClient(t, func(e *cli.Engine, i *mocksdk.Interface) { - me := &mockstdcli.Executor{} - me.On("Execute", "kubectl", "get", "ns", "--selector=system=convox,type=rack", "--output=name").Return([]byte("namespace/dev\nnamespace/dev2\n"), nil) - e.Executor = me +// ms := &mockstart.Interface{} +// cli.Starter = ms - ms := &mockstart.Interface{} - cli.Starter = ms +// opts := start.Options2{ +// App: "app1", +// Build: true, +// Cache: true, +// Sync: true, +// } - opts := start.Options2{ - App: "app1", - Build: true, - Cache: true, - Sync: true, - } +// ms.On("Start2", mock.Anything, opts).Return(nil).Run(func(args mock.Arguments) { +// s := args.Get(0).(*sdk.Client) +// require.Equal(t, "https", s.Client.Endpoint.Scheme) +// require.Equal(t, "rack.classic", s.Client.Endpoint.Host) +// }) - ms.On("Start2", mock.Anything, opts).Return(nil).Run(func(args mock.Arguments) { - s := args.Get(0).(*sdk.Client) - require.Equal(t, "https", s.Client.Endpoint.Scheme) - require.Equal(t, "rack.classic", s.Client.Endpoint.Host) - }) - - i.On("SystemGet").Return(fxSystem(), nil) - - res, err := testExecute(e, "start -g 2 -a app1", nil) - require.NoError(t, err) - require.Equal(t, 1, res.Code) - res.RequireStderr(t, []string{"ERROR: multiple local racks detected, use `convox switch` to select one"}) - res.RequireStdout(t, []string{""}) - }) -} +// res, err := testExecute(e, "start -g 2 -a app1", nil) +// require.NoError(t, err) +// require.Equal(t, 1, res.Code) +// res.RequireStderr(t, []string{"ERROR: multiple local racks detected, use `convox switch` to select one"}) +// res.RequireStdout(t, []string{""}) +// }) +// } diff --git a/pkg/start/gen2_test.go b/pkg/start/gen2_test.go index f4f0ac6..7cc566c 100644 --- a/pkg/start/gen2_test.go +++ b/pkg/start/gen2_test.go @@ -9,11 +9,11 @@ import ( "testing" "time" - "github.com/convox/exec" "github.com/convox/convox/pkg/common" "github.com/convox/convox/pkg/options" "github.com/convox/convox/pkg/start" "github.com/convox/convox/pkg/structs" + "github.com/convox/exec" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -74,7 +74,7 @@ func TestStart2(t *testing.T) { } func TestStart2Options(t *testing.T) { - helpers.ProviderWaitDuration = 1 + common.ProviderWaitDuration = 1 p := &structs.MockProvider{} diff --git a/provider/k8s/app_test.go b/provider/k8s/app_test.go index e6d4870..f1b09ee 100644 --- a/provider/k8s/app_test.go +++ b/provider/k8s/app_test.go @@ -83,7 +83,7 @@ func TestAppCreate(t *testing.T) { require.NoError(t, err) require.NotNil(t, a) - assert.Equal(t, "2", a.Generation) + assert.Equal(t, "3", a.Generation) assert.Equal(t, "app1", a.Name) }) } @@ -124,7 +124,7 @@ func TestAppGet(t *testing.T) { a, err := p.AppGet("app1") require.NoError(t, err) - assert.Equal(t, "2", a.Generation) + assert.Equal(t, "3", a.Generation) assert.Equal(t, false, a.Locked) assert.Equal(t, "app1", a.Name) assert.Equal(t, "R1234567", a.Release) @@ -174,17 +174,18 @@ func TestAppList(t *testing.T) { require.NoError(t, appCreate(kk, "rack1", "app2")) as, err := p.AppList() + fmt.Printf("as: %+v\n", as) require.NoError(t, err) require.Equal(t, 2, len(as)) - assert.Equal(t, "2", as[0].Generation) + assert.Equal(t, "3", as[0].Generation) assert.Equal(t, false, as[0].Locked) assert.Equal(t, "app1", as[0].Name) assert.Equal(t, "R1234567", as[0].Release) assert.Equal(t, "", as[0].Router) assert.Equal(t, "running", as[0].Status) - assert.Equal(t, "2", as[1].Generation) + assert.Equal(t, "3", as[1].Generation) assert.Equal(t, false, as[1].Locked) assert.Equal(t, "app2", as[1].Name) assert.Equal(t, "R2345678", as[1].Release) diff --git a/provider/k8s/testdata/app-locked.yml b/provider/k8s/testdata/app-locked.yml index 958c828..7206f33 100644 --- a/provider/k8s/testdata/app-locked.yml +++ b/provider/k8s/testdata/app-locked.yml @@ -30,6 +30,5 @@ spec: - from: - namespaceSelector: matchLabels: - scope: system system: convox podSelector: {} \ No newline at end of file diff --git a/provider/k8s/testdata/app-params.yml b/provider/k8s/testdata/app-params.yml index bc0c58f..24cba2a 100644 --- a/provider/k8s/testdata/app-params.yml +++ b/provider/k8s/testdata/app-params.yml @@ -30,6 +30,5 @@ spec: - from: - namespaceSelector: matchLabels: - scope: system system: convox podSelector: {} \ No newline at end of file diff --git a/provider/k8s/testdata/app.yml b/provider/k8s/testdata/app.yml index 65d03fb..4c5c817 100644 --- a/provider/k8s/testdata/app.yml +++ b/provider/k8s/testdata/app.yml @@ -30,6 +30,5 @@ spec: - from: - namespaceSelector: matchLabels: - scope: system system: convox podSelector: {} \ No newline at end of file