|
| 1 | +/* |
| 2 | + * Copyright (c) 2012-2022 Red Hat, Inc. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Red Hat, Inc. - initial API and implementation |
| 11 | + */ |
| 12 | +package org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator; |
| 13 | + |
| 14 | +import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.GIT_USERDATA_CONFIGMAP_NAME; |
| 15 | +import static org.mockito.ArgumentMatchers.anyString; |
| 16 | +import static org.mockito.Mockito.mock; |
| 17 | +import static org.mockito.Mockito.spy; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 22 | +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; |
| 23 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 24 | +import io.fabric8.kubernetes.client.server.mock.KubernetesServer; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import org.eclipse.che.api.core.NotFoundException; |
| 29 | +import org.eclipse.che.api.core.ServerException; |
| 30 | +import org.eclipse.che.api.core.model.user.User; |
| 31 | +import org.eclipse.che.api.factory.server.scm.GitUserData; |
| 32 | +import org.eclipse.che.api.factory.server.scm.GitUserDataFetcher; |
| 33 | +import org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException; |
| 34 | +import org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException; |
| 35 | +import org.eclipse.che.api.user.server.UserManager; |
| 36 | +import org.eclipse.che.api.workspace.server.spi.InfrastructureException; |
| 37 | +import org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext; |
| 38 | +import org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientFactory; |
| 39 | +import org.mockito.Mock; |
| 40 | +import org.mockito.testng.MockitoTestNGListener; |
| 41 | +import org.testng.Assert; |
| 42 | +import org.testng.annotations.BeforeMethod; |
| 43 | +import org.testng.annotations.Listeners; |
| 44 | +import org.testng.annotations.Test; |
| 45 | + |
| 46 | +@Listeners(MockitoTestNGListener.class) |
| 47 | +public class GitconfigUserdataConfiguratorTest { |
| 48 | + |
| 49 | + private NamespaceConfigurator configurator; |
| 50 | + |
| 51 | + @Mock private KubernetesClientFactory clientFactory; |
| 52 | + @Mock private GitUserDataFetcher gitUserDataFetcher; |
| 53 | + @Mock private UserManager userManager; |
| 54 | + private KubernetesServer serverMock; |
| 55 | + |
| 56 | + private NamespaceResolutionContext namespaceResolutionContext; |
| 57 | + private final String TEST_NAMESPACE_NAME = "namespace123"; |
| 58 | + private final String TEST_WORKSPACE_ID = "workspace123"; |
| 59 | + private final String TEST_USER_ID = "user123"; |
| 60 | + private final String TEST_USERNAME = "jondoe"; |
| 61 | + |
| 62 | + @BeforeMethod |
| 63 | + public void setUp() |
| 64 | + throws InfrastructureException, ScmCommunicationException, ScmUnauthorizedException { |
| 65 | + configurator = |
| 66 | + new GitconfigUserDataConfigurator(clientFactory, Set.of(gitUserDataFetcher), userManager); |
| 67 | + |
| 68 | + serverMock = new KubernetesServer(true, true); |
| 69 | + serverMock.before(); |
| 70 | + KubernetesClient client = spy(serverMock.getClient()); |
| 71 | + when(clientFactory.create()).thenReturn(client); |
| 72 | + |
| 73 | + namespaceResolutionContext = |
| 74 | + new NamespaceResolutionContext(TEST_WORKSPACE_ID, TEST_USER_ID, TEST_USERNAME); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void createUserdataConfigmapWhenDoesNotExist() |
| 79 | + throws ScmCommunicationException, ScmUnauthorizedException, InfrastructureException, |
| 80 | + InterruptedException { |
| 81 | + // given |
| 82 | + when(gitUserDataFetcher.fetchGitUserData()).thenReturn(new GitUserData("gitUser", "gitEmail")); |
| 83 | + |
| 84 | + // when |
| 85 | + configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); |
| 86 | + |
| 87 | + // then create a secret |
| 88 | + Assert.assertEquals(serverMock.getLastRequest().getMethod(), "POST"); |
| 89 | + Assert.assertNotNull( |
| 90 | + serverMock |
| 91 | + .getClient() |
| 92 | + .configMaps() |
| 93 | + .inNamespace(TEST_NAMESPACE_NAME) |
| 94 | + .withName(GIT_USERDATA_CONFIGMAP_NAME) |
| 95 | + .get()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void doNothingWhenGitUserDataAndCheUserAreNull() |
| 100 | + throws InfrastructureException, ServerException, NotFoundException { |
| 101 | + // when |
| 102 | + when(userManager.getById(anyString())).thenThrow(new NotFoundException("not found")); |
| 103 | + configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); |
| 104 | + |
| 105 | + // then - don't create the configmap |
| 106 | + var configMaps = |
| 107 | + serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).list().getItems(); |
| 108 | + Assert.assertEquals(configMaps.size(), 0); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void doNothingWhenSecretAlreadyExists() |
| 113 | + throws InfrastructureException, InterruptedException, ScmCommunicationException, |
| 114 | + ScmUnauthorizedException { |
| 115 | + // given |
| 116 | + when(gitUserDataFetcher.fetchGitUserData()).thenReturn(new GitUserData("gitUser", "gitEmail")); |
| 117 | + Map<String, String> annotations = |
| 118 | + ImmutableMap.of( |
| 119 | + "controller.devfile.io/mount-as", |
| 120 | + "subpath", |
| 121 | + "controller.devfile.io/mount-path", |
| 122 | + "/etc/", |
| 123 | + "already", |
| 124 | + "created"); |
| 125 | + Map<String, String> labels = |
| 126 | + ImmutableMap.of( |
| 127 | + "controller.devfile.io/mount-to-devworkspace", |
| 128 | + "true", |
| 129 | + "controller.devfile.io/watch-configmap", |
| 130 | + "true", |
| 131 | + "already", |
| 132 | + "created"); |
| 133 | + ConfigMap configMap = |
| 134 | + new ConfigMapBuilder() |
| 135 | + .withNewMetadata() |
| 136 | + .withName(GIT_USERDATA_CONFIGMAP_NAME) |
| 137 | + .withLabels(labels) |
| 138 | + .withAnnotations(annotations) |
| 139 | + .endMetadata() |
| 140 | + .build(); |
| 141 | + configMap.setData(Collections.singletonMap("gitconfig", "empty")); |
| 142 | + serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).create(configMap); |
| 143 | + |
| 144 | + // when |
| 145 | + configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); |
| 146 | + |
| 147 | + // then - don't create the configmap |
| 148 | + Assert.assertEquals(serverMock.getLastRequest().getMethod(), "GET"); |
| 149 | + var configMaps = |
| 150 | + serverMock.getClient().configMaps().inNamespace(TEST_NAMESPACE_NAME).list().getItems(); |
| 151 | + Assert.assertEquals(configMaps.size(), 1); |
| 152 | + Assert.assertEquals(configMaps.get(0).getMetadata().getAnnotations().get("already"), "created"); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void createUserdataConfigmapFromCheUserData() |
| 157 | + throws InfrastructureException, ServerException, NotFoundException, InterruptedException { |
| 158 | + // given |
| 159 | + User user = mock(User.class); |
| 160 | + when(user.getName()).thenReturn("test name"); |
| 161 | + when( user. getEmail()). thenReturn( "[email protected]"); |
| 162 | + when(userManager.getById(anyString())).thenReturn(user); |
| 163 | + |
| 164 | + // when |
| 165 | + configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME); |
| 166 | + |
| 167 | + // then create a secret |
| 168 | + Assert.assertEquals(serverMock.getLastRequest().getMethod(), "POST"); |
| 169 | + ConfigMap configMap = |
| 170 | + serverMock |
| 171 | + .getClient() |
| 172 | + .configMaps() |
| 173 | + .inNamespace(TEST_NAMESPACE_NAME) |
| 174 | + .withName(GIT_USERDATA_CONFIGMAP_NAME) |
| 175 | + .get(); |
| 176 | + Assert.assertNotNull(configMap); |
| 177 | + Assert.assertTrue(configMap.getData().get("gitconfig").contains("test name")); |
| 178 | + Assert. assertTrue( configMap. getData(). get( "gitconfig"). contains( "[email protected]")); |
| 179 | + } |
| 180 | +} |
0 commit comments