GitConfigProvisioner implements ConfigurationProvisioner<KubernetesEnvironment> { @Override public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { try { Pair<String, String> userAndEmail = getUserFromPreferences(); if (userAndEmail == null) { userAndEmail = getUserFromUserManager(); } prepareAndProvisionGitConfiguration( userAndEmail.first, userAndEmail.second, k8sEnv, identity); } catch (ServerException | NotFoundException e) { reportWarning( k8sEnv, Warnings.EXCEPTION_IN_USER_MANAGEMENT_DURING_GIT_PROVISION_WARNING_CODE, format( Warnings.EXCEPTION_IN_USER_MANAGEMENT_DURING_GIT_PROVISION_MESSAGE_FMT, e.getMessage())); } catch (JsonSyntaxException e) { reportWarning( k8sEnv, Warnings.JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_WARNING_CODE, format( Warnings.JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_MESSAGE_FMT, e.getMessage())); } } @Inject  GitConfigProvisioner(
      PreferenceManager preferenceManager,
      UserManager userManager,
      VcsSslCertificateProvisioner vcsSslCertificateProvisioner); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity); static final String GIT_CONFIG_MAP_NAME_SUFFIX; static final String GIT_CONFIG; }
@Test public void testShouldDoNothingWhenGitPreferencesAndUserManagerAreEmpty() throws Exception { Map<String, String> preferences = singletonMap("theia-user-preferences", "{}"); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); User user = mock(User.class); when(userManager.getById(eq("id"))).thenReturn(user); when(user.getName()).thenReturn(null); when(user.getEmail()).thenReturn(null); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); verifyZeroInteractions(runtimeIdentity); }
@Test public void testShouldExpectWarningWhenPreferenceManagerThrowsServerException() throws Exception { when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))) .thenThrow(new ServerException("message")); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); verifyZeroInteractions(runtimeIdentity); List<Warning> warnings = k8sEnv.getWarnings(); assertEquals(warnings.size(), 1); Warning actualWarning = warnings.get(0); String warnMsg = format(Warnings.EXCEPTION_IN_USER_MANAGEMENT_DURING_GIT_PROVISION_MESSAGE_FMT, "message"); Warning expectedWarning = new WarningImpl( Warnings.EXCEPTION_IN_USER_MANAGEMENT_DURING_GIT_PROVISION_WARNING_CODE, warnMsg); assertEquals(expectedWarning, actualWarning); }
@Test public void testShouldExpectWarningWhenJsonObjectInPreferencesIsNotValid() throws Exception { Map<String, String> preferences = singletonMap("theia-user-preferences", "{#$%}"); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); verifyZeroInteractions(runtimeIdentity); List<Warning> warnings = k8sEnv.getWarnings(); assertEquals(warnings.size(), 1); Warning actualWarning = warnings.get(0); String warnMsg = format( Warnings.JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_MESSAGE_FMT, "java.io.EOFException: End of input at line 1 column 6 path $."); Warning expectedWarning = new WarningImpl( Warnings.JSON_IS_NOT_A_VALID_REPRESENTATION_FOR_AN_OBJECT_OF_TYPE_WARNING_CODE, warnMsg); assertEquals(expectedWarning, actualWarning); }
@Test public void testShouldExpectWarningWhenUserManagerThrowsServerException() throws Exception { when(userManager.getById(eq("id"))).thenThrow(new ServerException("message")); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); verifyZeroInteractions(runtimeIdentity); List<Warning> warnings = k8sEnv.getWarnings(); assertEquals(warnings.size(), 1); Warning actualWarning = warnings.get(0); String warnMsg = format(Warnings.EXCEPTION_IN_USER_MANAGEMENT_DURING_GIT_PROVISION_MESSAGE_FMT, "message"); Warning expectedWarning = new WarningImpl( Warnings.EXCEPTION_IN_USER_MANAGEMENT_DURING_GIT_PROVISION_WARNING_CODE, warnMsg); assertEquals(expectedWarning, actualWarning); }
@Test public void testShouldCheckIfPodHasMountAndK8HasConfigMapForGitConfig() throws Exception { String json = "{\"git.user.name\":\"user\",\"git.user.email\":\"email\"}"; Map<String, String> preferences = singletonMap("theia-user-preferences", json); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); ObjectMeta podMeta = new ObjectMetaBuilder().withName("wksp").build(); when(pod.getMetadata()).thenReturn(podMeta); when(pod.getSpec()).thenReturn(podSpec); when(podSpec.getContainers()).thenReturn(singletonList(container)); List<VolumeMount> volumeMounts = new ArrayList<>(); when(container.getVolumeMounts()).thenReturn(volumeMounts); k8sEnv.addPod(pod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(volumeMounts.size(), 1); VolumeMount mount = volumeMounts.get(0); assertEquals(mount.getMountPath(), "/etc/gitconfig"); assertEquals(mount.getName(), "gitconfigvolume"); assertFalse(mount.getReadOnly()); assertEquals(mount.getSubPath(), "gitconfig"); assertEquals(k8sEnv.getConfigMaps().size(), 1); assertTrue(k8sEnv.getConfigMaps().containsKey("wksp-gitconfig")); ConfigMap configMap = k8sEnv.getConfigMaps().get("wksp-gitconfig"); assertEquals(configMap.getData().size(), 1); assertTrue(configMap.getData().containsKey("gitconfig")); String gitconfig = configMap.getData().get("gitconfig"); String expectedGitconfig = "[user]\n\tname = user\n\temail = email\n"; assertEquals(gitconfig, expectedGitconfig); }
@Test(dataProvider = "invalidEmailValues") public void testShouldParseOnlyNameWhenEmailIsNotAString(String json) throws Exception { Map<String, String> preferences = singletonMap("theia-user-preferences", json); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); ObjectMeta podMeta = new ObjectMetaBuilder().withName("wksp").build(); when(pod.getMetadata()).thenReturn(podMeta); when(pod.getSpec()).thenReturn(podSpec); when(podSpec.getContainers()).thenReturn(singletonList(container)); List<VolumeMount> volumeMounts = new ArrayList<>(); when(container.getVolumeMounts()).thenReturn(volumeMounts); k8sEnv.addPod(pod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(volumeMounts.size(), 1); VolumeMount mount = volumeMounts.get(0); assertEquals(mount.getMountPath(), "/etc/gitconfig"); assertEquals(mount.getName(), "gitconfigvolume"); assertFalse(mount.getReadOnly()); assertEquals(mount.getSubPath(), "gitconfig"); assertEquals(k8sEnv.getConfigMaps().size(), 1); assertTrue(k8sEnv.getConfigMaps().containsKey("wksp-gitconfig")); ConfigMap configMap = k8sEnv.getConfigMaps().get("wksp-gitconfig"); assertEquals(configMap.getData().size(), 1); assertTrue(configMap.getData().containsKey("gitconfig")); String gitconfig = configMap.getData().get("gitconfig"); String expectedGitconfig = "[user]\n\tname = user\n"; assertEquals(gitconfig, expectedGitconfig); }
@Test(dataProvider = "invalidNameValues") public void testShouldParseOnlyEmailWhenNameIsNotAString(String json) throws Exception { Map<String, String> preferences = singletonMap("theia-user-preferences", json); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); ObjectMeta podMeta = new ObjectMetaBuilder().withName("wksp").build(); when(pod.getMetadata()).thenReturn(podMeta); when(pod.getSpec()).thenReturn(podSpec); when(podSpec.getContainers()).thenReturn(singletonList(container)); List<VolumeMount> volumeMounts = new ArrayList<>(); when(container.getVolumeMounts()).thenReturn(volumeMounts); k8sEnv.addPod(pod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(volumeMounts.size(), 1); VolumeMount mount = volumeMounts.get(0); assertEquals(mount.getMountPath(), "/etc/gitconfig"); assertEquals(mount.getName(), "gitconfigvolume"); assertFalse(mount.getReadOnly()); assertEquals(mount.getSubPath(), "gitconfig"); assertEquals(k8sEnv.getConfigMaps().size(), 1); assertTrue(k8sEnv.getConfigMaps().containsKey("wksp-gitconfig")); ConfigMap configMap = k8sEnv.getConfigMaps().get("wksp-gitconfig"); assertEquals(configMap.getData().size(), 1); assertTrue(configMap.getData().containsKey("gitconfig")); String gitconfig = configMap.getData().get("gitconfig"); String expectedGitconfig = "[user]\n\temail = email\n"; assertEquals(gitconfig, expectedGitconfig); }
@Test public void testShouldCheckIfPodHasMountAndK8HasConfigMapForGitConfigForDifferentTypeOfPreference() throws Exception { String json = "{\"chePlugins.repositories\":{\"Plugins\":\"http: Map<String, String> preferences = singletonMap("theia-user-preferences", json); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); ObjectMeta podMeta = new ObjectMetaBuilder().withName("wksp").build(); when(pod.getMetadata()).thenReturn(podMeta); when(pod.getSpec()).thenReturn(podSpec); when(podSpec.getContainers()).thenReturn(singletonList(container)); List<VolumeMount> volumeMounts = new ArrayList<>(); when(container.getVolumeMounts()).thenReturn(volumeMounts); k8sEnv.addPod(pod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(volumeMounts.size(), 1); VolumeMount mount = volumeMounts.get(0); assertEquals(mount.getMountPath(), "/etc/gitconfig"); assertEquals(mount.getName(), "gitconfigvolume"); assertFalse(mount.getReadOnly()); assertEquals(mount.getSubPath(), "gitconfig"); assertEquals(k8sEnv.getConfigMaps().size(), 1); assertTrue(k8sEnv.getConfigMaps().containsKey("wksp-gitconfig")); ConfigMap configMap = k8sEnv.getConfigMaps().get("wksp-gitconfig"); assertEquals(configMap.getData().size(), 1); assertTrue(configMap.getData().containsKey("gitconfig")); String gitconfig = configMap.getData().get("gitconfig"); String expectedGitconfig = "[user]\n\tname = user\n\temail = email\n"; assertEquals(gitconfig, expectedGitconfig); }
@Test public void testShouldProvisionNameAndEmailFromUserManagerWhenUserPreferencesEmpty() throws Exception { String json = "{}"; Map<String, String> preferences = singletonMap("theia-user-preferences", json); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); ObjectMeta podMeta = new ObjectMetaBuilder().withName("wksp").build(); when(pod.getMetadata()).thenReturn(podMeta); when(pod.getSpec()).thenReturn(podSpec); when(podSpec.getContainers()).thenReturn(singletonList(container)); User userMock = mock(User.class); when(userMock.getName()).thenReturn("userMockName"); when(userMock.getEmail()).thenReturn("userMockEmail"); when(userManager.getById(eq("id"))).thenReturn(userMock); List<VolumeMount> volumeMounts = new ArrayList<>(); when(container.getVolumeMounts()).thenReturn(volumeMounts); k8sEnv.addPod(pod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(volumeMounts.size(), 1); VolumeMount mount = volumeMounts.get(0); assertEquals(mount.getMountPath(), "/etc/gitconfig"); assertEquals(mount.getName(), "gitconfigvolume"); assertFalse(mount.getReadOnly()); assertEquals(mount.getSubPath(), "gitconfig"); assertEquals(k8sEnv.getConfigMaps().size(), 1); assertTrue(k8sEnv.getConfigMaps().containsKey("wksp-gitconfig")); ConfigMap configMap = k8sEnv.getConfigMaps().get("wksp-gitconfig"); assertEquals(configMap.getData().size(), 1); assertTrue(configMap.getData().containsKey("gitconfig")); String gitconfig = configMap.getData().get("gitconfig"); String expectedGitconfig = "[user]\n\tname = userMockName\n\temail = userMockEmail\n"; assertEquals(gitconfig, expectedGitconfig); }
@Test public void testShouldProvisionConfigForHttpsServer() throws Exception { when(vcsSslCertificateProvisioner.isConfigured()).thenReturn(true); when(vcsSslCertificateProvisioner.getGitServerHost()).thenReturn("https: when(vcsSslCertificateProvisioner.getCertPath()).thenReturn("/some/path"); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); ObjectMeta podMeta = new ObjectMetaBuilder().withName("wksp").build(); when(pod.getMetadata()).thenReturn(podMeta); when(pod.getSpec()).thenReturn(podSpec); when(podSpec.getContainers()).thenReturn(singletonList(container)); User userMock = mock(User.class); when(userMock.getName()).thenReturn("userMockName"); when(userMock.getEmail()).thenReturn("userMockEmail"); when(userManager.getById(eq("id"))).thenReturn(userMock); List<VolumeMount> volumeMounts = new ArrayList<>(); when(container.getVolumeMounts()).thenReturn(volumeMounts); k8sEnv.addPod(pod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(volumeMounts.size(), 1); VolumeMount mount = volumeMounts.get(0); assertEquals(mount.getMountPath(), "/etc/gitconfig"); assertEquals(mount.getName(), "gitconfigvolume"); assertFalse(mount.getReadOnly()); assertEquals(mount.getSubPath(), "gitconfig"); assertEquals(k8sEnv.getConfigMaps().size(), 1); assertTrue(k8sEnv.getConfigMaps().containsKey("wksp-gitconfig")); ConfigMap configMap = k8sEnv.getConfigMaps().get("wksp-gitconfig"); assertEquals(configMap.getData().size(), 1); assertTrue(configMap.getData().containsKey("gitconfig")); String gitconfig = configMap.getData().get("gitconfig"); String expectedGitconfig = "[user]\n\tname = userMockName\n\temail = userMockEmail\n[http \"https: assertEquals(gitconfig, expectedGitconfig); }
@Test public void shouldNotProvisionVolumeButShouldMountInInjectablePods() throws Exception { Map<String, String> preferences = singletonMap( "theia-user-preferences", "{\"git.user.name\":\"user\",\"git.user.email\":\"email\"}"); when(preferenceManager.find(eq("id"), eq("theia-user-preferences"))).thenReturn(preferences); when(runtimeIdentity.getWorkspaceId()).thenReturn("wksp"); Pod pod = new PodBuilder() .withNewMetadata() .withName("wkspc") .and() .withNewSpec() .withContainers(new ContainerBuilder().withImage("image").build()) .and() .build(); k8sEnv = KubernetesEnvironment.builder().build(); k8sEnv.addPod(pod); Pod injectedPod = new PodBuilder() .withNewMetadata() .withName("injected") .and() .withNewSpec() .withContainers(new ContainerBuilder().withImage("image").build()) .and() .build(); k8sEnv.addInjectablePod("r", "i", injectedPod); gitConfigProvisioner.provision(k8sEnv, runtimeIdentity); assertEquals(pod.getSpec().getVolumes().size(), 1); assertEquals(injectedPod.getSpec().getVolumes().size(), 0); Container podContainer = pod.getSpec().getContainers().get(0); Container injectedPodContainer = injectedPod.getSpec().getContainers().get(0); assertEquals(podContainer.getVolumeMounts().size(), 1); assertEquals(injectedPodContainer.getVolumeMounts().size(), 1); }