1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

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
This commit is contained in:
Marcus Holl 2020-07-01 14:19:21 +02:00 committed by GitHub
parent 4f2ba73314
commit 580e6f8989
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 7 deletions

View File

@ -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.

View File

@ -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)
}
})
}