-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use logger * add test for Errors class Co-authored-by: Karl DeBisschop <[email protected]>
- Loading branch information
1 parent
6d748f1
commit 160fcd4
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/test/java/com/bioraft/rundeck/rancher/RancherNodeExecutorPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |