...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/reaper/psutil_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/reaper

     1  package reaper
     2  
     3  import (
     4  	"slices"
     5  	"testing"
     6  
     7  	"github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
     8  
     9  	"maps"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestGetGoPsutilCPU(t *testing.T) {
    15  	a := assert.New(t)
    16  
    17  	// Call the GetGoPsutilCPU function with a 1-second interval
    18  	result, err := GetGoPsutilCPU(1.0)
    19  	a.NoError(err)
    20  	a.NotEmpty(result)
    21  
    22  	// Check if the result contains the expected keys
    23  	expectedKeys := []string{metrics.EpochColumnName, "cpu_utilization", "load_1m_norm", "load_1m", "load_5m_norm", "load_5m", "user", "system", "idle", "iowait", "irqs", "other"}
    24  	resultKeys := slices.Collect(maps.Keys(result[0]))
    25  	a.ElementsMatch(resultKeys, expectedKeys)
    26  
    27  	// Check if the CPU utilization is within the expected range
    28  	cpuUtilization := result[0]["cpu_utilization"].(float64)
    29  	a.GreaterOrEqual(cpuUtilization, 0.0)
    30  	a.LessOrEqual(cpuUtilization, 100.0)
    31  }
    32  
    33  func TestGetGoPsutilMem(t *testing.T) {
    34  	a := assert.New(t)
    35  
    36  	// Call the GetGoPsutilMem function
    37  	result, err := GetGoPsutilMem()
    38  	a.NoError(err)
    39  	a.NotEmpty(result)
    40  
    41  	// Check if the result contains the expected keys
    42  	expectedKeys := []string{metrics.EpochColumnName, "total", "used", "free", "buff_cache", "available", "percent", "swap_total", "swap_used", "swap_free", "swap_percent"}
    43  	resultKeys := slices.Collect(maps.Keys(result[0]))
    44  	a.ElementsMatch(resultKeys, expectedKeys)
    45  }
    46  
    47  func TestGetGoPsutilDiskTotals(t *testing.T) {
    48  	a := assert.New(t)
    49  
    50  	// Call the GetGoPsutilDiskTotals function
    51  	result, err := GetGoPsutilDiskTotals()
    52  	if err != nil {
    53  		t.Skip("skipping test; disk.IOCounters() failed")
    54  	}
    55  	a.NotEmpty(result)
    56  
    57  	// Check if the result contains the expected keys
    58  	expectedKeys := []string{metrics.EpochColumnName, "read_bytes", "write_bytes", "read_count", "write_count"}
    59  	resultKeys := slices.Collect(maps.Keys(result[0]))
    60  	a.ElementsMatch(resultKeys, expectedKeys)
    61  }
    62  
    63  func TestGetLoadAvgLocal(t *testing.T) {
    64  	a := assert.New(t)
    65  
    66  	// Call the GetLoadAvgLocal function
    67  	result, err := GetLoadAvgLocal()
    68  	a.NoError(err)
    69  	a.NotEmpty(result)
    70  
    71  	// Check if the result contains the expected keys
    72  	expectedKeys := []string{metrics.EpochColumnName, "load_1min", "load_5min", "load_15min"}
    73  	resultKeys := slices.Collect(maps.Keys(result[0]))
    74  	a.ElementsMatch(resultKeys, expectedKeys)
    75  }
    76