Thomas Vachuska

Adding ability to delay before executing a step in STC.

Change-Id: I9822ac35d37e6735068ebdf39b952db913a10e14
...@@ -73,7 +73,7 @@ ...@@ -73,7 +73,7 @@
73 exec="mvn clean install"/> 73 exec="mvn clean install"/>
74 <step name="Install-REST-App" requires="^,~Verify-UI-Topo" 74 <step name="Install-REST-App" requires="^,~Verify-UI-Topo"
75 exec="onos-app ${OCI} reinstall! target/test-app-1.2.3.oar"/> 75 exec="onos-app ${OCI} reinstall! target/test-app-1.2.3.oar"/>
76 - <Xstep name="Verify-REST-App" requires="^" delay="2" 76 + <step name="Verify-REST-App" requires="^" delay="2"
77 exec="curl --fail -isS --user ${ONOS_WEB_USER}:${ONOS_WEB_PASS} http://${OCI}:8181/onos/test-app/sample"/> 77 exec="curl --fail -isS --user ${ONOS_WEB_USER}:${ONOS_WEB_PASS} http://${OCI}:8181/onos/test-app/sample"/>
78 78
79 <step name="Uninstall-App" requires="^" 79 <step name="Uninstall-App" requires="^"
......
...@@ -32,6 +32,7 @@ import java.util.Set; ...@@ -32,6 +32,7 @@ import java.util.Set;
32 32
33 import static com.google.common.base.Preconditions.*; 33 import static com.google.common.base.Preconditions.*;
34 import static com.google.common.base.Strings.isNullOrEmpty; 34 import static com.google.common.base.Strings.isNullOrEmpty;
35 +import static java.lang.Integer.parseInt;
35 import static org.onlab.graph.DepthFirstSearch.EdgeType.BACK_EDGE; 36 import static org.onlab.graph.DepthFirstSearch.EdgeType.BACK_EDGE;
36 import static org.onlab.graph.GraphPathSearch.ALL_PATHS; 37 import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
37 import static org.onlab.stc.Scenario.loadScenario; 38 import static org.onlab.stc.Scenario.loadScenario;
...@@ -56,6 +57,7 @@ public class Compiler { ...@@ -56,6 +57,7 @@ public class Compiler {
56 private static final String COMMAND = "[@exec]"; 57 private static final String COMMAND = "[@exec]";
57 private static final String ENV = "[@env]"; 58 private static final String ENV = "[@env]";
58 private static final String CWD = "[@cwd]"; 59 private static final String CWD = "[@cwd]";
60 + private static final String DELAY = "[@delay]";
59 private static final String REQUIRES = "[@requires]"; 61 private static final String REQUIRES = "[@requires]";
60 private static final String IF = "[@if]"; 62 private static final String IF = "[@if]";
61 private static final String UNLESS = "[@unless]"; 63 private static final String UNLESS = "[@unless]";
...@@ -245,9 +247,10 @@ public class Compiler { ...@@ -245,9 +247,10 @@ public class Compiler {
245 String command = expand(cfg.getString(COMMAND, parentGroup != null ? parentGroup.command() : null), true); 247 String command = expand(cfg.getString(COMMAND, parentGroup != null ? parentGroup.command() : null), true);
246 String env = expand(cfg.getString(ENV, parentGroup != null ? parentGroup.env() : null)); 248 String env = expand(cfg.getString(ENV, parentGroup != null ? parentGroup.env() : null));
247 String cwd = expand(cfg.getString(CWD, parentGroup != null ? parentGroup.cwd() : null)); 249 String cwd = expand(cfg.getString(CWD, parentGroup != null ? parentGroup.cwd() : null));
250 + int delay = parseInt(expand(cfg.getString(DELAY, parentGroup != null ? "" + parentGroup.delay() : "0")));
248 251
249 - print("step name=%s command=%s env=%s cwd=%s", name, command, env, cwd); 252 + print("step name=%s command=%s env=%s cwd=%s delay=%d", name, command, env, cwd, delay);
250 - Step step = new Step(name, command, env, cwd, parentGroup); 253 + Step step = new Step(name, command, env, cwd, parentGroup, delay);
251 registerStep(step, cfg, namespace, parentGroup); 254 registerStep(step, cfg, namespace, parentGroup);
252 } 255 }
253 256
...@@ -264,9 +267,10 @@ public class Compiler { ...@@ -264,9 +267,10 @@ public class Compiler {
264 String command = expand(cfg.getString(COMMAND, parentGroup != null ? parentGroup.command() : null), true); 267 String command = expand(cfg.getString(COMMAND, parentGroup != null ? parentGroup.command() : null), true);
265 String env = expand(cfg.getString(ENV, parentGroup != null ? parentGroup.env() : null)); 268 String env = expand(cfg.getString(ENV, parentGroup != null ? parentGroup.env() : null));
266 String cwd = expand(cfg.getString(CWD, parentGroup != null ? parentGroup.cwd() : null)); 269 String cwd = expand(cfg.getString(CWD, parentGroup != null ? parentGroup.cwd() : null));
270 + int delay = parseInt(expand(cfg.getString(DELAY, parentGroup != null ? "" + parentGroup.delay() : "0")));
267 271
268 - print("group name=%s command=%s env=%s cwd=%s", name, command, env, cwd); 272 + print("group name=%s command=%s env=%s cwd=%s delay=%d", name, command, env, cwd, delay);
269 - Group group = new Group(name, command, env, cwd, parentGroup); 273 + Group group = new Group(name, command, env, cwd, parentGroup, delay);
270 if (registerStep(group, cfg, namespace, parentGroup)) { 274 if (registerStep(group, cfg, namespace, parentGroup)) {
271 compile(cfg, namespace, group); 275 compile(cfg, namespace, group);
272 } 276 }
......
...@@ -35,9 +35,10 @@ public class Group extends Step { ...@@ -35,9 +35,10 @@ public class Group extends Step {
35 * @param env default path to file to be sourced into the environment 35 * @param env default path to file to be sourced into the environment
36 * @param cwd default path to current working directory for the step 36 * @param cwd default path to current working directory for the step
37 * @param group optional group to which this step belongs 37 * @param group optional group to which this step belongs
38 + * @param delay seconds to delay before executing
38 */ 39 */
39 - public Group(String name, String command, String env, String cwd, Group group) { 40 + public Group(String name, String command, String env, String cwd, Group group, int delay) {
40 - super(name, command, env, cwd, group); 41 + super(name, command, env, cwd, group, delay);
41 } 42 }
42 43
43 /** 44 /**
......
...@@ -32,6 +32,7 @@ public class Step implements Vertex { ...@@ -32,6 +32,7 @@ public class Step implements Vertex {
32 protected final String env; 32 protected final String env;
33 protected final String cwd; 33 protected final String cwd;
34 protected final Group group; 34 protected final Group group;
35 + protected final int delay;
35 36
36 /** 37 /**
37 * Creates a new test step. 38 * Creates a new test step.
...@@ -41,10 +42,12 @@ public class Step implements Vertex { ...@@ -41,10 +42,12 @@ public class Step implements Vertex {
41 * @param env path to file to be sourced into the environment 42 * @param env path to file to be sourced into the environment
42 * @param cwd path to current working directory for the step 43 * @param cwd path to current working directory for the step
43 * @param group optional group to which this step belongs 44 * @param group optional group to which this step belongs
45 + * @param delay seconds to delay before executing
44 */ 46 */
45 - public Step(String name, String command, String env, String cwd, Group group) { 47 + public Step(String name, String command, String env, String cwd, Group group, int delay) {
46 this.name = checkNotNull(name, "Name cannot be null"); 48 this.name = checkNotNull(name, "Name cannot be null");
47 this.group = group; 49 this.group = group;
50 + this.delay = delay;
48 51
49 // Set the command, environment and cwd 52 // Set the command, environment and cwd
50 // If one is not given use the value from the enclosing group 53 // If one is not given use the value from the enclosing group
...@@ -98,6 +101,14 @@ public class Step implements Vertex { ...@@ -98,6 +101,14 @@ public class Step implements Vertex {
98 return group; 101 return group;
99 } 102 }
100 103
104 + /**
105 + * Returns the start delay in seconds.
106 + *
107 + * @return number of seconds
108 + */
109 + public int delay() {
110 + return delay;
111 + }
101 112
102 @Override 113 @Override
103 public int hashCode() { 114 public int hashCode() {
...@@ -124,6 +135,7 @@ public class Step implements Vertex { ...@@ -124,6 +135,7 @@ public class Step implements Vertex {
124 .add("env", env) 135 .add("env", env)
125 .add("cwd", cwd) 136 .add("cwd", cwd)
126 .add("group", group) 137 .add("group", group)
138 + .add("delay", delay)
127 .toString(); 139 .toString();
128 } 140 }
129 } 141 }
......
...@@ -38,6 +38,7 @@ class StepProcessor implements Runnable { ...@@ -38,6 +38,7 @@ class StepProcessor implements Runnable {
38 private static final String NEGATE_CODE = "!"; 38 private static final String NEGATE_CODE = "!";
39 39
40 private static final int FAIL = -1; 40 private static final int FAIL = -1;
41 + private static final int SECONDS = 1_000;
41 42
42 static String launcher = "stc-launcher "; 43 static String launcher = "stc-launcher ";
43 44
...@@ -67,6 +68,7 @@ class StepProcessor implements Runnable { ...@@ -67,6 +68,7 @@ class StepProcessor implements Runnable {
67 @Override 68 @Override
68 public void run() { 69 public void run() {
69 delegate.onStart(step, command); 70 delegate.onStart(step, command);
71 + delayIfNeeded();
70 int code = execute(); 72 int code = execute();
71 boolean ignoreCode = step.env() != null && step.env.equals(IGNORE_CODE); 73 boolean ignoreCode = step.env() != null && step.env.equals(IGNORE_CODE);
72 boolean negateCode = step.env() != null && step.env.equals(NEGATE_CODE); 74 boolean negateCode = step.env() != null && step.env.equals(NEGATE_CODE);
...@@ -76,6 +78,19 @@ class StepProcessor implements Runnable { ...@@ -76,6 +78,19 @@ class StepProcessor implements Runnable {
76 } 78 }
77 79
78 /** 80 /**
81 + * Pauses if the step requires it.
82 + */
83 + private void delayIfNeeded() {
84 + if (step.delay() > 0) {
85 + try {
86 + Thread.sleep(step.delay() * SECONDS);
87 + } catch (InterruptedException e) {
88 + throw new RuntimeException("Interrupted", e);
89 + }
90 + }
91 + }
92 +
93 + /**
79 * Executes the step process. 94 * Executes the step process.
80 * 95 *
81 * @return exit code 96 * @return exit code
......
...@@ -32,8 +32,8 @@ public class DependencyTest extends StepTest { ...@@ -32,8 +32,8 @@ public class DependencyTest extends StepTest {
32 @Before 32 @Before
33 public void setUp() throws ConfigurationException { 33 public void setUp() throws ConfigurationException {
34 super.setUp(); 34 super.setUp();
35 - step1 = new Step("step1", CMD, null, null, null); 35 + step1 = new Step("step1", CMD, null, null, null, 0);
36 - step2 = new Step("step2", CMD, null, null, null); 36 + step2 = new Step("step2", CMD, null, null, null, 0);
37 } 37 }
38 38
39 @Test 39 @Test
......
...@@ -28,23 +28,24 @@ public class GroupTest extends StepTest { ...@@ -28,23 +28,24 @@ public class GroupTest extends StepTest {
28 28
29 @Test 29 @Test
30 public void basics() { 30 public void basics() {
31 - Group group = new Group(NAME, CMD, ENV, CWD, parent); 31 + Group group = new Group(NAME, CMD, ENV, CWD, parent, 1);
32 assertEquals("incorrect name", NAME, group.name()); 32 assertEquals("incorrect name", NAME, group.name());
33 assertEquals("incorrect command", CMD, group.command()); 33 assertEquals("incorrect command", CMD, group.command());
34 assertEquals("incorrect env", ENV, group.env()); 34 assertEquals("incorrect env", ENV, group.env());
35 assertEquals("incorrect cwd", CWD, group.cwd()); 35 assertEquals("incorrect cwd", CWD, group.cwd());
36 assertSame("incorrect group", parent, group.group()); 36 assertSame("incorrect group", parent, group.group());
37 + assertEquals("incorrect delay", 1, group.delay());
37 38
38 - Step step = new Step("step", null, null, null, group); 39 + Step step = new Step("step", null, null, null, group, 0);
39 group.addChild(step); 40 group.addChild(step);
40 assertSame("incorrect child", step, group.children().iterator().next()); 41 assertSame("incorrect child", step, group.children().iterator().next());
41 } 42 }
42 43
43 @Test 44 @Test
44 public void equality() { 45 public void equality() {
45 - Group g1 = new Group(NAME, CMD, null, null, parent); 46 + Group g1 = new Group(NAME, CMD, null, null, parent, 0);
46 - Group g2 = new Group(NAME, CMD, ENV, CWD, null); 47 + Group g2 = new Group(NAME, CMD, ENV, CWD, null, 0);
47 - Group g3 = new Group("foo", null, null, null, parent); 48 + Group g3 = new Group("foo", null, null, null, parent, 0);
48 new EqualsTester() 49 new EqualsTester()
49 .addEqualityGroup(g1, g2) 50 .addEqualityGroup(g1, g2)
50 .addEqualityGroup(g3) 51 .addEqualityGroup(g3)
......
...@@ -50,7 +50,7 @@ public class StepProcessorTest { ...@@ -50,7 +50,7 @@ public class StepProcessorTest {
50 50
51 @Test 51 @Test
52 public void basics() { 52 public void basics() {
53 - Step step = new Step("foo", "ls " + DIR.getAbsolutePath(), null, null, null); 53 + Step step = new Step("foo", "ls " + DIR.getAbsolutePath(), null, null, null, 0);
54 StepProcessor processor = new StepProcessor(step, DIR, delegate, step.command()); 54 StepProcessor processor = new StepProcessor(step, DIR, delegate, step.command());
55 processor.run(); 55 processor.run();
56 assertTrue("should be started", delegate.started); 56 assertTrue("should be started", delegate.started);
......
...@@ -36,24 +36,25 @@ public class StepTest { ...@@ -36,24 +36,25 @@ public class StepTest {
36 36
37 @Before 37 @Before
38 public void setUp() throws ConfigurationException { 38 public void setUp() throws ConfigurationException {
39 - parent = new Group("parent", null, null, null, null); 39 + parent = new Group("parent", null, null, null, null, 0);
40 } 40 }
41 41
42 @Test 42 @Test
43 public void basics() { 43 public void basics() {
44 - Step step = new Step(NAME, CMD, ENV, CWD, parent); 44 + Step step = new Step(NAME, CMD, ENV, CWD, parent, 1);
45 assertEquals("incorrect name", NAME, step.name()); 45 assertEquals("incorrect name", NAME, step.name());
46 assertEquals("incorrect command", CMD, step.command()); 46 assertEquals("incorrect command", CMD, step.command());
47 assertEquals("incorrect env", ENV, step.env()); 47 assertEquals("incorrect env", ENV, step.env());
48 assertEquals("incorrect cwd", CWD, step.cwd()); 48 assertEquals("incorrect cwd", CWD, step.cwd());
49 assertSame("incorrect group", parent, step.group()); 49 assertSame("incorrect group", parent, step.group());
50 + assertEquals("incorrect delay", 1, step.delay());
50 } 51 }
51 52
52 @Test 53 @Test
53 public void equality() { 54 public void equality() {
54 - Step s1 = new Step(NAME, CMD, null, null, parent); 55 + Step s1 = new Step(NAME, CMD, null, null, parent, 0);
55 - Step s2 = new Step(NAME, CMD, ENV, CWD, null); 56 + Step s2 = new Step(NAME, CMD, ENV, CWD, null, 0);
56 - Step s3 = new Step("foo", null, null, null, parent); 57 + Step s3 = new Step("foo", null, null, null, parent, 0);
57 new EqualsTester() 58 new EqualsTester()
58 .addEqualityGroup(s1, s2) 59 .addEqualityGroup(s1, s2)
59 .addEqualityGroup(s3) 60 .addEqualityGroup(s3)
......