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
42ba0dc7
Commit
42ba0dc7
authored
Jul 03, 2018
by
Tobias Skarhed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Karma to Jest: Cloudwatch datasource
parent
d4bd5213
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
76 deletions
+104
-76
public/app/plugins/datasource/cloudwatch/specs/datasource.jest.ts
+86
-76
public/test/specs/helpers.ts
+18
-0
No files found.
public/app/plugins/datasource/cloudwatch/specs/datasource
_specs
.ts
→
public/app/plugins/datasource/cloudwatch/specs/datasource
.jest
.ts
View file @
42ba0dc7
import
'../datasource'
;
import
{
describe
,
beforeEach
,
it
,
expect
,
angularMocks
}
from
'test/lib/common'
;
import
helpers
from
'test/specs/helpers'
;
//
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import
{
TemplateSrvStub
,
jestTimeSrvStub
}
from
'test/specs/helpers'
;
import
CloudWatchDatasource
from
'../datasource'
;
import
'app/features/dashboard/time_srv'
;
describe
(
'CloudWatchDatasource'
,
function
()
{
var
ctx
=
new
helpers
.
ServiceTestContext
();
var
instanceSettings
=
{
let
instanceSettings
=
{
jsonData
:
{
defaultRegion
:
'us-east-1'
,
access
:
'proxy'
},
};
let
templateSrv
=
new
TemplateSrvStub
();
let
timeSrv
=
new
jestTimeSrvStub
();
let
backendSrv
=
{};
let
ctx
=
<
any
>
{
backendSrv
,
templateSrv
};
beforeEach
(
angularMocks
.
module
(
'grafana.core'
));
beforeEach
(
angularMocks
.
module
(
'grafana.services'
));
beforeEach
(
angularMocks
.
module
(
'grafana.controllers'
));
beforeEach
(
ctx
.
providePhase
([
'templateSrv'
,
'backendSrv'
]));
beforeEach
(
ctx
.
createService
(
'timeSrv'
));
beforeEach
(
angularMocks
.
inject
(
function
(
$q
,
$rootScope
,
$httpBackend
,
$injector
)
{
ctx
.
$q
=
$q
;
ctx
.
$httpBackend
=
$httpBackend
;
ctx
.
$rootScope
=
$rootScope
;
ctx
.
ds
=
$injector
.
instantiate
(
CloudWatchDatasource
,
{
instanceSettings
:
instanceSettings
,
});
$httpBackend
.
when
(
'GET'
,
/
\.
html$/
).
respond
(
''
);
})
);
beforeEach
(()
=>
{
ctx
.
ds
=
new
CloudWatchDatasource
(
instanceSettings
,
{},
backendSrv
,
templateSrv
,
timeSrv
);
});
// beforeEach(angularMocks.module('grafana.core'));
// beforeEach(angularMocks.module('grafana.services'));
// beforeEach(angularMocks.module('grafana.controllers'));
// beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
// beforeEach(ctx.createService('timeSrv'));
// beforeEach(
// angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
// ctx.$q = $q;
// ctx.$httpBackend = $httpBackend;
// ctx.$rootScope = $rootScope;
// ctx.ds = $injector.instantiate(CloudWatchDatasource, {
// instanceSettings: instanceSettings,
// });
// $httpBackend.when('GET', /\.html$/).respond('');
// })
// );
describe
(
'When performing CloudWatch query'
,
function
()
{
var
requestParams
;
...
...
@@ -67,24 +77,24 @@ describe('CloudWatchDatasource', function() {
},
};
beforeEach
(
function
()
{
ctx
.
backendSrv
.
datasourceRequest
=
function
(
params
)
{
beforeEach
(
async
()
=>
{
ctx
.
backendSrv
.
datasourceRequest
=
await
jest
.
fn
((
params
)
=>
{
requestParams
=
params
.
data
;
return
ctx
.
$q
.
when
({
data
:
response
});
};
return
Promise
.
resolve
({
data
:
response
});
}
)
;
});
it
(
'should generate the correct query'
,
function
(
done
)
{
ctx
.
ds
.
query
(
query
).
then
(
function
()
{
var
params
=
requestParams
.
queries
[
0
];
expect
(
params
.
namespace
).
to
.
b
e
(
query
.
targets
[
0
].
namespace
);
expect
(
params
.
metricName
).
to
.
b
e
(
query
.
targets
[
0
].
metricName
);
expect
(
params
.
dimensions
[
'InstanceId'
]).
to
.
b
e
(
'i-12345678'
);
expect
(
params
.
statistics
).
to
.
eq
l
(
query
.
targets
[
0
].
statistics
);
expect
(
params
.
period
).
to
.
b
e
(
query
.
targets
[
0
].
period
);
expect
(
params
.
namespace
).
to
B
e
(
query
.
targets
[
0
].
namespace
);
expect
(
params
.
metricName
).
to
B
e
(
query
.
targets
[
0
].
metricName
);
expect
(
params
.
dimensions
[
'InstanceId'
]).
to
B
e
(
'i-12345678'
);
expect
(
params
.
statistics
).
to
Equa
l
(
query
.
targets
[
0
].
statistics
);
expect
(
params
.
period
).
to
B
e
(
query
.
targets
[
0
].
period
);
done
();
});
ctx
.
$rootScope
.
$apply
();
//
ctx.$rootScope.$apply();
});
it
(
'should generate the correct query with interval variable'
,
function
(
done
)
{
...
...
@@ -111,19 +121,19 @@ describe('CloudWatchDatasource', function() {
ctx
.
ds
.
query
(
query
).
then
(
function
()
{
var
params
=
requestParams
.
queries
[
0
];
expect
(
params
.
period
).
to
.
b
e
(
'600'
);
expect
(
params
.
period
).
to
B
e
(
'600'
);
done
();
});
ctx
.
$rootScope
.
$apply
();
//
ctx.$rootScope.$apply();
});
it
(
'should return series list'
,
function
(
done
)
{
ctx
.
ds
.
query
(
query
).
then
(
function
(
result
)
{
expect
(
result
.
data
[
0
].
target
).
to
.
b
e
(
response
.
results
.
A
.
series
[
0
].
name
);
expect
(
result
.
data
[
0
].
datapoints
[
0
][
0
]).
to
.
b
e
(
response
.
results
.
A
.
series
[
0
].
points
[
0
][
0
]);
expect
(
result
.
data
[
0
].
target
).
to
B
e
(
response
.
results
.
A
.
series
[
0
].
name
);
expect
(
result
.
data
[
0
].
datapoints
[
0
][
0
]).
to
B
e
(
response
.
results
.
A
.
series
[
0
].
points
[
0
][
0
]);
done
();
});
ctx
.
$rootScope
.
$apply
();
//
ctx.$rootScope.$apply();
});
it
(
'should generate the correct targets by expanding template variables'
,
function
()
{
...
...
@@ -173,7 +183,7 @@ describe('CloudWatchDatasource', function() {
];
var
result
=
ctx
.
ds
.
expandTemplateVariable
(
targets
,
{},
templateSrv
);
expect
(
result
[
0
].
dimensions
.
InstanceId
).
to
.
b
e
(
'i-34567890'
);
expect
(
result
[
0
].
dimensions
.
InstanceId
).
to
B
e
(
'i-34567890'
);
});
it
(
'should generate the correct targets by expanding template variables from url'
,
function
()
{
...
...
@@ -220,7 +230,7 @@ describe('CloudWatchDatasource', function() {
];
var
result
=
ctx
.
ds
.
expandTemplateVariable
(
targets
,
{},
templateSrv
);
expect
(
result
[
0
].
dimensions
.
InstanceId
).
to
.
b
e
(
'i-45678901'
);
expect
(
result
[
0
].
dimensions
.
InstanceId
).
to
B
e
(
'i-45678901'
);
});
});
...
...
@@ -228,21 +238,21 @@ describe('CloudWatchDatasource', function() {
it
(
'should return the datasource region if empty or "default"'
,
function
()
{
var
defaultRegion
=
instanceSettings
.
jsonData
.
defaultRegion
;
expect
(
ctx
.
ds
.
getActualRegion
()).
to
.
b
e
(
defaultRegion
);
expect
(
ctx
.
ds
.
getActualRegion
(
''
)).
to
.
b
e
(
defaultRegion
);
expect
(
ctx
.
ds
.
getActualRegion
(
'default'
)).
to
.
b
e
(
defaultRegion
);
expect
(
ctx
.
ds
.
getActualRegion
()).
to
B
e
(
defaultRegion
);
expect
(
ctx
.
ds
.
getActualRegion
(
''
)).
to
B
e
(
defaultRegion
);
expect
(
ctx
.
ds
.
getActualRegion
(
'default'
)).
to
B
e
(
defaultRegion
);
});
it
(
'should return the specified region if specified'
,
function
()
{
expect
(
ctx
.
ds
.
getActualRegion
(
'some-fake-region-1'
)).
to
.
b
e
(
'some-fake-region-1'
);
expect
(
ctx
.
ds
.
getActualRegion
(
'some-fake-region-1'
)).
to
B
e
(
'some-fake-region-1'
);
});
var
requestParams
;
beforeEach
(
function
()
{
ctx
.
ds
.
performTimeSeriesQuery
=
function
(
request
)
{
ctx
.
ds
.
performTimeSeriesQuery
=
jest
.
fn
((
request
)
=>
{
requestParams
=
request
;
return
ctx
.
$q
.
when
({
data
:
{}
});
};
return
Promise
.
resolve
({
data
:
{}
});
}
)
;
});
it
(
'should query for the datasource region if empty or "default"'
,
function
(
done
)
{
...
...
@@ -264,10 +274,10 @@ describe('CloudWatchDatasource', function() {
};
ctx
.
ds
.
query
(
query
).
then
(
function
(
result
)
{
expect
(
requestParams
.
queries
[
0
].
region
).
to
.
b
e
(
instanceSettings
.
jsonData
.
defaultRegion
);
expect
(
requestParams
.
queries
[
0
].
region
).
to
B
e
(
instanceSettings
.
jsonData
.
defaultRegion
);
done
();
});
ctx
.
$rootScope
.
$apply
();
//
ctx.$rootScope.$apply();
});
});
...
...
@@ -311,18 +321,18 @@ describe('CloudWatchDatasource', function() {
};
beforeEach
(
function
()
{
ctx
.
backendSrv
.
datasourceRequest
=
function
(
params
)
{
return
ctx
.
$q
.
when
({
data
:
response
});
};
ctx
.
backendSrv
.
datasourceRequest
=
jest
.
fn
((
params
)
=>
{
return
Promise
.
resolve
({
data
:
response
});
}
)
;
});
it
(
'should return series list'
,
function
(
done
)
{
ctx
.
ds
.
query
(
query
).
then
(
function
(
result
)
{
expect
(
result
.
data
[
0
].
target
).
to
.
b
e
(
response
.
results
.
A
.
series
[
0
].
name
);
expect
(
result
.
data
[
0
].
datapoints
[
0
][
0
]).
to
.
b
e
(
response
.
results
.
A
.
series
[
0
].
points
[
0
][
0
]);
expect
(
result
.
data
[
0
].
target
).
to
B
e
(
response
.
results
.
A
.
series
[
0
].
name
);
expect
(
result
.
data
[
0
].
datapoints
[
0
][
0
]).
to
B
e
(
response
.
results
.
A
.
series
[
0
].
points
[
0
][
0
]);
done
();
});
ctx
.
$rootScope
.
$apply
();
//
ctx.$rootScope.$apply();
});
});
...
...
@@ -332,14 +342,14 @@ describe('CloudWatchDatasource', function() {
scenario
.
setup
=
setupCallback
=>
{
beforeEach
(()
=>
{
setupCallback
();
ctx
.
backendSrv
.
datasourceRequest
=
args
=>
{
ctx
.
backendSrv
.
datasourceRequest
=
jest
.
fn
((
args
)
=>
{
scenario
.
request
=
args
.
data
;
return
ctx
.
$q
.
when
({
data
:
scenario
.
requestResponse
});
};
return
Promise
.
resolve
({
data
:
scenario
.
requestResponse
});
}
)
;
ctx
.
ds
.
metricFindQuery
(
query
).
then
(
args
=>
{
scenario
.
result
=
args
;
});
ctx
.
$rootScope
.
$apply
();
//
ctx.$rootScope.$apply();
});
};
...
...
@@ -359,9 +369,9 @@ describe('CloudWatchDatasource', function() {
});
it
(
'should call __GetRegions and return result'
,
()
=>
{
expect
(
scenario
.
result
[
0
].
text
).
to
.
c
ontain
(
'us-east-1'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
.
b
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
.
b
e
(
'regions'
);
expect
(
scenario
.
result
[
0
].
text
).
to
C
ontain
(
'us-east-1'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
B
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
B
e
(
'regions'
);
});
});
...
...
@@ -377,9 +387,9 @@ describe('CloudWatchDatasource', function() {
});
it
(
'should call __GetNamespaces and return result'
,
()
=>
{
expect
(
scenario
.
result
[
0
].
text
).
to
.
c
ontain
(
'AWS/EC2'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
.
b
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
.
b
e
(
'namespaces'
);
expect
(
scenario
.
result
[
0
].
text
).
to
C
ontain
(
'AWS/EC2'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
B
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
B
e
(
'namespaces'
);
});
});
...
...
@@ -395,9 +405,9 @@ describe('CloudWatchDatasource', function() {
});
it
(
'should call __GetMetrics and return result'
,
()
=>
{
expect
(
scenario
.
result
[
0
].
text
).
to
.
b
e
(
'CPUUtilization'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
.
b
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
.
b
e
(
'metrics'
);
expect
(
scenario
.
result
[
0
].
text
).
to
B
e
(
'CPUUtilization'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
B
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
B
e
(
'metrics'
);
});
});
...
...
@@ -413,9 +423,9 @@ describe('CloudWatchDatasource', function() {
});
it
(
'should call __GetDimensions and return result'
,
()
=>
{
expect
(
scenario
.
result
[
0
].
text
).
to
.
b
e
(
'InstanceId'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
.
b
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
.
b
e
(
'dimension_keys'
);
expect
(
scenario
.
result
[
0
].
text
).
to
B
e
(
'InstanceId'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
B
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
B
e
(
'dimension_keys'
);
});
});
...
...
@@ -431,9 +441,9 @@ describe('CloudWatchDatasource', function() {
});
it
(
'should call __ListMetrics and return result'
,
()
=>
{
expect
(
scenario
.
result
[
0
].
text
).
to
.
c
ontain
(
'i-12345678'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
.
b
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
.
b
e
(
'dimension_values'
);
expect
(
scenario
.
result
[
0
].
text
).
to
C
ontain
(
'i-12345678'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
B
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
B
e
(
'dimension_values'
);
});
});
...
...
@@ -449,9 +459,9 @@ describe('CloudWatchDatasource', function() {
});
it
(
'should call __ListMetrics and return result'
,
()
=>
{
expect
(
scenario
.
result
[
0
].
text
).
to
.
c
ontain
(
'i-12345678'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
.
b
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
.
b
e
(
'dimension_values'
);
expect
(
scenario
.
result
[
0
].
text
).
to
C
ontain
(
'i-12345678'
);
expect
(
scenario
.
request
.
queries
[
0
].
type
).
to
B
e
(
'metricFindQuery'
);
expect
(
scenario
.
request
.
queries
[
0
].
subtype
).
to
B
e
(
'dimension_values'
);
});
});
...
...
@@ -544,7 +554,7 @@ describe('CloudWatchDatasource', function() {
let
now
=
new
Date
(
options
.
range
.
from
.
valueOf
()
+
t
[
2
]
*
1000
);
let
expected
=
t
[
3
];
let
actual
=
ctx
.
ds
.
getPeriod
(
target
,
options
,
now
);
expect
(
actual
).
to
.
b
e
(
expected
);
expect
(
actual
).
to
B
e
(
expected
);
}
});
});
public/test/specs/helpers.ts
View file @
42ba0dc7
...
...
@@ -195,6 +195,24 @@ export function TemplateSrvStub() {
};
}
export
function
jestTimeSrvStub
()
{
this
.
init
=
jest
.
fn
();
this
.
time
=
{
from
:
'now-1h'
,
to
:
'now'
};
this
.
timeRange
=
jest
.
fn
(
parse
=>
{
if
(
parse
===
false
)
{
return
this
.
time
;
}
return
{
from
:
dateMath
.
parse
(
this
.
time
.
from
,
false
),
to
:
dateMath
.
parse
(
this
.
time
.
to
,
true
),
};
});
this
.
replace
=
jest
.
fn
(
target
=>
target
);
this
.
setTime
=
jest
.
fn
(
time
=>
{
this
.
time
=
time
;
});
}
var
allDeps
=
{
ContextSrvStub
,
TemplateSrvStub
,
...
...
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