|
| 1 | +package tripperware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/stretchr/testify/require" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestInstantLogicalPlan ensures that the instant logical plan generation middleware |
| 11 | +// correctly produces a logical plan and insert it into the Prometheus request body. |
| 12 | + |
| 13 | +func TestInstantLogicalPlan(t *testing.T) { |
| 14 | + for i, tc := range []struct { |
| 15 | + name string |
| 16 | + input *PrometheusRequest |
| 17 | + err error |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "rate vector selector", |
| 21 | + input: &PrometheusRequest{ |
| 22 | + Start: 100000, |
| 23 | + End: 100000, |
| 24 | + Query: "rate(node_cpu_seconds_total{mode!=\"idle\"}[5m])", |
| 25 | + }, |
| 26 | + err: nil, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "memory usage expression", |
| 30 | + input: &PrometheusRequest{ |
| 31 | + Start: 100000, |
| 32 | + End: 100000, |
| 33 | + Query: "100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))", |
| 34 | + }, |
| 35 | + err: nil, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "scalar only query", |
| 39 | + input: &PrometheusRequest{ |
| 40 | + Start: 100000, |
| 41 | + End: 100000, |
| 42 | + Query: "42", |
| 43 | + }, |
| 44 | + err: nil, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "vector arithmetic", |
| 48 | + input: &PrometheusRequest{ |
| 49 | + Start: 100000, |
| 50 | + End: 100000, |
| 51 | + Query: "node_load1 / ignoring(cpu) node_cpu_seconds_total", |
| 52 | + }, |
| 53 | + err: nil, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "avg_over_time with nested rate", |
| 57 | + input: &PrometheusRequest{ |
| 58 | + Start: 100000, |
| 59 | + End: 100000, |
| 60 | + Query: "avg_over_time(rate(http_requests_total[5m])[30m:5m])", |
| 61 | + }, |
| 62 | + err: nil, |
| 63 | + }, |
| 64 | + } { |
| 65 | + tc := tc |
| 66 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + lpm := LogicalPlanGenMiddleware() |
| 70 | + handler := lpm.Wrap(HandlerFunc(func(_ context.Context, req Request) (Response, error) { |
| 71 | + return nil, nil |
| 72 | + })) |
| 73 | + |
| 74 | + // Test: Execute middleware to populate the logical plan |
| 75 | + _, err := handler.Do(context.Background(), tc.input) |
| 76 | + require.NoError(t, err) |
| 77 | + require.NotEmpty(t, tc.input.LogicalPlan, "prom request should not be empty") |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TestRangeLogicalPlan validates the range logical plan generation middleware. |
| 83 | +func TestRangeLogicalPlan(t *testing.T) { |
| 84 | + testCases := []struct { |
| 85 | + name string |
| 86 | + input *PrometheusRequest |
| 87 | + }{ |
| 88 | + { |
| 89 | + name: "rate vector over time", |
| 90 | + input: &PrometheusRequest{ |
| 91 | + Start: 100000, |
| 92 | + End: 200000, |
| 93 | + Step: 15000, |
| 94 | + Query: "rate(node_cpu_seconds_total{mode!=\"idle\"}[5m])", |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "memory usage ratio", |
| 99 | + input: &PrometheusRequest{ |
| 100 | + Start: 100000, |
| 101 | + End: 200000, |
| 102 | + Step: 30000, |
| 103 | + Query: "100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))", |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "avg_over_time function", |
| 108 | + input: &PrometheusRequest{ |
| 109 | + Start: 100000, |
| 110 | + End: 200000, |
| 111 | + Step: 60000, |
| 112 | + Query: "avg_over_time(http_requests_total[5m])", |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "vector arithmetic with range", |
| 117 | + input: &PrometheusRequest{ |
| 118 | + Start: 100000, |
| 119 | + End: 200000, |
| 120 | + Step: 10000, |
| 121 | + Query: "rate(node_network_receive_bytes_total[1m]) / rate(node_network_transmit_bytes_total[1m])", |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "simple scalar operation", |
| 126 | + input: &PrometheusRequest{ |
| 127 | + Start: 100000, |
| 128 | + End: 200000, |
| 129 | + Step: 15000, |
| 130 | + Query: "2 + 2", |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for i, tc := range testCases { |
| 136 | + tc := tc |
| 137 | + t.Run(strconv.Itoa(i)+"_"+tc.name, func(t *testing.T) { |
| 138 | + t.Parallel() |
| 139 | + |
| 140 | + middleware := LogicalPlanGenMiddleware() |
| 141 | + |
| 142 | + handler := middleware.Wrap(HandlerFunc(func(_ context.Context, req Request) (Response, error) { |
| 143 | + return nil, nil |
| 144 | + })) |
| 145 | + |
| 146 | + // Test: Execute middleware to populate the logical plan |
| 147 | + _, err := handler.Do(context.Background(), tc.input) |
| 148 | + require.NoError(t, err) |
| 149 | + require.NotEmpty(t, tc.input.LogicalPlan, "logical plan should be populated") |
| 150 | + |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments