summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java
blob: 0d59341f1c91bfafea3ded0885bf51f86fffc9fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.monitor.os;

import org.apache.lucene.util.Constants;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.*;

public class OsProbeTests extends ESTestCase {
    OsProbe probe = OsProbe.getInstance();

    public void testOsInfo() {
        OsInfo info = probe.osInfo();
        assertNotNull(info);
        assertThat(info.getRefreshInterval(), anyOf(equalTo(-1L), greaterThanOrEqualTo(0L)));
        assertThat(info.getName(), equalTo(Constants.OS_NAME));
        assertThat(info.getArch(), equalTo(Constants.OS_ARCH));
        assertThat(info.getVersion(), equalTo(Constants.OS_VERSION));
        assertThat(info.getAvailableProcessors(), equalTo(Runtime.getRuntime().availableProcessors()));
    }

    public void testOsStats() {
        OsStats stats = probe.osStats();
        assertNotNull(stats);
        assertThat(stats.getTimestamp(), greaterThan(0L));
        assertThat(stats.getCpu().getPercent(), anyOf(equalTo((short) -1), is(both(greaterThanOrEqualTo((short) 0)).and(lessThanOrEqualTo((short) 100)))));
        if (Constants.WINDOWS) {
            // Load average is always -1 on Windows platforms
            assertThat(stats.getCpu().getLoadAverage(), equalTo((double) -1));
        } else {
            // Load average can be negative if not available or not computed yet, otherwise it should be >= 0
            assertThat(stats.getCpu().getLoadAverage(), anyOf(lessThan((double) 0), greaterThanOrEqualTo((double) 0)));
        }

        assertNotNull(stats.getMem());
        assertThat(stats.getMem().getTotal().bytes(), greaterThan(0L));
        assertThat(stats.getMem().getFree().bytes(), greaterThan(0L));
        assertThat(stats.getMem().getFreePercent(), allOf(greaterThanOrEqualTo((short) 0), lessThanOrEqualTo((short) 100)));
        assertThat(stats.getMem().getUsed().bytes(), greaterThan(0L));
        assertThat(stats.getMem().getUsedPercent(), allOf(greaterThanOrEqualTo((short) 0), lessThanOrEqualTo((short) 100)));

        assertNotNull(stats.getSwap());
        assertNotNull(stats.getSwap().getTotal());

        long total = stats.getSwap().getTotal().bytes();
        if (total > 0) {
            assertThat(stats.getSwap().getTotal().bytes(), greaterThan(0L));
            assertThat(stats.getSwap().getFree().bytes(), greaterThan(0L));
            assertThat(stats.getSwap().getUsed().bytes(), greaterThanOrEqualTo(0L));
        } else {
            // On platforms with no swap
            assertThat(stats.getSwap().getTotal().bytes(), equalTo(0L));
            assertThat(stats.getSwap().getFree().bytes(), equalTo(0L));
            assertThat(stats.getSwap().getUsed().bytes(), equalTo(0L));
        }
    }
}