OpenShiftStopWorkspaceRoleProvisioner { public void provision(String projectName) throws InfrastructureException { if (stopWorkspaceRoleEnabled && installationLocation != null) { OpenShiftClient osClient = clientFactory.createOC(); String stopWorkspacesRoleName = "workspace-stop"; if (osClient.roles().inNamespace(projectName).withName(stopWorkspacesRoleName).get() == null) { osClient .roles() .inNamespace(projectName) .createOrReplace(createStopWorkspacesRole(stopWorkspacesRoleName)); } osClient .roleBindings() .inNamespace(projectName) .createOrReplace(createStopWorkspacesRoleBinding(projectName)); } else { LOG.warn( "Stop workspace Role and RoleBinding will not be provisioned to the '{}' namespace. 'che.workspace.stop.role.enabled' property is set to '{}'", installationLocation, stopWorkspaceRoleEnabled); } } @Inject  OpenShiftStopWorkspaceRoleProvisioner(
      OpenShiftClientFactory clientFactory,
      CheInstallationLocation installationLocation,
      @Named("che.workspace.stop.role.enabled") boolean stopWorkspaceRoleEnabled); void provision(String projectName);  }
@Test public void shouldCreateRoleAndRoleBindingWhenRoleDoesNotYetExist() throws InfrastructureException { stopWorkspaceRoleProvisioner.provision("developer-che"); verify(osClient, times(2)).roles(); verify(osClient.roles(), times(2)).inNamespace("developer-che"); verify(osClient.roles().inNamespace("developer-che")).withName("workspace-stop"); verify(osClient.roles().inNamespace("developer-che")).createOrReplace(expectedRole); verify(osClient).roleBindings(); verify(osClient.roleBindings()).inNamespace("developer-che"); verify(osClient.roleBindings().inNamespace("developer-che")) .createOrReplace(expectedRoleBinding); }
@Test public void shouldCreateRoleBindingWhenRoleAlreadyExists() throws InfrastructureException { lenient().when(roleResource.get()).thenReturn(expectedRole); stopWorkspaceRoleProvisioner.provision("developer-che"); verify(osClient, times(1)).roles(); verify(osClient).roleBindings(); verify(osClient.roleBindings()).inNamespace("developer-che"); verify(osClient.roleBindings().inNamespace("developer-che")) .createOrReplace(expectedRoleBinding); }
@Test public void shouldNotCreateRoleBindingWhenStopWorkspaceRolePropertyIsDisabled() throws InfrastructureException { OpenShiftStopWorkspaceRoleProvisioner disabledStopWorkspaceRoleProvisioner = new OpenShiftStopWorkspaceRoleProvisioner(clientFactory, cheInstallationLocation, false); disabledStopWorkspaceRoleProvisioner.provision("developer-che"); verify(osClient, never()).roles(); verify(osClient, never()).roleBindings(); verify(osClient.roleBindings(), never()).inNamespace("developer-che"); }
@Test public void shouldNotCreateRoleBindingWhenInstallationLocationIsNull() throws InfrastructureException { lenient().when(cheInstallationLocation.getInstallationLocationNamespace()).thenReturn(null); OpenShiftStopWorkspaceRoleProvisioner stopWorkspaceRoleProvisionerWithoutValidInstallationLocation = new OpenShiftStopWorkspaceRoleProvisioner(clientFactory, cheInstallationLocation, true); stopWorkspaceRoleProvisionerWithoutValidInstallationLocation.provision("developer-che"); verify(osClient, never()).roles(); verify(osClient, never()).roleBindings(); verify(osClient.roleBindings(), never()).inNamespace("developer-che"); }