(t *testing.T)
| 33 | var updateFeatures = flag.Bool("update-features", false, "update features.json golden file") |
| 34 | |
| 35 | func TestFeaturesAPI(t *testing.T) { |
| 36 | if testing.Short() { |
| 37 | t.Skip("skipping test in short mode.") |
| 38 | } |
| 39 | t.Parallel() |
| 40 | |
| 41 | tmpDir := t.TempDir() |
| 42 | configFile := filepath.Join(tmpDir, "prometheus.yml") |
| 43 | require.NoError(t, os.WriteFile(configFile, []byte{}, 0o644)) |
| 44 | |
| 45 | port := testutil.RandomUnprivilegedPort(t) |
| 46 | prom := prometheusCommandWithLogging( |
| 47 | t, |
| 48 | configFile, |
| 49 | port, |
| 50 | fmt.Sprintf("--storage.tsdb.path=%s", tmpDir), |
| 51 | ) |
| 52 | require.NoError(t, prom.Start()) |
| 53 | |
| 54 | baseURL := fmt.Sprintf("http://127.0.0.1:%d", port) |
| 55 | |
| 56 | // Wait for Prometheus to be ready. |
| 57 | require.Eventually(t, func() bool { |
| 58 | resp, err := http.Get(baseURL + "/-/ready") |
| 59 | if err != nil { |
| 60 | return false |
| 61 | } |
| 62 | defer resp.Body.Close() |
| 63 | return resp.StatusCode == http.StatusOK |
| 64 | }, 10*time.Second, 100*time.Millisecond, "Prometheus didn't become ready in time") |
| 65 | |
| 66 | // Fetch features from the API. |
| 67 | resp, err := http.Get(baseURL + "/api/v1/features") |
| 68 | require.NoError(t, err) |
| 69 | defer resp.Body.Close() |
| 70 | require.Equal(t, http.StatusOK, resp.StatusCode) |
| 71 | |
| 72 | body, err := io.ReadAll(resp.Body) |
| 73 | require.NoError(t, err) |
| 74 | |
| 75 | // Parse API response. |
| 76 | var apiResponse struct { |
| 77 | Status string `json:"status"` |
| 78 | Data map[string]map[string]bool `json:"data"` |
| 79 | } |
| 80 | require.NoError(t, json.Unmarshal(body, &apiResponse)) |
| 81 | require.Equal(t, "success", apiResponse.Status) |
| 82 | |
| 83 | goldenPath := filepath.Join("testdata", "features.json") |
| 84 | |
| 85 | // If update flag is set, write the current features to the golden file. |
| 86 | if *updateFeatures { |
| 87 | var buf bytes.Buffer |
| 88 | encoder := json.NewEncoder(&buf) |
| 89 | encoder.SetEscapeHTML(false) |
| 90 | encoder.SetIndent("", " ") |
| 91 | require.NoError(t, encoder.Encode(apiResponse.Data)) |
| 92 | // Ensure testdata directory exists. |
nothing calls this directly
no test coverage detected
searching dependent graphs…