Skip to content

Commit 4d16607

Browse files
committed
implement fake for prometheus API
Signed-off-by: Ying WANG <[email protected]>
1 parent 34eaefd commit 4d16607

File tree

2 files changed

+263
-0
lines changed

2 files changed

+263
-0
lines changed

api/prometheus/v1/api_fake.go

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/prometheus/common/model"
8+
)
9+
10+
type FakeAPI struct {
11+
// FakeAPI is a mock API for testing purposes.
12+
// It implements the API interface and provides fake data for testing.
13+
ExpectedAlertsResult []*Alert
14+
ExpectedAlertsError error
15+
16+
ExpectedAlertManagersResult AlertManagersResult
17+
ExpectedAlertManagersError error
18+
19+
ExpectedCleanTombstonesError error
20+
21+
ExpectedConfigResult ConfigResult
22+
ExpectedConfigError error
23+
24+
ExpectedDeleteSeriesError error
25+
26+
ExpectedFlagsResult FlagsResult
27+
ExpectedFlagsError error
28+
29+
ExpectedLabelNamesResult []string
30+
ExpectedLabelNamesWarnings Warnings
31+
ExpectedLabelNamesError error
32+
33+
ExpectedLabelValuesResult model.LabelValues
34+
ExpectedLabelValuesWarnings Warnings
35+
ExpectedLabelValuesError error
36+
37+
ExpectedQueryResult model.Value
38+
ExpectedQueryWarnings Warnings
39+
ExpectedQueryError error
40+
41+
ExpectedQueryRangeResult model.Value
42+
ExpectedQueryRangeWarnings Warnings
43+
ExpectedQueryRangeError error
44+
45+
ExpectedQueryExemplarsResult []ExemplarQueryResult
46+
ExpectedQueryExemplarsError error
47+
48+
ExpectedBuildinfoResult BuildinfoResult
49+
ExpectedBuildinfoError error
50+
51+
ExpectedRuntimeinfoResult RuntimeinfoResult
52+
ExpectedRuntimeinfoError error
53+
54+
ExpectedSeriesResult []model.LabelSet
55+
ExpectedSeriesWarnings Warnings
56+
ExpectedSeriesError error
57+
58+
ExpectedSnapshotResult SnapshotResult
59+
ExpectedSnapshotError error
60+
61+
ExpectedRulesResult RulesResult
62+
ExpectedRulesError error
63+
64+
ExpectedTargetsResult TargetsResult
65+
ExpectedTargetsError error
66+
67+
ExpectedTargetsMetadataResult []MetricMetadata
68+
ExpectedTargetsMetadataError error
69+
70+
ExpectedMetadataResult map[string][]Metadata
71+
ExpectedMetadataError error
72+
73+
ExpectedTSDBResult TSDBResult
74+
ExpectedTSDBError error
75+
76+
ExpectedWalReplayResult WalReplayStatus
77+
ExpectedWalReplayError error
78+
}
79+
80+
func (f *FakeAPI) Alerts(ctx context.Context) ([]*Alert, error) {
81+
return f.ExpectedAlertsResult, f.ExpectedAlertsError
82+
}
83+
84+
func (f *FakeAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error) {
85+
return f.ExpectedAlertManagersResult, f.ExpectedAlertManagersError
86+
}
87+
88+
func (f *FakeAPI) CleanTombstones(ctx context.Context) error {
89+
return f.ExpectedCleanTombstonesError
90+
}
91+
92+
func (f *FakeAPI) Config(ctx context.Context) (ConfigResult, error) {
93+
return f.ExpectedConfigResult, f.ExpectedConfigError
94+
}
95+
96+
func (f *FakeAPI) DeleteSeries(ctx context.Context, matches []string, startTime, endTime time.Time) error {
97+
return f.ExpectedDeleteSeriesError
98+
}
99+
func (f *FakeAPI) Flags(ctx context.Context) (FlagsResult, error) {
100+
return f.ExpectedFlagsResult, f.ExpectedFlagsError
101+
}
102+
103+
func (f *FakeAPI) LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time, opts ...Option) ([]string, Warnings, error) {
104+
return f.ExpectedLabelNamesResult, f.ExpectedLabelNamesWarnings, f.ExpectedLabelNamesError
105+
}
106+
107+
func (f *FakeAPI) LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time, opts ...Option) (model.LabelValues, Warnings, error) {
108+
return f.ExpectedLabelValuesResult, f.ExpectedLabelValuesWarnings, f.ExpectedLabelValuesError
109+
}
110+
111+
func (f *FakeAPI) Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error) {
112+
return f.ExpectedQueryResult, f.ExpectedQueryWarnings, f.ExpectedQueryError
113+
}
114+
115+
func (f *FakeAPI) QueryRange(ctx context.Context, query string, r Range, opts ...Option) (model.Value, Warnings, error) {
116+
return f.ExpectedQueryRangeResult, f.ExpectedQueryRangeWarnings, f.ExpectedQueryRangeError
117+
}
118+
119+
func (f *FakeAPI) QueryExemplars(ctx context.Context, query string, startTime, endTime time.Time) ([]ExemplarQueryResult, error) {
120+
return f.ExpectedQueryExemplarsResult, f.ExpectedQueryExemplarsError
121+
}
122+
123+
func (f *FakeAPI) Buildinfo(ctx context.Context) (BuildinfoResult, error) {
124+
return f.ExpectedBuildinfoResult, f.ExpectedBuildinfoError
125+
}
126+
127+
func (f *FakeAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
128+
return f.ExpectedRuntimeinfoResult, f.ExpectedRuntimeinfoError
129+
}
130+
131+
func (f *FakeAPI) Series(ctx context.Context, matches []string, startTime, endTime time.Time, opts ...Option) ([]model.LabelSet, Warnings, error) {
132+
return f.ExpectedSeriesResult, f.ExpectedSeriesWarnings, f.ExpectedSeriesError
133+
}
134+
135+
func (f *FakeAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error) {
136+
return f.ExpectedSnapshotResult, f.ExpectedSnapshotError
137+
}
138+
139+
func (f *FakeAPI) Rules(ctx context.Context) (RulesResult, error) {
140+
return f.ExpectedRulesResult, f.ExpectedRulesError
141+
}
142+
143+
func (f *FakeAPI) Targets(ctx context.Context) (TargetsResult, error) {
144+
return f.ExpectedTargetsResult, f.ExpectedTargetsError
145+
}
146+
147+
func (f *FakeAPI) TargetsMetadata(ctx context.Context, matchTarget, metric, limit string) ([]MetricMetadata, error) {
148+
return f.ExpectedTargetsMetadataResult, f.ExpectedTargetsMetadataError
149+
}
150+
151+
func (f *FakeAPI) Metadata(ctx context.Context, metric, limit string) (map[string][]Metadata, error) {
152+
return f.ExpectedMetadataResult, f.ExpectedMetadataError
153+
}
154+
155+
func (f *FakeAPI) TSDB(ctx context.Context, opts ...Option) (TSDBResult, error) {
156+
return f.ExpectedTSDBResult, f.ExpectedTSDBError
157+
}
158+
159+
func (f *FakeAPI) WalReplay(ctx context.Context) (WalReplayStatus, error) {
160+
return f.ExpectedWalReplayResult, f.ExpectedWalReplayError
161+
}

