@@ -231,6 +231,97 @@ defmodule Zebra.Workers.DispatcherTest do
231231      # a NOT_FOUND response from chmura, and stopped trying to occupy agents. 
232232      assert  Zebra.Workers.DispatcherTest.Counter . value ( )  ==  10 
233233    end 
234+ 
235+     test  "isolates dispatching by both machine_type and os_image"  do 
236+       System . put_env ( "DISPATCH_SELF_HOSTED_ONLY" ,  "false" ) 
237+       System . put_env ( "DISPATCH_CLOUD_ONLY" ,  "false" ) 
238+ 
239+       # we need to have at least 20 for each os_image to ensure that we 
240+       # don't stop batching when we receive a NOT_FOUND response from chmura 
241+       # and stop trying to occupy agents. 
242+ 
243+       ubuntu2404_jobs  = 
244+         Enum . map ( 1 .. 20 ,  fn  _  -> 
245+           { :ok ,  job }  = 
246+             Support.Factories.Job . create ( :scheduled ,  % { 
247+               machine_type:  "e1-standard-2" , 
248+               machine_os_image:  "ubuntu2404" 
249+             } ) 
250+ 
251+           job 
252+         end ) 
253+ 
254+       # Create jobs with same machine_type but different os_images 
255+       ubuntu1804_jobs  = 
256+         Enum . map ( 1 .. 20 ,  fn  _  -> 
257+           { :ok ,  job }  = 
258+             Support.Factories.Job . create ( :scheduled ,  % { 
259+               machine_type:  "e1-standard-2" , 
260+               machine_os_image:  "ubuntu1804" 
261+             } ) 
262+ 
263+           job 
264+         end ) 
265+ 
266+       ubuntu2004_jobs  = 
267+         Enum . map ( 1 .. 20 ,  fn  _  -> 
268+           { :ok ,  job }  = 
269+             Support.Factories.Job . create ( :scheduled ,  % { 
270+               machine_type:  "e1-standard-2" , 
271+               machine_os_image:  "ubuntu2004" 
272+             } ) 
273+ 
274+           job 
275+         end ) 
276+ 
277+       # Track which os_images were requested 
278+       agent_requests  =  Agent . start_link ( fn  ->  [ ]  end ) 
279+ 
280+       GrpcMock . stub ( Support.FakeServers.ChmuraApi ,  :occupy_agent ,  fn  req ,  _  -> 
281+         Agent . update ( elem ( agent_requests ,  1 ) ,  fn  list  -> 
282+           [ req . machine . os_image  |  list ] 
283+         end ) 
284+ 
285+         if  req . machine . os_image  ==  "ubuntu2404"  do 
286+           raise  GRPC.RPCError ,  status:  GRPC.Status . not_found ( ) ,  message:  "No suitable agent found" 
287+         else 
288+           % InternalApi.Chmura.OccupyAgentResponse { 
289+             agent:  % InternalApi.Chmura.Agent { 
290+               id:  Ecto.UUID . generate ( ) , 
291+               ip_address:  "1.2.3.4" , 
292+               ssh_port:  80 , 
293+               ctrl_port:  80 , 
294+               auth_token:  "asdas" 
295+             } 
296+           } 
297+         end 
298+       end ) 
299+ 
300+       with_stubbed_http_calls ( fn  -> 
301+         Worker . init ( )  |>  Zebra.Workers.DbWorker . tick ( ) 
302+       end ) 
303+ 
304+       # ubuntu1804 and ubuntu2004 jobs should be started 
305+       ( ubuntu1804_jobs  ++  ubuntu2004_jobs ) 
306+       |>  Enum . each ( fn  job  -> 
307+         job  =  Job . reload ( job ) 
308+         assert  Job . started? ( job )  ==  true 
309+       end ) 
310+ 
311+       # ubuntu2404 jobs should remain scheduled (no agents available) 
312+       ubuntu2404_jobs 
313+       |>  Enum . each ( fn  job  -> 
314+         job  =  Job . reload ( job ) 
315+         assert  Job . scheduled? ( job )  ==  true 
316+       end ) 
317+ 
318+       # Verify that requests were made with the correct os_images 
319+       requested_os_images  =  Agent . get ( elem ( agent_requests ,  1 ) ,  &  & 1 ) 
320+       assert  length ( requested_os_images )  ==  50 
321+       assert  Enum . count ( requested_os_images ,  & ( & 1  ==  "ubuntu1804" ) )  ==  20 
322+       assert  Enum . count ( requested_os_images ,  & ( & 1  ==  "ubuntu2004" ) )  ==  20 
323+       assert  Enum . count ( requested_os_images ,  & ( & 1  ==  "ubuntu2404" ) )  ==  10  # only one batch requested 
324+     end 
234325  end 
235326
236327  describe  ".process"  do 
0 commit comments