Skip to content

Commit cb17074

Browse files
committed
add spock tests
1 parent bfdafcd commit cb17074

File tree

5 files changed

+259
-25
lines changed

5 files changed

+259
-25
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.gradle/
3+
*.ipr
4+
*.iml
5+
*.iws
6+
.idea/
7+
build/

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ dependencies {
4040
testImplementation 'junit:junit:4.13.2'
4141
testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'
4242
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
43+
testImplementation 'cglib:cglib-nodep:3.3.0'
44+
testImplementation 'org.objenesis:objenesis:3.2'
4345
}
4446

4547
// task to copy plugin libs to output/lib dir

src/main/java/com/plugin/sequentialcommands/PluginUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package com.plugin.sequentialcommands;
22

3+
import com.dtolabs.rundeck.core.common.INodeEntry;
34
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
5+
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepException;
46
import com.dtolabs.rundeck.core.storage.ResourceMeta;
57
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
68

79
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
811

912
public class PluginUtil {
1013

11-
public static String getPasswordFromKeyStorage(String path, PluginStepContext context) throws StepException {
14+
public static String getPasswordFromKeyStorage(String path, PluginStepContext context, INodeEntry node) throws NodeStepException {
1215
try{
1316
ResourceMeta contents = context.getExecutionContext().getStorageTree().getResource(path).getContents();
1417
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1518
contents.writeContent(byteArrayOutputStream);
1619

1720
return byteArrayOutputStream.toString();
1821
}catch(Exception e){
19-
return "Error accessing ${path} from Key Storage.";
22+
throw new NodeStepException("error accessing " + path + " from Key Storage: no resource for path", Sequentialcommands.Reason.KeyStorage, node.getNodename());
23+
// return "Error accessing ${path} from Key Storage.";
2024
}
2125
}
2226

src/main/java/com/plugin/sequentialcommands/Sequentialcommands.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.jcraft.jsch.JSch;
2121
import com.jcraft.jsch.JSchException;
2222
import com.jcraft.jsch.Session;
23+
import org.jaxen.expr.Step;
24+
2325
import java.io.IOException;
2426
import java.io.InputStream;
2527
import java.io.OutputStream;
@@ -67,7 +69,8 @@ public Description getDescription() {
6769
* This enum lists the known reasons this plugin might fail
6870
*/
6971
static enum Reason implements FailureReason{
70-
ExampleReason
72+
SSHKeyNotFound,
73+
KeyStorage
7174
}
7275

7376
@Override
@@ -86,16 +89,13 @@ public void executeNodeStep(final PluginStepContext context,
8689
if (entry.getAttributes().get("ssh-password-storage-path") != null) {
8790
sshKeyStoragePath = entry.getAttributes().get("ssh-password-storage-path");
8891
usePrivKey = false;
89-
} else {
92+
} else if (entry.getAttributes().get("ssh-key-storage-path") != null) {
9093
sshKeyStoragePath = entry.getAttributes().get("ssh-key-storage-path");
9194
usePrivKey = true;
95+
} else {
96+
throw new NodeStepException("SSH Key or Password must be defined as node attribute.", Reason.SSHKeyNotFound, entry.getNodename());
9297
}
93-
94-
try {
95-
sshPrivKey = PluginUtil.getPasswordFromKeyStorage(sshKeyStoragePath, context);
96-
} catch (StepException e) {
97-
e.printStackTrace();
98-
}
98+
sshPrivKey = PluginUtil.getPasswordFromKeyStorage(sshKeyStoragePath, context, entry);
9999

100100
Gson gson = new GsonBuilder().create();
101101
JsonArray customFields = gson.fromJson(configuration.get("custom").toString(), JsonArray.class);
Lines changed: 236 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.plugin.sequentialcommands
22

3+
import com.dtolabs.rundeck.core.common.INodeDesc
4+
import com.dtolabs.rundeck.core.execution.ExecutionContext
5+
import com.dtolabs.rundeck.core.execution.ExecutionListener
6+
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepException
37
import com.dtolabs.rundeck.plugins.step.PluginStepContext
48
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException
59
import com.dtolabs.rundeck.plugins.PluginLogger
610
import com.dtolabs.rundeck.core.common.INodeEntry
11+
import com.dtolabs.rundeck.core.common.Framework
12+
import com.dtolabs.rundeck.core.storage.keys.KeyStorageTree
713
import spock.lang.Specification
814

915
class SequentialcommandsSpec extends Specification {
@@ -14,45 +20,260 @@ class SequentialcommandsSpec extends Specification {
1420
}
1521
}
1622

17-
def "check Boolean parameter"(){
23+
def "Check SSH Key path"() {
1824

1925
given:
26+
def Sequential = new Sequentialcommands()
27+
def storageTree = Mock(KeyStorageTree)
28+
storageTree.getResource("keys/123") >> { throw NodeStepException(e);}
29+
def executionListener = Mock(ExecutionListener)
30+
def executionContext = Mock(ExecutionContext){
31+
getExecutionListener() >> executionListener
32+
getStorageTree() >> storageTree
2033

21-
def example = new Sequentialcommands()
22-
def context = getContext(Mock(PluginLogger))
23-
def node = Mock(INodeEntry){
24-
getNodename()>>"Test"
25-
getAttributes()>>["attr:name":"Test"]
26-
}
34+
}
35+
INodeEntry node = new INodeEntry() {
36+
@Override
37+
String getOsFamily() {
38+
return null
39+
}
2740

28-
def configuration = [example:"example123",exampleBoolean:"true"]
41+
@Override
42+
String getOsArch() {
43+
return null
44+
}
45+
46+
@Override
47+
String getOsVersion() {
48+
return null
49+
}
50+
51+
@Override
52+
String getOsName() {
53+
return null
54+
}
55+
56+
@Override
57+
String getNodename() {
58+
return null
59+
}
60+
61+
@Override
62+
String getUsername() {
63+
return null
64+
}
65+
66+
@Override
67+
boolean containsUserName() {
68+
return false
69+
}
70+
71+
@Override
72+
boolean containsPort() {
73+
return false
74+
}
2975

76+
@Override
77+
String extractUserName() {
78+
return null
79+
}
80+
81+
@Override
82+
String extractHostname() {
83+
return null
84+
}
85+
86+
@Override
87+
String extractPort() {
88+
return null
89+
}
90+
91+
@Override
92+
String getFrameworkProject() {
93+
return null
94+
}
95+
96+
@Override
97+
String getDescription() {
98+
return null
99+
}
100+
101+
@Override
102+
Set getTags() {
103+
return null
104+
}
105+
106+
@Override
107+
Map<String, String> getAttributes() {
108+
return ["nodename":"myDevice","username":"myUserName","ssh-password-storage-path":"keys/ssh-pass"]
109+
}
110+
111+
@Override
112+
String getHostname() {
113+
return null
114+
}
115+
116+
@Override
117+
boolean equals(INodeDesc n) {
118+
return false
119+
}
120+
}
121+
def context = Mock(PluginStepContext) {
122+
getExecutionContext() >> executionContext
123+
getLogger() >> Mock(PluginLogger)
124+
}
125+
def configuration = [region:"us-west-1",accountId:"1234567890"]
30126
when:
31-
example.executeNodeStep(context,configuration,node)
127+
Sequential.executeNodeStep(context,configuration,node)
32128

33129
then:
34-
thrown StepException
130+
NodeStepException e = thrown()
131+
e.message == "error accessing keys/ssh-pass from Key Storage: no resource for path"
132+
35133
}
36134

37-
def "run OK"(){
135+
def "Check SSH Key Node Attribute"() {
136+
137+
given:
138+
def Sequential = new Sequentialcommands()
139+
def context = Mock(PluginStepContext){
140+
getFramework() >> Mock(Framework)
141+
}
142+
INodeEntry node = new INodeEntry() {
143+
@Override
144+
String getOsFamily() {
145+
return null
146+
}
147+
148+
@Override
149+
String getOsArch() {
150+
return null
151+
}
152+
153+
@Override
154+
String getOsVersion() {
155+
return null
156+
}
157+
158+
@Override
159+
String getOsName() {
160+
return null
161+
}
162+
163+
@Override
164+
String getNodename() {
165+
return null
166+
}
167+
168+
@Override
169+
String getUsername() {
170+
return null
171+
}
172+
173+
@Override
174+
boolean containsUserName() {
175+
return false
176+
}
177+
178+
@Override
179+
boolean containsPort() {
180+
return false
181+
}
182+
183+
@Override
184+
String extractUserName() {
185+
return null
186+
}
187+
188+
@Override
189+
String extractHostname() {
190+
return null
191+
}
192+
193+
@Override
194+
String extractPort() {
195+
return null
196+
}
197+
198+
@Override
199+
String getFrameworkProject() {
200+
return null
201+
}
202+
203+
@Override
204+
String getDescription() {
205+
return null
206+
}
207+
208+
@Override
209+
Set getTags() {
210+
return null
211+
}
212+
213+
@Override
214+
Map<String, String> getAttributes() {
215+
return ["nodename":"myDevice","username":"myUserName"]
216+
}
217+
218+
@Override
219+
String getHostname() {
220+
return null
221+
}
222+
223+
@Override
224+
boolean equals(INodeDesc n) {
225+
return false
226+
}
227+
}
228+
def configuration = [region:"us-west-1",accountId:"1234567890"]
229+
when:
230+
Sequential.executeNodeStep(context,configuration,node)
231+
232+
then:
233+
NodeStepException e = thrown()
234+
e.message == "SSH Key or Password must be defined as node attribute."
235+
236+
}
237+
238+
def "check Boolean parameter"(){
38239

39240
given:
40241

41242
def example = new Sequentialcommands()
42-
def logger = Mock(PluginLogger)
43-
def context = getContext(logger)
243+
def context = getContext(Mock(PluginLogger))
44244
def node = Mock(INodeEntry){
45245
getNodename()>>"Test"
46246
getAttributes()>>["attr:name":"Test"]
47247
}
48248

49-
def configuration = [example:"example123",exampleBoolean:"false"]
249+
def configuration = [example:"example123",exampleBoolean:"true"]
50250

51251
when:
52252
example.executeNodeStep(context,configuration,node)
53253

54254
then:
55-
1 * logger.log(2, "Example node step executing on node: Test")
255+
thrown StepException
56256
}
57257

258+
// def "run OK"(){
259+
//
260+
// given:
261+
//
262+
// def example = new Sequentialcommands()
263+
// def logger = Mock(PluginLogger)
264+
// def context = getContext(logger)
265+
// def node = Mock(INodeEntry){
266+
// getNodename()>>"Test"
267+
// getAttributes()>>["attr:name":"Test"]
268+
// }
269+
//
270+
// def configuration = [example:"example123",exampleBoolean:"false"]
271+
//
272+
// when:
273+
// example.executeNodeStep(context,configuration,node)
274+
//
275+
// then:
276+
// 1 * logger.log(2, "Example node step executing on node: Test")
277+
// }
278+
58279
}

0 commit comments

Comments
 (0)