Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update service provider manager test case #2399

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@

import static com.yahoo.athenz.zms.ZMSConsts.*;
import static java.util.stream.Collectors.toList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;

Expand All @@ -64,7 +62,7 @@ public void clean() {
public void testIsServiceProvider() throws InterruptedException, ExecutionException {
String testDomainName = "test.domain";
String testRoleName = "test_role";
long fetchFrequency = 1L; // For the test, fetch every second
long fetchFrequency = 5L; // For the test, fetch every 5 seconds
int numberOfThreads = 10;
System.setProperty(ZMS_PROP_SERVICE_PROVIDER_MANAGER_DOMAIN, testDomainName);
System.setProperty(ZMS_PROP_SERVICE_PROVIDER_MANAGER_ROLE, testRoleName);
Expand All @@ -84,7 +82,7 @@ public void testIsServiceProvider() throws InterruptedException, ExecutionExcept
"service.provider.test2",
"service.provider.test3"), testRoleName);

when(dbService.getRole(Mockito.eq(testDomainName), Mockito.eq(testRoleName), Mockito.eq(false), Mockito.eq(true), Mockito.eq(false)))
when(dbService.getRole(testDomainName, testRoleName, false, true, false))
.thenReturn(testRole1)
.thenReturn(testRole2)
.thenReturn(testRole3);
Expand All @@ -93,13 +91,13 @@ public void testIsServiceProvider() throws InterruptedException, ExecutionExcept
ServiceIdentity serviceIdentity = new ServiceIdentity();
serviceIdentity.setName("service.provider.test" + i);
serviceIdentity.setProviderEndpoint("https://localhost:4443/test" + i);
when(dbService.getServiceIdentity(eq("service.provider"), eq("test" + i), eq(true))).thenReturn(serviceIdentity);


when(dbService.getServiceIdentity("service.provider", "test" + i, true)).thenReturn(serviceIdentity);
}

Principal providerServicePrincipal = SimplePrincipal.create("service.provider", "test1", (String) null);
Authorizer authorizer = Mockito.mock(Authorizer.class);
when(authorizer.access(eq(ServerCommonConsts.ACTION_LAUNCH), eq(ServerCommonConsts.RESOURCE_INSTANCE), eq(providerServicePrincipal), isNull())).thenReturn(true);
when(authorizer.access(ServerCommonConsts.ACTION_LAUNCH, ServerCommonConsts.RESOURCE_INSTANCE,
providerServicePrincipal, null)).thenReturn(true);
ServiceProviderManager serviceProviderManager = ServiceProviderManager.getInstance(dbService, authorizer);

// Simulate calls from different threads to check service provider
Expand All @@ -116,7 +114,7 @@ public void testIsServiceProvider() throws InterruptedException, ExecutionExcept
}
}


serviceProviderManager.shutdown();
System.clearProperty(ZMS_PROP_SERVICE_PROVIDER_MANAGER_FREQUENCY_SECONDS);
System.clearProperty(ZMS_PROP_SERVICE_PROVIDER_MANAGER_DOMAIN);
System.clearProperty(ZMS_PROP_SERVICE_PROVIDER_MANAGER_ROLE);
Expand Down Expand Up @@ -160,7 +158,8 @@ public Boolean call() {
assertEquals(serviceProvidersWithEndpoints.size(), 1);

// Now sleep until the second fetch from DB (fetchFrequency + add a small delta)
ZMSTestUtils.sleep((1000 * fetchFrequency) + 50);
LOGGER.info("sleeping until the second refresh happens...");
ZMSTestUtils.sleep((1000 * fetchFrequency) + 1000);

// Assert service.provider.test2 and service.provider.test3 added
assertFalse(serviceProviderManager.isServiceProvider(null));
Expand All @@ -179,7 +178,8 @@ public Boolean call() {
assertEquals(serviceProvidersWithEndpoints.get("service.provider.test3").getProviderEndpoint(), "https://localhost:4443/test3");

// Now check final fetch from DB
ZMSTestUtils.sleep(1000 * fetchFrequency);
LOGGER.info("sleeping until final refresh happens...");
ZMSTestUtils.sleep(1000 * fetchFrequency + 1000);

// Assert service.provider.test1 was removed
assertFalse(serviceProviderManager.isServiceProvider(null));
Expand Down