blob: 70a5960ef2217af6cabdee84030d3677ef4ade1d [file] [log] [blame]
Thomas Vachuska5420ba32016-05-13 14:45:25 -04001/*
2 * Copyright 2016 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
17package org.onlab.warden;
18
19import com.google.common.io.Files;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.util.Tools;
24
25import java.io.File;
26import java.io.IOException;
27import java.util.Objects;
28
29import static org.junit.Assert.*;
30
31/**
32 * Suite of tests for the cell warden.
33 */
34public class WardenTest {
35
36 private Warden warden;
37 private File cells;
38 private File supportedCells;
39
40 @Before
41 public void setUp() throws IOException {
42 // Setup warden to be tested
43 Warden.root = Files.createTempDir();
44 Warden.cmdPrefix = "echo ";
45 cells = new File(Warden.root, "cells");
46 supportedCells = new File(cells, "supported");
47 warden = new Warden();
48
49 // Setup test cell information
50 createCell("alpha", "foo");
51 createCell("bravo", "foo");
52 createCell("charlie", "foo");
53 createCell("delta", "bar");
54 createCell("echo", "bar");
55 createCell("foxtrot", "bar");
56
57 new File("warden.log").deleteOnExit();
58 }
59
60 private void createCell(String cellName, String hostName) throws IOException {
61 File cellFile = new File(supportedCells, cellName);
62 Files.createParentDirs(cellFile);
63 Files.write((hostName + " " + cellName).getBytes(), cellFile);
64 }
65
66 @After
67 public void tearDown() throws IOException {
68 Tools.removeDirectory(Warden.root);
69 }
70
71 @Test
72 public void basics() {
73 assertEquals("incorrect number of cells", 6, warden.getCells().size());
74 validateSizes(6, 0);
75
76 String cellDefinition = warden.borrowCell("dude", "the-key", 0, null);
77 assertTrue("incorrect definition", cellDefinition.contains("cell-def"));
78 validateSizes(5, 1);
79
80 Reservation dudeCell = warden.currentUserReservation("dude");
81 validateCellState(dudeCell);
82
83 warden.borrowCell("dolt", "a-key", 0, "4+1");
84 Reservation doltCell = warden.currentUserReservation("dolt");
85 validateCellState(doltCell);
86 validateSizes(4, 2);
87
Thomas Vachuskaea316092016-05-16 09:50:44 -070088 assertFalse("cells should not be on the same host",
89 Objects.equals(warden.getCellHost(dudeCell.cellName),
90 warden.getCellHost(doltCell.cellName)));
Thomas Vachuska5420ba32016-05-13 14:45:25 -040091
92 warden.returnCell("dude");
93 validateSizes(5, 1);
94
95 warden.borrowCell("dolt", "a-key", 30, null);
96 validateSizes(5, 1);
97
98 warden.returnCell("dolt");
99 validateSizes(6, 0);
100 }
101
102 private void validateSizes(int available, int reserved) {
103 assertEquals("incorrect number of available cells", available,
104 warden.getAvailableCells().size());
105 assertEquals("incorrect number of reserved cells", reserved,
106 warden.getReservedCells().size());
107 }
108
109 private void validateCellState(Reservation reservation) {
110 assertFalse("cell should not be available",
111 warden.getAvailableCells().contains(reservation.cellName));
112 assertTrue("cell should be reserved",
113 warden.getReservedCells().contains(reservation.cellName));
114 }
115
116}