-
Notifications
You must be signed in to change notification settings - Fork 26
/
GraphiteEventServiceSpec.groovy
131 lines (110 loc) · 5.44 KB
/
GraphiteEventServiceSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* OpenSpeedMonitor (OSM)
* Copyright 2014 iteratec GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.iteratec.osm.report.external
import de.iteratec.osm.batch.Activity
import de.iteratec.osm.batch.BatchActivity
import de.iteratec.osm.batch.BatchActivityService
import de.iteratec.osm.batch.BatchActivityUpdaterDummy
import de.iteratec.osm.measurement.environment.wptserver.HttpRequestService
import de.iteratec.osm.measurement.environment.wptserver.Protocol
import de.iteratec.osm.measurement.schedule.JobGroup
import de.iteratec.osm.report.chart.CsiAggregationUtilService
import de.iteratec.osm.report.chart.Event
import de.iteratec.osm.report.chart.EventDaoService
import grails.buildtestdata.BuildDataTest
import grails.testing.services.ServiceUnitTest
import groovy.json.JsonSlurper
import org.joda.time.DateTime
import spock.lang.Specification
class GraphiteEventServiceSpec extends Specification implements BuildDataTest, ServiceUnitTest<GraphiteEventService>{
public static final DateTime untilDateTime = new DateTime(2015, 5, 29, 5, 0, 0)
public static final int minutesInPast = 1
GraphiteEventService serviceUnderTest
public static final String jobGroupName = 'associated JobGroup'
public static String metricName = 'alias(drawAsInfinite(server.monitor02.*.load.load_fifteen),"my-graph")'
Closure doWithSpring() {
return {
eventDaoService(EventDaoService)
csiAggregationUtilService(CsiAggregationUtilService)
httpRequestService(HttpRequestService)
}
}
def setup() {
serviceUnderTest = service
//mocks common for all tests/////////////////////////////////////////////////////////////////////////////////////////////
mockBatchActivityService()
serviceUnderTest.eventDaoService = grailsApplication.mainContext.getBean('eventDaoService')
File jsonFile = new File("src/test/resources/GraphiteEvents/GraphiteEventServiceSpec_retreive_events.json")
def jsonData = new JsonSlurper().parse(jsonFile)
serviceUnderTest.httpRequestService = Stub(HttpRequestService) {
getJsonResponse(_ as String, _ as String, _ as Object) >> jsonData
}
mockCsiAggregationUtilService()
}
void setupSpec() {
mockDomains(GraphiteServer, BatchActivity, Event, JobGroup, GraphiteEventSourcePath)
}
def "retrieve events from test graphite server test"() {
given:
createGraphiteServerWithSourcePaths()
when: "we fetch graphite events and create some filtered lists"
serviceUnderTest.fetchGraphiteEvents(false, minutesInPast)
List<Event> allEvents = Event.list()
List<Event> eventsWithExpectedNames = allEvents.findAll { it.shortName == 'from graphite|my-graph' }
List<Event> eventsWithExpectedDescriptions = allEvents.findAll{it.description == "Read from Graphite: my-graph [$metricName]" }
List<Event> eventsWhichAreNotGloballyVisible = allEvents.findAll{ !it.globallyVisible }
List<Event> eventsWithExpectedJobGroup = allEvents.findAll{it.jobGroups*.name.contains(jobGroupName)}
then: "the filtered events should match contain the expected events"
allEvents.size() == 6
eventsWithExpectedNames.size() == allEvents.size()
eventsWithExpectedDescriptions.size() == allEvents.size()
eventsWhichAreNotGloballyVisible.size() == allEvents.size()
eventsWithExpectedJobGroup.size() == allEvents.size()
}
private void createGraphiteServerWithSourcePaths() {
GraphiteServer server = new GraphiteServer(
serverAdress: 'url.to.carbon',
port: 2003,
webappUrl: 'monitoring.hh.iteratec.de/',
webappProtocol: Protocol.HTTP,
webappPathToRenderingEngine: 'render'
)
JobGroup jobGroup = new JobGroup(
name: jobGroupName,
resultGraphiteServers: [server],
)
GraphiteEventSourcePath eventSourcePath = new GraphiteEventSourcePath(
staticPrefix: 'from graphite|',
targetMetricName: metricName,
jobGroups: [jobGroup]
)
server.addToGraphiteEventSourcePaths(eventSourcePath)
server.save(failOnError: true)
}
private void mockBatchActivityService(){
serviceUnderTest.batchActivityService = Stub(BatchActivityService){
getActiveBatchActivity(_ as Class, _ as Activity,_ as String, _ as int,_ as Boolean) >> { Class c, Activity activity, String name, int maxStages, boolean observe ->
return new BatchActivityUpdaterDummy(name,c.name,activity, maxStages, 5000)
}
}
}
private void mockCsiAggregationUtilService(){
CsiAggregationUtilService mockedCsiAggregationUtilService = Spy(CsiAggregationUtilService)
mockedCsiAggregationUtilService.getNowInUtc() >> untilDateTime
serviceUnderTest.csiAggregationUtilService = mockedCsiAggregationUtilService
}
}