| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | package server | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/db" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/modules" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/tests/testsuite" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMain(m *testing.M) { | 
					
						
							|  |  |  | 	testsuite.Run(m) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestIntegrationWillRunInstrumentationServerWhenTargetHasNoHttpServer(t *testing.T) { | 
					
						
							|  |  |  | 	if testing.Short() { | 
					
						
							|  |  |  | 		t.Skip("skipping integration test") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 	if db.IsTestDbSQLite() { | 
					
						
							|  |  |  | 		t.Skip("sqlite is not supported by the storage server target") | 
					
						
							| 
									
										
										
										
											2024-12-10 12:32:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-24 16:38:40 +08:00
										 |  |  | 	_, cfg := db.InitTestDBWithCfg(t) | 
					
						
							| 
									
										
										
										
											2024-05-29 16:02:35 +08:00
										 |  |  | 	cfg.HTTPPort = "3001" | 
					
						
							| 
									
										
										
										
											2024-11-20 18:13:33 +08:00
										 |  |  | 	cfg.GRPCServer.Network = "tcp" | 
					
						
							|  |  |  | 	cfg.GRPCServer.Address = "localhost:10000" | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 	cfg.Target = []string{modules.StorageServer} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ms, err := InitializeModuleServer(cfg, Options{}, api.ServerOptions{}) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 	errChan := make(chan error, 1) | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 	go func() { | 
					
						
							| 
									
										
										
										
											2025-05-20 04:25:08 +08:00
										 |  |  | 		time.Sleep(1 * time.Second) | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 		errChan <- ms.Run() | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 	require.Eventually(t, func() bool { | 
					
						
							| 
									
										
										
										
											2025-05-20 04:25:08 +08:00
										 |  |  | 		client := http.Client{ | 
					
						
							|  |  |  | 			Timeout: 1 * time.Second, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 		res, err := client.Get("http://localhost:3001/metrics") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		defer func() { | 
					
						
							|  |  |  | 			if err := res.Body.Close(); err != nil { | 
					
						
							|  |  |  | 				t.Fatalf("failed to close response body: %v", err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 		return res.StatusCode == http.StatusOK | 
					
						
							|  |  |  | 	}, 10*time.Second, 1*time.Second) | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err = ms.Shutdown(context.Background(), "test over") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-08 17:45:10 +08:00
										 |  |  | 	select { | 
					
						
							|  |  |  | 	case err := <-errChan: | 
					
						
							|  |  |  | 		if err != nil && !errors.Is(err, context.Canceled) { | 
					
						
							|  |  |  | 			t.Fatalf("unexpected error from module server: %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	case <-time.After(10 * time.Second): | 
					
						
							|  |  |  | 		t.Fatal("timeout waiting for module server to shut down") | 
					
						
							| 
									
										
										
										
											2024-04-08 22:35:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |