blob: ec9727fe2f2e1797a10dbcf27cfb64436a8c546e [file] [log] [blame]
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07003 *
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
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080018import org.junit.AfterClass;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070019import org.junit.BeforeClass;
20import org.junit.Test;
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080021import org.onlab.util.Tools;
22
23import com.google.common.io.Files;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070024
25import java.io.File;
26import java.io.FileInputStream;
27import java.io.FileNotFoundException;
28import java.io.IOException;
29
30import static com.google.common.io.ByteStreams.toByteArray;
31import static com.google.common.io.Files.write;
32import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertSame;
34import static org.onlab.stc.Scenario.loadScenario;
35
36/**
37 * Test of the test scenario compiler.
38 */
39public class CompilerTest {
40
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080041
42 private static File testDir;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070043
44 @BeforeClass
45 public static void setUpClass() throws IOException {
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080046 testDir = Files.createTempDir();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070047 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 Vachuska8189a742015-05-29 10:02:52 -070054 System.setProperty("TOC1", "1.2.3.1");
55 System.setProperty("TOC2", "1.2.3.2");
56 System.setProperty("TOC3", "1.2.3.3");
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080057 System.setProperty("test.dir", testDir.getAbsolutePath());
58 }
59
60 @AfterClass
61 public static void tearDownClass() throws IOException {
62 Tools.removeDirectory(testDir.getPath());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070063 }
64
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070065 static FileInputStream getStream(String name) throws FileNotFoundException {
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080066 return new FileInputStream(new File(testDir, name));
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070067 }
68
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -070069 static void stageTestResource(String name) throws IOException {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070070 byte[] bytes = toByteArray(CompilerTest.class.getResourceAsStream(name));
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080071 write(bytes, new File(testDir, name));
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070072 }
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 Vachuska4be30542015-10-21 18:15:52 -070082 assertEquals("incorrect step count", 33, flow.getVertexes().size());
83 assertEquals("incorrect dependency count", 26, flow.getEdges().size());
Brian O'Connorc6dca1d2015-06-25 17:45:47 -040084 assertEquals("incorrect logDir",
HIGUCHI Yutad9c61172016-01-04 23:31:13 -080085 new File(testDir.getAbsolutePath(), "foo"), compiler.logDir());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070086
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}