|
| 1 | +package frontend |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/grafana/grafana/pkg/services/contexthandler" |
| 15 | + "github.com/grafana/grafana/pkg/services/featuremgmt" |
| 16 | + "github.com/grafana/grafana/pkg/services/licensing" |
| 17 | + "github.com/grafana/grafana/pkg/setting" |
| 18 | + "github.com/grafana/grafana/pkg/web" |
| 19 | +) |
| 20 | + |
| 21 | +// Helper function to create a test service with minimal configuration |
| 22 | +func createTestService(t *testing.T, cfg *setting.Cfg) *frontendService { |
| 23 | + t.Helper() |
| 24 | + |
| 25 | + features := featuremgmt.WithFeatures() |
| 26 | + license := &licensing.OSSLicensingService{} |
| 27 | + |
| 28 | + var promRegister prometheus.Registerer = prometheus.NewRegistry() |
| 29 | + promGatherer := promRegister.(*prometheus.Registry) |
| 30 | + |
| 31 | + service, err := ProvideFrontendService(cfg, features, promGatherer, promRegister, license) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + return service |
| 35 | +} |
| 36 | + |
| 37 | +func TestFrontendService_ServerCreation(t *testing.T) { |
| 38 | + t.Run("should create HTTP server with correct configuration", func(t *testing.T) { |
| 39 | + publicDir := setupTestWebAssets(t) |
| 40 | + cfg := &setting.Cfg{ |
| 41 | + HTTPPort: "1234", |
| 42 | + StaticRootPath: publicDir, |
| 43 | + } |
| 44 | + service := createTestService(t, cfg) |
| 45 | + |
| 46 | + ctx := context.Background() |
| 47 | + server := service.newFrontendServer(ctx) |
| 48 | + |
| 49 | + assert.NotNil(t, server) |
| 50 | + assert.Equal(t, ":1234", server.Addr) |
| 51 | + assert.NotNil(t, server.Handler) |
| 52 | + assert.NotNil(t, server.BaseContext) |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func TestFrontendService_Routes(t *testing.T) { |
| 57 | + publicDir := setupTestWebAssets(t) |
| 58 | + cfg := &setting.Cfg{ |
| 59 | + HTTPPort: "3000", |
| 60 | + StaticRootPath: publicDir, |
| 61 | + } |
| 62 | + service := createTestService(t, cfg) |
| 63 | + |
| 64 | + // Create a test mux to verify route registration |
| 65 | + mux := web.New() |
| 66 | + service.addMiddlewares(mux) |
| 67 | + service.registerRoutes(mux) |
| 68 | + |
| 69 | + t.Run("should handle frontend wildcard routes", func(t *testing.T) { |
| 70 | + // Test that routes are registered by making requests |
| 71 | + testCases := []struct { |
| 72 | + path string |
| 73 | + description string |
| 74 | + }{ |
| 75 | + // Metrics isn't registered in the test service? |
| 76 | + {"/", "index route should return HTML"}, |
| 77 | + {"/dashboards", "browse dashboards route should return HTML"}, |
| 78 | + {"/d/de773f33s8qgwf/fep-homepage", "dashboard route should return HTML"}, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tc := range testCases { |
| 82 | + t.Run(tc.description, func(t *testing.T) { |
| 83 | + req := httptest.NewRequest("GET", tc.path, nil) |
| 84 | + recorder := httptest.NewRecorder() |
| 85 | + |
| 86 | + mux.ServeHTTP(recorder, req) |
| 87 | + |
| 88 | + assert.Equal(t, 200, recorder.Code) |
| 89 | + assert.Contains(t, recorder.Body.String(), "<div id=\"reactRoot\"></div>") |
| 90 | + }) |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("should handle assets 404 correctly", func(t *testing.T) { |
| 95 | + req := httptest.NewRequest("GET", "/public/build/app.js", nil) |
| 96 | + recorder := httptest.NewRecorder() |
| 97 | + |
| 98 | + mux.ServeHTTP(recorder, req) |
| 99 | + |
| 100 | + assert.Equal(t, 404, recorder.Code) |
| 101 | + assert.Equal(t, "404 page not found", strings.TrimSpace(recorder.Body.String())) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("should return prometheus metrics", func(t *testing.T) { |
| 105 | + testCounter := prometheus.NewCounter(prometheus.CounterOpts{ |
| 106 | + Name: "shrimp_count", |
| 107 | + }) |
| 108 | + err := service.promRegister.Register(testCounter) |
| 109 | + require.NoError(t, err) |
| 110 | + testCounter.Inc() |
| 111 | + |
| 112 | + req := httptest.NewRequest("GET", "/metrics", nil) |
| 113 | + recorder := httptest.NewRecorder() |
| 114 | + |
| 115 | + mux.ServeHTTP(recorder, req) |
| 116 | + |
| 117 | + assert.Contains(t, recorder.Body.String(), "\nshrimp_count 1\n") |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +func TestFrontendService_Middleware(t *testing.T) { |
| 122 | + publicDir := setupTestWebAssets(t) |
| 123 | + cfg := &setting.Cfg{ |
| 124 | + HTTPPort: "3000", |
| 125 | + StaticRootPath: publicDir, |
| 126 | + } |
| 127 | + |
| 128 | + t.Run("should register route prom metrics", func(t *testing.T) { |
| 129 | + service := createTestService(t, cfg) |
| 130 | + mux := web.New() |
| 131 | + service.addMiddlewares(mux) |
| 132 | + service.registerRoutes(mux) |
| 133 | + |
| 134 | + req := httptest.NewRequest("GET", "/dashboards", nil) |
| 135 | + recorder := httptest.NewRecorder() |
| 136 | + mux.ServeHTTP(recorder, req) |
| 137 | + |
| 138 | + req = httptest.NewRequest("GET", "/public/build/app.js", nil) |
| 139 | + mux.ServeHTTP(recorder, req) |
| 140 | + |
| 141 | + req = httptest.NewRequest("GET", "/metrics", nil) |
| 142 | + mux.ServeHTTP(recorder, req) |
| 143 | + |
| 144 | + metricsBody := recorder.Body.String() |
| 145 | + assert.Contains(t, metricsBody, "# TYPE grafana_http_request_duration_seconds histogram") |
| 146 | + assert.Contains(t, metricsBody, "grafana_http_request_duration_seconds_bucket{handler=\"public-assets\"") // assets 404 |
| 147 | + assert.Contains(t, metricsBody, "grafana_http_request_duration_seconds_bucket{handler=\"/*\"") // index route |
| 148 | + }) |
| 149 | + |
| 150 | + t.Run("should add context middleware", func(t *testing.T) { |
| 151 | + service := createTestService(t, cfg) |
| 152 | + mux := web.New() |
| 153 | + service.addMiddlewares(mux) |
| 154 | + |
| 155 | + mux.Get("/test-route", func(w http.ResponseWriter, r *http.Request) { |
| 156 | + ctx := contexthandler.FromContext(r.Context()) |
| 157 | + assert.NotNil(t, ctx) |
| 158 | + assert.NotNil(t, ctx.Context) |
| 159 | + assert.NotNil(t, ctx.Logger) |
| 160 | + |
| 161 | + w.WriteHeader(200) |
| 162 | + _, err := w.Write([]byte("ok")) |
| 163 | + require.NoError(t, err) |
| 164 | + }) |
| 165 | + |
| 166 | + req := httptest.NewRequest("GET", "/test-route", nil) |
| 167 | + recorder := httptest.NewRecorder() |
| 168 | + mux.ServeHTTP(recorder, req) |
| 169 | + }) |
| 170 | +} |
0 commit comments