@@ -78,6 +78,7 @@ var _ = Describe("DeployCommand", func() {
7878 const testArchive = "mtaArchive.mtar"
7979 const mtaArchivePath = testFilesLocation + testArchive
8080 const extDescriptorPath = testFilesLocation + "extDescriptor.mtaext"
81+ const userProvidedServiceSecurityRelated = "__mta-secure-anatz"
8182
8283 var name string
8384 var cliConnection * plugin_fakes.FakeCliConnection
@@ -105,7 +106,7 @@ var _ = Describe("DeployCommand", func() {
105106 }
106107 }
107108
108- var getOutputLines = func (extDescriptor , processAborted , fromUrl bool ) []string {
109+ var getOutputLines = func (extDescriptor , processAborted , fromUrl , existentUserProvidedServiceSecurity , createdUserProvidedServiceSecurity bool ) []string {
109110 var lines []string
110111 mtaNameToPrint := mtaArchivePath
111112 if fromUrl {
@@ -134,6 +135,14 @@ var _ = Describe("DeployCommand", func() {
134135 " " + fullExtDescriptorPath ,
135136 "OK" )
136137 }
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." )
145+ }
137146 lines = append (lines ,
138147 "Test message" ,
139148 "Process finished." ,
@@ -246,7 +255,7 @@ var _ = Describe("DeployCommand", func() {
246255 output , status := oc .CaptureOutputAndStatus (func () int {
247256 return command .Execute ([]string {}).ToInt ()
248257 })
249- ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , true ))
258+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , true , false , false ))
250259 })
251260 })
252261
@@ -348,7 +357,7 @@ var _ = Describe("DeployCommand", func() {
348357 output , status := oc .CaptureOutputAndStatus (func () int {
349358 return command .Execute ([]string {mtaArchivePath }).ToInt ()
350359 })
351- ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , false ))
360+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , false , false , false ))
352361 // operation := mtaClient.StartMtaOperationArgsForCall(1)
353362 // expectProcessParameters(getProcessParameters(false), operation.Parameters)
354363 })
@@ -360,7 +369,7 @@ var _ = Describe("DeployCommand", func() {
360369 output , status := oc .CaptureOutputAndStatus (func () int {
361370 return command .Execute ([]string {mtaArchivePath , "-e" , extDescriptorPath }).ToInt ()
362371 })
363- ex .ExpectSuccessWithOutput (status , output , getOutputLines (true , false , false ))
372+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (true , false , false , false , false ))
364373 // operation := mtaClient.StartMtaOperationArgsForCall(1)
365374 // expectProcessParameters(getProcessParameters(false), operation.Parameters)
366375 })
@@ -372,7 +381,7 @@ var _ = Describe("DeployCommand", func() {
372381 output , status := oc .CaptureOutputAndStatus (func () int {
373382 return command .Execute ([]string {mtaArchivePath , "-f" , "-delete-services" , "-no-start" , "-keep-files" , "-do-not-fail-on-missing-permissions" }).ToInt ()
374383 })
375- ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , false ))
384+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , false , false , false ))
376385 // operation := mtaClient.StartMtaOperationArgsForCall(1)
377386 // expectProcessParameters(getProcessParameters(true), operation.Parameters)
378387 })
@@ -412,7 +421,7 @@ var _ = Describe("DeployCommand", func() {
412421 output , status := oc .CaptureOutputAndStatus (func () int {
413422 return command .Execute ([]string {mtaArchivePath }).ToInt ()
414423 })
415- ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , false ))
424+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , false , false , false ))
416425 // operation := mtaClient.StartMtaOperationArgsForCall(1)
417426 // expectProcessParameters(getProcessParameters(false), operation.Parameters)
418427 })
@@ -494,5 +503,101 @@ var _ = Describe("DeployCommand", func() {
494503 ex .ExpectSuccessWithOutput (status , output , getLinesForAbortingProcess ())
495504 })
496505 })
506+
507+ Context ("with --require-secure-parameters flag and a user-provided service instance which already exists" , func () {
508+ It ("should not create a new user-provided service" , func () {
509+ os .Setenv ("__MTA___fake-variable" , "fakeSecret" )
510+ defer os .Unsetenv ("__MTA___fake-variable" )
511+ command .FileUrlReader = newMockFileReader (correctMtaUrl )
512+
513+ 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\n another-service-instance managed fake-plan\n " , upsName )
517+ return []string {table }, nil
518+ }
519+ return []string {}, nil
520+ }
521+
522+ output , status := oc .CaptureOutputAndStatus (func () int {
523+ return command .Execute ([]string {"--require-secure-parameters" }).ToInt ()
524+ })
525+
526+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , true , true , false ))
527+ Expect (output ).To (ContainElement (ContainSubstring ("Using existing user-provided service" )))
528+ Expect (output ).To (ContainElement (ContainSubstring (upsName )))
529+
530+ callCount := mtaClient .StartMtaOperationCallCount ()
531+ Expect (callCount ).To (BeNumerically (">" , 0 ))
532+ operation := mtaClient .StartMtaOperationArgsForCall (callCount - 1 )
533+ Expect (operation .Parameters ["isSecurityEnabled" ]).To (Equal ("true" ))
534+ })
535+ })
536+
537+ Context ("with --require-secure-parameters flag and a user-provided service instance missing" , func () {
538+ It ("should create a new user-provided service using the appropriate cf command" , func () {
539+ os .Setenv ("__MTA___fake-variable" , "fakeSecret" )
540+ defer os .Unsetenv ("__MTA___fake-variable" )
541+ command .FileUrlReader = newMockFileReader (correctMtaUrl )
542+
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
555+ }
556+
557+ output , status := oc .CaptureOutputAndStatus (func () int {
558+ return command .Execute ([]string {"--require-secure-parameters" }).ToInt ()
559+ })
560+
561+ ex .ExpectSuccessWithOutput (status , output , getOutputLines (false , false , true , false , true ))
562+ Expect (output ).To (ContainElement (ContainSubstring ("Created user-provided service" )))
563+ Expect (output ).To (ContainElement (ContainSubstring ("__mta-secure-anatz" )))
564+
565+ callCount := mtaClient .StartMtaOperationCallCount ()
566+ Expect (callCount ).To (BeNumerically (">" , 0 ))
567+ operation := mtaClient .StartMtaOperationArgsForCall (callCount - 1 )
568+ Expect (operation .Parameters ["isSecurityEnabled" ]).To (Equal ("true" ))
569+ })
570+ })
571+
572+ Context ("with --require-secure-parameters and `cf services` fails" , func () {
573+ It ("should return an error from the UPS existence check" , func () {
574+ os .Setenv ("__MTA___fake-variable" , "fakeSecret" )
575+ defer os .Unsetenv ("__MTA___fake-variable" )
576+ command .FileUrlReader = newMockFileReader (correctMtaUrl )
577+
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
590+ }
591+
592+ output , status := oc .CaptureOutputAndStatus (func () int {
593+ return command .Execute ([]string {"--require-secure-parameters" }).ToInt ()
594+ })
595+
596+ ex .ExpectFailure (status , output , "" )
597+ Expect (output ).To (ContainElement (ContainSubstring ("Could not ensure user-provided service" )))
598+ Expect (mtaClient .StartMtaOperationCallCount ()).To (Equal (0 ))
599+ })
600+ })
601+
497602 })
498603})
0 commit comments