Skip to content

Commit 0bfa2b8

Browse files
author
Matthew Peck
committed
force install tests
1 parent 9396611 commit 0bfa2b8

File tree

3 files changed

+112
-124
lines changed

3 files changed

+112
-124
lines changed

config/config.exs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ else
1212
config :cog, :access_rules, :enforcing
1313
end
1414

15-
# ========================================================================
16-
# Set this to true to disable bundle version checking. Cog will overwrite
17-
# bundles with the same version instead of requiring a new version number.
18-
# NOTE: This should only ever be set for development. Setting this in a
19-
# production environment could cause unexpected behavior.
20-
# ========================================================================
21-
22-
if System.get_env("DISABLE_BUNDLE_VERSION_ENFORCEMENT") do
23-
config :cog, :enforce_bundle_version, false
24-
else
25-
config :cog, :enforce_bundle_version, true
26-
end
27-
2815
# ========================================================================
2916
# Embedded Command Bundle Version (for built-in commands)
3017
# NOTE: Do not change this value unless you know what you're doing.

test/cog/repository/bundles_test.exs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
407519
end

test/controllers/v1/bundles_controller_test.exs

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -134,117 +134,6 @@ defmodule Cog.V1.BundlesControllerTest do
134134
"version has already been taken"] = json_response(conn, 409)["errors"]
135135
end
136136

137-
test "Overwrites an installed version when bundle version enforcement is disabled", %{authed: requestor} do
138-
Application.put_env(:cog, :enforce_bundle_version, false)
139-
140-
config = config(:map)
141-
conn = api_request(requestor, :post, "/v1/bundles", body: %{"bundle" => %{"config" => config}})
142-
assert response(conn, 201)
143-
144-
# Now try to do the same thing again
145-
conn = api_request(requestor, :post, "/v1/bundles", body: %{"bundle" => %{"config" => config}})
146-
147-
bundle_version_id = Poison.decode!(conn.resp_body)
148-
|> get_in(["bundle_version", "id"])
149-
150-
bundle_version = Cog.Repository.Bundles.version(bundle_version_id)
151-
152-
assert conn.status == 201
153-
assert bundle_version.config_file == config
154-
assert bundle_version.bundle.name == config["name"]
155-
156-
Application.put_env(:cog, :enforce_bundle_version, true)
157-
end
158-
159-
test "Overwrite an installed version with a bundle that resembles the old only in name and version number", %{authed: requestor} do
160-
Application.put_env(:cog, :enforce_bundle_version, false)
161-
162-
# Make the original request
163-
config = config(:map)
164-
conn = api_request(requestor, :post, "/v1/bundles", body: %{"bundle" => %{"config" => config}})
165-
assert response(conn, 201)
166-
167-
# Change everything about the config except the bundle name and version number
168-
new_config = Spanner.Config.Parser.read_from_string!(overwrite_bundle_test_config)
169-
170-
# Make the request with the new config
171-
conn = api_request(requestor, :post, "/v1/bundles", body: %{"bundle" => %{"config" => new_config}})
172-
assert conn.status == 201
173-
174-
# Grab the new bundle version from the db
175-
bundle_version = Poison.decode!(conn.resp_body)
176-
|> get_in(["bundle_version", "id"])
177-
|> Cog.Repository.Bundles.version
178-
|> Repo.preload(:templates)
179-
180-
# Make our assertions
181-
assert bundle_version.config_file == new_config
182-
assert bundle_version.bundle.name == config["name"]
183-
184-
expected_command_names = Map.keys(new_config["commands"]) |> Enum.sort
185-
actual_command_names = Enum.map(bundle_version.commands, &(&1.command.name)) |> Enum.sort
186-
assert actual_command_names == expected_command_names
187-
188-
expected_permissions = new_config["permissions"] |> Enum.sort
189-
actual_permissions = Enum.map(bundle_version.permissions, &("test_bundle:#{&1.name}")) |> Enum.sort
190-
assert actual_permissions == expected_permissions
191-
192-
expected_template_names = Enum.map(new_config["templates"], fn({name, _}) -> name end) |> Enum.sort
193-
actual_template_names = Enum.map(bundle_version.templates, &(&1.name)) |> Enum.sort
194-
assert actual_template_names == expected_template_names
195-
196-
expected_templates = Enum.map(new_config["templates"], fn({_, source}) -> source["body"] end) |> Enum.sort
197-
actual_templates = Enum.map(bundle_version.templates, &(&1.source)) |> Enum.sort
198-
assert actual_templates == expected_templates
199-
200-
Application.put_env(:cog, :enforce_bundle_version, true)
201-
end
202-
203-
defp overwrite_bundle_test_config do
204-
"""
205-
---
206-
# Format version
207-
cog_bundle_version: 4
208-
209-
name: test_bundle
210-
description: A test bundle
211-
version: "0.1.0"
212-
permissions:
213-
- test_bundle:new_date
214-
- test_bundle:new_time
215-
- test_bundle:another_command
216-
docker:
217-
image: operable-bundle/test_bundle_update
218-
tag: v0.1.1
219-
commands:
220-
new_date:
221-
executable: /usr/local/bin/new_date
222-
description: A new date command
223-
arguments: "[format]"
224-
options:
225-
new_option1:
226-
type: string
227-
description: A different option
228-
required: true
229-
short_flag: a
230-
rules:
231-
- when command is test_bundle:new_date must have test_bundle:new_date
232-
new_time:
233-
executable: /usr/local/bin/new_time
234-
rules:
235-
- when command is test_bundle:new_time must have test_bundle:new_time
236-
another_command:
237-
executable: /usr/local/bin/another_command
238-
rules:
239-
- when command is test_bundle:another_command must have test_bundle:another_command
240-
templates:
241-
time:
242-
body: ~$results[0].time~
243-
date:
244-
body: ~$results[0].date~
245-
"""
246-
end
247-
248137
test "fails to install with semantically invalid config", %{authed: requestor} do
249138
# The config includes rules that mention permissions; if we remove
250139
# those permissions, installation should fail

0 commit comments

Comments
 (0)