github.com/Kong/go-pdk@v0.11.0/node/node_test.go (about)

     1  package node
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Kong/go-pdk/bridge"
     7  	"github.com/Kong/go-pdk/bridge/bridgetest"
     8  	"github.com/Kong/go-pdk/server/kong_plugin_protocol"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func mockNode(t *testing.T, s []bridgetest.MockStep) Node {
    13  	return Node{bridge.New(bridgetest.Mock(t, s))}
    14  }
    15  
    16  func TestGetId(t *testing.T) {
    17  	node := mockNode(t, []bridgetest.MockStep{
    18  		{Method: "kong.node.get_id", Ret: bridge.WrapString("001:002:0003")},
    19  	})
    20  
    21  	ret, err := node.GetId()
    22  	assert.NoError(t, err)
    23  	assert.Equal(t, "001:002:0003", ret)
    24  }
    25  
    26  func TestGetMemoryStats(t *testing.T) {
    27  	node := mockNode(t, []bridgetest.MockStep{
    28  		{
    29  			Method: "kong.node.get_memory_stats",
    30  			Ret: &kong_plugin_protocol.MemoryStats{
    31  				LuaSharedDicts: &kong_plugin_protocol.MemoryStats_LuaSharedDicts{
    32  					Kong: &kong_plugin_protocol.MemoryStats_LuaSharedDicts_DictStats{
    33  						AllocatedSlabs: 1027,
    34  						Capacity:       4423543,
    35  					},
    36  					KongDbCache: &kong_plugin_protocol.MemoryStats_LuaSharedDicts_DictStats{
    37  						AllocatedSlabs: 4093,
    38  						Capacity:       3424875,
    39  					},
    40  				},
    41  				WorkersLuaVms: []*kong_plugin_protocol.MemoryStats_WorkerLuaVm{
    42  					{HttpAllocatedGc: 123456, Pid: 543},
    43  					{HttpAllocatedGc: 345678, Pid: 876},
    44  				},
    45  			},
    46  		},
    47  	})
    48  
    49  	ret, err := node.GetMemoryStats()
    50  	assert.NoError(t, err)
    51  	assert.Equal(t, MemoryStats{
    52  		LuaSharedDicts: struct {
    53  			Kong struct {
    54  				AllocatedSlabs int64 "json:\"allocated_slabs\""
    55  				Capacity       int64 "json:\"capacity\""
    56  			} "json:\"kong\""
    57  			KongDbCache struct {
    58  				AllocatedSlabs int64 "json:\"allocated_slabs\""
    59  				Capacity       int64 "json:\"capacity\""
    60  			} "json:\"kong_db_cache\""
    61  		}{
    62  			Kong: struct {
    63  				AllocatedSlabs int64 "json:\"allocated_slabs\""
    64  				Capacity       int64 "json:\"capacity\""
    65  			}{AllocatedSlabs: 1027, Capacity: 4423543},
    66  			KongDbCache: struct {
    67  				AllocatedSlabs int64 "json:\"allocated_slabs\""
    68  				Capacity       int64 "json:\"capacity\""
    69  			}{AllocatedSlabs: 4093, Capacity: 3424875},
    70  		},
    71  		WorkersLuaVms: []workerLuaVmStats{
    72  			{HttpAllocatedGc: 123456, Pid: 543},
    73  			{HttpAllocatedGc: 345678, Pid: 876},
    74  		},
    75  	}, ret)
    76  }