@@ -8,14 +8,17 @@ import com.google.cloud.batch.v1.{
88 BatchServiceSettings ,
99 GetJobRequest ,
1010 Job ,
11- JobStatus
11+ JobStatus ,
12+ StatusEvent
1213}
1314import com .google .cloud .batch .v1 .AllocationPolicy .{
1415 InstancePolicy ,
1516 InstancePolicyOrTemplate ,
1617 LocationPolicy ,
1718 ProvisioningModel
1819}
20+ import com .google .cloud .batch .v1 .JobStatus .State
21+ import com .google .protobuf .Timestamp
1922import common .mock .MockSugar
2023import cromwell .backend .google .batch .api .BatchApiResponse
2124import cromwell .backend .google .batch .models .RunStatus
@@ -32,30 +35,57 @@ class BatchRequestExecutorSpec
3235 with MockSugar
3336 with PrivateMethodTester {
3437
35- behavior of " BatchRequestExecutor "
36-
37- it should " create instantiatedVmInfo correctly " in {
38-
38+ def setupBatchClient ( machineType : String = " n1-standard-1 " ,
39+ location : String = " regions/us-central1 " ,
40+ jobState : State = JobStatus . State . SUCCEEDED
41+ ) : BatchServiceClient = {
3942 val instancePolicy = InstancePolicy
4043 .newBuilder()
41- .setMachineType(" n1-standard-1 " )
44+ .setMachineType(machineType )
4245 .setProvisioningModel(ProvisioningModel .PREEMPTIBLE )
4346 .build()
4447
4548 val allocationPolicy = AllocationPolicy
4649 .newBuilder()
47- .setLocation(LocationPolicy .newBuilder().addAllowedLocations(" regions/us-central1 " ))
50+ .setLocation(LocationPolicy .newBuilder().addAllowedLocations(location ))
4851 .addInstances(InstancePolicyOrTemplate .newBuilder().setPolicy(instancePolicy))
4952 .build()
5053
51- val jobStatus = JobStatus .newBuilder().setState(JobStatus .State .RUNNING ).build()
54+ val startStatusEvent = StatusEvent
55+ .newBuilder()
56+ .setType(" STATUS_CHANGED" )
57+ .setEventTime(Timestamp .newBuilder().setSeconds(1 ).build())
58+ .setDescription(" Job state is set from SCHEDULED to RUNNING for job..." )
59+ .build()
60+
61+ val endStatusEvent = StatusEvent
62+ .newBuilder()
63+ .setType(" STATUS_CHANGED" )
64+ .setEventTime(Timestamp .newBuilder().setSeconds(2 ).build())
65+ .setDescription(" Job state is set from RUNNING to SOME_OTHER_STATUS for job..." )
66+ .build()
67+
68+ val jobStatus = JobStatus
69+ .newBuilder()
70+ .setState(jobState)
71+ .addStatusEvents(startStatusEvent)
72+ .addStatusEvents(endStatusEvent)
73+ .build()
5274
5375 val job = Job .newBuilder().setAllocationPolicy(allocationPolicy).setStatus(jobStatus).build()
5476
5577 val mockClient = mock[BatchServiceClient ]
5678 doReturn(job).when(mockClient).getJob(any[GetJobRequest ])
5779 doReturn(job).when(mockClient).getJob(any[String ])
5880
81+ mockClient
82+ }
83+
84+ behavior of " BatchRequestExecutor"
85+
86+ it should " create instantiatedVmInfo correctly" in {
87+
88+ val mockClient = setupBatchClient(jobState = JobStatus .State .RUNNING )
5989 // Create the BatchRequestExecutor
6090 val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
6191
@@ -72,4 +102,90 @@ class BatchRequestExecutorSpec
72102 case _ => fail(" Expected RunStatus.Running with instantiatedVmInfo" )
73103 }
74104 }
105+
106+ it should " create instantiatedVmInfo correctly with different location info" in {
107+
108+ val mockClient = setupBatchClient(location = " zones/us-central1-a" , jobState = JobStatus .State .RUNNING )
109+
110+ // Create the BatchRequestExecutor
111+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
112+
113+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
114+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
115+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
116+
117+ // Verify the instantiatedVmInfo
118+ result.status match {
119+ case RunStatus .Running (_, Some (instantiatedVmInfo)) =>
120+ instantiatedVmInfo.region shouldBe " us-central1-a"
121+ instantiatedVmInfo.machineType shouldBe " n1-standard-1"
122+ instantiatedVmInfo.preemptible shouldBe true
123+ case _ => fail(" Expected RunStatus.Running with instantiatedVmInfo" )
124+ }
125+ }
126+
127+ it should " create instantiatedVmInfo correctly with missing location info" in {
128+
129+ val mockClient = setupBatchClient(jobState = JobStatus .State .RUNNING )
130+
131+ // Create the BatchRequestExecutor
132+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
133+
134+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
135+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
136+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
137+
138+ // Verify the instantiatedVmInfo
139+ result.status match {
140+ case RunStatus .Running (_, Some (instantiatedVmInfo)) =>
141+ instantiatedVmInfo.region shouldBe " us-central1"
142+ instantiatedVmInfo.machineType shouldBe " n1-standard-1"
143+ instantiatedVmInfo.preemptible shouldBe true
144+ case _ => fail(" Expected RunStatus.Running with instantiatedVmInfo" )
145+ }
146+ }
147+
148+ it should " send vmStartTime and vmEndTime metadata info when a workflow succeeds" in {
149+
150+ val mockClient = setupBatchClient()
151+
152+ // Create the BatchRequestExecutor
153+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
154+
155+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
156+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
157+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
158+
159+ // Verify the events
160+ result.status match {
161+ case RunStatus .Success (events, _) =>
162+ val eventNames = events.map(_.name)
163+ val eventTimes = events.map(_.offsetDateTime.toString)
164+ eventNames should contain theSameElementsAs List (" vmStartTime" , " vmEndTime" )
165+ eventTimes should contain theSameElementsAs List (" 1970-01-01T00:00:01Z" , " 1970-01-01T00:00:02Z" )
166+ case _ => fail(" Expected RunStatus.Success with events" )
167+ }
168+ }
169+
170+ it should " send vmStartTime and vmEndTime metadata info when a workflow fails" in {
171+ val mockClient = setupBatchClient(jobState = JobStatus .State .FAILED )
172+
173+ // Create the BatchRequestExecutor
174+ val batchRequestExecutor = new BatchRequestExecutor .CloudImpl (BatchServiceSettings .newBuilder().build())
175+
176+ // testing a private method see https://www.scalatest.org/user_guide/using_PrivateMethodTester
177+ val internalGetHandler = PrivateMethod [BatchApiResponse .StatusQueried ](Symbol (" internalGetHandler" ))
178+ val result = batchRequestExecutor invokePrivate internalGetHandler(mockClient, GetJobRequest .newBuilder().build())
179+
180+ // Verify the events
181+ result.status match {
182+ case RunStatus .Failed (_, events, _) =>
183+ val eventNames = events.map(_.name)
184+ val eventTimes = events.map(_.offsetDateTime.toString)
185+ eventNames should contain theSameElementsAs List (" vmStartTime" , " vmEndTime" )
186+ eventTimes should contain theSameElementsAs List (" 1970-01-01T00:00:01Z" , " 1970-01-01T00:00:02Z" )
187+ case _ => fail(" Expected RunStatus.Success with events" )
188+ }
189+ }
190+
75191}
0 commit comments