@@ -21,7 +21,7 @@ describe.sequential("CredentialProvider", () => {
2121 } ) ;
2222
2323 it ( "should return the auth config for a registry" , async ( ) => {
24- mockSpawnReturns (
24+ mockSpawnEmitsData (
2525 0 ,
2626 JSON . stringify ( {
2727 ServerURL : "registry" ,
@@ -40,7 +40,7 @@ describe.sequential("CredentialProvider", () => {
4040 } ) ;
4141
4242 it ( "should default to the registry url when the server url is not returned" , async ( ) => {
43- mockSpawnReturns (
43+ mockSpawnEmitsData (
4444 0 ,
4545 JSON . stringify ( {
4646 Username : "username" ,
@@ -61,8 +61,8 @@ describe.sequential("CredentialProvider", () => {
6161 expect ( await credentialProvider . getAuthConfig ( "registry" , containerRuntimeConfig ) ) . toBeUndefined ( ) ;
6262 } ) ;
6363
64- it ( "should throw when get credentials fails" , async ( ) => {
65- mockSpawnReturns (
64+ it ( "should return undefined when get credentials fails because we lookup optimistically " , async ( ) => {
65+ mockSpawnEmitsData (
6666 1 ,
6767 JSON . stringify ( {
6868 ServerURL : "registry" ,
@@ -71,21 +71,27 @@ describe.sequential("CredentialProvider", () => {
7171 } )
7272 ) ;
7373
74+ expect ( await credentialProvider . getAuthConfig ( "registry" , containerRuntimeConfig ) ) . toBeUndefined ( ) ;
75+ } ) ;
76+
77+ it ( "should throw when credential provider emits error" , async ( ) => {
78+ mockSpawnEmitsError ( "ERROR" ) ;
79+
7480 await expect ( ( ) => credentialProvider . getAuthConfig ( "registry" , containerRuntimeConfig ) ) . rejects . toThrow (
75- "An error occurred getting a credential "
81+ "Error from Docker credential provider: Error: ERROR "
7682 ) ;
7783 } ) ;
7884
7985 it ( "should throw when get credentials output cannot be parsed" , async ( ) => {
80- mockSpawnReturns ( 0 , "CANNOT_PARSE" ) ;
86+ mockSpawnEmitsData ( 0 , "CANNOT_PARSE" ) ;
8187
8288 await expect ( ( ) => credentialProvider . getAuthConfig ( "registry" , containerRuntimeConfig ) ) . rejects . toThrow (
8389 "Unexpected response from Docker credential provider GET command"
8490 ) ;
8591 } ) ;
8692} ) ;
8793
88- function mockSpawnReturns ( exitCode : number , stdout : string ) {
94+ function mockSpawnEmitsData ( exitCode : number , stdout : string ) {
8995 const sink = new EventEmitter ( ) as ChildProcess ;
9096
9197 sink . stdout = new Readable ( {
@@ -104,6 +110,21 @@ function mockSpawnReturns(exitCode: number, stdout: string) {
104110 mockSpawn . mockReturnValueOnce ( sink ) ;
105111}
106112
113+ function mockSpawnEmitsError ( message : string ) {
114+ const sink = new EventEmitter ( ) as ChildProcess ;
115+
116+ sink . kill = ( ) => true ;
117+ sink . stdout = new Readable ( { read ( ) { } } ) ;
118+ sink . stdin = new Writable ( {
119+ write ( _chunk , _enc , cb ) {
120+ sink . emit ( "error" , new Error ( message ) ) ;
121+ cb ?.( ) ;
122+ } ,
123+ } ) ;
124+
125+ mockSpawn . mockReturnValueOnce ( sink ) ;
126+ }
127+
107128class TestCredentialProvider extends CredentialProvider {
108129 constructor (
109130 private readonly name : string ,
0 commit comments