EnvVarEnvironmentProvisioner implements InternalEnvironmentProvisioner { @Override public void provision(RuntimeIdentity id, InternalEnvironment internalEnvironment) throws InfrastructureException { LOG.debug("Provisioning environment variables for workspace '{}'", id.getWorkspaceId()); for (EnvVarProvider envVarProvider : envVarProviders) { Pair<String, String> envVar = envVarProvider.get(id); if (envVar != null) { internalEnvironment .getMachines() .values() .forEach(m -> m.getEnv().putIfAbsent(envVar.first, envVar.second)); } } LOG.debug("Environment variables provisioning done for workspace '{}'", id.getWorkspaceId()); } @Inject  EnvVarEnvironmentProvisioner(Set<EnvVarProvider> envVarProviders); @Override void provision(RuntimeIdentity id, InternalEnvironment internalEnvironment);  }
@Test public void shouldProvideRuntimeIdentityToEnvProviders() throws Exception { EnvVarEnvironmentProvisioner provisioner = new EnvVarEnvironmentProvisioner(singleton(provider1)); when(provider1.get(any())).thenReturn(Pair.of("test", "test")); provisioner.provision(RUNTIME_IDENTITY, internalEnvironment); verify(provider1).get(eq(RUNTIME_IDENTITY)); }
@Test public void shouldCallAllProviders() throws Exception { EnvVarEnvironmentProvisioner provisioner = new EnvVarEnvironmentProvisioner(ImmutableSet.of(provider1, provider2)); when(provider1.get(any())).thenReturn(Pair.of("test", "test")); when(provider2.get(any())).thenReturn(Pair.of("test", "test")); provisioner.provision(RUNTIME_IDENTITY, internalEnvironment); verify(provider1).get(eq(RUNTIME_IDENTITY)); verify(provider2).get(eq(RUNTIME_IDENTITY)); }
@Test public void shouldAddAllEnvVarsToAllContainers() throws Exception { EnvVarEnvironmentProvisioner provisioner = new EnvVarEnvironmentProvisioner(ImmutableSet.of(provider1, provider2)); Pair<String, String> envVar1 = Pair.of("env1", "value1"); Pair<String, String> envVar2 = Pair.of("env2", "value2"); ImmutableMap<String, String> envVarsFromProviders = ImmutableMap.of( envVar1.first, envVar1.second, envVar2.first, envVar2.second); when(provider1.get(any(RuntimeIdentity.class))).thenReturn(envVar1); when(provider2.get(any(RuntimeIdentity.class))).thenReturn(envVar2); provisioner.provision(RUNTIME_IDENTITY, internalEnvironment); assertEquals(machine1Env, envVarsFromProviders); assertEquals(machine2Env, envVarsFromProviders); }
@Test public void shouldNotRemoveExistingEnvVarsWithDifferentNames() throws Exception { EnvVarEnvironmentProvisioner provisioner = new EnvVarEnvironmentProvisioner(singleton(provider1)); Pair<String, String> existingEnvVar = Pair.of("existingEnvVar", "some-value"); machine1Env.put(existingEnvVar.first, existingEnvVar.second); Pair<String, String> envVar1 = Pair.of("env1", "value1"); when(provider1.get(any(RuntimeIdentity.class))).thenReturn(envVar1); provisioner.provision(RUNTIME_IDENTITY, internalEnvironment); assertEquals( ImmutableMap.of(existingEnvVar.first, existingEnvVar.second, envVar1.first, envVar1.second), machine1Env); }
@Test public void shouldNotReplaceExistingEnvVarsWithMatchingNames() throws Exception { EnvVarEnvironmentProvisioner provisioner = new EnvVarEnvironmentProvisioner(singleton(provider1)); String existingEnvVarName = "existingEnvVar"; String oldEnvVarValue = "some-value"; machine1Env.put(existingEnvVarName, oldEnvVarValue); String envVarValueFromProvider = "value1"; when(provider1.get(any(RuntimeIdentity.class))) .thenReturn(Pair.of(existingEnvVarName, envVarValueFromProvider)); provisioner.provision(RUNTIME_IDENTITY, internalEnvironment); assertEquals(ImmutableMap.of(existingEnvVarName, oldEnvVarValue), machine1Env); }