From 580e6f89896afaa80736c5cdef70e0a53f3e0660 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 1 Jul 2020 14:19:21 +0200 Subject: [PATCH] More acurate tests for cf login check (#1738) - mock is used now for cf api commands - tests for cf api and cfk login are checking which commands are really executed - some minor simplifications wrt asserts --- pkg/cloudfoundry/Authentication.go | 2 +- pkg/cloudfoundry/CloudFoundry_test.go | 106 ++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/pkg/cloudfoundry/Authentication.go b/pkg/cloudfoundry/Authentication.go index 624a25b4e..31bad9962 100644 --- a/pkg/cloudfoundry/Authentication.go +++ b/pkg/cloudfoundry/Authentication.go @@ -10,7 +10,7 @@ import ( "github.com/SAP/jenkins-library/pkg/log" ) -var c = command.Command{} +var c command.ExecRunner = &command.Command{} //LoginCheck checks if user is logged in to Cloud Foundry. //If user is not logged in 'cf api' command will return string that contains 'User is not logged in' only if user is not logged in. diff --git a/pkg/cloudfoundry/CloudFoundry_test.go b/pkg/cloudfoundry/CloudFoundry_test.go index 5ebb49d27..2df712a31 100644 --- a/pkg/cloudfoundry/CloudFoundry_test.go +++ b/pkg/cloudfoundry/CloudFoundry_test.go @@ -1,36 +1,118 @@ package cloudfoundry import ( - "testing" - + "fmt" + "github.com/SAP/jenkins-library/pkg/command" + "github.com/SAP/jenkins-library/pkg/mock" "github.com/stretchr/testify/assert" + "testing" ) +func loginMockCleanup(m *mock.ExecMockRunner) { + m.ShouldFailOnCommand = map[string]error{} + m.StdoutReturn = map[string]string{} + m.Calls = []mock.ExecCall{} +} + func TestCloudFoundryLoginCheck(t *testing.T) { - t.Run("CF Login check: missing parameter", func(t *testing.T) { + + m := &mock.ExecMockRunner{} + + defer func() { + c = &command.Command{} + }() + c = m + + t.Run("CF Login check: missing endpoint parameter", func(t *testing.T) { cfconfig := LoginOptions{} loggedIn, err := LoginCheck(cfconfig) - assert.Equal(t, false, loggedIn) + assert.False(t, loggedIn) assert.EqualError(t, err, "Cloud Foundry API endpoint parameter missing. Please provide the Cloud Foundry Endpoint") }) t.Run("CF Login check: failure case", func(t *testing.T) { + + defer loginMockCleanup(m) + + m.ShouldFailOnCommand = map[string]error{"cf api.*": fmt.Errorf("Cannot perform login check")} cfconfig := LoginOptions{ CfAPIEndpoint: "https://api.endpoint.com", } loggedIn, err := LoginCheck(cfconfig) - assert.Equal(t, false, loggedIn) + assert.False(t, loggedIn) assert.Error(t, err) + assert.Equal(t, []mock.ExecCall{mock.ExecCall{Exec: "cf", Params: []string{"api", "https://api.endpoint.com"}}}, m.Calls) + }) + + t.Run("CF Login check: success case", func(t *testing.T) { + + defer loginMockCleanup(m) + + cfconfig := LoginOptions{ + CfAPIEndpoint: "https://api.endpoint.com", + } + loggedIn, err := LoginCheck(cfconfig) + if assert.NoError(t, err) { + assert.True(t, loggedIn) + assert.Equal(t, []mock.ExecCall{mock.ExecCall{Exec: "cf", Params: []string{"api", "https://api.endpoint.com"}}}, m.Calls) + } }) } func TestCloudFoundryLogin(t *testing.T) { + + m := &mock.ExecMockRunner{} + + defer func() { + c = &command.Command{} + }() + c = m + t.Run("CF Login: missing parameter", func(t *testing.T) { + + defer loginMockCleanup(m) + cfconfig := LoginOptions{} err := Login(cfconfig) assert.EqualError(t, err, "Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password") }) t.Run("CF Login: failure", func(t *testing.T) { + + defer loginMockCleanup(m) + + m.StdoutReturn = map[string]string{"cf api .*": "Not logged in"} + m.ShouldFailOnCommand = map[string]error{"cf login .*": fmt.Errorf("wrong password or account does not exist")} + + cfconfig := LoginOptions{ + CfAPIEndpoint: "https://api.endpoint.com", + CfSpace: "testSpace", + CfOrg: "testOrg", + Username: "testUser", + Password: "testPassword", + } + + err := Login(cfconfig) + if assert.EqualError(t, err, "Failed to login to Cloud Foundry: wrong password or account does not exist") { + assert.Equal(t, []mock.ExecCall{ + mock.ExecCall{Exec: "cf", Params: []string{"api", "https://api.endpoint.com"}}, + mock.ExecCall{Exec: "cf", Params: []string{ + "login", + "-a", "https://api.endpoint.com", + "-o", "testOrg", + "-s", "testSpace", + "-u", "testUser", + "-p", "testPassword", + }}, + }, m.Calls) + } + }) + + t.Run("CF Login: success", func(t *testing.T) { + + defer loginMockCleanup(m) + + m.StdoutReturn = map[string]string{"cf api:*": "Not logged in"} + cfconfig := LoginOptions{ CfAPIEndpoint: "https://api.endpoint.com", CfSpace: "testSpace", @@ -39,7 +121,19 @@ func TestCloudFoundryLogin(t *testing.T) { Password: "testPassword", } err := Login(cfconfig) - assert.Error(t, err) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{ + mock.ExecCall{Exec: "cf", Params: []string{"api", "https://api.endpoint.com"}}, + mock.ExecCall{Exec: "cf", Params: []string{ + "login", + "-a", "https://api.endpoint.com", + "-o", "testOrg", + "-s", "testSpace", + "-u", "testUser", + "-p", "testPassword", + }}, + }, m.Calls) + } }) }