readiness_test.go 3.2 KB

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