Skip to content

Commit

Permalink
Code coverage node executor (#52)
Browse files Browse the repository at this point in the history
* use logger

* add test for Errors class

Co-authored-by: Karl DeBisschop <[email protected]>
  • Loading branch information
kdebisschop and Karl DeBisschop authored Mar 12, 2020
1 parent 6d748f1 commit 160fcd4
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/test/java/com/bioraft/rundeck/rancher/ErrorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.bioraft.rundeck.rancher;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;

import static org.junit.Assert.assertTrue;

public class ErrorsTest {

@Test(expected = InvocationTargetException.class)
public void testConstructorIsPrivate() throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
Constructor<Errors> constructor = Errors.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.bioraft.rundeck.rancher;

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.service.NodeExecutorResult;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepFailureReason;
import com.dtolabs.rundeck.core.storage.ResourceMeta;
import com.dtolabs.rundeck.core.storage.StorageTree;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.rundeck.storage.api.Resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.bioraft.rundeck.rancher.Constants.CONFIG_ACCESSKEY_PATH;
import static com.bioraft.rundeck.rancher.Constants.CONFIG_SECRETKEY_PATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RancherNodeExecutorPluginTest {

@Mock
ExecutionContext executionContext;

@Mock
StorageTree storageTree;

@Mock
Resource<ResourceMeta> resource;

@Mock
ResourceMeta resourceMeta;

@Mock
INodeEntry node;

Map<String, String> nodeAttributes;

@Before
public void setUp() {
nodeAttributes = new HashMap<>();
}

@Test
public void testDescription() {
RancherNodeExecutorPlugin nodeExecutorPlugin = new RancherNodeExecutorPlugin();
assertTrue(nodeExecutorPlugin.getDescription().getDescription().startsWith("Executes a command "));
}

@Test
public void serviceIsNotYetSupported() {
RancherNodeExecutorPlugin nodeExecutorPlugin = new RancherNodeExecutorPlugin();
String[] command = { "ls" };
nodeAttributes.put("type", "service");
when(node.getAttributes()).thenReturn(nodeAttributes);
NodeExecutorResult result = nodeExecutorPlugin.executeCommand(executionContext, command, node);
String message = "Node executor is not currently supported for services";
assertEquals(message, result.getFailureMessage());
assertEquals(StepFailureReason.PluginFailed, result.getFailureReason());
assertEquals(-1, result.getResultCode());
}

@Test
public void missingKeyCreatesFailure() {
RancherNodeExecutorPlugin nodeExecutorPlugin = new RancherNodeExecutorPlugin();
String[] command = { "ls" };
nodeAttributes.put("type", "container");
when(node.getAttributes()).thenReturn(nodeAttributes);
NodeExecutorResult result = nodeExecutorPlugin.executeCommand(executionContext, command, node);
String message = "Storage path is not defined.";
assertEquals(message, result.getFailureMessage());
assertEquals(StepFailureReason.IOFailure, result.getFailureReason());
assertEquals(-1, result.getResultCode());
}

@Test
public void missingKeyValueCreatesFailure() throws IOException {
RancherNodeExecutorPlugin nodeExecutorPlugin = new RancherNodeExecutorPlugin();
String[] command = { "ls" };
nodeAttributes.put("type", "container");
nodeAttributes.put(CONFIG_ACCESSKEY_PATH, "access_key");
nodeAttributes.put(CONFIG_SECRETKEY_PATH, "secret_key");
when(node.getAttributes()).thenReturn(nodeAttributes);
when(executionContext.getStorageTree()).thenReturn(storageTree);
when(storageTree.getResource(anyString())).thenReturn(resource);
when(resource.getContents()).thenReturn(resourceMeta);
doThrow(new IOException("Storage failure.")).when(resourceMeta).writeContent(any());
NodeExecutorResult result = nodeExecutorPlugin.executeCommand(executionContext, command, node);
String message = "Storage failure.";
assertEquals(message, result.getFailureMessage());
assertEquals(StepFailureReason.IOFailure, result.getFailureReason());
assertEquals(-1, result.getResultCode());
}
}

0 comments on commit 160fcd4

Please sign in to comment.