readiness_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package metrics
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/google/uuid"
  6. "github.com/rs/zerolog"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/cloudflare/cloudflared/connection"
  9. "github.com/cloudflare/cloudflared/tunnelstate"
  10. )
  11. func TestReadyServer_makeResponse(t *testing.T) {
  12. type fields struct {
  13. isConnected map[uint8]tunnelstate.ConnectionInfo
  14. }
  15. tests := []struct {
  16. name string
  17. fields fields
  18. wantOK bool
  19. wantReadyConnections uint
  20. }{
  21. {
  22. name: "One connection online => HTTP 200",
  23. fields: fields{
  24. isConnected: map[uint8]tunnelstate.ConnectionInfo{
  25. 0: {IsConnected: false},
  26. 1: {IsConnected: false},
  27. 2: {IsConnected: true},
  28. 3: {IsConnected: false},
  29. },
  30. },
  31. wantOK: true,
  32. wantReadyConnections: 1,
  33. },
  34. {
  35. name: "No connections online => no HTTP 200",
  36. fields: fields{
  37. isConnected: map[uint8]tunnelstate.ConnectionInfo{
  38. 0: {IsConnected: false},
  39. 1: {IsConnected: false},
  40. 2: {IsConnected: false},
  41. 3: {IsConnected: false},
  42. },
  43. },
  44. wantReadyConnections: 0,
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. rs := &ReadyServer{
  50. tracker: tunnelstate.MockedConnTracker(tt.fields.isConnected),
  51. }
  52. gotStatusCode, gotReadyConnections := rs.makeResponse()
  53. if tt.wantOK && gotStatusCode != http.StatusOK {
  54. t.Errorf("ReadyServer.makeResponse() gotStatusCode = %v, want ok = %v", gotStatusCode, tt.wantOK)
  55. }
  56. if gotReadyConnections != tt.wantReadyConnections {
  57. t.Errorf("ReadyServer.makeResponse() gotReadyConnections = %v, want %v", gotReadyConnections, tt.wantReadyConnections)
  58. }
  59. })
  60. }
  61. }
  62. func TestReadinessEventHandling(t *testing.T) {
  63. nopLogger := zerolog.Nop()
  64. rs := NewReadyServer(&nopLogger, uuid.Nil)
  65. // start not ok
  66. code, ready := rs.makeResponse()
  67. assert.NotEqualValues(t, http.StatusOK, code)
  68. assert.Zero(t, ready)
  69. // one connected => ok
  70. rs.OnTunnelEvent(connection.Event{
  71. Index: 1,
  72. EventType: connection.Connected,
  73. })
  74. code, ready = rs.makeResponse()
  75. assert.EqualValues(t, http.StatusOK, code)
  76. assert.EqualValues(t, 1, ready)
  77. // another connected => still ok
  78. rs.OnTunnelEvent(connection.Event{
  79. Index: 2,
  80. EventType: connection.Connected,
  81. })
  82. code, ready = rs.makeResponse()
  83. assert.EqualValues(t, http.StatusOK, code)
  84. assert.EqualValues(t, 2, ready)
  85. // one reconnecting => still ok
  86. rs.OnTunnelEvent(connection.Event{
  87. Index: 2,
  88. EventType: connection.Reconnecting,
  89. })
  90. code, ready = rs.makeResponse()
  91. assert.EqualValues(t, http.StatusOK, code)
  92. assert.EqualValues(t, 1, ready)
  93. // Regression test for TUN-3777
  94. rs.OnTunnelEvent(connection.Event{
  95. Index: 1,
  96. EventType: connection.RegisteringTunnel,
  97. })
  98. code, ready = rs.makeResponse()
  99. assert.NotEqualValues(t, http.StatusOK, code)
  100. assert.Zero(t, ready)
  101. // other connected then unregistered => not ok
  102. rs.OnTunnelEvent(connection.Event{
  103. Index: 1,
  104. EventType: connection.Connected,
  105. })
  106. code, ready = rs.makeResponse()
  107. assert.EqualValues(t, http.StatusOK, code)
  108. assert.EqualValues(t, 1, ready)
  109. rs.OnTunnelEvent(connection.Event{
  110. Index: 1,
  111. EventType: connection.Unregistering,
  112. })
  113. code, ready = rs.makeResponse()
  114. assert.NotEqualValues(t, http.StatusOK, code)
  115. assert.Zero(t, ready)
  116. // other disconnected => not ok
  117. rs.OnTunnelEvent(connection.Event{
  118. Index: 1,
  119. EventType: connection.Disconnected,
  120. })
  121. code, ready = rs.makeResponse()
  122. assert.NotEqualValues(t, http.StatusOK, code)
  123. assert.Zero(t, ready)
  124. }