/*
* Copyright 2017 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.thoughtworks.go.agent;
import com.thoughtworks.go.agent.service.AgentUpgradeService;
import com.thoughtworks.go.agent.service.SslInfrastructureService;
import com.thoughtworks.go.config.AgentRegistry;
import com.thoughtworks.go.plugin.access.packagematerial.PackageRepositoryExtension;
import com.thoughtworks.go.plugin.access.pluggabletask.TaskExtension;
import com.thoughtworks.go.plugin.access.scm.SCMExtension;
import com.thoughtworks.go.plugin.infra.PluginManager;
import com.thoughtworks.go.publishers.GoArtifactsManipulator;
import com.thoughtworks.go.remote.AgentIdentifier;
import com.thoughtworks.go.remote.BuildRepositoryRemote;
import com.thoughtworks.go.remote.work.Work;
import com.thoughtworks.go.util.HttpService;
import com.thoughtworks.go.util.SubprocessLogger;
import com.thoughtworks.go.util.SystemEnvironment;
import org.apache.http.client.HttpClient;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import java.security.GeneralSecurityException;
import static com.thoughtworks.go.util.SystemUtil.getFirstLocalNonLoopbackIpAddress;
import static com.thoughtworks.go.util.SystemUtil.getLocalhostName;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class AgentControllerTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Mock
private BuildRepositoryRemote loopServer;
@Mock
private GoArtifactsManipulator artifactsManipulator;
@Mock
private SslInfrastructureService sslInfrastructureService;
@Mock
private Work work;
@Mock
private SubprocessLogger subprocessLogger;
@Mock
private SystemEnvironment systemEnvironment;
@Mock
private AgentUpgradeService agentUpgradeService;
@Mock
private PluginManager pluginManager;
@Mock
private PackageRepositoryExtension packageRepositoryExtension;
@Mock
private SCMExtension scmExtension;
@Mock
private TaskExtension taskExtension;
@Mock
private HttpService httpService;
@Mock
private HttpClient httpClient;
private AgentController agentController;
private String agentUuid = "uuid";
private AgentIdentifier agentIdentifier;
@Mock
private AgentRegistry agentRegistry;
@Before
public void setUp() throws Exception {
agentIdentifier = new AgentIdentifier(getLocalhostName(), getFirstLocalNonLoopbackIpAddress(), agentUuid);
}
@Test
public void shouldReturnTrueIfCausedBySecurity() throws Exception {
Exception exception = new Exception(new RuntimeException(new GeneralSecurityException()));
agentController = createAgentController();
assertTrue(agentController.isCausedBySecurity(exception));
}
@Test
public void shouldReturnFalseIfNotCausedBySecurity() throws Exception {
Exception exception = new Exception(new IOException());
agentController = createAgentController();
assertFalse(agentController.isCausedBySecurity(exception));
}
@Test
public void shouldUpgradeAgentBeforeAgentRegistration() throws Exception {
agentController = createAgentController();
InOrder inOrder = inOrder(agentUpgradeService, sslInfrastructureService);
agentController.loop();
inOrder.verify(agentUpgradeService).checkForUpgrade();
inOrder.verify(sslInfrastructureService).registerIfNecessary(agentController.getAgentAutoRegistrationProperties());
}
private AgentController createAgentController() {
return new AgentController(sslInfrastructureService, systemEnvironment, agentRegistry, pluginManager,
subprocessLogger, agentUpgradeService) {
@Override
public void ping() {
}
@Override
public void execute() {
}
@Override
protected void work() throws Exception {
}
};
}
}