managerserver_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package manager
  11. import (
  12. "reflect"
  13. "testing"
  14. )
  15. type TokenTester struct {
  16. t *testing.T
  17. invalidRequest map[RequestArgument]interface{}
  18. }
  19. func (t TokenTester) TestPing() {
  20. if err := server.Ping(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  21. t.t.Error("Unexpected result:", err)
  22. return
  23. }
  24. }
  25. func (t TokenTester) TestAcquireLock() {
  26. if err := server.AcquireLock(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  27. t.t.Error("Unexpected result:", err)
  28. return
  29. }
  30. }
  31. func (t TokenTester) TestReleaseLock() {
  32. if err := server.ReleaseLock(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  33. t.t.Error("Unexpected result:", err)
  34. return
  35. }
  36. }
  37. func (t TokenTester) TestAddMember() {
  38. if err := server.AddMember(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  39. t.t.Error("Unexpected result:", err)
  40. return
  41. }
  42. }
  43. func (t TokenTester) TestEjectMember() {
  44. if err := server.EjectMember(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  45. t.t.Error("Unexpected result:", err)
  46. return
  47. }
  48. }
  49. func (t TokenTester) TestJoinCluster() {
  50. if err := server.JoinCluster(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  51. t.t.Error("Unexpected result:", err)
  52. return
  53. }
  54. }
  55. func (t TokenTester) TestStateInfoRequest() {
  56. if err := server.StateInfoRequest(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  57. t.t.Error("Unexpected result:", err)
  58. return
  59. }
  60. }
  61. func (t TokenTester) TestMemberInfoRequest() {
  62. if err := server.MemberInfoRequest(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  63. t.t.Error("Unexpected result:", err)
  64. return
  65. }
  66. }
  67. func (t TokenTester) TestUpdateStateInfo() {
  68. if err := server.UpdateStateInfo(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  69. t.t.Error("Unexpected result:", err)
  70. return
  71. }
  72. }
  73. func (t TokenTester) TestDataRequest() {
  74. if err := server.DataRequest(t.invalidRequest, nil); err.Error() != "Invalid member token" {
  75. t.t.Error("Unexpected result:", err)
  76. return
  77. }
  78. }
  79. func TestTokenCheck(t *testing.T) {
  80. // Check that all exposed functions check the giveen token
  81. mm := createCluster(1)[0]
  82. mm.Start()
  83. defer mm.Shutdown()
  84. request := make(map[RequestArgument]interface{})
  85. request[RequestTARGET] = "TestClusterMember-0"
  86. request[RequestTOKEN] = &MemberToken{"123", "123"}
  87. tester := &TokenTester{t, request}
  88. typ := reflect.TypeOf(server)
  89. testerVal := reflect.ValueOf(tester)
  90. for m := 0; m < typ.NumMethod(); m++ {
  91. methodType := typ.Method(m)
  92. mname := methodType.Name
  93. if mname == "checkToken" {
  94. // Exclude the checkToken function itself
  95. continue
  96. }
  97. method := testerVal.MethodByName("Test" + mname)
  98. if !method.IsValid() {
  99. t.Error("Method test for ", mname, "missing")
  100. continue
  101. }
  102. method.Call([]reflect.Value{})
  103. }
  104. }