WorkspaceManager { @Traced public WorkspaceImpl createWorkspace( WorkspaceConfig config, String namespace, Map<String, String> attributes) throws ServerException, NotFoundException, ConflictException, ValidationException { requireNonNull(config, "Required non-null config"); requireNonNull(namespace, "Required non-null namespace"); validator.validateConfig(config); validator.validateAttributes(attributes); WorkspaceImpl workspace = doCreateWorkspace(config, accountManager.getByName(namespace), attributes, false); TracingTags.WORKSPACE_ID.set(workspace.getId()); return workspace; } @Inject  WorkspaceManager(
      WorkspaceDao workspaceDao,
      WorkspaceRuntimes runtimes,
      EventService eventService,
      AccountManager accountManager,
      PreferenceManager preferenceManager,
      WorkspaceValidator validator,
      DevfileIntegrityValidator devfileIntegrityValidator); @Traced WorkspaceImpl createWorkspace(
      WorkspaceConfig config, String namespace, Map<String, String> attributes); @Traced WorkspaceImpl createWorkspace(
      Devfile devfile,
      String namespace,
      Map<String, String> attributes,
      FileContentProvider contentProvider); WorkspaceImpl getWorkspace(String key); WorkspaceImpl getWorkspace(String name, String namespace); Page<WorkspaceImpl> getWorkspaces(
      String user, boolean includeRuntimes, int maxItems, long skipCount); Page<WorkspaceImpl> getByNamespace(
      String namespace, boolean includeRuntimes, int maxItems, long skipCount); WorkspaceImpl updateWorkspace(String id, Workspace update); @Traced void removeWorkspace(String workspaceId); WorkspaceImpl startWorkspace(
      String workspaceId, @Nullable String envName, @Nullable Map<String, String> options); WorkspaceImpl startWorkspace(
      WorkspaceConfig config, String namespace, boolean isTemporary, Map<String, String> options); void stopWorkspace(String workspaceId, Map<String, String> options); Set<String> getSupportedRecipes(); long getWorkspacesTotalCount();  }
@Test public void createsWorkspace() throws Exception { final WorkspaceConfig cfg = createConfig(); when(runtimes.evalInfrastructureNamespace(any())).thenReturn("ns"); final WorkspaceImpl workspace = workspaceManager.createWorkspace(cfg, NAMESPACE_1, ImmutableMap.of("attr", "value")); assertNotNull(workspace); assertFalse(isNullOrEmpty(workspace.getId())); assertEquals(workspace.getNamespace(), NAMESPACE_1); assertEquals(workspace.getConfig(), cfg); assertFalse(workspace.isTemporary()); assertEquals(workspace.getStatus(), STOPPED); assertNotNull(workspace.getAttributes().get(CREATED_ATTRIBUTE_NAME)); assertEquals("ns", workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE)); verify(workspaceDao).create(workspace); verify(validator).validateAttributes(ImmutableMap.of("attr", "value")); }
@Test public void createsWorkspaceFromDevfile() throws ValidationException, ConflictException, NotFoundException, ServerException, InfrastructureException { final DevfileImpl devfile = new DevfileImpl(); devfile.setApiVersion(CURRENT_API_VERSION); devfile.setName("ws"); when(runtimes.evalInfrastructureNamespace(any())).thenReturn("ns"); Workspace workspace = workspaceManager.createWorkspace(devfile, NAMESPACE_1, null, null); assertEquals(workspace.getDevfile(), devfile); }
@Test public void createsWorkspaceFromDevfileWithGenerateName() throws ValidationException, ConflictException, NotFoundException, ServerException, InfrastructureException { final String testDevfileGenerateName = "ws-"; final DevfileImpl devfile = new DevfileImpl(); devfile.setApiVersion(CURRENT_API_VERSION); devfile.getMetadata().setGenerateName(testDevfileGenerateName); when(runtimes.evalInfrastructureNamespace(any())).thenReturn("ns"); Workspace workspace = workspaceManager.createWorkspace(devfile, NAMESPACE_1, null, null); assertTrue(workspace.getDevfile().getName().startsWith(testDevfileGenerateName)); assertEquals( workspace.getDevfile().getName().length(), testDevfileGenerateName.length() + WORKSPACE_GENERATE_NAME_CHARS_APPEND); }
@Test public void evaluatesInfraNamespaceIfMissingOnWorkspaceCreation() throws Exception { when(runtimes.evalInfrastructureNamespace(any())).thenReturn("evaluated"); final WorkspaceConfig cfg = createConfig(); final WorkspaceImpl workspace = workspaceManager.createWorkspace(cfg, NAMESPACE_1, null); assertNotNull(workspace); assertNotNull( workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE), "evaluated"); verify(workspaceDao).create(workspace); verify(runtimes) .evalInfrastructureNamespace( new NamespaceResolutionContext(workspace.getId(), USER_ID, NAMESPACE_1, true)); }
@Test public void propagatesInfraNamespaceFromAttributesOnWorkspaceCreation() throws Exception { final WorkspaceConfig cfg = createConfig(); final WorkspaceImpl workspace = workspaceManager.createWorkspace( cfg, NAMESPACE_1, ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "user-defined")); assertNotNull(workspace); assertNotNull( workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE), "user-defined"); verify(workspaceDao).create(workspace); verify(runtimes, never()).evalInfrastructureNamespace(any()); }
@Test public void nameIsUsedWhenNameAndGenerateNameSet() throws ValidationException, ConflictException, NotFoundException, ServerException, InfrastructureException { final String devfileName = "workspacename"; final DevfileImpl devfile = new DevfileImpl(); devfile.setApiVersion(CURRENT_API_VERSION); devfile.getMetadata().setName(devfileName); devfile.getMetadata().setGenerateName("this_will_not_be_set_as_a_name"); when(runtimes.evalInfrastructureNamespace(any())).thenReturn("ns"); Workspace workspace = workspaceManager.createWorkspace(devfile, NAMESPACE_1, null, null); assertEquals(workspace.getDevfile().getName(), devfileName); }
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "boom") public void shouldFailTocreateWorkspaceUsingInconsistentDevfile() throws Exception { doThrow(new DevfileFormatException("boom")) .when(devfileIntegrityValidator) .validateContentReferences(any(), any()); Devfile devfile = mock(Devfile.class); workspaceManager.createWorkspace(devfile, "ns", emptyMap(), null); }