Search in sources :

Example 1 with Ports

use of com.microsoft.frameworklauncher.common.model.Ports in project pai by Microsoft.

the class ValueRangeUtilsTest method ValueRangeUtilsTest.

@Test
public void ValueRangeUtilsTest() {
    List<ValueRange> testRangeList = new ArrayList<ValueRange>();
    testRangeList.add(ValueRange.newInstance(6, 7));
    testRangeList.add(ValueRange.newInstance(10, 100));
    testRangeList.add(ValueRange.newInstance(3, 5));
    testRangeList.add(ValueRange.newInstance(90, 102));
    List<ValueRange> testRangeList2 = new ArrayList<ValueRange>();
    testRangeList2.add(ValueRange.newInstance(2, 3));
    testRangeList2.add(ValueRange.newInstance(7, 8));
    testRangeList2.add(ValueRange.newInstance(10, 20));
    List<ValueRange> testRangeList3 = ValueRangeUtils.cloneList(testRangeList2);
    List<ValueRange> sortedResult = ValueRangeUtils.SortRangeList(testRangeList);
    Assert.assertEquals(3, sortedResult.get(0).getBegin().intValue());
    Assert.assertEquals(6, sortedResult.get(1).getBegin().intValue());
    Assert.assertEquals(10, sortedResult.get(2).getBegin().intValue());
    Assert.assertEquals(90, sortedResult.get(3).getBegin().intValue());
    List<ValueRange> coalesceResult = ValueRangeUtils.coalesceRangeList(testRangeList);
    Assert.assertEquals(2, coalesceResult.size());
    Assert.assertEquals(3, coalesceResult.get(0).getBegin().intValue());
    Assert.assertEquals(7, coalesceResult.get(0).getEnd().intValue());
    Assert.assertEquals(10, coalesceResult.get(1).getBegin().intValue());
    Assert.assertEquals(102, coalesceResult.get(1).getEnd().intValue());
    List<ValueRange> result = ValueRangeUtils.intersectRangeList(coalesceResult, testRangeList2);
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(3, result.get(0).getBegin().intValue());
    Assert.assertEquals(3, result.get(0).getEnd().intValue());
    Assert.assertEquals(7, result.get(1).getBegin().intValue());
    Assert.assertEquals(7, result.get(1).getEnd().intValue());
    Assert.assertEquals(10, result.get(2).getBegin().intValue());
    Assert.assertEquals(20, result.get(2).getEnd().intValue());
    result = ValueRangeUtils.subtractRange(coalesceResult, testRangeList2);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(4, result.get(0).getBegin().intValue());
    Assert.assertEquals(6, result.get(0).getEnd().intValue());
    Assert.assertEquals(21, result.get(1).getBegin().intValue());
    Assert.assertEquals(102, result.get(1).getEnd().intValue());
    List<ValueRange> testRangeList7 = new ArrayList<ValueRange>();
    testRangeList7.add(ValueRange.newInstance(80, 80));
    result = ValueRangeUtils.subtractRange(coalesceResult, testRangeList7);
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(3, result.get(0).getBegin().intValue());
    Assert.assertEquals(7, result.get(0).getEnd().intValue());
    Assert.assertEquals(10, result.get(1).getBegin().intValue());
    Assert.assertEquals(79, result.get(1).getEnd().intValue());
    Assert.assertEquals(81, result.get(2).getBegin().intValue());
    Assert.assertEquals(102, result.get(2).getEnd().intValue());
    result = ValueRangeUtils.addRange(sortedResult, testRangeList2);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(2, result.get(0).getBegin().intValue());
    Assert.assertEquals(8, result.get(0).getEnd().intValue());
    Assert.assertEquals(10, result.get(1).getBegin().intValue());
    Assert.assertEquals(102, result.get(1).getEnd().intValue());
    List<ValueRange> testRangeList4 = new ArrayList<ValueRange>();
    testRangeList4.add(ValueRange.newInstance(2, 3));
    Assert.assertTrue(ValueRangeUtils.fitInRange(testRangeList4, testRangeList3));
    List<ValueRange> testRangeList5 = new ArrayList<ValueRange>();
    testRangeList5.add(ValueRange.newInstance(1, 3));
    Assert.assertTrue(!ValueRangeUtils.fitInRange(testRangeList5, testRangeList3));
    List<ValueRange> testRangeList6 = new ArrayList<ValueRange>();
    testRangeList6.add(ValueRange.newInstance(9, 9));
    Assert.assertTrue(!ValueRangeUtils.fitInRange(testRangeList6, testRangeList3));
    result = ValueRangeUtils.getSubRange(testRangeList3, 1, 0);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(1, ValueRangeUtils.getValueNumber(result));
    result = ValueRangeUtils.getSubRange(testRangeList3, 3, 0);
    Assert.assertEquals(3, ValueRangeUtils.getValueNumber(result));
    result = ValueRangeUtils.getSubRange(testRangeList3, 3, 10);
    Assert.assertEquals(3, ValueRangeUtils.getValueNumber(result));
    Assert.assertTrue(result.get(0).getBegin() > 10);
    Assert.assertTrue(result.get(0).getEnd() > 10);
    List<ValueRange> testRangeList10 = new ArrayList<ValueRange>();
    testRangeList10.add(ValueRange.newInstance(80, 80));
    testRangeList10.add(ValueRange.newInstance(80, 81));
    testRangeList10.add(ValueRange.newInstance(100, 103));
    int[] expectedResult = { 80, 81, 100, 101, 102, 103 };
    for (int i = 0; i < ValueRangeUtils.getValueNumber(testRangeList10); i++) {
        Assert.assertEquals(expectedResult[i], ValueRangeUtils.getValue(testRangeList10, i).intValue());
    }
    Map<String, Ports> portDefinitions = new HashMap<String, Ports>();
    Ports ports1 = new Ports();
    ports1.setCount(2);
    ports1.setStart(8000);
    portDefinitions.put("http_Port", ports1);
    Ports ports2 = new Ports();
    ports2.setCount(4);
    ports2.setStart(9000);
    portDefinitions.put("http_SSH", ports2);
    List<ValueRange> testValueRange = new ArrayList<ValueRange>();
    testValueRange.add(ValueRange.newInstance(8000, 8001));
    List<ValueRange> testValueRange2 = new ArrayList<ValueRange>();
    testValueRange.add(ValueRange.newInstance(9000, 9003));
    String environmentString = ValueRangeUtils.toEnviromentVariableString(testValueRange, portDefinitions);
    Assert.assertEquals(environmentString, "http_SSH:9000,9001,9002,9003;http_Port:8000,8001;");
    Map<String, Ports> portDefinitions2 = new HashMap<String, Ports>();
    Ports ports3 = new Ports();
    ports3.setCount(2);
    ports3.setStart(0);
    portDefinitions2.put("http_Port", ports3);
    Ports ports4 = new Ports();
    ports4.setCount(4);
    ports4.setStart(0);
    portDefinitions2.put("http_SSH", ports4);
    environmentString = ValueRangeUtils.toEnviromentVariableString(testRangeList, portDefinitions2);
    Assert.assertEquals(environmentString.split(";").length, 2);
    Assert.assertEquals(environmentString.split(",").length, 5);
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Ports(com.microsoft.frameworklauncher.common.model.Ports) Test(org.junit.Test)

Example 2 with Ports

use of com.microsoft.frameworklauncher.common.model.Ports in project pai by Microsoft.

the class ValueRangeUtils method toEnviromentVariableString.

// This function is to convert port from List<ValueRange> format to string format
// The string format is "httpPort:80,81,82;sshPort:1021,1022,1023;"
// the Ports label defined in portsDefinitions
public static String toEnviromentVariableString(List<ValueRange> portRanges, Map<String, Ports> portsDefinitions) {
    StringBuilder portsString = new StringBuilder();
    if (portsDefinitions != null && !portsDefinitions.isEmpty()) {
        Iterator iter = portsDefinitions.entrySet().iterator();
        int basePort = 0;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            Ports ports = (Ports) entry.getValue();
            // if user specified ports, directly use the PortDefinitions in request.
            if (ports.getStart() > 0) {
                portsString.append(key + ":" + ports.getStart());
                for (int i = 2; i <= ports.getCount(); i++) {
                    portsString.append("," + (ports.getStart() + i - 1));
                }
                portsString.append(";");
            } else {
                // if user not specified ports, assign the allocated ContainerPorts to each port label.
                List<ValueRange> assignPorts = ValueRangeUtils.getSubRange(portRanges, ports.getCount(), basePort);
                basePort = assignPorts.get(assignPorts.size() - 1).getEnd() + 1;
                portsString.append(key + ":" + assignPorts.get(0).toDetailString(","));
                for (int i = 1; i < assignPorts.size(); i++) {
                    portsString.append("," + assignPorts.get(i).toDetailString(","));
                }
                portsString.append(";");
            }
        }
    }
    return portsString.toString();
}
Also used : ValueRange(com.microsoft.frameworklauncher.common.model.ValueRange) Ports(com.microsoft.frameworklauncher.common.model.Ports)

Aggregations

Ports (com.microsoft.frameworklauncher.common.model.Ports)2 ValueRange (com.microsoft.frameworklauncher.common.model.ValueRange)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1