|
| 1 | +package victoria |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestVictoriaExport(t *testing.T) { |
| 15 | + // Sample export data |
| 16 | + exportData := `{"metric":{"__name__":"up","job":"test"},"values":[1],"timestamps":[1596698684000]} |
| 17 | +{"metric":{"__name__":"cpu_usage","instance":"server1"},"values":[0.45],"timestamps":[1596698684000]}` |
| 18 | + |
| 19 | + // Setup test server |
| 20 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | + if r.URL.Path == "/api/v1/export" { |
| 22 | + // Check query parameters |
| 23 | + query := r.URL.Query() |
| 24 | + assert.Equal(t, "{__name__=\"up\"}", query.Get("match[]")) |
| 25 | + assert.Equal(t, "2020-01-01T00:00:00Z", query.Get("start")) |
| 26 | + assert.Equal(t, "2020-01-02T00:00:00Z", query.Get("end")) |
| 27 | + |
| 28 | + // Return export data |
| 29 | + w.WriteHeader(http.StatusOK) |
| 30 | + w.Write([]byte(exportData)) |
| 31 | + } else { |
| 32 | + w.WriteHeader(http.StatusNotFound) |
| 33 | + } |
| 34 | + })) |
| 35 | + defer testServer.Close() |
| 36 | + |
| 37 | + // Parse the URL for our test server |
| 38 | + serverURL := strings.TrimPrefix(testServer.URL, "http://") |
| 39 | + |
| 40 | + // Create driver |
| 41 | + d := &Victoria{} |
| 42 | + dsn := "victoria://" + serverURL + "?label_filter={__name__=\"up\"}&start=2020-01-01T00:00:00Z&end=2020-01-02T00:00:00Z" |
| 43 | + // No need to store the returned driver since we're testing the receiver methods directly |
| 44 | + _, err := d.Open(dsn) |
| 45 | + assert.NoError(t, err) |
| 46 | + |
| 47 | + // Test export |
| 48 | + var buf bytes.Buffer |
| 49 | + err = d.Export(context.Background(), &buf) |
| 50 | + assert.NoError(t, err) |
| 51 | + assert.Equal(t, exportData, buf.String()) |
| 52 | + |
| 53 | + // Test export with closed connection |
| 54 | + d.Close() |
| 55 | + err = d.Export(context.Background(), &buf) |
| 56 | + assert.Error(t, err) |
| 57 | + assert.Equal(t, ErrClosed, err) |
| 58 | +} |
| 59 | + |
| 60 | +func TestVictoriaExportError(t *testing.T) { |
| 61 | + // Setup test server that returns an error |
| 62 | + testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 63 | + w.WriteHeader(http.StatusInternalServerError) |
| 64 | + w.Write([]byte("Internal server error")) |
| 65 | + })) |
| 66 | + defer testServer.Close() |
| 67 | + |
| 68 | + // Parse the URL for our test server |
| 69 | + serverURL := strings.TrimPrefix(testServer.URL, "http://") |
| 70 | + |
| 71 | + // Create driver |
| 72 | + d := &Victoria{} |
| 73 | + dsn := "victoria://" + serverURL |
| 74 | + _, err := d.Open(dsn) |
| 75 | + assert.NoError(t, err) |
| 76 | + |
| 77 | + // Test export |
| 78 | + var buf bytes.Buffer |
| 79 | + err = d.Export(context.Background(), &buf) |
| 80 | + assert.Error(t, err) |
| 81 | + assert.Contains(t, err.Error(), "status 500") |
| 82 | +} |
0 commit comments