OSUtilsTest.java 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dolphinscheduler.common.utils;

19 20 21
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.dolphinscheduler.common.Constants;
22 23 24 25
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
26 27

import java.io.IOException;
28 29 30 31 32 33 34 35 36 37 38
import java.util.List;

public class OSUtilsTest {
    private static final Logger logger = LoggerFactory.getLogger(OSUtilsTest.class);

    @Test
    public void getUserList() {
        List<String> userList = OSUtils.getUserList();
        Assert.assertNotEquals("System user list should not be empty", userList.size(), 0);
        logger.info("OS user list : {}", userList.toString());
    }
39

40 41
    @Test
    public void testOSMetric(){
42 43
        if (!OSUtils.isWindows()) {
            double availablePhysicalMemorySize = OSUtils.availablePhysicalMemorySize();
44
            Assert.assertTrue(availablePhysicalMemorySize >= 0.0d);
45
            double totalMemorySize = OSUtils.totalMemorySize();
46
            Assert.assertTrue(totalMemorySize >= 0.0d);
47 48 49
            double loadAverage = OSUtils.loadAverage();
            logger.info("loadAverage {}", loadAverage);
            double memoryUsage = OSUtils.memoryUsage();
50
            Assert.assertTrue(memoryUsage >= 0.0d);
51
            double cpuUsage = OSUtils.cpuUsage();
52
            Assert.assertTrue(cpuUsage >= 0.0d || cpuUsage == -1.0d);
53 54 55
        } else {
            // TODO window ut
        }
56
    }
57

58 59
    @Test
    public void getGroup() {
60 61 62 63 64
        try {
            String group = OSUtils.getGroup();
            Assert.assertNotNull(group);
        } catch (IOException e) {
            Assert.fail("get group failed " + e.getMessage());
65 66
        }
    }
67 68 69 70

    @Test
    public void createUser() {
        boolean result = OSUtils.createUser("test123");
71 72 73 74 75
        if (result) {
            Assert.assertTrue("create user test123 success", true);
        } else {
            Assert.assertTrue("create user test123 fail", true);
        }
76 77
    }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    @Test
    public void exeCmd() {
        if(OSUtils.isMacOS() || !OSUtils.isWindows()){
            try {
                String result = OSUtils.exeCmd("echo helloWorld");
                Assert.assertEquals("helloWorld\n",result);
            } catch (IOException e) {
                Assert.fail("exeCmd " + e.getMessage());
            }
        }
    }
    @Test
    public void getProcessID(){
        int processId = OSUtils.getProcessID();
        Assert.assertNotEquals(0, processId);
    }
    @Test
    public void getHost(){
96
        String host = NetUtils.getHost();
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        Assert.assertNotNull(host);
        Assert.assertNotEquals("", host);
    }
    @Test
    public void checkResource(){
        boolean resource = OSUtils.checkResource(100,0);
        Assert.assertTrue(resource);
        resource = OSUtils.checkResource(0,Double.MAX_VALUE);
        Assert.assertFalse(resource);

        Configuration configuration = new PropertiesConfiguration();

        configuration.setProperty(Constants.MASTER_MAX_CPULOAD_AVG,100);
        configuration.setProperty(Constants.MASTER_RESERVED_MEMORY,0);
        resource = OSUtils.checkResource(configuration,true);
        Assert.assertTrue(resource);

        configuration.setProperty(Constants.MASTER_MAX_CPULOAD_AVG,0);
        configuration.setProperty(Constants.MASTER_RESERVED_MEMORY,Double.MAX_VALUE);
        resource = OSUtils.checkResource(configuration,true);
        Assert.assertFalse(resource);

        configuration.setProperty(Constants.WORKER_MAX_CPULOAD_AVG,100);
        configuration.setProperty(Constants.WORKER_RESERVED_MEMORY,0);
        resource = OSUtils.checkResource(configuration,false);
        Assert.assertTrue(resource);

        configuration.setProperty(Constants.WORKER_MAX_CPULOAD_AVG,0);
        configuration.setProperty(Constants.WORKER_RESERVED_MEMORY,Double.MAX_VALUE);
        resource = OSUtils.checkResource(configuration,false);
        Assert.assertFalse(resource);

    }
130

131
}