node_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package cluster // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/cluster"
  4. import (
  5. "testing"
  6. "time"
  7. as "github.com/aerospike/aerospike-client-go/v6"
  8. "github.com/stretchr/testify/require"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/aerospikereceiver/cluster/mocks"
  10. )
  11. func TestNode_NewConnNode(t *testing.T) {
  12. conn := mocks.NewAsconn(t)
  13. nodeName := "BB990C28F270008"
  14. cPolicy := as.NewClientPolicy()
  15. host := as.NewHost("testip", 3000)
  16. authEnabled := true
  17. conn.On("SetTimeout", time.Time{}, cPolicy.Timeout).Return(nil)
  18. conn.On("RequestInfo", "node").Return(map[string]string{"node": nodeName}, nil)
  19. conn.On("Login", cPolicy).Return(nil)
  20. expectedConnNode := &connNode{
  21. conn: conn,
  22. policy: cPolicy,
  23. name: nodeName,
  24. }
  25. connFactoryPos := newMockConnFactoryFunc(t)
  26. connFactoryPos.On("Execute", cPolicy, host).Return(conn, nil)
  27. factoryFuncPos := func(policy *as.ClientPolicy, host *as.Host) (asconn, as.Error) {
  28. return connFactoryPos.Execute(policy, host)
  29. }
  30. testNode, err := _newConnNode(cPolicy, host, authEnabled, factoryFuncPos)
  31. require.NoError(t, err)
  32. connFactoryPos.AssertExpectations(t)
  33. conn.AssertExpectations(t)
  34. testConnNode := testNode.(*connNode)
  35. require.Equal(t, expectedConnNode, testConnNode)
  36. connFactoryNegHost := newMockConnFactoryFunc(t)
  37. connFactoryNegHost.On("Execute", cPolicy, host).Return(nil, as.ErrTimeout)
  38. factoryFuncNegHost := func(policy *as.ClientPolicy, host *as.Host) (asconn, as.Error) {
  39. return connFactoryNegHost.Execute(policy, host)
  40. }
  41. _, err = _newConnNode(cPolicy, host, authEnabled, factoryFuncNegHost)
  42. connFactoryNegHost.AssertExpectations(t)
  43. require.ErrorContains(t, err, "ResultCode: TIMEOUT")
  44. connBadLogin := mocks.NewAsconn(t)
  45. connBadLogin.On("Login", cPolicy).Return(as.ErrNotAuthenticated)
  46. connBadLogin.On("SetTimeout", time.Time{}, cPolicy.Timeout).Return(nil)
  47. connFactoryNegLogin := newMockConnFactoryFunc(t)
  48. connFactoryNegLogin.On("Execute", cPolicy, host).Return(connBadLogin, nil)
  49. factoryFuncNegLogin := func(policy *as.ClientPolicy, host *as.Host) (asconn, as.Error) {
  50. return connFactoryNegLogin.Execute(policy, host)
  51. }
  52. _, err = _newConnNode(cPolicy, host, authEnabled, factoryFuncNegLogin)
  53. connFactoryNegLogin.AssertExpectations(t)
  54. require.ErrorContains(t, err, "ResultCode: NOT_AUTHENTICATED")
  55. }
  56. func TestNode_RequestInfo(t *testing.T) {
  57. conn := mocks.NewAsconn(t)
  58. nodeName := "BB990C28F270008"
  59. commands := []any{"node", "statistics"}
  60. cPolicy := as.NewClientPolicy()
  61. expectedRes := map[string]string{
  62. "node": "BB990C28F270008",
  63. "statistics": "mem_use=1;objects=2",
  64. }
  65. conn.On("RequestInfo", commands...).Return(expectedRes, nil)
  66. connNodePos := &connNode{
  67. conn: conn,
  68. policy: cPolicy,
  69. name: nodeName,
  70. }
  71. actualRes, err := connNodePos.RequestInfo(nil, "node", "statistics")
  72. require.NoError(t, err)
  73. conn.AssertExpectations(t)
  74. require.Equal(t, expectedRes, actualRes)
  75. connBadSession := mocks.NewAsconn(t)
  76. connBadSession.On("RequestInfo", commands...).Return(nil, as.ErrNotAuthenticated)
  77. connNodeNeg := &connNode{
  78. conn: connBadSession,
  79. policy: cPolicy,
  80. name: nodeName,
  81. }
  82. _, err = connNodeNeg.RequestInfo(nil, "node", "statistics")
  83. require.ErrorContains(t, err, "ResultCode: NOT_AUTHENTICATED")
  84. connBadSession.AssertExpectations(t)
  85. }