github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-redis/lib/redis_test.go (about)

     1  //go:build linux
     2  
     3  package mpredis
     4  
     5  import (
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/go-redis/redismock/v9"
    10  )
    11  
    12  var metrics = []string{
    13  	"instantaneous_ops_per_sec", "total_connections_received", "rejected_connections", "connected_clients",
    14  	"blocked_clients", "connected_slaves", "keys", "expires", "expired", "evicted_keys", "keyspace_hits", "keyspace_misses", "used_memory",
    15  	"used_memory_rss", "used_memory_peak", "used_memory_lua", "uptime_in_seconds",
    16  }
    17  
    18  func TestFetchMetrics(t *testing.T) {
    19  	db, mock := redismock.NewClientMock()
    20  
    21  	mock.ExpectInfo().SetVal(strings.Join([]string{
    22  		"db0:keys=4,expires=3,avg_ttl=0",
    23  		"expired_keys:2",
    24  
    25  		"instantaneous_ops_per_sec:0",
    26  		"total_connections_received:0",
    27  		"rejected_connections:0",
    28  		"connected_clients:1",
    29  		"blocked_clients:1",
    30  		"connected_slaves:0",
    31  		"evicted_keys:0",
    32  		"keyspace_hits:0",
    33  		"keyspace_misses:0",
    34  		"used_memory:0",
    35  		"used_memory_rss:0",
    36  		"used_memory_peak:0",
    37  		"used_memory_lua:0",
    38  		"uptime_in_seconds:1",
    39  	},
    40  		"\r\n"),
    41  	)
    42  
    43  	redis := RedisPlugin{
    44  		rdb:           db,
    45  		Timeout:       5,
    46  		Prefix:        "redis",
    47  		ConfigCommand: "CONFIG",
    48  	}
    49  	stat, err := redis.FetchMetrics()
    50  
    51  	if err != nil {
    52  		t.Errorf("something went wrong")
    53  	}
    54  
    55  	for _, v := range metrics {
    56  		value, ok := stat[v]
    57  		if !ok {
    58  			t.Errorf("metric of %s cannot be fetched", v)
    59  		}
    60  		if v == "keys" && value != 4.0 {
    61  			t.Errorf("metric of key should be 4, but %v", value)
    62  		}
    63  		if v == "expires" && value != 3.0 {
    64  			t.Errorf("metric of expires should be 3, but %v", value)
    65  		}
    66  		if v == "expired" && value != 2.0 {
    67  			t.Errorf("metric of expired should be 2, but %v", value)
    68  		}
    69  	}
    70  }
    71  
    72  func TestFetchMetricsPercentageOfMemory(t *testing.T) {
    73  	db, mock := redismock.NewClientMock()
    74  
    75  	val := map[string]string{"maxmemory": "0.0"}
    76  	mock.ExpectConfigGet("maxmemory").SetVal(val)
    77  	rp := RedisPlugin{
    78  		rdb:           db,
    79  		Timeout:       5,
    80  		Prefix:        "redis",
    81  		ConfigCommand: "config",
    82  	}
    83  
    84  	stat1 := make(map[string]interface{})
    85  	err := rp.fetchPercentageOfMemory(stat1)
    86  	if err != nil {
    87  		t.Errorf("something went wrong")
    88  	}
    89  
    90  	if value, ok := stat1["percentage_of_memory"]; !ok {
    91  		t.Errorf("metric of 'percentage_of_memory' cannnot be fetched")
    92  	} else if value != 0.0 {
    93  		t.Errorf("metric of 'percentage_of_memory' should be 0.0, but %v", value)
    94  	}
    95  }
    96  
    97  func TestFetchMetricsPercentageOfMemory_100percent(t *testing.T) {
    98  	db, mock := redismock.NewClientMock()
    99  
   100  	val := map[string]string{"maxmemory": "1048576.0"}
   101  	mock.ExpectConfigGet("maxmemory").SetVal(val)
   102  	rp := RedisPlugin{
   103  		rdb:           db,
   104  		Timeout:       5,
   105  		Prefix:        "redis",
   106  		ConfigCommand: "config",
   107  	}
   108  
   109  	stat1 := make(map[string]interface{})
   110  	stat1["used_memory"] = float64(1048576)
   111  	err := rp.fetchPercentageOfMemory(stat1)
   112  	if err != nil {
   113  		t.Errorf("something went wrong")
   114  	}
   115  
   116  	if value, ok := stat1["percentage_of_memory"]; !ok {
   117  		t.Errorf("metric of 'percentage_of_memory' cannnot be fetched")
   118  	} else if value == 0.0 {
   119  		t.Errorf("metric of 'percentage_of_memory' should not be 0.0, but %v", value)
   120  	}
   121  }