@@ -17,13 +17,14 @@ package cmd
1717import (
1818 "encoding/json"
1919 "fmt"
20- "github.com/stretchr/testify/assert"
2120 "os"
2221 "path"
2322 "path/filepath"
2423 "strconv"
2524 "strings"
2625 "testing"
26+
27+ "github.com/stretchr/testify/assert"
2728)
2829
2930func Test_PAMHelpCmd (t * testing.T ) {
@@ -70,10 +71,12 @@ func Test_PAMTypesListCmd(t *testing.T) {
7071 // test
7172 var err error
7273 testCmd .SetArgs ([]string {"pam" , "types-list" })
73- output := captureOutput (func () {
74- err = testCmd .Execute ()
75- assert .NoError (t , err )
76- })
74+ output := captureOutput (
75+ func () {
76+ err = testCmd .Execute ()
77+ assert .NoError (t , err )
78+ },
79+ )
7780
7881 if err != nil {
7982 t .Errorf ("failed to list PAM provider types: %v" , err )
@@ -157,10 +160,12 @@ func Test_PAMGetCmd(t *testing.T) {
157160 idInt := int (providerConfig ["Id" ].(float64 ))
158161 idStr := strconv .Itoa (idInt )
159162 testCmd .SetArgs ([]string {"pam" , "get" , "--id" , idStr })
160- output := captureOutput (func () {
161- err := testCmd .Execute ()
162- assert .NoError (t , err )
163- })
163+ output := captureOutput (
164+ func () {
165+ err := testCmd .Execute ()
166+ assert .NoError (t , err )
167+ },
168+ )
164169 var pamProvider interface {}
165170 if err := json .Unmarshal ([]byte (output ), & pamProvider ); err != nil {
166171 t .Fatalf ("Error unmarshalling JSON: %v" , err )
@@ -184,10 +189,12 @@ func Test_PAMTypesCreateCmd(t *testing.T) {
184189 randomName := generateRandomUUID ()
185190 t .Logf ("randomName: %s" , randomName )
186191 testCmd .SetArgs ([]string {"pam" , "types-create" , "--repo" , "hashicorp-vault-pam" , "--name" , randomName })
187- output := captureOutput (func () {
188- err := testCmd .Execute ()
189- assert .NoError (t , err )
190- })
192+ output := captureOutput (
193+ func () {
194+ err := testCmd .Execute ()
195+ assert .NoError (t , err )
196+ },
197+ )
191198 var createResponse interface {}
192199 if err := json .Unmarshal ([]byte (output ), & createResponse ); err != nil {
193200 t .Log (output )
@@ -291,10 +298,12 @@ func Test_PAMUpdateCmd(t *testing.T) {
291298 testCmd := RootCmd
292299 // test
293300 testCmd .SetArgs ([]string {"pam" , "update" , "--from-file" , updatedFileName })
294- output := captureOutput (func () {
295- err := testCmd .Execute ()
296- assert .NoError (t , err )
297- })
301+ output := captureOutput (
302+ func () {
303+ err := testCmd .Execute ()
304+ assert .NoError (t , err )
305+ },
306+ )
298307
299308 var updateResponse interface {}
300309 if err := json .Unmarshal ([]byte (output ), & updateResponse ); err != nil {
@@ -386,51 +395,59 @@ func testListPamProviders(t *testing.T) ([]interface{}, error) {
386395 var pamProviders []interface {}
387396 var err error
388397
389- t .Run ("Listing PAM provider instances" , func (t * testing.T ) {
390- testCmd := RootCmd
391- // test
392- testCmd .SetArgs ([]string {"pam" , "list" })
393- output = captureOutput (func () {
394- err = testCmd .Execute ()
395- assert .NoError (t , err )
396- })
397-
398- if err != nil {
399- t .Errorf ("failed to list PAM providers: %v" , err )
400- return
401- }
402-
403- if err = json .Unmarshal ([]byte (output ), & pamProviders ); err != nil {
404- t .Fatalf ("Error unmarshalling JSON: %v" , err )
405- }
398+ t .Run (
399+ "Listing PAM provider instances" , func (t * testing.T ) {
400+ testCmd := RootCmd
401+ // test
402+ testCmd .SetArgs ([]string {"pam" , "list" })
403+ output = captureOutput (
404+ func () {
405+ err = testCmd .Execute ()
406+ assert .NoError (t , err )
407+ },
408+ )
409+
410+ if err != nil {
411+ t .Errorf ("failed to list PAM providers: %v" , err )
412+ return
413+ }
406414
407- // assert slice is len >= 0
408- assert .GreaterOrEqual (t , len (pamProviders ), 0 )
415+ if err = json .Unmarshal ([]byte (output ), & pamProviders ); err != nil {
416+ t .Fatalf ("Error unmarshalling JSON: %v" , err )
417+ }
409418
410- if len (pamProviders ) > 0 {
411- for _ , p := range pamProviders {
412- providerConfig := p .(map [string ]interface {})
413- // assert that each p has a name
414- assert .NotEmpty (t , providerConfig ["Name" ])
415- // assert that each p has an ID
416- assert .NotEmpty (t , providerConfig ["Id" ])
417- // assert that each p has a type
418- assert .NotEmpty (t , providerConfig ["ProviderType" ])
419-
420- // Check params is a list of maps
421- pTypeParams := providerConfig ["ProviderType" ].(map [string ]interface {})["ProviderTypeParams" ].([]interface {})
422- assert .NotEmpty (t , pTypeParams )
423- assert .GreaterOrEqual (t , len (pTypeParams ), 0 )
424- if len (pTypeParams ) > 0 {
425- for _ , param := range pTypeParams {
426- assert .NotEmpty (t , param .(map [string ]interface {})["Id" ])
427- assert .NotEmpty (t , param .(map [string ]interface {})["Name" ])
428- assert .NotEmpty (t , param .(map [string ]interface {})["DataType" ])
419+ // assert slice is len >= 0
420+ assert .GreaterOrEqual (t , len (pamProviders ), 0 )
421+
422+ if len (pamProviders ) > 0 {
423+ for _ , p := range pamProviders {
424+ providerConfig := p .(map [string ]interface {})
425+ // assert that each p has a name
426+ assert .NotEmpty (t , providerConfig ["Name" ])
427+ // assert that each p has an ID
428+ assert .NotEmpty (t , providerConfig ["Id" ])
429+ // assert that each p has a type
430+ assert .NotEmpty (t , providerConfig ["ProviderType" ])
431+
432+ // Check params is a list of maps
433+ pTypeParams := providerConfig ["ProviderType" ].(map [string ]interface {})["ProviderTypeParams" ].([]interface {})
434+ assert .NotEmpty (t , pTypeParams )
435+ assert .GreaterOrEqual (t , len (pTypeParams ), 0 )
436+ if len (pTypeParams ) > 0 {
437+ for _ , param := range pTypeParams {
438+ assert .NotEmpty (t , param .(map [string ]interface {})["Id" ])
439+ assert .NotEmpty (t , param .(map [string ]interface {})["Name" ])
440+ assert .NotEmpty (t , param .(map [string ]interface {})["DataType" ])
441+ }
429442 }
430443 }
444+ } else {
445+ t .Errorf ("0 PAM providers found, cannot test list" )
446+ t .Fail ()
431447 }
432- }
433- })
448+
449+ },
450+ )
434451 if err != nil {
435452 t .Log (output )
436453 return nil , err
@@ -447,57 +464,65 @@ func testCreatePamProvider(t *testing.T, fileName string, providerName string, a
447464 } else {
448465 testName = fmt .Sprintf ("Create PAM provider '%s'" , providerName )
449466 }
450- t .Run (testName , func (t * testing.T ) {
451- testCmd := RootCmd
467+ t .Run (
468+ testName , func (t * testing.T ) {
469+ testCmd := RootCmd
470+
471+ args := []string {"pam" , "create" , "--from-file" , fileName }
472+ // log the args as a string
473+ t .Logf ("args: %s" , args )
474+ testCmd .SetArgs (args )
475+ t .Logf ("fileName: %s" , fileName )
476+ output := captureOutput (
477+ func () {
478+ err = testCmd .Execute ()
479+ if ! allowFail {
480+ assert .NoError (t , err )
481+ }
482+ },
483+ )
484+ if err = json .Unmarshal ([]byte (output ), & createResponse ); err != nil {
485+ if allowFail {
486+ t .Logf ("Error unmarshalling JSON: %v" , err )
487+ } else {
488+ t .Errorf ("failed to create a PAM provider: %v" , err )
489+ }
490+ return
491+ }
452492
453- args := []string {"pam" , "create" , "--from-file" , fileName }
454- // log the args as a string
455- t .Logf ("args: %s" , args )
456- testCmd .SetArgs (args )
457- t .Logf ("fileName: %s" , fileName )
458- output := captureOutput (func () {
459- err = testCmd .Execute ()
460493 if ! allowFail {
461- assert .NoError (t , err )
494+ assert .NotEmpty (t , createResponse .(map [string ]interface {})["Id" ])
495+ assert .NotEmpty (t , createResponse .(map [string ]interface {})["Name" ])
496+ assert .Equal (t , createResponse .(map [string ]interface {})["Name" ], providerName )
497+ assert .NotEmpty (t , createResponse .(map [string ]interface {})["ProviderType" ])
462498 }
463- })
464- if err = json .Unmarshal ([]byte (output ), & createResponse ); err != nil {
465- if allowFail {
466- t .Logf ("Error unmarshalling JSON: %v" , err )
467- } else {
468- t .Errorf ("failed to create a PAM provider: %v" , err )
469- }
470- return
471- }
472-
473- if ! allowFail {
474- assert .NotEmpty (t , createResponse .(map [string ]interface {})["Id" ])
475- assert .NotEmpty (t , createResponse .(map [string ]interface {})["Name" ])
476- assert .Equal (t , createResponse .(map [string ]interface {})["Name" ], providerName )
477- assert .NotEmpty (t , createResponse .(map [string ]interface {})["ProviderType" ])
478- }
479- })
499+ },
500+ )
480501
481502 return createResponse , err
482503}
483504
484505func testDeletePamProvider (t * testing.T , pID int , allowFail bool ) error {
485506 var err error
486507 var output string
487- t .Run (fmt .Sprintf ("Deleting PAM provider %d" , pID ), func (t * testing.T ) {
488- testCmd := RootCmd
489-
490- testCmd .SetArgs ([]string {"pam" , "delete" , "--id" , strconv .Itoa (pID )})
491- output = captureOutput (func () {
492- err = testCmd .Execute ()
508+ t .Run (
509+ fmt .Sprintf ("Deleting PAM provider %d" , pID ), func (t * testing.T ) {
510+ testCmd := RootCmd
511+
512+ testCmd .SetArgs ([]string {"pam" , "delete" , "--id" , strconv .Itoa (pID )})
513+ output = captureOutput (
514+ func () {
515+ err = testCmd .Execute ()
516+ if ! allowFail {
517+ assert .NoError (t , err )
518+ }
519+ },
520+ )
493521 if ! allowFail {
494- assert .NoError (t , err )
522+ assert .Contains (t , output , fmt . Sprintf ( "Deleted PAM provider with ID %d" , pID ) )
495523 }
496- })
497- if ! allowFail {
498- assert .Contains (t , output , fmt .Sprintf ("Deleted PAM provider with ID %d" , pID ))
499- }
500- })
524+ },
525+ )
501526 if err != nil && ! allowFail {
502527 t .Log (output )
503528 return err
@@ -513,12 +538,14 @@ func testListPamProviderTypes(t *testing.T, name string, allowFail bool, allowEm
513538 testCmd := RootCmd
514539 // test
515540 testCmd .SetArgs ([]string {"pam" , "types-list" })
516- output = captureOutput (func () {
517- err = testCmd .Execute ()
518- if ! allowFail {
519- assert .NoError (t , err )
520- }
521- })
541+ output = captureOutput (
542+ func () {
543+ err = testCmd .Execute ()
544+ if ! allowFail {
545+ assert .NoError (t , err )
546+ }
547+ },
548+ )
522549 var pTypes []interface {}
523550 if err = json .Unmarshal ([]byte (output ), & pTypes ); err != nil && ! allowFail {
524551 t .Errorf ("Error unmarshalling JSON: %v" , err )
@@ -632,7 +659,11 @@ func testFormatPamCreateConfig(t *testing.T, inputFileName string, providerName
632659 if oErr == nil {
633660 oErr = fmt .Errorf ("failed to find PAM provider type '%s' unable to create PAM provider" , cProviderTypeName )
634661 } else {
635- oErr = fmt .Errorf ("failed to find PAM provider type '%s' unable to create PAM provider: %v" , cProviderTypeName , oErr )
662+ oErr = fmt .Errorf (
663+ "failed to find PAM provider type '%s' unable to create PAM provider: %v" ,
664+ cProviderTypeName ,
665+ oErr ,
666+ )
636667 }
637668 t .Error (oErr )
638669 return "" , oErr
0 commit comments