2929from  simcore_service_clusters_keeper .utils .clusters  import  (
3030    _prepare_environment_variables ,
3131    create_cluster_from_ec2_instance ,
32+     create_deploy_cluster_stack_script ,
3233    create_startup_script ,
3334)
3435from  types_aiobotocore_ec2 .literals  import  InstanceStateNameType 
@@ -51,16 +52,26 @@ def ec2_boot_specs(app_settings: ApplicationSettings) -> EC2InstanceBootSpecific
5152    return  ec2_boot_specs 
5253
5354
55+ @pytest .fixture (params = [TLSAuthentication , NoAuthentication ]) 
56+ def  backend_cluster_auth (
57+     request : pytest .FixtureRequest ,
58+ ) ->  InternalClusterAuthentication :
59+     return  request .param 
60+ 
61+ 
5462@pytest .fixture  
5563def  app_environment (
5664    app_environment : EnvVarsDict ,
5765    monkeypatch : pytest .MonkeyPatch ,
66+     backend_cluster_auth : InternalClusterAuthentication ,
5867) ->  EnvVarsDict :
5968    return  app_environment  |  setenvs_from_dict (
6069        monkeypatch ,
6170        {
6271            "CLUSTERS_KEEPER_COMPUTATIONAL_BACKEND_DEFAULT_CLUSTER_AUTH" : json_dumps (
6372                TLSAuthentication .Config .schema_extra ["examples" ][0 ]
73+                 if  isinstance (backend_cluster_auth , TLSAuthentication )
74+                 else  NoAuthentication .Config .schema_extra ["examples" ][0 ]
6475            )
6576        },
6677    )
@@ -72,36 +83,49 @@ def test_create_startup_script(
7283    mocked_ssm_server_envs : EnvVarsDict ,
7384    mocked_redis_server : None ,
7485    app_settings : ApplicationSettings ,
75-     cluster_machines_name_prefix : str ,
76-     clusters_keeper_docker_compose : dict [str , Any ],
7786    ec2_boot_specs : EC2InstanceBootSpecific ,
7887):
79-     additional_custom_tags  =  {
80-         AWSTagKey ("pytest-tag-key" ): AWSTagValue ("pytest-tag-value" )
81-     }
8288    startup_script  =  create_startup_script (
8389        app_settings ,
84-         cluster_machines_name_prefix = cluster_machines_name_prefix ,
8590        ec2_boot_specific = ec2_boot_specs ,
86-         additional_custom_tags = additional_custom_tags ,
8791    )
8892    assert  isinstance (startup_script , str )
8993    assert  len (ec2_boot_specs .custom_boot_scripts ) >  0 
9094    for  boot_script  in  ec2_boot_specs .custom_boot_scripts :
9195        assert  boot_script  in  startup_script 
96+ 
97+ 
98+ def  test_create_deploy_cluster_stack_script (
99+     disabled_rabbitmq : None ,
100+     mocked_ec2_server_envs : EnvVarsDict ,
101+     mocked_ssm_server_envs : EnvVarsDict ,
102+     mocked_redis_server : None ,
103+     app_settings : ApplicationSettings ,
104+     cluster_machines_name_prefix : str ,
105+     clusters_keeper_docker_compose : dict [str , Any ],
106+ ):
107+     additional_custom_tags  =  {
108+         AWSTagKey ("pytest-tag-key" ): AWSTagValue ("pytest-tag-value" )
109+     }
110+     deploy_script  =  create_deploy_cluster_stack_script (
111+         app_settings ,
112+         cluster_machines_name_prefix = cluster_machines_name_prefix ,
113+         additional_custom_tags = additional_custom_tags ,
114+     )
115+     assert  isinstance (deploy_script , str )
92116    # we have commands to pipe into a docker-compose file 
93-     assert  " | base64 -d > /docker-compose.yml"  in  startup_script 
117+     assert  " | base64 -d > /docker-compose.yml"  in  deploy_script 
94118    # we have commands to init a docker-swarm 
95-     assert  "docker swarm init"  in  startup_script 
119+     assert  "docker swarm init --default-addr-pool "  in  deploy_script 
96120    # we have commands to deploy a stack 
97121    assert  (
98122        "docker stack deploy --with-registry-auth --compose-file=/docker-compose.yml dask_stack" 
99-         in  startup_script 
123+         in  deploy_script 
100124    )
101125    # before that we have commands that setup ENV variables, let's check we have all of them as defined in the docker-compose 
102126    # let's get what was set in the startup script and compare with the expected one of the docker-compose 
103127    startup_script_envs_definition  =  (
104-         startup_script .splitlines ()[- 1 ].split ("docker stack deploy" )[0 ].strip ()
128+         deploy_script .splitlines ()[- 1 ].split ("docker stack deploy" )[0 ].strip ()
105129    )
106130    assert  startup_script_envs_definition 
107131    # Use regular expression to split the string into key-value pairs (courtesy of chatGPT) 
@@ -138,7 +162,7 @@ def test_create_startup_script(
138162        "WORKERS_EC2_INSTANCES_SECURITY_GROUP_IDS" ,
139163    ]
140164    assert  all (
141-         re .search (rf"{ i }  =\[(\\\".+\\\")*\]" , startup_script ) for  i  in  list_settings 
165+         re .search (rf"{ i }  =\[(\\\".+\\\")*\]" , deploy_script ) for  i  in  list_settings 
142166    )
143167
144168    # check dicts have \' in front 
@@ -147,36 +171,55 @@ def test_create_startup_script(
147171        "WORKERS_EC2_INSTANCES_CUSTOM_TAGS" ,
148172    ]
149173    assert  all (
150-         re .search (rf"{ i }  =\'{{(\".+\":\s\".*\")+}}\'" , startup_script )
174+         re .search (rf"{ i }  =\'{{(\".+\":\s\".*\")+}}\'" , deploy_script )
151175        for  i  in  dict_settings 
152176    )
153177
154178    # check the additional tags are in 
155179    assert  all (
156-         f'"{ key }  ": "{ value }  "'  in  startup_script 
180+         f'"{ key }  ": "{ value }  "'  in  deploy_script 
157181        for  key , value  in  additional_custom_tags .items ()
158182    )
159183
160184
161- def  test_create_startup_script_script_size_below_16kb (
185+ def  test_create_deploy_cluster_stack_script_below_64kb (
162186    disabled_rabbitmq : None ,
163187    mocked_ec2_server_envs : EnvVarsDict ,
164188    mocked_ssm_server_envs : EnvVarsDict ,
165189    mocked_redis_server : None ,
166190    app_settings : ApplicationSettings ,
167191    cluster_machines_name_prefix : str ,
168192    clusters_keeper_docker_compose : dict [str , Any ],
169-     ec2_boot_specs : EC2InstanceBootSpecific ,
170193):
171194    additional_custom_tags  =  {
172195        AWSTagKey ("pytest-tag-key" ): AWSTagValue ("pytest-tag-value" )
173196    }
174-     startup_script  =  create_startup_script (
197+     deploy_script  =  create_deploy_cluster_stack_script (
175198        app_settings ,
176199        cluster_machines_name_prefix = cluster_machines_name_prefix ,
177-         ec2_boot_specific = ec2_boot_specs ,
178200        additional_custom_tags = additional_custom_tags ,
179201    )
202+     deploy_script_size_in_bytes  =  len (deploy_script .encode ("utf-8" ))
203+     assert  deploy_script_size_in_bytes  <  64000 , (
204+         f"script size is { deploy_script_size_in_bytes }   bytes that exceeds the SSM command of 64KB. " 
205+         "TIP: split commands or reduce size." 
206+     )
207+ 
208+ 
209+ def  test_create_startup_script_script_size_below_16kb (
210+     disabled_rabbitmq : None ,
211+     mocked_ec2_server_envs : EnvVarsDict ,
212+     mocked_ssm_server_envs : EnvVarsDict ,
213+     mocked_redis_server : None ,
214+     app_settings : ApplicationSettings ,
215+     cluster_machines_name_prefix : str ,
216+     clusters_keeper_docker_compose : dict [str , Any ],
217+     ec2_boot_specs : EC2InstanceBootSpecific ,
218+ ):
219+     startup_script  =  create_startup_script (
220+         app_settings ,
221+         ec2_boot_specific = ec2_boot_specs ,
222+     )
180223    script_size_in_bytes  =  len (startup_script .encode ("utf-8" ))
181224
182225    print (
@@ -186,14 +229,13 @@ def test_create_startup_script_script_size_below_16kb(
186229    assert  script_size_in_bytes  <  15  *  1024 
187230
188231
189- def  test_startup_script_defines_all_envs_for_docker_compose (
232+ def  test__prepare_environment_variables_defines_all_envs_for_docker_compose (
190233    disabled_rabbitmq : None ,
191234    mocked_ec2_server_envs : EnvVarsDict ,
192235    mocked_ssm_server_envs : EnvVarsDict ,
193236    mocked_redis_server : None ,
194237    app_settings : ApplicationSettings ,
195238    cluster_machines_name_prefix : str ,
196-     ec2_boot_specs : EC2InstanceBootSpecific ,
197239    clusters_keeper_docker_compose_file : Path ,
198240):
199241    additional_custom_tags  =  {
@@ -205,8 +247,8 @@ def test_startup_script_defines_all_envs_for_docker_compose(
205247        additional_custom_tags = additional_custom_tags ,
206248    )
207249    assert  environment_variables 
208-     process  =  subprocess .run (
209-         [  # noqa: S603,  S607 
250+     process  =  subprocess .run (   # noqa: S603 
251+         [  # noqa: S607 
210252            "docker" ,
211253            "compose" ,
212254            "--dry-run" ,
0 commit comments