Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onlab.stc; |
| 17 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 18 | import org.junit.BeforeClass; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 19 | import org.junit.ClassRule; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 21 | import org.junit.rules.TemporaryFolder; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 22 | import java.io.File; |
Thomas Vachuska | 8044c09 | 2015-08-04 11:06:41 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.junit.Assert.assertTrue; |
| 26 | import static org.onlab.stc.Coordinator.Status.SUCCEEDED; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Test of the step processor. |
| 30 | */ |
| 31 | public class StepProcessorTest { |
| 32 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 33 | @ClassRule |
| 34 | public static TemporaryFolder testFolder = new TemporaryFolder(); |
| 35 | |
| 36 | private static File dir; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 37 | private final Listener delegate = new Listener(); |
| 38 | |
| 39 | @BeforeClass |
| 40 | public static void setUpClass() { |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 41 | dir = testFolder.getRoot(); |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 42 | StepProcessor.launcher = "echo"; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 43 | checkState(dir.exists() || dir.mkdirs(), "Unable to create directory"); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Test |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 47 | public void basics() { |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 48 | Step step = new Step("foo", "ls " + dir.getAbsolutePath(), null, null, null, 0); |
| 49 | StepProcessor processor = new StepProcessor(step, dir, delegate, step.command()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 50 | processor.run(); |
| 51 | assertTrue("should be started", delegate.started); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 52 | assertTrue("should be stopped", delegate.stopped); |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 53 | assertEquals("incorrect status", SUCCEEDED, delegate.status); |
Thomas Vachuska | 8044c09 | 2015-08-04 11:06:41 -0700 | [diff] [blame] | 54 | assertTrue("should have output", delegate.output); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private class Listener implements StepProcessListener { |
| 58 | |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 59 | private Coordinator.Status status; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 60 | private boolean started, stopped, output; |
| 61 | |
| 62 | @Override |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 63 | public void onStart(Step step, String command) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 64 | started = true; |
| 65 | } |
| 66 | |
| 67 | @Override |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 68 | public void onCompletion(Step step, Coordinator.Status status) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 69 | stopped = true; |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 70 | this.status = status; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onOutput(Step step, String line) { |
| 75 | output = true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } |