Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync environments when a config repo is deleted #6879

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -141,6 +141,10 @@ public boolean isReferenceAllowed(ConfigOrigin from, ConfigOrigin to) {
return true;
}

public boolean hasConfigRepo(String configRepoId) {
return this.getConfigRepo(configRepoId) != null;
}

private boolean isLocal(ConfigOrigin from) {
// we assume that configuration is local (from file or from UI) when origin is not specified
if (from == null) {
Expand Down
Expand Up @@ -105,4 +105,17 @@ public void shouldNotErrorWhenReposFingerprintDiffer() {
assertThat(repo1.errors().isEmpty(), is(true));
assertThat(repo2.errors().isEmpty(), is(true));
}

@Test
public void shouldReturnTrueIfContainsConfigRepoWithTheSpecifiedId() {
ConfigRepoConfig repo = new ConfigRepoConfig(git("http://git1"), "myplugin", "id");
repos.add(repo);

assertThat(repos.hasConfigRepo(repo.getId()), is(true));
}

@Test
public void shouldReturnFalseIfDoesNotContainTheConfigRepoWithTheSpecifiedId() {
assertThat(repos.hasConfigRepo("unknown"), is(false));
}
}
Expand Up @@ -21,6 +21,7 @@
import com.thoughtworks.go.config.exceptions.EntityType;
import com.thoughtworks.go.config.exceptions.GoConfigInvalidException;
import com.thoughtworks.go.config.exceptions.RecordNotFoundException;
import com.thoughtworks.go.config.remote.ConfigRepoConfig;
import com.thoughtworks.go.config.update.AddEnvironmentCommand;
import com.thoughtworks.go.config.update.DeleteEnvironmentCommand;
import com.thoughtworks.go.config.update.PatchEnvironmentCommand;
Expand Down Expand Up @@ -79,6 +80,15 @@ public void onEntityConfigChange(EnvironmentConfig entity) {
sync(goConfigService.getEnvironments());
}
});

goConfigService.register(new EntityConfigChangedListener<ConfigRepoConfig>() {
@Override
public void onEntityConfigChange(ConfigRepoConfig entity) {
if(!goConfigService.getCurrentConfig().getConfigRepos().hasConfigRepo(entity.getId())) {
sync(goConfigService.getEnvironments());
}
}
});
}

public void sync(EnvironmentsConfig environments) {
Expand Down
Expand Up @@ -22,18 +22,19 @@
import com.thoughtworks.go.config.remote.RepoConfigOrigin;
import com.thoughtworks.go.domain.*;
import com.thoughtworks.go.helper.EnvironmentConfigMother;
import com.thoughtworks.go.listener.EntityConfigChangedListener;
import com.thoughtworks.go.presentation.environment.EnvironmentPipelineModel;
import com.thoughtworks.go.server.domain.Username;
import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult;
import com.thoughtworks.go.util.command.EnvironmentVariableContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.*;

import static com.thoughtworks.go.helper.EnvironmentConfigMother.*;
import static com.thoughtworks.go.helper.EnvironmentConfigMother.environment;
import static com.thoughtworks.go.helper.EnvironmentConfigMother.environments;
import static com.thoughtworks.go.helper.PipelineConfigMother.pipelineConfig;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -57,6 +58,7 @@ void setUp() throws Exception {
void shouldRegisterAsACruiseConfigChangeListener() {
environmentConfigService.initialize();
Mockito.verify(mockGoConfigService).register(environmentConfigService);
verify(mockGoConfigService, times(3)).register(any(EntityConfigChangedListener.class));
}

@Test
Expand Down
Expand Up @@ -17,7 +17,11 @@

import com.thoughtworks.go.config.*;
import com.thoughtworks.go.config.exceptions.EntityType;
import com.thoughtworks.go.config.remote.ConfigRepoConfig;
import com.thoughtworks.go.config.remote.PartialConfig;
import com.thoughtworks.go.config.remote.RepoConfigOrigin;
import com.thoughtworks.go.domain.ConfigElementForEdit;
import com.thoughtworks.go.helper.PartialConfigMother;
import com.thoughtworks.go.helper.PipelineConfigMother;
import com.thoughtworks.go.server.domain.Username;
import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult;
Expand Down Expand Up @@ -59,6 +63,8 @@ public class EnvironmentConfigServiceIntegrationTest {
private AgentConfigService agentConfigService;
@Autowired
private EnvironmentConfigService service;
@Autowired
private ConfigRepoService configRepoService;

private GoConfigFileHelper configHelper = new GoConfigFileHelper();

Expand Down Expand Up @@ -138,7 +144,7 @@ public void shouldPointOutDuplicatePipelinesInAnEnvironmentOnEnvironmentPatch()

HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
BasicEnvironmentConfig environmentConfigBeingUpdated = new BasicEnvironmentConfig(new CaseInsensitiveString(environmentBeingUpdated));
service.patchEnvironment(environmentConfigBeingUpdated, Arrays.asList(pipelineName),new ArrayList<>(),new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
service.patchEnvironment(environmentConfigBeingUpdated, Arrays.asList(pipelineName), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
user, result);
assertThat(result.message(), is("Failed to update environment 'environment-2'. Associating pipeline(s) which is already part of environment-1 environment"));
}
Expand Down Expand Up @@ -308,6 +314,45 @@ public void shouldPopulateResultWithErrorIfEnvNotFound() {
assertThat(edit, is(nullValue()));
}

@Test
public void shouldSyncEnvironmentsIfAConfigRepoIsRemoved() {
String uuid = "uuid-1";
String envName = "env";
Username user = Username.ANONYMOUS;
agentConfigService.addAgent(new AgentConfig(uuid, "host-1", "192.168.1.2"), user);
String configRepoId = createMergeEnvironment(envName, uuid);

EnvironmentConfig envConfig = service.getEnvironmentConfig(envName);

assertThat(envConfig.getAgents().size(), is(1));
assertThat(envConfig.getAgents().getUuids(), contains(uuid));

HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
configRepoService.deleteConfigRepo(configRepoId, user, result);

EnvironmentConfig envConfigPostDelete = service.getEnvironmentConfig(envName);

assertThat(envConfigPostDelete.getAgents().size(), is(0));
}

private String createMergeEnvironment(String envName, String agentUuid) {
RepoConfigOrigin repoConfigOrigin = PartialConfigMother.createRepoOrigin();
ConfigRepoConfig configRepo = repoConfigOrigin.getConfigRepo();
PartialConfig partialConfig = new PartialConfig();
BasicEnvironmentConfig envConfig = new BasicEnvironmentConfig(new CaseInsensitiveString(envName));
envConfig.addAgent(agentUuid);
partialConfig.getEnvironments().add(envConfig);
partialConfig.setOrigins(repoConfigOrigin);
goConfigService.updateConfig(cruiseConfig -> {
cruiseConfig.getConfigRepos().add(configRepo);
cruiseConfig.getPartials().add(partialConfig);
cruiseConfig.addEnvironment(envName);
return cruiseConfig;
});
goConfigService.forceNotifyListeners();
return configRepo.getId();
}

private BasicEnvironmentConfig environmentConfig(String name) {
return new BasicEnvironmentConfig(new CaseInsensitiveString(name));
}
Expand Down