Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 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 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 18 | import org.junit.AfterClass; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 21 | import org.onlab.util.Tools; |
| 22 | |
| 23 | import com.google.common.io.Files; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 24 | |
| 25 | import java.io.File; |
| 26 | import java.io.FileInputStream; |
| 27 | import java.io.FileNotFoundException; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import static com.google.common.io.ByteStreams.toByteArray; |
| 31 | import static com.google.common.io.Files.write; |
| 32 | import static org.junit.Assert.assertEquals; |
| 33 | import static org.junit.Assert.assertSame; |
| 34 | import static org.onlab.stc.Scenario.loadScenario; |
| 35 | |
| 36 | /** |
| 37 | * Test of the test scenario compiler. |
| 38 | */ |
| 39 | public class CompilerTest { |
| 40 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 41 | |
| 42 | private static File testDir; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 43 | |
| 44 | @BeforeClass |
| 45 | public static void setUpClass() throws IOException { |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 46 | testDir = Files.createTempDir(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 47 | stageTestResource("scenario.xml"); |
| 48 | stageTestResource("simple-scenario.xml"); |
| 49 | stageTestResource("one-scenario.xml"); |
| 50 | stageTestResource("two-scenario.xml"); |
| 51 | |
| 52 | System.setProperty("prop.foo", "Foobar"); |
| 53 | System.setProperty("prop.bar", "Barfoo"); |
Thomas Vachuska | 8189a74 | 2015-05-29 10:02:52 -0700 | [diff] [blame] | 54 | System.setProperty("TOC1", "1.2.3.1"); |
| 55 | System.setProperty("TOC2", "1.2.3.2"); |
| 56 | System.setProperty("TOC3", "1.2.3.3"); |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 57 | System.setProperty("test.dir", testDir.getAbsolutePath()); |
| 58 | } |
| 59 | |
| 60 | @AfterClass |
| 61 | public static void tearDownClass() throws IOException { |
| 62 | Tools.removeDirectory(testDir.getPath()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 65 | static FileInputStream getStream(String name) throws FileNotFoundException { |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 66 | return new FileInputStream(new File(testDir, name)); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 69 | static void stageTestResource(String name) throws IOException { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 70 | byte[] bytes = toByteArray(CompilerTest.class.getResourceAsStream(name)); |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 71 | write(bytes, new File(testDir, name)); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void basics() throws Exception { |
| 76 | Scenario scenario = loadScenario(getStream("scenario.xml")); |
| 77 | Compiler compiler = new Compiler(scenario); |
| 78 | compiler.compile(); |
| 79 | ProcessFlow flow = compiler.processFlow(); |
| 80 | |
| 81 | assertSame("incorrect scenario", scenario, compiler.scenario()); |
Thomas Vachuska | 4be3054 | 2015-10-21 18:15:52 -0700 | [diff] [blame] | 82 | assertEquals("incorrect step count", 33, flow.getVertexes().size()); |
| 83 | assertEquals("incorrect dependency count", 26, flow.getEdges().size()); |
Brian O'Connor | c6dca1d | 2015-06-25 17:45:47 -0400 | [diff] [blame] | 84 | assertEquals("incorrect logDir", |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 85 | new File(testDir.getAbsolutePath(), "foo"), compiler.logDir()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 86 | |
| 87 | Step step = compiler.getStep("there"); |
| 88 | assertEquals("incorrect edge count", 2, flow.getEdgesFrom(step).size()); |
| 89 | assertEquals("incorrect edge count", 0, flow.getEdgesTo(step).size()); |
| 90 | |
| 91 | Step group = compiler.getStep("three"); |
| 92 | assertEquals("incorrect edge count", 2, flow.getEdgesFrom(group).size()); |
| 93 | assertEquals("incorrect edge count", 0, flow.getEdgesTo(group).size()); |
| 94 | } |
| 95 | |
| 96 | } |