| 
									
										
										
										
											2020-10-19 21:17:06 +02:00
										 |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as path from 'path'; | 
					
						
							| 
									
										
										
										
											2020-08-23 03:31:38 +02:00
										 |  |  | import * as semver from 'semver'; | 
					
						
							| 
									
										
										
										
											2020-11-17 21:38:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 22:18:15 +02:00
										 |  |  | import * as buildx from '../src/buildx'; | 
					
						
							| 
									
										
										
										
											2020-10-19 21:17:06 +02:00
										 |  |  | import * as context from '../src/context'; | 
					
						
							| 
									
										
										
										
											2020-11-17 21:38:45 +01:00
										 |  |  | import * as docker from '../src/docker'; | 
					
						
							| 
									
										
										
										
											2020-08-17 22:18:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 12:21:44 -04:00
										 |  |  | const tmpNameSync = path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep); | 
					
						
							| 
									
										
										
										
											2020-08-17 22:18:15 +02:00
										 |  |  | const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-19 21:17:06 +02:00
										 |  |  | jest.spyOn(context, 'tmpDir').mockImplementation((): string => { | 
					
						
							|  |  |  |   const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep); | 
					
						
							|  |  |  |   if (!fs.existsSync(tmpDir)) { | 
					
						
							|  |  |  |     fs.mkdirSync(tmpDir, {recursive: true}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return tmpDir; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { | 
					
						
							| 
									
										
										
										
											2020-10-23 12:21:44 -04:00
										 |  |  |   return tmpNameSync; | 
					
						
							| 
									
										
										
										
											2020-10-19 21:17:06 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 22:18:15 +02:00
										 |  |  | describe('getImageID', () => { | 
					
						
							|  |  |  |   it('matches', async () => { | 
					
						
							|  |  |  |     const imageIDFile = await buildx.getImageIDFile(); | 
					
						
							|  |  |  |     console.log(`imageIDFile: ${imageIDFile}`); | 
					
						
							|  |  |  |     await fs.writeFileSync(imageIDFile, digest); | 
					
						
							|  |  |  |     const imageID = await buildx.getImageID(); | 
					
						
							|  |  |  |     console.log(`imageID: ${imageID}`); | 
					
						
							|  |  |  |     expect(imageID).toEqual(digest); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-23 03:31:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 15:18:02 +02:00
										 |  |  | describe('isLocalOrTarExporter', () => { | 
					
						
							|  |  |  |   // prettier-ignore
 | 
					
						
							|  |  |  |   test.each([ | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         'type=registry,ref=user/app', | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         'type=docker', | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         'type=local,dest=./release-out' | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         'type=tar,dest=/tmp/image.tar' | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         'type=docker', | 
					
						
							|  |  |  |         'type=tar,dest=/tmp/image.tar' | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         '"type=tar","dest=/tmp/image.tar"' | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         '" type= local" , dest=./release-out' | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         '.' | 
					
						
							|  |  |  |       ], | 
					
						
							| 
									
										
										
										
											2020-10-20 17:53:03 +02:00
										 |  |  |       true | 
					
						
							| 
									
										
										
										
											2020-10-20 15:18:02 +02:00
										 |  |  |     ], | 
					
						
							|  |  |  |   ])( | 
					
						
							|  |  |  |     'given %p returns %p', | 
					
						
							|  |  |  |     async (outputs: Array<string>, expected: boolean) => { | 
					
						
							|  |  |  |       expect(buildx.isLocalOrTarExporter(outputs)).toEqual(expected); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 03:31:38 +02:00
										 |  |  | describe('getVersion', () => { | 
					
						
							| 
									
										
										
										
											2020-10-23 21:58:59 +02:00
										 |  |  |   async function isDaemonRunning() { | 
					
						
							|  |  |  |     return await docker.isDaemonRunning(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   (isDaemonRunning() ? it : it.skip)( | 
					
						
							|  |  |  |     'valid', | 
					
						
							|  |  |  |     async () => { | 
					
						
							|  |  |  |       const version = await buildx.getVersion(); | 
					
						
							|  |  |  |       console.log(`version: ${version}`); | 
					
						
							|  |  |  |       expect(semver.valid(version)).not.toBeNull(); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     100000 | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2020-08-23 03:31:38 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('parseVersion', () => { | 
					
						
							|  |  |  |   test.each([ | 
					
						
							| 
									
										
										
										
											2020-08-23 04:01:20 +02:00
										 |  |  |     ['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'], | 
					
						
							| 
									
										
										
										
											2020-08-23 03:31:38 +02:00
										 |  |  |     ['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'], | 
					
						
							|  |  |  |     ['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2'] | 
					
						
							|  |  |  |   ])('given %p', async (stdout, expected) => { | 
					
						
							|  |  |  |     expect(await buildx.parseVersion(stdout)).toEqual(expected); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-10-23 12:21:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('getSecret', () => { | 
					
						
							| 
									
										
										
										
											2020-10-23 22:51:59 +02:00
										 |  |  |   test.each([ | 
					
						
							| 
									
										
										
										
											2021-02-15 10:08:19 +01:00
										 |  |  |     ['A_SECRET=abcdef0123456789', false, 'A_SECRET', 'abcdef0123456789', false], | 
					
						
							|  |  |  |     ['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789', false, 'GIT_AUTH_TOKEN', 'abcdefghijklmno=0123456789', false], | 
					
						
							|  |  |  |     ['MY_KEY=c3RyaW5nLXdpdGgtZXF1YWxzCg==', false, 'MY_KEY', 'c3RyaW5nLXdpdGgtZXF1YWxzCg==', false], | 
					
						
							|  |  |  |     ['aaaaaaaa', false, '', '', true], | 
					
						
							|  |  |  |     ['aaaaaaaa=', false, '', '', true], | 
					
						
							|  |  |  |     ['=bbbbbbb', false, '', '', true], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       `foo=${path.join(__dirname, 'fixtures', 'secret.txt').split(path.sep).join(path.posix.sep)}`, | 
					
						
							|  |  |  |       true, | 
					
						
							|  |  |  |       'foo', | 
					
						
							|  |  |  |       'bar', | 
					
						
							|  |  |  |       false | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [`notfound=secret`, true, '', '', true] | 
					
						
							|  |  |  |   ])('given %p key and %p secret', async (kvp, file, exKey, exValue, invalid) => { | 
					
						
							| 
									
										
										
										
											2020-11-17 21:38:45 +01:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2021-02-15 10:08:19 +01:00
										 |  |  |       let secret: string; | 
					
						
							|  |  |  |       if (file) { | 
					
						
							|  |  |  |         secret = await buildx.getSecretFile(kvp); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         secret = await buildx.getSecretString(kvp); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-11-17 21:38:45 +01:00
										 |  |  |       expect(true).toBe(!invalid); | 
					
						
							| 
									
										
										
										
											2021-02-15 10:08:19 +01:00
										 |  |  |       console.log(`secret: ${secret}`); | 
					
						
							|  |  |  |       expect(secret).toEqual(`id=${exKey},src=${tmpNameSync}`); | 
					
						
							|  |  |  |       const secretValue = await fs.readFileSync(tmpNameSync, 'utf-8'); | 
					
						
							|  |  |  |       console.log(`secretValue: ${secretValue}`); | 
					
						
							|  |  |  |       expect(secretValue).toEqual(exValue); | 
					
						
							| 
									
										
										
										
											2020-11-17 21:38:45 +01:00
										 |  |  |     } catch (err) { | 
					
						
							|  |  |  |       expect(true).toBe(invalid); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-23 12:21:44 -04:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |