2222import com .aws .greengrass .testcommons .testutilities .NoOpPathOwnershipHandler ;
2323import com .aws .greengrass .util .Pair ;
2424import com .aws .greengrass .util .platforms .SystemResourceController ;
25- import com .aws .greengrass .util .platforms .unix .linux .*;
25+ import com .aws .greengrass .util .platforms .unix .linux .Cgroup ;
26+ import com .aws .greengrass .util .platforms .unix .linux .CgroupSubSystem ;
27+ import com .aws .greengrass .util .platforms .unix .linux .CgroupSubSystemV2 ;
28+ import com .aws .greengrass .util .platforms .unix .linux .LinuxPlatform ;
29+ import com .aws .greengrass .util .platforms .unix .linux .LinuxSystemResourceController ;
30+ import com .aws .greengrass .util .platforms .unix .linux .LinuxSystemResourceControllerV2 ;
2631import org .apache .commons .io .FileUtils ;
2732import org .apache .commons .lang3 .SystemUtils ;
2833import org .apache .commons .lang3 .reflect .FieldUtils ;
3641import org .junit .jupiter .params .ParameterizedTest ;
3742import org .junit .jupiter .params .provider .Arguments ;
3843import org .junit .jupiter .params .provider .MethodSource ;
39- import org .mockito .* ;
44+ import org .mockito .Spy ;
4045import org .mockito .junit .jupiter .MockitoExtension ;
4146
4247import java .io .IOException ;
4651import java .nio .file .Files ;
4752import java .nio .file .Path ;
4853import java .nio .file .Paths ;
49- import java .util .*;
54+ import java .util .ArrayList ;
55+ import java .util .List ;
5056import java .util .concurrent .CompletableFuture ;
5157import java .util .concurrent .CopyOnWriteArrayList ;
5258import java .util .concurrent .CountDownLatch ;
8894
8995@ ExtendWith ({GGExtension .class , MockitoExtension .class })
9096class GenericExternalServiceIntegTest extends BaseITCase {
91-
9297 private Kernel kernel ;
9398
99+ @ Spy
94100 LinuxPlatform linuxPlatform ;
95101
96102 private final static String ROOT_PATH_STRING = "/systest21/fs/cgroup" ;
97103 private final static String GG_PATH_STRING = "greengrass" ;
98104
99- @ Spy
100- SystemResourceController spySystemResourceController ;
105+ SystemResourceController systemResourceController ;
101106
102107 static Stream <Arguments > posixTestUserConfig () {
103108 return Stream .of (
@@ -652,24 +657,25 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re
652657 }
653658 });
654659
655- linuxPlatform = kernel .getContext ().get (LinuxPlatform .class );
660+ linuxPlatform = spy ( kernel .getContext ().get (LinuxPlatform .class ) );
656661
657662 createComponentData (echoComponentName );
658663 createComponentData (mainComponentName );
659664
660- setFinalStatic (linuxPlatform , LinuxPlatform .class .getDeclaredField ("CGROUP_CONTROLLERS" ), Paths .get (componentPathString + "/memory.max" ));
661-
662- spySystemResourceController = linuxPlatform .getSystemResourceController ();
663- LinuxSystemResourceControllerV2 controllerV2 = (LinuxSystemResourceControllerV2 ) spySystemResourceController ;
664-
665+ // Due to cgroup v1 is active by default (in test platform), and the directories of cgroup v1 are read-only
666+ // therefore, here create some directories and files as fake cgroup v2 files to support testing
667+ Field controllerField = LinuxPlatform .class .getDeclaredField ("CGROUP_CONTROLLERS" );
668+ setFinalStatic (controllerField , Paths .get (componentPathString + "/memory.max" ));
669+ systemResourceController = linuxPlatform .getSystemResourceController ();
670+ LinuxSystemResourceControllerV2 controllerV2 = (LinuxSystemResourceControllerV2 ) systemResourceController ;
665671 Field memoryCgroupField = LinuxSystemResourceControllerV2 .class .getSuperclass ().getDeclaredField ("memoryCgroup" );
666672 memoryCgroupField .setAccessible (true );
667673 Cgroup memoryCgroup = (Cgroup ) memoryCgroupField .get (controllerV2 );
668674 Field subsystem = memoryCgroup .getClass ().getDeclaredField ("subSystem" );
669675 subsystem .setAccessible (true );
670676 CgroupSubSystemV2 cg = (CgroupSubSystemV2 ) subsystem .get (memoryCgroup );
671677 Field f = cg .getClass ().getInterfaces ()[0 ].getDeclaredField ("CGROUP_ROOT" );
672- setFinalStatic (cg , f , Paths .get (ROOT_PATH_STRING ));
678+ setFinalStatic (f , Paths .get (ROOT_PATH_STRING ));
673679
674680 Field mountsField = LinuxSystemResourceControllerV2 .class .getSuperclass ().getDeclaredField ("MOUNT_PATH" );
675681 mountsField .setAccessible (true );
@@ -680,7 +686,7 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re
680686 Files .write (mountPathFilePath , String .format ("test1 %s test2 test3 test4 test5" , ROOT_PATH_STRING ).getBytes (StandardCharsets .UTF_8 ));
681687
682688 }
683- setFinalStatic (controllerV2 , mountsField , mountPathFile );
689+ setFinalStatic (mountsField , mountPathFile );
684690
685691 kernel .launch ();
686692 assertResourceLimits_V2 (10240l * 1024 , 1.5 );
@@ -706,10 +712,10 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re
706712 FileUtils .deleteDirectory (Paths .get ("/systest21" ).toFile ());
707713 }
708714
709- private void setFinalStatic (Object obj , Field field , Object newValue ) throws Exception {
715+ private void setFinalStatic (Field field , Object newValue ) throws Exception {
710716 field .setAccessible (true );
711717 FieldUtils .removeFinalModifier (field , true );
712- field .set (obj , newValue );
718+ field .set (null , newValue );
713719 }
714720
715721 @ Test
@@ -866,8 +872,8 @@ private void createComponentData(String componentName) throws IOException {
866872 Files .createFile (path .resolve ("cpu.max" ));
867873 }
868874 Files .write (path .resolve ("cpu.max" ), "max 100000" .getBytes (StandardCharsets .UTF_8 ));
869- if (!Files .exists (path .resolve ("cgroup.proc " ))) {
870- Files .createFile (path .resolve ("cgroup.proc " ));
875+ if (!Files .exists (path .resolve ("cgroup.procs " ))) {
876+ Files .createFile (path .resolve ("cgroup.procs " ));
871877 }
872878 }
873879
0 commit comments