blob: a2f766fc0fb5d9bead285f2f1c2045508c0e30d7 [file] [log] [blame]
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.stc;
17
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070018import org.junit.BeforeClass;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080019import org.junit.ClassRule;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070020import org.junit.Test;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080021import org.junit.rules.TemporaryFolder;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070022import java.io.File;
Thomas Vachuska8044c092015-08-04 11:06:41 -070023import static com.google.common.base.Preconditions.checkState;
Thomas Vachuska86439372015-06-05 09:21:32 -070024import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertTrue;
26import static org.onlab.stc.Coordinator.Status.SUCCEEDED;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070027
28/**
29 * Test of the step processor.
30 */
31public class StepProcessorTest {
32
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080033 @ClassRule
34 public static TemporaryFolder testFolder = new TemporaryFolder();
35
36 private static File dir;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070037 private final Listener delegate = new Listener();
38
39 @BeforeClass
40 public static void setUpClass() {
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080041 dir = testFolder.getRoot();
Thomas Vachuska4bfccd542015-05-30 00:35:25 -070042 StepProcessor.launcher = "echo";
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080043 checkState(dir.exists() || dir.mkdirs(), "Unable to create directory");
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070044 }
45
46 @Test
Thomas Vachuska86439372015-06-05 09:21:32 -070047 public void basics() {
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080048 Step step = new Step("foo", "ls " + dir.getAbsolutePath(), null, null, null, 0);
49 StepProcessor processor = new StepProcessor(step, dir, delegate, step.command());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070050 processor.run();
51 assertTrue("should be started", delegate.started);
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070052 assertTrue("should be stopped", delegate.stopped);
Thomas Vachuska86439372015-06-05 09:21:32 -070053 assertEquals("incorrect status", SUCCEEDED, delegate.status);
Thomas Vachuska8044c092015-08-04 11:06:41 -070054 assertTrue("should have output", delegate.output);
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070055 }
56
57 private class Listener implements StepProcessListener {
58
Thomas Vachuska86439372015-06-05 09:21:32 -070059 private Coordinator.Status status;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070060 private boolean started, stopped, output;
61
62 @Override
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070063 public void onStart(Step step, String command) {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070064 started = true;
65 }
66
67 @Override
Thomas Vachuska86439372015-06-05 09:21:32 -070068 public void onCompletion(Step step, Coordinator.Status status) {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070069 stopped = true;
Thomas Vachuska86439372015-06-05 09:21:32 -070070 this.status = status;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070071 }
72
73 @Override
74 public void onOutput(Step step, String line) {
75 output = true;
76 }
77 }
78
79}