Skip to content

Commit ad47fc6

Browse files
committed
Fixing comments and cf client in tests
LMCROSSITXSADEPLOY-2301
1 parent 81224d9 commit ad47fc6

5 files changed

Lines changed: 88 additions & 65 deletions

File tree

clients/cfrestclient/rest_cloud_foundry_client_extended.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c CloudFoundryRestClient) GetAppProcessStatistics(appGuid string) ([]model
6161
apiEndpoint, _ := c.cliConn.ApiEndpoint()
6262

6363
getAppProcessStatsUrl := fmt.Sprintf("%s/%sapps/%s/processes/web/stats", apiEndpoint, cfBaseUrl, appGuid)
64-
body, err := executeRequest("GET", getAppProcessStatsUrl, token, c.isSslDisabled, nil)
64+
body, err := executeRequest(http.MethodGet, getAppProcessStatsUrl, token, c.isSslDisabled, nil)
6565
if err != nil {
6666
return nil, err
6767
}
@@ -190,7 +190,7 @@ func (c CloudFoundryRestClient) CreateUserProvidedServiceInstance(serviceName st
190190
func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, error) {
191191
var result []T
192192
for url != "" {
193-
body, err := executeRequest("GET", url, token, isSslDisabled, nil)
193+
body, err := executeRequest(http.MethodGet, url, token, isSslDisabled, nil)
194194
if err != nil {
195195
return nil, err
196196
}
@@ -210,7 +210,7 @@ func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, e
210210
func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string, isSslDisabled bool, auxiliaryContentHandler func(T, Auxiliary) T) ([]T, error) {
211211
var result []T
212212
for url != "" {
213-
body, err := executeRequest("GET", url, token, isSslDisabled, nil)
213+
body, err := executeRequest(http.MethodGet, url, token, isSslDisabled, nil)
214214
if err != nil {
215215
return nil, err
216216
}

clients/models/cf_services_response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type LastOperation struct {
3434
type ServicePlan struct {
3535
Guid string `json:"guid"`
3636
Name string `json:"name"`
37-
OfferingGuid string `jsonry:"rela tionships.service_offering.data.guid,omitempty"`
37+
OfferingGuid string `jsonry:"relationships.service_offering.data.guid,omitempty"`
3838
}
3939

4040
type ServiceOffering struct {

commands/deploy_command.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ func (c *DeployCommand) executeInternal(positionalArgs []string, dsHost string,
304304
return Failure
305305
}
306306
mtaId, fileId, schemaVersion = asyncUploadJobResult.MtaId, asyncUploadJobResult.FileId, asyncUploadJobResult.SchemaVersion
307-
ui.Say("-------------------- mtaId: %s, fileId: %s, schemaVersion: %s", mtaId, fileId, schemaVersion)
308307
// Check for an ongoing operation for this MTA ID and abort it
309308
wasAborted, err := c.CheckOngoingOperation(mtaId, namespace, dsHost, force, cfTarget)
310309
if err != nil {
@@ -318,7 +317,7 @@ func (c *DeployCommand) executeInternal(positionalArgs []string, dsHost string,
318317
uploadedArchivePartIds = append(uploadedArchivePartIds, fileId)
319318

320319
if GetBoolOpt(requireSecureParameters, flags) {
321-
result := setUpSpecificsForDeploymentUsingSecrerts(flags, c, mtaId, namespace, schemaVersion, &disposableUserProvidedServiceName, &yamlBytes)
320+
result := setUpSpecificsForDeploymentUsingSecrets(flags, c, mtaId, namespace, schemaVersion, &disposableUserProvidedServiceName, &yamlBytes)
322321
if result != Success {
323322
return Failure
324323
}
@@ -355,7 +354,7 @@ func (c *DeployCommand) executeInternal(positionalArgs []string, dsHost string,
355354
}
356355

357356
if GetBoolOpt(requireSecureParameters, flags) {
358-
result := setUpSpecificsForDeploymentUsingSecrerts(flags, c, mtaId, namespace, descriptor.SchemaVersion, &disposableUserProvidedServiceName, &yamlBytes)
357+
result := setUpSpecificsForDeploymentUsingSecrets(flags, c, mtaId, namespace, descriptor.SchemaVersion, &disposableUserProvidedServiceName, &yamlBytes)
359358
if result != Success {
360359
return Failure
361360
}
@@ -444,7 +443,7 @@ func getRandomisedUpsName(mtaId, namespace string) (disposableUpsName string, er
444443
return "__mta-secure-" + mtaId + "-" + namespace + "-" + resultSuffix, nil
445444
}
446445

447-
func setUpSpecificsForDeploymentUsingSecrerts(flags *flag.FlagSet, c *DeployCommand, mtaId, namespace, schemaVersion string, disposableUserProvidedServiceName *string, yamlBytes *[]byte) ExecutionStatus {
446+
func setUpSpecificsForDeploymentUsingSecrets(flags *flag.FlagSet, c *DeployCommand, mtaId, namespace, schemaVersion string, disposableUserProvidedServiceName *string, yamlBytes *[]byte) ExecutionStatus {
448447
// Collect special ENVs: __MTA___<name>, __MTA_JSON___<name>, __MTA_CERT___<name>
449448
parameters, err := secure_parameters.CollectFromEnv("__MTA")
450449
if err != nil {

commands/deploy_command_test.go

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands_test
22

33
import (
44
"encoding/base64"
5+
"errors"
56
"fmt"
67
"io"
78
"io/fs"
@@ -12,6 +13,7 @@ import (
1213
"time"
1314

1415
cli_fakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/cli/fakes"
16+
cf_client_fakes "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/cfrestclient/fakes"
1517
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
1618
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
1719
mtafake "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient/fakes"
@@ -112,41 +114,62 @@ var _ = Describe("DeployCommand", func() {
112114
if fromUrl {
113115
mtaNameToPrint = "from url"
114116
}
117+
115118
lines = append(lines,
116-
"Deploying multi-target app archive "+mtaNameToPrint+" in org "+org+" / space "+space+" as "+user+"...")
117-
lines = append(lines, "")
119+
"Deploying multi-target app archive "+mtaNameToPrint+" in org "+org+" / space "+space+" as "+user+"...",
120+
"",
121+
)
122+
118123
if processAborted {
119124
lines = append(lines,
120125
"Executing action \"abort\" on operation test-process-id...",
121126
"OK",
122127
)
123128
}
124-
if fromUrl {
125-
lines = append(lines, "OK")
126-
} else {
129+
130+
if !fromUrl {
131+
if existentUserProvidedServiceSecurity {
132+
lines = append(lines,
133+
"Using existing user-provided service "+userProvidedServiceSecurityRelated+" for secure parameters.")
134+
}
135+
136+
if createdUserProvidedServiceSecurity {
137+
lines = append(lines,
138+
"Created user-provided service "+userProvidedServiceSecurityRelated+" for secure parameters.")
139+
}
140+
127141
lines = append(lines,
128142
"Uploading 1 files...",
129143
" "+fullMtaArchivePath,
130-
"OK")
144+
"OK",
145+
)
146+
} else {
147+
if existentUserProvidedServiceSecurity {
148+
lines = append(lines,
149+
"Using existing user-provided service "+userProvidedServiceSecurityRelated+" for secure parameters.")
150+
}
151+
152+
if createdUserProvidedServiceSecurity {
153+
lines = append(lines,
154+
"Created user-provided service "+userProvidedServiceSecurityRelated+" for secure parameters.")
155+
}
156+
lines = append(lines, "OK")
131157
}
158+
132159
if extDescriptor {
133160
lines = append(lines,
134161
"Uploading 1 files...",
135162
" "+fullExtDescriptorPath,
136-
"OK")
137-
}
138-
if existentUserProvidedServiceSecurity {
139-
lines = append(lines,
140-
"Using existing user-provided service "+userProvidedServiceSecurityRelated+" for secure parameters.")
141-
}
142-
if createdUserProvidedServiceSecurity {
143-
lines = append(lines,
144-
"Created user-provided service "+userProvidedServiceSecurityRelated+" for secure parameters.")
163+
"OK",
164+
)
145165
}
166+
146167
lines = append(lines,
147168
"Test message",
148169
"Process finished.",
149-
"Use \"cf dmol -i 1000\" to download the logs of the process.")
170+
"Use \"cf dmol -i 1000\" to download the logs of the process.",
171+
)
172+
150173
return lines
151174
}
152175

@@ -180,6 +203,7 @@ var _ = Describe("DeployCommand", func() {
180203

181204
BeforeEach(func() {
182205
ui.DisableTerminalOutput(true)
206+
command = commands.NewDeployCommand()
183207
name = command.GetPluginCommand().Name
184208
cliConnection = cli_fakes.NewFakeCliConnectionBuilder().
185209
CurrentOrg("test-org-guid", org, nil).
@@ -194,8 +218,9 @@ var _ = Describe("DeployCommand", func() {
194218
jobId := "one"
195219
fileUploadJobId.Add("Location", jobId)
196220
jobResult := mtaclient.AsyncUploadJobResult{
197-
File: mtaArchive,
198-
MtaId: "anatz",
221+
File: mtaArchive,
222+
MtaId: "anatz",
223+
SchemaVersion: "3.1.0",
199224
}
200225
mtaClient = mtafake.NewFakeMtaClientBuilder().
201226
GetMtaFiles([]*models.FileMetadata{&testutil.SimpleFile}, nil).
@@ -209,7 +234,6 @@ var _ = Describe("DeployCommand", func() {
209234
GetMtaOperationLogContent("1000", testutil.LogID, testutil.LogContent, nil).
210235
GetMtaOperations(nil, nil, nil, []*models.Operation{&testutil.OperationResult}, nil).Build()
211236
testClientFactory = commands.NewTestClientFactory(mtaClient, nil, nil)
212-
command = commands.NewDeployCommand()
213237
testTokenFactory := commands.NewTestTokenFactory(cliConnection)
214238
deployServiceURLCalculator := util_fakes.NewDeployServiceURLFakeCalculator("deploy-service.test.ondemand.com")
215239
command.InitializeAll(name, cliConnection, testutil.NewCustomTransport(200), testClientFactory, testTokenFactory, deployServiceURLCalculator)
@@ -511,12 +535,12 @@ var _ = Describe("DeployCommand", func() {
511535
command.FileUrlReader = newMockFileReader(correctMtaUrl)
512536

513537
upsName := "__mta-secure-anatz"
514-
cliConnection.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
515-
if len(args) > 0 && args[0] == "services" {
516-
table := fmt.Sprintf("%s user-provided fake-plan\nanother-service-instance managed fake-plan\n", upsName)
517-
return []string{table}, nil
518-
}
519-
return []string{}, nil
538+
command.CfClient = &cf_client_fakes.FakeCloudFoundryClient{
539+
Services: []models.CloudFoundryServiceInstance{{
540+
Guid: "ups-guid",
541+
Name: upsName},
542+
},
543+
ServiceBindingsErr: nil,
520544
}
521545

522546
output, status := oc.CaptureOutputAndStatus(func() int {
@@ -540,18 +564,14 @@ var _ = Describe("DeployCommand", func() {
540564
defer os.Unsetenv("__MTA___fake-variable")
541565
command.FileUrlReader = newMockFileReader(correctMtaUrl)
542566

543-
cliConnection.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
544-
if len(args) > 0 && args[0] == "services" {
545-
return []string{"another-service-instance managed fake-plan\n"}, nil
546-
}
547-
return []string{}, nil
548-
}
549-
550-
cliConnection.CliCommandStub = func(args ...string) ([]string, error) {
551-
if len(args) > 0 && args[0] == "create-user-provided-service" {
552-
return []string{}, nil
553-
}
554-
return []string{}, nil
567+
upsName := "__mta-secure-anatz"
568+
command.CfClient = &cf_client_fakes.FakeCloudFoundryClient{
569+
Services: []models.CloudFoundryServiceInstance{{
570+
Guid: "ups-guid",
571+
Name: upsName},
572+
},
573+
ServiceBindingsErr: errors.New("service instance not found"),
574+
ServicesErr: nil,
555575
}
556576

557577
output, status := oc.CaptureOutputAndStatus(func() int {
@@ -575,18 +595,10 @@ var _ = Describe("DeployCommand", func() {
575595
defer os.Unsetenv("__MTA___fake-variable")
576596
command.FileUrlReader = newMockFileReader(correctMtaUrl)
577597

578-
cliConnection.CliCommandWithoutTerminalOutputStub = func(args ...string) ([]string, error) {
579-
if len(args) > 0 && args[0] == "services" {
580-
return []string{"another-service-instance managed fake-plan\n"}, nil
581-
}
582-
return []string{}, nil
583-
}
584-
585-
cliConnection.CliCommandStub = func(args ...string) ([]string, error) {
586-
if len(args) > 0 && args[0] == "create-user-provided-service" {
587-
return nil, fmt.Errorf("error - could not be created")
588-
}
589-
return []string{}, nil
598+
command.CfClient = &cf_client_fakes.FakeCloudFoundryClient{
599+
Services: []models.CloudFoundryServiceInstance{{Name: "fakeName"}},
600+
ServiceBindingsErr: errors.New("error with cf api"),
601+
ServicesErr: nil,
590602
}
591603

592604
output, status := oc.CaptureOutputAndStatus(func() int {

secure_parameters/secure_parameters_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,30 @@ func TestCollectFromEnv(t *testing.T) {
2626
t.Fatalf("Collecting environment variables has failed: %s", err.Error())
2727
}
2828

29-
parameterValue, ok := resultToTest["fakePassword"]
29+
testNormalVariable(t, &resultToTest)
30+
31+
testJsonVariable(t, &resultToTest)
32+
33+
testCertificateVariable(t, &resultToTest, testCertificate)
34+
35+
if _, exists := resultToTest["other"]; exists {
36+
t.Fatalf("Unexpected value and environment variable")
37+
}
38+
}
39+
40+
func testNormalVariable(t *testing.T, resultToTest *map[string]ParameterValue) {
41+
parameterValue, ok := (*resultToTest)["fakePassword"]
3042
if !ok {
3143
t.Fatalf("Missing key 'fakePassword' in map")
3244
}
3345

3446
if parameterValue.Type != typeString || parameterValue.StringContent != "secretValue" {
3547
t.Fatalf("The value of 'fakePassword' key is not correct")
3648
}
49+
}
3750

38-
jsonValue, ok := resultToTest["fakeJson"]
51+
func testJsonVariable(t *testing.T, resultToTest *map[string]ParameterValue) {
52+
jsonValue, ok := (*resultToTest)["fakeJson"]
3953
if !ok {
4054
t.Fatalf("Missing key 'fakeJson' in map")
4155
}
@@ -56,8 +70,10 @@ func TestCollectFromEnv(t *testing.T) {
5670
if castedValue["b"] != "secretValueJson" {
5771
t.Fatalf("The second value of the json is not what it should be: %v", castedValue["b"])
5872
}
73+
}
5974

60-
certificateValue, ok := resultToTest["fakeCertificate"]
75+
func testCertificateVariable(t *testing.T, resultToTest *map[string]ParameterValue, testCertificate string) {
76+
certificateValue, ok := (*resultToTest)["fakeCertificate"]
6177

6278
if !ok {
6379
t.Fatalf("The value of the certificate is not present")
@@ -66,10 +82,6 @@ func TestCollectFromEnv(t *testing.T) {
6682
if certificateValue.Type != typeMultiline || certificateValue.StringContent != testCertificate {
6783
t.Fatalf("The value of the certificate is not what it should be: %v", certificateValue)
6884
}
69-
70-
if _, exists := resultToTest["other"]; exists {
71-
t.Fatalf("Unexpected value and environment variable")
72-
}
7385
}
7486

7587
func TestCollectFromEnvWhenWrongName(t *testing.T) {
@@ -133,7 +145,7 @@ func TestBuildSecureExtension(t *testing.T) {
133145
yamlResult, err := BuildSecureExtension(parameters, "test-mta", "")
134146

135147
if err != nil {
136-
t.Fatalf("Error while building the secure extension descriotor: %s", err.Error())
148+
t.Fatalf("Error while building the secure extension descriptor: %s", err.Error())
137149
}
138150

139151
var unmarshaledBack map[string]interface{}

0 commit comments

Comments
 (0)