@@ -145,6 +145,72 @@ defmodule Cog.Repository.BundlesTest do
145145 assert { :error , { :db_errors , [ version: { "has already been taken" , [ ] } ] } } = Bundles . install ( % { "name" => "testing" , "version" => "1.0.0" , "config_file" => % { } } )
146146 end
147147
148+ describe "forced bundle installation" do
149+
150+ setup [ :forced_installation_configs ]
151+
152+ test "installing the same version overwrites the original version" , % { old_config: config } do
153+ { :ok , _version } = Bundles . install ( % { "name" => config [ "name" ] , "version" => config [ "version" ] , "config_file" => config } )
154+
155+ assert { :ok , _overwritten_version } = Bundles . install ( :force , % { "name" => config [ "name" ] , "version" => config [ "version" ] , "config_file" => config } )
156+ end
157+
158+ test "different configs persist properly" , % { old_config: old_config , new_config: new_config } do
159+
160+ { :ok , orig_version } = Bundles . install ( % { "name" => old_config [ "name" ] ,
161+ "version" => old_config [ "version" ] ,
162+ "config_file" => old_config } )
163+
164+ # Make sure the bundle installs
165+ assert { :ok , new_version } = Bundles . install ( :force , % { "name" => new_config [ "name" ] ,
166+ "version" => new_config [ "version" ] ,
167+ "config_file" => new_config } )
168+
169+ new_version = Repo . preload ( new_version , :templates )
170+
171+ # Make sure the bundle name and version didn't change
172+ assert orig_version . bundle . name == new_version . bundle . name
173+ assert orig_version . version == new_version . version
174+
175+ # Check the config file
176+ assert new_version . config_file == new_config
177+
178+ # Check commands
179+ expected_command_names = Map . keys ( new_config [ "commands" ] ) |> Enum . sort
180+ actual_command_names = Enum . map ( new_version . commands , & ( & 1 . command . name ) ) |> Enum . sort
181+ assert actual_command_names == expected_command_names
182+
183+ # Check permissions
184+ expected_permissions = new_config [ "permissions" ] |> Enum . sort
185+ actual_permissions = Enum . map ( new_version . permissions , & ( "test_bundle:#{ & 1 . name } " ) ) |> Enum . sort
186+ assert actual_permissions == expected_permissions
187+
188+ # Check templates
189+ expected_template_names = Map . keys ( new_config [ "templates" ] ) |> Enum . sort
190+ actual_template_names = Enum . map ( new_version . templates , & ( & 1 . name ) ) |> Enum . sort
191+ assert actual_template_names == expected_template_names
192+
193+ expected_templates = Enum . map ( new_config [ "templates" ] , fn ( { _ , source } ) -> source [ "body" ] end ) |> Enum . sort
194+ actual_templates = Enum . map ( new_version . templates , & ( & 1 . source ) ) |> Enum . sort
195+ assert actual_templates == expected_templates
196+ end
197+
198+ test "maintains enabled status" , % { old_config: old_config , new_config: new_config } do
199+ { :ok , orig_version } = Bundles . install ( % { "name" => old_config [ "name" ] ,
200+ "version" => old_config [ "version" ] ,
201+ "config_file" => old_config } )
202+
203+ :ok = Bundles . set_bundle_version_status ( orig_version , :enabled )
204+
205+ { :ok , new_version } = Bundles . install ( :force , % { "name" => new_config [ "name" ] ,
206+ "version" => new_config [ "version" ] ,
207+ "config_file" => new_config } )
208+
209+ assert Bundles . enabled? ( new_version )
210+ end
211+
212+ end
213+
148214 test "deleting the last version of a bundle deletes the bundle itself" do
149215 { :ok , version } = Bundles . install ( % { "name" => "testing" , "version" => "1.0.0" , "config_file" => % { } } )
150216
@@ -404,4 +470,50 @@ defmodule Cog.Repository.BundlesTest do
404470 v
405471 end
406472
473+ # Setup function for testing forced bundle installations
474+ defp forced_installation_configs ( context ) do
475+ old_config =
476+ % { "cog_bundle_version" => 4 ,
477+ "name" => "test_bundle" ,
478+ "description" => "A test bundle" ,
479+ "version" => "0.1.0" ,
480+ "permissions" => [ "test_bundle:date" , "test_bundle:time" ] ,
481+ "docker" => % { "image" => "operable-bundle/test_bundle" ,
482+ "tag" => "v0.1.0" } ,
483+ "commands" => % { "date" => % { "executable" => "/usr/local/bin/date" ,
484+ "options" => % { "option1" => % { "type" => "string" ,
485+ "description" => "An option" ,
486+ "required" => false ,
487+ "short_flag" => "o" } } ,
488+ "rules" => [ "when command is test_bundle:date must have test_bundle:date" ] } ,
489+ "time" => % { "executable" => "/usr/local/bin/time" ,
490+ "rules" => [ "when command is test_bundle:time must have test_bundle:time" ] } } ,
491+ "templates" => % { "time" => % { "body" => "~$results[0].time~" } ,
492+ "date" => % { "body" => "~$results[0].date~" } } }
493+
494+ new_config =
495+ % { "cog_bundle_version" => 4 ,
496+ "name" => "test_bundle" ,
497+ "description" => "An updated test bundle" ,
498+ "version" => "0.1.0" ,
499+ "permissions" => [ "test_bundle:new_date" , "test_bundle:new_time" , "test_bundle:another_command" ] ,
500+ "docker" => % { "image" => "operable-bundle/test_bundle_update" ,
501+ "tag" => "v0.1.1" } ,
502+ "commands" => % { "new_date" => % { "executable" => "/usr/local/bin/date" ,
503+ "options" => % { "option1" => % { "type" => "string" ,
504+ "description" => "An option" ,
505+ "required" => false ,
506+ "short_flag" => "o" } } ,
507+ "rules" => [ "when command is test_bundle:date must have test_bundle:date" ] } ,
508+ "new_time" => % { "executable" => "/usr/local/bin/time" ,
509+ "rules" => [ "when command is test_bundle:time must have test_bundle:time" ] } ,
510+ "another_command" => % { "executable" => "/usr/local/bin/time" ,
511+ "rules" => [ "when command is test_bundle:time must have test_bundle:time" ] } } ,
512+ "templates" => % { "new_time" => % { "body" => "~$results[0].new_time~" } ,
513+ "new_date" => % { "body" => "~$results[0].new_date~" } ,
514+ "another" => % { "body" => "~$results[0].another~" } } }
515+
516+ Map . merge ( context , % { old_config: old_config , new_config: new_config } )
517+ end
518+
407519end
0 commit comments