api/prometheus/v1/api_fake_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"testing"
8+
"time"
9+
10+
"github.com/prometheus/common/model"
11+
)
12+
13+
func assertEqual(t *testing.T, a, b interface{}) {
14+
if !reflect.DeepEqual(a, b) {
15+
t.Errorf("%v != %v", a, b)
16+
}
17+
}
18+
19+
func TestFakeAPI_Query(t *testing.T) {
20+
tests := []struct {
21+
name string
22+
query string
23+
expectedResult model.Value
24+
expectedWarnings Warnings
25+
expectedError error
26+
}{
27+
{
28+
name: "Valid query",
29+
query: "up == 1",
30+
expectedResult: &model.String{Value: "1"},
31+
},
32+
{
33+
name: "Query with no results, warning present",
34+
query: "up == 0",
35+
expectedResult: nil,
36+
expectedWarnings: Warnings{"Warning: No data found for query, check if the time range is correct"},
37+
expectedError: nil,
38+
},
39+
{
40+
name: "Error query",
41+
query: "invalid_query",
42+
expectedError: fmt.Errorf("mock error"),
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
// Setup FakeAPI
49+
fakeAPI := &FakeAPI{
50+
ExpectedQueryResult: tt.expectedResult,
51+
ExpectedQueryWarnings: tt.expectedWarnings,
52+
ExpectedQueryError: tt.expectedError,
53+
}
54+
55+
result, warnings, err := fakeAPI.Query(context.Background(), tt.query, time.Now())
56+
assertEqual(t, tt.expectedResult, result)
57+
assertEqual(t, tt.expectedWarnings, warnings)
58+
assertEqual(t, tt.expectedError, err)
59+
})
60+
}
61+
}
62+
63+
func TestFakeAPI_LabelNames(t *testing.T) {
64+
tests := []struct {
65+
name string
66+
matches []string
67+
expectedLabels []string
68+
expectedWarnings Warnings
69+
expectedError error
70+
}{
71+
{
72+
name: "Valid label names",
73+
matches: []string{"up"},
74+
expectedLabels: []string{"label1", "label2"},
75+
expectedWarnings: nil,
76+
expectedError: nil,
77+
},
78+
{
79+
name: "Error in label names",
80+
matches: []string{"error"},
81+
expectedLabels: nil,
82+
expectedWarnings: nil,
83+
expectedError: fmt.Errorf("mock label error"),
84+
},
85+
}
86+
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
// Setup FakeAPI
90+
fakeAPI := &FakeAPI{
91+
ExpectedLabelNamesResult: tt.expectedLabels,
92+
ExpectedLabelNamesWarnings: tt.expectedWarnings,
93+
ExpectedLabelNamesError: tt.expectedError,
94+
}
95+
96+
result, warnings, err := fakeAPI.LabelNames(context.Background(), tt.matches, time.Now(), time.Now())
97+
assertEqual(t, tt.expectedLabels, result)
98+
assertEqual(t, tt.expectedWarnings, warnings)
99+
assertEqual(t, tt.expectedError, err)
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)