@@ -2,20 +2,10 @@ package cromwell.backend.google.batch.api.request
22
33import akka .actor .ActorSystem
44import akka .testkit .TestKit
5- import com .google .cloud .batch .v1 .{
6- AllocationPolicy ,
7- BatchServiceClient ,
8- BatchServiceSettings ,
9- GetJobRequest ,
10- Job ,
11- JobStatus
12- }
13- import com .google .cloud .batch .v1 .AllocationPolicy .{
14- InstancePolicy ,
15- InstancePolicyOrTemplate ,
16- LocationPolicy ,
17- ProvisioningModel
18- }
5+ import com .google .cloud .batch .v1 .{AllocationPolicy , BatchServiceClient , BatchServiceSettings , GetJobRequest , Job , JobStatus , StatusEvent }
6+ import com .google .cloud .batch .v1 .AllocationPolicy .{InstancePolicy , InstancePolicyOrTemplate , LocationPolicy , ProvisioningModel }
7+ import com .google .cloud .batch .v1 .JobStatus .State
8+ import com .google .protobuf .Timestamp
199import common .mock .MockSugar
2010import cromwell .backend .google .batch .api .BatchApiResponse
2111import cromwell .backend .google .batch .models .RunStatus
@@ -32,30 +22,57 @@ class BatchRequestExecutorSpec
3222 with MockSugar
3323 with PrivateMethodTester {
3424
35- behavior of " BatchRequestExecutor "
36-
37- it should " create instantiatedVmInfo correctly " in {
38-
25+ def setupBatchClient ( machineType : String = " n1-standard-1 " ,
26+ location : String = " regions/us-central1 " ,
27+ jobState : State = JobStatus . State . SUCCEEDED
28+ ) : BatchServiceClient = {
3929 val instancePolicy = InstancePolicy
4030 .newBuilder()
41- .setMachineType(" n1-standard-1 " )
31+ .setMachineType(machineType )
4232 .setProvisioningModel(ProvisioningModel .PREEMPTIBLE )
4333 .build()
4434
4535 val allocationPolicy = AllocationPolicy
4636 .newBuilder()
47- .setLocation(LocationPolicy .newBuilder().addAllowedLocations(" regions/us-central1 " ))
37+ .setLocation(LocationPolicy .newBuilder().addAllowedLocations(location ))
4838 .addInstances(InstancePolicyOrTemplate .newBuilder().setPolicy(instancePolicy))
4939 .build()
5040
51- val jobStatus = JobStatus .newBuilder().setState(JobStatus .State .RUNNING ).build()
41+ val startStatusEvent = StatusEvent
42+ .newBuilder()
43+ .setType(" STATUS_CHANGED" )
44+ .setEventTime(Timestamp .newBuilder().setSeconds(1 ).build())
45+ .setDescription(" Job state is set from SCHEDULED to RUNNING for job..." )
46+ .build()
47+
48+ val endStatusEvent = StatusEvent
49+ .newBuilder()
50+ .setType(" STATUS_CHANGED" )
51+ .setEventTime(Timestamp .newBuilder().setSeconds(2 ).build())
52+ .setDescription(" Job state is set from RUNNING to SOME_OTHER_STATUS for job..." )
53+ .build()
54+
55+ val jobStatus = JobStatus
56+ .newBuilder()
57+ .setState(jobState)
58+ .addStatusEvents(startStatusEvent)
59+ .addStatusEvents(endStatusEvent)
60+ .build()
5261
5362 val job = Job .newBuilder().setAllocationPolicy(allocationPolicy).setStatus(jobStatus).build()
5463
5564 val mockClient = mock[BatchServiceClient ]
5665 doReturn(job).when(mockClient).getJob(any[GetJobRequest ])
5766 doReturn(job).when(mockClient).getJob(any[String ])
5867
68+ mockClient
69+ }
70+
71+ behavior of " BatchRequestExecutor"
72+
73+ it should " create instantiatedVmInfo correctly" in {
74+
75+ val mockClient = setupBatchClient(jobState = JobStatus .State .RUNNING )
5976 // Create the BatchRequestExecutor
6077 val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
6178
@@ -72,4 +89,90 @@ class BatchRequestExecutorSpec
7289 case _ => fail(" Expected RunStatus.Running with instantiatedVmInfo" )
7390 }
7491 }
92+
93+ it should " create instantiatedVmInfo correctly with different location info" in {
94+
95+ val mockClient = setupBatchClient(location = " zones/us-central1-a" , jobState = JobStatus .State .RUNNING )
96+
97+ // Create the BatchRequestExecutor
98+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
99+
100+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
101+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
102+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
103+
104+ // Verify the instantiatedVmInfo
105+ result.status match {
106+ case RunStatus .Running (_, Some (instantiatedVmInfo)) =>
107+ instantiatedVmInfo.region shouldBe " us-central1-a"
108+ instantiatedVmInfo.machineType shouldBe " n1-standard-1"
109+ instantiatedVmInfo.preemptible shouldBe true
110+ case _ => fail(" Expected RunStatus.Running with instantiatedVmInfo" )
111+ }
112+ }
113+
114+ it should " create instantiatedVmInfo correctly with missing location info" in {
115+
116+ val mockClient = setupBatchClient(jobState = JobStatus .State .RUNNING )
117+
118+ // Create the BatchRequestExecutor
119+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
120+
121+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
122+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
123+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
124+
125+ // Verify the instantiatedVmInfo
126+ result.status match {
127+ case RunStatus .Running (_, Some (instantiatedVmInfo)) =>
128+ instantiatedVmInfo.region shouldBe " us-central1"
129+ instantiatedVmInfo.machineType shouldBe " n1-standard-1"
130+ instantiatedVmInfo.preemptible shouldBe true
131+ case _ => fail(" Expected RunStatus.Running with instantiatedVmInfo" )
132+ }
133+ }
134+
135+ it should " send vmStartTime and vmEndTime metadata info when a workflow succeeds" in {
136+
137+ val mockClient = setupBatchClient()
138+
139+ // Create the BatchRequestExecutor
140+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
141+
142+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
143+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
144+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
145+
146+ // Verify the events
147+ result.status match {
148+ case RunStatus .Success (events, _) =>
149+ val eventNames = events.map(_.name)
150+ val eventTimes = events.map(_.offsetDateTime.toString)
151+ eventNames should contain theSameElementsAs List (" vmStartTime" , " vmEndTime" )
152+ eventTimes should contain theSameElementsAs List (" 1970-01-01T00:00:01Z" , " 1970-01-01T00:00:02Z" )
153+ case _ => fail(" Expected RunStatus.Success with events" )
154+ }
155+ }
156+
157+ it should " send vmStartTime and vmEndTime metadata info when a workflow fails" in {
158+ val mockClient = setupBatchClient(jobState = JobStatus .State .FAILED )
159+
160+ // Create the BatchRequestExecutor
161+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
162+
163+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
164+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
165+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
166+
167+ // Verify the events
168+ result.status match {
169+ case RunStatus .Failed (_, events, _) =>
170+ val eventNames = events.map(_.name)
171+ val eventTimes = events.map(_.offsetDateTime.toString)
172+ eventNames should contain theSameElementsAs List (" vmStartTime" , " vmEndTime" )
173+ eventTimes should contain theSameElementsAs List (" 1970-01-01T00:00:01Z" , " 1970-01-01T00:00:02Z" )
174+ case _ => fail(" Expected RunStatus.Success with events" )
175+ }
176+ }
177+
75178}
0 commit comments