24
24
import com .synopsys .integration .blackduck .imageinspector .image .common .ImageLayerApplier ;
25
25
import com .synopsys .integration .blackduck .imageinspector .linux .TarOperations ;
26
26
import org .apache .commons .lang3 .StringUtils ;
27
- import org .junit .jupiter .api .BeforeAll ;
28
- import org .junit .jupiter .api .Disabled ;
29
- import org .junit .jupiter .api .Tag ;
30
- import org .junit .jupiter .api .Test ;
27
+ import org .junit .jupiter .api .*;
31
28
import org .junit .jupiter .api .io .TempDir ;
32
29
import org .mockito .Mockito ;
33
30
@@ -52,30 +49,38 @@ public class ImageInspectorApiIntTest {
52
49
private static final String SIMPLE_IMAGE_TARFILE = "build/images/test/alpine.tar" ;
53
50
private static final String MULTILAYER_IMAGE_TARFILE = "build/images/test/centos_minus_vim_plus_bacula.tar" ;
54
51
private static final String NOPKGMGR_IMAGE_TARFILE = "build/images/test/nopkgmgr.tar" ;
55
- // TODO need to build this:
56
- private static final String OCI_IMAGE_TARFILE = "/tmp/ccc/centos_minus_vim_plus_bacula-oci.tar" ;
52
+ private static final String OCI_IMAGE_TARFILE = "src/test/resources/oci/u_multi_tagged_gutted.tar" ;
57
53
58
- private static Os os ;
59
- private static ImageInspectorApi imageInspectorApi ;
60
- private static List <PkgMgr > pkgMgrs ;
61
- private static PackageGetter packageGetter ;
62
- private static RpmPkgMgr rpmPkgMgr ;
54
+ private Os os ;
55
+ private ImageInspectorApi imageInspectorApi ;
56
+ private List <PkgMgr > pkgMgrs ;
57
+ private PackageGetter packageGetter ;
58
+ private RpmPkgMgr rpmPkgMgr ;
59
+ private DpkgPkgMgr dpkgPkgMgr ;
63
60
64
61
private static final String [] apkOutput = { "ca-certificates-20171114-r0" , "boost-unit_test_framework-1.62.0-r5" };
65
62
66
63
@ TempDir
67
64
File tempDir ;
68
65
69
- @ BeforeAll
70
- public static void setup () throws IntegrationException , InterruptedException {
66
+ @ BeforeEach
67
+ public void setup () throws IntegrationException , InterruptedException {
68
+ dpkgPkgMgr = Mockito .mock (DpkgPkgMgr .class );
69
+ List <ComponentDetails > ubuntuComps = new ArrayList <>();
70
+ ubuntuComps .add (new ComponentDetails ("comp0" , "version0" , "testExternalId0" , "testArch" , "ubuntu" ));
71
+ Mockito .when (dpkgPkgMgr .extractComponentsFromPkgMgrOutput (Mockito .any (File .class ), Mockito .anyString (), Mockito .any (String [].class ))).thenReturn (ubuntuComps );
72
+ Mockito .when (dpkgPkgMgr .getType ()).thenReturn (PackageManagerEnum .DPKG );
73
+ Mockito .when (dpkgPkgMgr .getImagePackageManagerDirectory (Mockito .any (File .class ))).thenReturn (new File ("." ));
74
+
71
75
rpmPkgMgr = Mockito .mock (RpmPkgMgr .class );
72
- List <ComponentDetails > comps = new ArrayList <>();
73
- comps .add (new ComponentDetails ("comp0" , "version0" , "testExternalId0" , "testArch" , "centos" ));
74
- Mockito .when (rpmPkgMgr .extractComponentsFromPkgMgrOutput (Mockito .any (File .class ), Mockito .anyString (), Mockito .any (String [].class ))).thenReturn (comps );
76
+ List <ComponentDetails > centosComps = new ArrayList <>();
77
+ centosComps .add (new ComponentDetails ("comp0" , "version0" , "testExternalId0" , "testArch" , "centos" ));
78
+ Mockito .when (rpmPkgMgr .extractComponentsFromPkgMgrOutput (Mockito .any (File .class ), Mockito .anyString (), Mockito .any (String [].class ))).thenReturn (centosComps );
79
+
75
80
FileOperations fileOperations = new FileOperations ();
76
81
pkgMgrs = new ArrayList <>(3 );
77
82
pkgMgrs .add (new ApkPkgMgr (fileOperations ));
78
- pkgMgrs .add (new DpkgPkgMgr ( fileOperations ) );
83
+ pkgMgrs .add (dpkgPkgMgr );
79
84
pkgMgrs .add (rpmPkgMgr );
80
85
os = Mockito .mock (Os .class );
81
86
@@ -155,54 +160,6 @@ public void testOnNoPkgMgrImage() throws IntegrationException, InterruptedExcept
155
160
assertTrue (containerFileSystemFile .length () < 1000 );
156
161
}
157
162
158
- @ Test
159
- public void testOnRightOs () throws IntegrationException , InterruptedException {
160
- doTest (null );
161
- }
162
-
163
- @ Test
164
- public void testOnRightOsDistroOverride () throws IntegrationException , InterruptedException {
165
- doTest ("overriddendistroname" );
166
- }
167
-
168
- private void doTest (String targetLinuxDistroOverride ) throws IntegrationException , InterruptedException {
169
- ComponentHierarchyBuilder componentHierarchyBuilder = new ComponentHierarchyBuilder (packageGetter );
170
- String expectedForgeName ;
171
- if (StringUtils .isNotBlank (targetLinuxDistroOverride )) {
172
- expectedForgeName = "@" + targetLinuxDistroOverride ;
173
- } else {
174
- expectedForgeName = "@alpine" ;
175
- }
176
- Mockito .when (os .getLinuxDistroNameFromEtcDir (Mockito .any (File .class ))).thenReturn (Optional .of ("alpine" ));
177
- Mockito .when (os .deriveOs (Mockito .any (String .class ))).thenReturn (ImageInspectorOsEnum .ALPINE );
178
- ImageInspectionRequest imageInspectionRequest = (new ImageInspectionRequestBuilder ())
179
- .setDockerTarfilePath (SIMPLE_IMAGE_TARFILE )
180
- .setBlackDuckProjectName (PROJECT )
181
- .setBlackDuckProjectVersion (PROJECT_VERSION )
182
- .setCurrentLinuxDistro ("ALPINE" )
183
- .setTargetLinuxDistroOverride (targetLinuxDistroOverride )
184
- .setCleanupWorkingDir (true )
185
- .build ();
186
- SimpleBdioDocument bdioDocument = imageInspectorApi .getBdio (componentHierarchyBuilder , imageInspectionRequest );
187
- System .out .printf ("bdioDocument: %s\n " , bdioDocument );
188
- assertEquals (PROJECT , bdioDocument .getProject ().name );
189
- assertEquals (PROJECT_VERSION , bdioDocument .getProject ().version );
190
- assertEquals (apkOutput .length , bdioDocument .getComponents ().size ());
191
- for (BdioComponent comp : bdioDocument .getComponents ()) {
192
- System .out .printf ("comp: %s:%s:%s\n " , comp .name , comp .version , comp .bdioExternalIdentifier .externalId );
193
- if (comp .name .equals ("boost-unit_test_framework" )) {
194
- assertEquals ("1.62.0-r5" , comp .version );
195
- assertEquals ("boost-unit_test_framework/1.62.0-r5/x86_64" , comp .bdioExternalIdentifier .externalId );
196
- assertEquals (expectedForgeName , comp .bdioExternalIdentifier .forge );
197
- } else {
198
- assertEquals ("ca-certificates" , comp .name );
199
- assertEquals ("20171114-r0" , comp .version );
200
- assertEquals ("ca-certificates/20171114-r0/x86_64" , comp .bdioExternalIdentifier .externalId );
201
- assertEquals (expectedForgeName , comp .bdioExternalIdentifier .forge );
202
- }
203
- }
204
- }
205
-
206
163
@ Test
207
164
public void testAppOnlyFileSystem () throws IntegrationException , IOException , InterruptedException {
208
165
ComponentHierarchyBuilder componentHierarchyBuilder = new ComponentHierarchyBuilder (packageGetter );
@@ -231,32 +188,103 @@ public void testAppOnlyFileSystem() throws IntegrationException, IOException, In
231
188
assertTrue (containerFileSystemFile .length () < 80000000 );
232
189
}
233
190
234
- @ Disabled
235
191
@ Test
236
- public void testOciImage () throws IntegrationException , IOException , InterruptedException {
192
+ public void testOciImageRepoTagNotSpecified () throws IntegrationException , IOException , InterruptedException {
193
+
237
194
ComponentHierarchyBuilder componentHierarchyBuilder = new ComponentHierarchyBuilder (packageGetter );
238
- Mockito .when (os .getLinuxDistroNameFromEtcDir (Mockito .any (File .class ))).thenReturn (Optional .of ("centos" ));
239
- Mockito .when (os .deriveOs (Mockito .any (String .class ))).thenReturn (ImageInspectorOsEnum .CENTOS );
240
- Mockito .when (rpmPkgMgr .isApplicable (Mockito .any (File .class ))).thenReturn (true );
241
- Mockito .when (rpmPkgMgr .getType ()).thenReturn (PackageManagerEnum .RPM );
242
- Mockito .when (rpmPkgMgr .getImagePackageManagerDirectory (Mockito .any (File .class ))).thenReturn (new File ("." ));
195
+ Mockito .when (os .getLinuxDistroNameFromEtcDir (Mockito .any (File .class ))).thenReturn (Optional .of ("ubuntu" ));
196
+ Mockito .when (os .deriveOs (Mockito .any (String .class ))).thenReturn (ImageInspectorOsEnum .UBUNTU );
197
+ Mockito .when (dpkgPkgMgr .isApplicable (Mockito .any (File .class ))).thenReturn (true );
198
+
199
+ File destinationFile = new File (tempDir , "out.tar.gz" );
200
+ String containerFileSystemOutputFilePath = destinationFile .getAbsolutePath ();
201
+ ImageInspectionRequest imageInspectionRequest = (new ImageInspectionRequestBuilder ())
202
+ .setDockerTarfilePath (OCI_IMAGE_TARFILE )
203
+ .setContainerFileSystemOutputPath (containerFileSystemOutputFilePath )
204
+ .setCurrentLinuxDistro ("UBUNTU" )
205
+ .setCleanupWorkingDir (true )
206
+ .build ();
207
+ SimpleBdioDocument bdioDocument ;
208
+ try {
209
+ bdioDocument = imageInspectorApi .getBdio (componentHierarchyBuilder , imageInspectionRequest );
210
+ fail ("Expected exception" );
211
+ } catch (IntegrationException e ) {
212
+ assertTrue (e .getMessage ().contains ("multiple manifests" ));
213
+ }
214
+ }
215
+
216
+
217
+ @ Test
218
+ public void testOciImageRepoTagSpecified () throws IntegrationException , IOException , InterruptedException {
219
+
220
+ ComponentHierarchyBuilder componentHierarchyBuilder = new ComponentHierarchyBuilder (packageGetter );
221
+ Mockito .when (os .getLinuxDistroNameFromEtcDir (Mockito .any (File .class ))).thenReturn (Optional .of ("ubuntu" ));
222
+ Mockito .when (os .deriveOs (Mockito .any (String .class ))).thenReturn (ImageInspectorOsEnum .UBUNTU );
223
+ Mockito .when (dpkgPkgMgr .isApplicable (Mockito .any (File .class ))).thenReturn (true );
243
224
244
225
File destinationFile = new File (tempDir , "out.tar.gz" );
245
226
String containerFileSystemOutputFilePath = destinationFile .getAbsolutePath ();
246
227
ImageInspectionRequest imageInspectionRequest = (new ImageInspectionRequestBuilder ())
247
228
.setDockerTarfilePath (OCI_IMAGE_TARFILE )
248
- //.setBlackDuckProjectName(PROJECT)
249
- //.setBlackDuckProjectVersion(PROJECT_VERSION)
250
- //.setGivenImageRepo("testrepo")
251
- //.setGivenImageTag("testtag")
229
+ .setGivenImageRepo ("testrepo" )
230
+ .setGivenImageTag ("testtag" )
252
231
.setContainerFileSystemOutputPath (containerFileSystemOutputFilePath )
253
- .setCurrentLinuxDistro ("CENTOS " )
232
+ .setCurrentLinuxDistro ("UBUNTU " )
254
233
.setCleanupWorkingDir (true )
255
234
.build ();
256
235
SimpleBdioDocument bdioDocument = imageInspectorApi .getBdio (componentHierarchyBuilder , imageInspectionRequest );
236
+ assertEquals ("testrepo" , bdioDocument .getProject ().name );
237
+ assertEquals ("testtag" , bdioDocument .getProject ().version );
257
238
258
239
File containerFileSystemFile = new File (containerFileSystemOutputFilePath );
259
- System .out .printf ("output file: %s\n " , containerFileSystemFile .getAbsolutePath ());
260
- assertTrue (containerFileSystemFile .length () > 10000000 );
240
+ assertTrue (containerFileSystemFile .exists ());
241
+ }
242
+
243
+ @ Test
244
+ public void testOnRightOs () throws IntegrationException , InterruptedException {
245
+ doAlpineTest (null );
246
+ }
247
+
248
+ @ Test
249
+ public void testOnRightOsDistroOverride () throws IntegrationException , InterruptedException {
250
+ doAlpineTest ("overriddendistroname" );
251
+ }
252
+
253
+ private void doAlpineTest (String targetLinuxDistroOverride ) throws IntegrationException , InterruptedException {
254
+ ComponentHierarchyBuilder componentHierarchyBuilder = new ComponentHierarchyBuilder (packageGetter );
255
+ String expectedForgeName ;
256
+ if (StringUtils .isNotBlank (targetLinuxDistroOverride )) {
257
+ expectedForgeName = "@" + targetLinuxDistroOverride ;
258
+ } else {
259
+ expectedForgeName = "@alpine" ;
260
+ }
261
+ Mockito .when (os .getLinuxDistroNameFromEtcDir (Mockito .any (File .class ))).thenReturn (Optional .of ("alpine" ));
262
+ Mockito .when (os .deriveOs (Mockito .any (String .class ))).thenReturn (ImageInspectorOsEnum .ALPINE );
263
+ ImageInspectionRequest imageInspectionRequest = (new ImageInspectionRequestBuilder ())
264
+ .setDockerTarfilePath (SIMPLE_IMAGE_TARFILE )
265
+ .setBlackDuckProjectName (PROJECT )
266
+ .setBlackDuckProjectVersion (PROJECT_VERSION )
267
+ .setCurrentLinuxDistro ("ALPINE" )
268
+ .setTargetLinuxDistroOverride (targetLinuxDistroOverride )
269
+ .setCleanupWorkingDir (true )
270
+ .build ();
271
+ SimpleBdioDocument bdioDocument = imageInspectorApi .getBdio (componentHierarchyBuilder , imageInspectionRequest );
272
+ System .out .printf ("bdioDocument: %s\n " , bdioDocument );
273
+ assertEquals (PROJECT , bdioDocument .getProject ().name );
274
+ assertEquals (PROJECT_VERSION , bdioDocument .getProject ().version );
275
+ assertEquals (apkOutput .length , bdioDocument .getComponents ().size ());
276
+ for (BdioComponent comp : bdioDocument .getComponents ()) {
277
+ System .out .printf ("comp: %s:%s:%s\n " , comp .name , comp .version , comp .bdioExternalIdentifier .externalId );
278
+ if (comp .name .equals ("boost-unit_test_framework" )) {
279
+ assertEquals ("1.62.0-r5" , comp .version );
280
+ assertEquals ("boost-unit_test_framework/1.62.0-r5/x86_64" , comp .bdioExternalIdentifier .externalId );
281
+ assertEquals (expectedForgeName , comp .bdioExternalIdentifier .forge );
282
+ } else {
283
+ assertEquals ("ca-certificates" , comp .name );
284
+ assertEquals ("20171114-r0" , comp .version );
285
+ assertEquals ("ca-certificates/20171114-r0/x86_64" , comp .bdioExternalIdentifier .externalId );
286
+ assertEquals (expectedForgeName , comp .bdioExternalIdentifier .forge );
287
+ }
288
+ }
261
289
}
262
290
}
0 commit comments