mirror of
				https://github.com/docker/login-action.git
				synced 2025-10-31 10:10:09 +08:00 
			
		
		
		
	add tests for retries
Signed-off-by: Fedor Dikarev <fedor.dikarev@gmail.com>
This commit is contained in:
		
							parent
							
								
									fd11be1ef0
								
							
						
					
					
						commit
						c1f8f4fdcd
					
				
							
								
								
									
										61
									
								
								__tests__/retries_fail.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								__tests__/retries_fail.test.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,61 @@ | |||||||
|  | import { expect, jest, test, } from '@jest/globals'; | ||||||
|  | import * as path from 'path'; | ||||||
|  | 
 | ||||||
|  | // import * as dockerModule from '../src/docker';
 | ||||||
|  | 
 | ||||||
|  | import { login } from '../src/docker'; | ||||||
|  | import { Docker } from '@docker/actions-toolkit/lib/docker/docker'; | ||||||
|  | 
 | ||||||
|  | test('login retries function', async () => { | ||||||
|  |   let stderr_strings: string[] = [] | ||||||
|  |   let call_count: number = -1 | ||||||
|  | 
 | ||||||
|  |   // const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
 | ||||||
|  |   Docker.getExecOutput = jest.fn(async () => { | ||||||
|  |     call_count++ | ||||||
|  |     console.log(`Mock: ${call_count}, ${stderr_strings}`) | ||||||
|  |     if (call_count >= stderr_strings.length) { | ||||||
|  |       return { | ||||||
|  |         exitCode: 0, | ||||||
|  |         stdout: 'Mock success', | ||||||
|  |         stderr: '' | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return { | ||||||
|  |       exitCode: 1, | ||||||
|  |       stdout: '', | ||||||
|  |       stderr: stderr_strings[(call_count) % stderr_strings.length] | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | 
 | ||||||
|  |   const username = 'dbowie'; | ||||||
|  |   const password = 'groundcontrol'; | ||||||
|  |   const registry = 'https://ghcr.io'; | ||||||
|  | 
 | ||||||
|  |   stderr_strings = [ | ||||||
|  |     'mock error, failed with status: 408 Request Timeout', | ||||||
|  |     'mock error, failed with status: 502 Request Timeout', | ||||||
|  |     'mock error, failed with status: 400 Request Timeout', | ||||||
|  |   ] | ||||||
|  |   call_count = -1 | ||||||
|  |   await expect(async () => { | ||||||
|  |     await login(registry, username, password, 'false', ['408', '400'], 5, 0.1); | ||||||
|  |   }) | ||||||
|  |     .rejects | ||||||
|  |     .toThrow("mock error, failed with status: 502 Request Timeout"); | ||||||
|  |   expect(Docker.getExecOutput).toHaveBeenCalledTimes(2); | ||||||
|  | 
 | ||||||
|  |   stderr_strings = [ | ||||||
|  |     'not matching error', | ||||||
|  |     'mock error, failed with status: 502 Request Timeout', | ||||||
|  |     'mock error, failed with status: 400 Request Timeout', | ||||||
|  |   ] | ||||||
|  |   call_count = -1 | ||||||
|  |   await expect(async () => { | ||||||
|  |     await login(registry, username, password, 'false', ['408', '400'], 5, 0.1); | ||||||
|  |   }) | ||||||
|  |     .rejects | ||||||
|  |     .toThrow('not matching error'); | ||||||
|  |   expect(Docker.getExecOutput).toHaveBeenCalledTimes(2 + 1); | ||||||
|  | 
 | ||||||
|  | }); | ||||||
							
								
								
									
										58
									
								
								__tests__/retries_success.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								__tests__/retries_success.test.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,58 @@ | |||||||
|  | import { expect, jest, test, } from '@jest/globals'; | ||||||
|  | import * as path from 'path'; | ||||||
|  | 
 | ||||||
|  | // import * as dockerModule from '../src/docker';
 | ||||||
|  | 
 | ||||||
|  | import { login } from '../src/docker'; | ||||||
|  | import { Docker } from '@docker/actions-toolkit/lib/docker/docker'; | ||||||
|  | 
 | ||||||
|  | test('login retries success function', async () => { | ||||||
|  |   // let stderr_strings: string[] = []
 | ||||||
|  |   let stderr_strings: string[] = [ | ||||||
|  |     'mock error, failed with status: 408 Request Timeout', | ||||||
|  |     'mock error, failed with status: 502 Request Timeout', | ||||||
|  |     'mock error, failed with status: 400 Request Timeout', | ||||||
|  |   ] | ||||||
|  |   let call_count: number = -1 | ||||||
|  | 
 | ||||||
|  |   Docker.getExecOutput = jest.fn(async () => { | ||||||
|  |     call_count++ | ||||||
|  |     console.log(`Mock: ${call_count}, ${stderr_strings}`) | ||||||
|  |     if (call_count >= stderr_strings.length) { | ||||||
|  |       return { | ||||||
|  |         exitCode: 0, | ||||||
|  |         stdout: 'Mock success', | ||||||
|  |         stderr: '' | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return { | ||||||
|  |       exitCode: 1, | ||||||
|  |       stdout: '', | ||||||
|  |       stderr: stderr_strings[(call_count) % stderr_strings.length] | ||||||
|  |     } | ||||||
|  |   }) | ||||||
|  | 
 | ||||||
|  |   const username = 'dbowie'; | ||||||
|  |   const password = 'groundcontrol'; | ||||||
|  |   const registry = 'https://ghcr.io'; | ||||||
|  | 
 | ||||||
|  |   // stderr_strings = []
 | ||||||
|  |   call_count = -1 | ||||||
|  |   expect(async () => { | ||||||
|  |     await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1); | ||||||
|  |   }) | ||||||
|  |     .resolves; | ||||||
|  |   expect(Docker.getExecOutput).toHaveBeenCalledTimes(1); | ||||||
|  | 
 | ||||||
|  |   stderr_strings = [ | ||||||
|  |     'mock error, failed with status: 408 Request Timeout', | ||||||
|  |     'mock error, failed with status: 502 Request Timeout', | ||||||
|  |     'mock error, failed with status: 400 Request Timeout', | ||||||
|  |   ] | ||||||
|  |   call_count = -1 | ||||||
|  |   expect(async () => { | ||||||
|  |     await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1); | ||||||
|  |   }) | ||||||
|  |     .resolves; | ||||||
|  |   expect(Docker.getExecOutput).toHaveBeenCalledTimes(0); | ||||||
|  | }); | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Fedor Dikarev
						Fedor Dikarev