Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
3c1bcc72
Unverified
Commit
3c1bcc72
authored
Dec 04, 2020
by
Emil Hessman
Committed by
GitHub
Dec 04, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Chore: Rewrite tsdb prometheus test to standard library (#29592)
Signed-off-by: Emil Hessman <emil@hessman.se>
parent
f326b79c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
108 deletions
+77
-108
pkg/tsdb/prometheus/prometheus_test.go
+77
-108
No files found.
pkg/tsdb/prometheus/prometheus_test.go
View file @
3c1bcc72
...
...
@@ -4,156 +4,125 @@ import (
"testing"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/components/simplejson"
p
"github.com/prometheus/common/model"
.
"github.com/smartystreets/goconvey/convey
"
"github.com/stretchr/testify/require
"
)
func
TestPrometheus
(
t
*
testing
.
T
)
{
Convey
(
"Prometheus"
,
t
,
func
()
{
dsInfo
:=
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
(),
dsInfo
:=
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
(),
}
t
.
Run
(
"converting metric name"
,
func
(
t
*
testing
.
T
)
{
metric
:=
map
[
p
.
LabelName
]
p
.
LabelValue
{
p
.
LabelName
(
"app"
)
:
p
.
LabelValue
(
"backend"
),
p
.
LabelName
(
"device"
)
:
p
.
LabelValue
(
"mobile"
),
}
Convey
(
"converting metric name"
,
func
()
{
metric
:=
map
[
p
.
LabelName
]
p
.
LabelValue
{
p
.
LabelName
(
"app"
)
:
p
.
LabelValue
(
"backend"
),
p
.
LabelName
(
"device"
)
:
p
.
LabelValue
(
"mobile"
),
}
query
:=
&
PrometheusQuery
{
LegendFormat
:
"legend {{app}} {{ device }} {{broken}}"
,
}
query
:=
&
PrometheusQuery
{
LegendFormat
:
"legend {{app}} {{ device }} {{broken}}"
,
}
So
(
formatLegend
(
metric
,
query
),
ShouldEqual
,
"legend backend mobile "
)
})
require
.
Equal
(
t
,
"legend backend mobile "
,
formatLegend
(
metric
,
query
)
)
})
Convey
(
"build full series name"
,
func
(
)
{
metric
:=
map
[
p
.
LabelName
]
p
.
LabelValue
{
p
.
LabelName
(
p
.
MetricNameLabel
)
:
p
.
LabelValue
(
"http_request_total"
),
p
.
LabelName
(
"app"
)
:
p
.
LabelValue
(
"backend"
),
p
.
LabelName
(
"device"
)
:
p
.
LabelValue
(
"mobile"
),
}
t
.
Run
(
"build full series name"
,
func
(
t
*
testing
.
T
)
{
metric
:=
map
[
p
.
LabelName
]
p
.
LabelValue
{
p
.
LabelName
(
p
.
MetricNameLabel
)
:
p
.
LabelValue
(
"http_request_total"
),
p
.
LabelName
(
"app"
)
:
p
.
LabelValue
(
"backend"
),
p
.
LabelName
(
"device"
)
:
p
.
LabelValue
(
"mobile"
),
}
query
:=
&
PrometheusQuery
{
LegendFormat
:
""
,
}
query
:=
&
PrometheusQuery
{
LegendFormat
:
""
,
}
So
(
formatLegend
(
metric
,
query
),
ShouldEqual
,
`http_request_total{app="backend", device="mobile"}`
)
})
require
.
Equal
(
t
,
`http_request_total{app="backend", device="mobile"}`
,
formatLegend
(
metric
,
query
)
)
})
Convey
(
"parsing query model with step"
,
func
(
)
{
json
:=
`{
t
.
Run
(
"parsing query model with step"
,
func
(
t
*
testing
.
T
)
{
json
:=
`{
"expr": "go_goroutines",
"format": "time_series",
"refId": "A"
}`
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"12h"
,
"now"
)
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"12h"
,
"now"
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Second
*
30
)
}
)
})
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
time
.
Second
*
30
,
models
[
0
]
.
Step
)
})
Convey
(
"parsing query model without step parameter"
,
func
(
)
{
json
:=
`{
t
.
Run
(
"parsing query model without step parameter"
,
func
(
t
*
testing
.
T
)
{
json
:=
`{
"expr": "go_goroutines",
"format": "time_series",
"intervalFactor": 1,
"refId": "A"
}`
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
2
)
})
Convey
(
"with 1h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"1h"
,
"now"
)
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
So
(
err
,
ShouldBeNil
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
time
.
Minute
*
2
,
models
[
0
]
.
Step
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Second
*
15
)
})
})
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"1h"
,
"now"
)
models
,
err
=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
time
.
Second
*
15
,
models
[
0
]
.
Step
)
})
Convey
(
"parsing query model with intervalFactor"
,
func
()
{
Convey
(
"high intervalFactor"
,
func
()
{
json
:=
`{
t
.
Run
(
"parsing query model with high intervalFactor"
,
func
(
t
*
testing
.
T
)
{
json
:=
`{
"expr": "go_goroutines",
"format": "time_series",
"intervalFactor": 10,
"refId": "A"
}`
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
So
(
err
,
ShouldBeNil
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
20
)
}
)
})
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
time
.
Minute
*
20
,
models
[
0
]
.
Step
)
})
Convey
(
"low intervalFactor"
,
func
(
)
{
json
:=
`{
t
.
Run
(
"parsing query model with low intervalFactor"
,
func
(
t
*
testing
.
T
)
{
json
:=
`{
"expr": "go_goroutines",
"format": "time_series",
"intervalFactor": 1,
"refId": "A"
}`
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
jsonModel
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
queryContext
:=
&
tsdb
.
TsdbQuery
{}
queryModels
:=
[]
*
tsdb
.
Query
{
{
Model
:
jsonModel
},
}
So
(
err
,
ShouldBeNil
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
2
)
})
})
})
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
time
.
Minute
*
2
,
models
[
0
]
.
Step
)
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment