github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/cloudfoundry/CloudFoundry_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cloudfoundry
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func loginMockCleanup(m *mock.ExecMockRunner) {
    15  	m.ShouldFailOnCommand = map[string]error{}
    16  	m.StdoutReturn = map[string]string{}
    17  	m.Calls = []mock.ExecCall{}
    18  }
    19  
    20  func TestCloudFoundryLoginCheck(t *testing.T) {
    21  
    22  	m := &mock.ExecMockRunner{}
    23  
    24  	t.Run("CF Login check: logged in", func(t *testing.T) {
    25  
    26  		defer loginMockCleanup(m)
    27  
    28  		cfconfig := LoginOptions{
    29  			CfAPIEndpoint: "https://api.endpoint.com",
    30  		}
    31  		cf := CFUtils{Exec: m, loggedIn: true}
    32  		loggedIn, err := cf.LoginCheck(cfconfig)
    33  		if assert.NoError(t, err) {
    34  			assert.True(t, loggedIn)
    35  		}
    36  	})
    37  
    38  	t.Run("CF Login check: not logged in", func(t *testing.T) {
    39  
    40  		defer loginMockCleanup(m)
    41  
    42  		cfconfig := LoginOptions{
    43  			CfAPIEndpoint: "https://api.endpoint.com",
    44  		}
    45  		cf := CFUtils{Exec: m, loggedIn: false}
    46  		loggedIn, err := cf.LoginCheck(cfconfig)
    47  		if assert.NoError(t, err) {
    48  			assert.False(t, loggedIn)
    49  		}
    50  	})
    51  }
    52  
    53  func TestCloudFoundryLogin(t *testing.T) {
    54  
    55  	m := &mock.ExecMockRunner{}
    56  
    57  	t.Run("CF Login: missing parameter", func(t *testing.T) {
    58  
    59  		defer loginMockCleanup(m)
    60  
    61  		cfconfig := LoginOptions{}
    62  		cf := CFUtils{Exec: m}
    63  		err := cf.Login(cfconfig)
    64  		assert.EqualError(t, err, "Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password")
    65  	})
    66  	t.Run("CF Login: failure", func(t *testing.T) {
    67  
    68  		defer loginMockCleanup(m)
    69  
    70  		m.ShouldFailOnCommand = map[string]error{"cf login .*": fmt.Errorf("wrong password or account does not exist")}
    71  
    72  		cfconfig := LoginOptions{
    73  			CfAPIEndpoint: "https://api.endpoint.com",
    74  			CfSpace:       "testSpace",
    75  			CfOrg:         "testOrg",
    76  			Username:      "testUser",
    77  			Password:      "testPassword",
    78  		}
    79  
    80  		cf := CFUtils{Exec: m}
    81  		err := cf.Login(cfconfig)
    82  		if assert.EqualError(t, err, "Failed to login to Cloud Foundry: wrong password or account does not exist") {
    83  			assert.False(t, cf.loggedIn)
    84  			assert.Equal(t, []mock.ExecCall{
    85  				{Exec: "cf", Params: []string{
    86  					"login",
    87  					"-a", "https://api.endpoint.com",
    88  					"-o", "testOrg",
    89  					"-s", "testSpace",
    90  					"-u", "testUser",
    91  					"-p", "testPassword",
    92  				}},
    93  			}, m.Calls)
    94  		}
    95  	})
    96  
    97  	t.Run("CF Login: success", func(t *testing.T) {
    98  
    99  		defer loginMockCleanup(m)
   100  
   101  		m.StdoutReturn = map[string]string{"cf api:*": "Not logged in"}
   102  
   103  		cfconfig := LoginOptions{
   104  			CfAPIEndpoint: "https://api.endpoint.com",
   105  			CfSpace:       "testSpace",
   106  			CfOrg:         "testOrg",
   107  			Username:      "testUser",
   108  			Password:      "testPassword",
   109  		}
   110  		cf := CFUtils{Exec: m}
   111  		err := cf.Login(cfconfig)
   112  		if assert.NoError(t, err) {
   113  			assert.True(t, cf.loggedIn)
   114  			assert.Equal(t, []mock.ExecCall{
   115  				{Exec: "cf", Params: []string{
   116  					"login",
   117  					"-a", "https://api.endpoint.com",
   118  					"-o", "testOrg",
   119  					"-s", "testSpace",
   120  					"-u", "testUser",
   121  					"-p", "testPassword",
   122  				}},
   123  			}, m.Calls)
   124  		}
   125  	})
   126  
   127  	t.Run("CF Login: with additional login options", func(t *testing.T) {
   128  
   129  		defer loginMockCleanup(m)
   130  
   131  		cfconfig := LoginOptions{
   132  			CfAPIEndpoint: "https://api.endpoint.com",
   133  			CfSpace:       "testSpace",
   134  			CfOrg:         "testOrg",
   135  			Username:      "testUser",
   136  			Password:      "testPassword",
   137  			CfLoginOpts: []string{
   138  				// should never used in productive environment, but it is useful for rapid prototyping/troubleshooting
   139  				"--skip-ssl-validation",
   140  				"--origin", "ldap",
   141  			},
   142  		}
   143  		cf := CFUtils{Exec: m}
   144  		err := cf.Login(cfconfig)
   145  		if assert.NoError(t, err) {
   146  			assert.True(t, cf.loggedIn)
   147  			assert.Equal(t, []mock.ExecCall{
   148  				{Exec: "cf", Params: []string{
   149  					"login",
   150  					"-a", "https://api.endpoint.com",
   151  					"-o", "testOrg",
   152  					"-s", "testSpace",
   153  					"-u", "testUser",
   154  					"-p", "testPassword",
   155  					"--skip-ssl-validation",
   156  					"--origin", "ldap",
   157  				}},
   158  			}, m.Calls)
   159  		}
   160  	})
   161  }
   162  
   163  func TestCloudFoundryLogout(t *testing.T) {
   164  	t.Run("CF Logout", func(t *testing.T) {
   165  		cf := CFUtils{Exec: &mock.ExecMockRunner{}, loggedIn: true}
   166  		err := cf.Logout()
   167  		if assert.NoError(t, err) {
   168  			assert.False(t, cf.loggedIn)
   169  		}
   170  	})
   171  }
   172  
   173  func TestCloudFoundryReadServiceKeyAbapEnvironment(t *testing.T) {
   174  
   175  	t.Run("CF ReadServiceKey", func(t *testing.T) {
   176  
   177  		//given
   178  		m := &mock.ExecMockRunner{}
   179  		defer loginMockCleanup(m)
   180  
   181  		const testURL = "testurl.com"
   182  		const oDataURL = "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull"
   183  		const username = "test_user"
   184  		const password = "test_password"
   185  		const serviceKey = `
   186  		cf comment test \n\n
   187  		{"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"` + username + `","password":"` + password + `","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}`
   188  
   189  		m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey}
   190  
   191  		cfconfig := ServiceKeyOptions{
   192  			CfAPIEndpoint:     "https://api.endpoint.com",
   193  			CfSpace:           "testSpace",
   194  			CfOrg:             "testOrg",
   195  			CfServiceInstance: "testInstance",
   196  			CfServiceKeyName:  "testServiceKeyName",
   197  			Username:          "testUser",
   198  			Password:          "testPassword",
   199  		}
   200  
   201  		//when
   202  		var err error
   203  		var abapServiceKey string
   204  		cf := CFUtils{Exec: m}
   205  
   206  		abapServiceKey, err = cf.ReadServiceKey(cfconfig)
   207  
   208  		//then
   209  		if assert.NoError(t, err) {
   210  			assert.Equal(t, []mock.ExecCall{
   211  				{Exec: "cf", Params: []string{
   212  					"login",
   213  					"-a", "https://api.endpoint.com",
   214  					"-o", "testOrg",
   215  					"-s", "testSpace",
   216  					"-u", "testUser",
   217  					"-p", "testPassword",
   218  				}},
   219  				{Exec: "cf", Params: []string{"service-key", "testInstance", "testServiceKeyName"}},
   220  				{Exec: "cf", Params: []string{"logout"}},
   221  			}, m.Calls)
   222  		}
   223  		assert.Equal(t, `		{"sap.cloud.service":"com.sap.cloud.abap","url": "`+testURL+`" ,"systemid":"H01","abap":{"username":"`+username+`","password":"`+password+`","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}`, abapServiceKey)
   224  	})
   225  }