2020package org .apache .ranger .usergroupsync ;
2121
2222import org .apache .hadoop .conf .Configuration ;
23+ import org .apache .ranger .ha .HAConfiguration ;
2324import org .apache .ranger .ugsyncutil .model .UgsyncAuditInfo ;
2425import org .apache .ranger .unixusersync .config .UserGroupSyncConfig ;
26+ import org .junit .jupiter .api .AfterEach ;
2527import org .junit .jupiter .api .BeforeAll ;
2628import org .junit .jupiter .api .MethodOrderer ;
2729import org .junit .jupiter .api .Test ;
4749@ ExtendWith (MockitoExtension .class )
4850@ TestMethodOrder (MethodOrderer .MethodName .class )
4951public class TestUserGroupSync {
52+ private Thread userGroupSyncRunThread ;
53+
54+ /** Hadoop Configuration for tests: no classpath defaults, explicit non-HA. */
55+ private static Configuration freshNonHaUserSyncConfiguration () {
56+ Configuration cfg = new Configuration (false );
57+ cfg .set (UserGroupSyncConfig .UGSYNC_SERVER_HA_ENABLED_PARAM , "false" );
58+ cfg .set (HAConfiguration .RANGER_SERVICE_NAME , "ranger-ugsync" );
59+ return cfg ;
60+ }
61+
62+ @ AfterEach
63+ public void stopBackgroundUserGroupSyncThread () {
64+ if (userGroupSyncRunThread != null ) {
65+ userGroupSyncRunThread .interrupt ();
66+ userGroupSyncRunThread = null ;
67+ }
68+ }
69+
5070 @ BeforeAll
5171 public static void setupHA () throws Exception {
5272 // Reset HA singletons to pick up non-HA config
@@ -125,9 +145,9 @@ public void testC_run_passiveMode_doesNotInitializeSourceOrSink() throws Excepti
125145 haInstance .set (null , null );
126146
127147 // Inject a Hadoop Configuration with HA enabled into UserGroupSyncConfig
128- Configuration haCfg = new Configuration ();
129- haCfg .set ("ranger.service.name" , "ranger-ugsync" );
130- haCfg .set ("ranger-ugsync.server.ha.enabled" , "true" );
148+ Configuration haCfg = new Configuration (false );
149+ haCfg .set (HAConfiguration . RANGER_SERVICE_NAME , "ranger-ugsync" );
150+ haCfg .set (UserGroupSyncConfig . UGSYNC_SERVER_HA_ENABLED_PARAM , "true" );
131151
132152 UserGroupSyncConfig config = UserGroupSyncConfig .getInstance ();
133153 Field userGroupConfigField = UserGroupSyncConfig .class .getDeclaredField ("userGroupConfig" );
@@ -160,8 +180,8 @@ public void testD_run_activeMode_initializesAndPerformsInitialSync() throws Exce
160180 haInstance .setAccessible (true );
161181 haInstance .set (null , null );
162182
163- // Provide fresh non-HA configuration
164- Configuration activeCfg = new Configuration ();
183+ // Provide fresh non-HA configuration (avoid loading classpath defaults that may enable HA)
184+ Configuration activeCfg = freshNonHaUserSyncConfiguration ();
165185 UserGroupSyncConfig config = UserGroupSyncConfig .getInstance ();
166186 Field userGroupConfigField = UserGroupSyncConfig .class .getDeclaredField ("userGroupConfig" );
167187 userGroupConfigField .setAccessible (true );
@@ -176,9 +196,9 @@ public void testD_run_activeMode_initializesAndPerformsInitialSync() throws Exce
176196 TestUserGroupSync .StubSource .recordedUpdates = 0 ;
177197
178198 UserGroupSync userGroupSync = new UserGroupSync ();
179- Thread t = new Thread (userGroupSync ::run );
180- t .setDaemon (true );
181- t .start ();
199+ userGroupSyncRunThread = new Thread (userGroupSync ::run );
200+ userGroupSyncRunThread .setDaemon (true );
201+ userGroupSyncRunThread .start ();
182202
183203 Thread .sleep (200 );
184204 }
@@ -195,8 +215,8 @@ public void testE_main_startsRun_andTriggersSyncPaths() throws Exception {
195215 haInstance .setAccessible (true );
196216 haInstance .set (null , null );
197217
198- // Provide fresh non-HA configuration
199- Configuration activeCfg = new Configuration ();
218+ // Provide fresh non-HA configuration (avoid loading classpath defaults that may enable HA)
219+ Configuration activeCfg = freshNonHaUserSyncConfiguration ();
200220 UserGroupSyncConfig config = UserGroupSyncConfig .getInstance ();
201221 Field userGroupConfigField = UserGroupSyncConfig .class .getDeclaredField ("userGroupConfig" );
202222 userGroupConfigField .setAccessible (true );
@@ -209,11 +229,11 @@ public void testE_main_startsRun_andTriggersSyncPaths() throws Exception {
209229 config .setProperty ("ranger.usersync.source.impl.class" , TestUserGroupSync .StubSource .class .getName ());
210230 config .setProperty ("ranger.usersync.sink.impl.class" , TestUserGroupSync .StubSink .class .getName ());
211231
212- int before = TestUserGroupSync .StubSource .recordedUpdates ;
213- Thread t = new Thread (() -> UserGroupSync .main (new String [] {}));
214- t .setDaemon (true );
215- t .start ();
216- Thread .sleep (200 );
232+ int before = TestUserGroupSync .StubSource .recordedUpdates ;
233+ userGroupSyncRunThread = new Thread (() -> UserGroupSync .main (new String [] {}));
234+ userGroupSyncRunThread .setDaemon (true );
235+ userGroupSyncRunThread .start ();
236+ Thread .sleep (500 );
217237 assertTrue (TestUserGroupSync .StubSource .recordedUpdates > before );
218238 }
219239
0 commit comments