Skip to content

Commit

Permalink
background: added tests for the enabled? methods
Browse files Browse the repository at this point in the history
I've also added a couple of fixes that were missing from SUSE#1679.

Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed Feb 12, 2018
1 parent 3803fac commit d6f9beb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
23 changes: 19 additions & 4 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,25 @@ security:
anonymous_browsing:
enabled: true

# Environment variables for ensuring the execution of the
# background tasks 'registry' and 'sync'.
# Configuration for the background tasks.
background:
registry:
# The registry integration: it processes the given registry events (e.g. a new
# tag was pushed). It's therefore highly *discouraged* to disable this task.
registry:
enabled: true
sync:

# Registry synchronization: it synchronizes all the contents from the registry
# into the database.
sync:
enabled: true

# There are four accepted values:
# - update-delete: it performs a full synchronization.
# - update: it only adds missing tags, but it does not remove any contents
# from the database.
# - on-start: when starting Portus it runs an `update-delete` and then it
# gets disabled (i.e. it will only run once).
# - initial: like `on-start`, but it only runs if the database is
# empty. This is the default value since it's deemed to be the most
# common use-case.
sync-strategy: initial
7 changes: 4 additions & 3 deletions lib/portus/background/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def work?

def enabled?
val = APP_CONFIG.enabled?("background.registry")
Rails.logger.warn("Registry integration has been disabled. This is highly discouraged!") unless val
val
end
msg = "Registry integration has been disabled. This is highly discouraged!"
Rails.logger.warn(msg) unless val
val
end

def execute!
RegistryEvent.where(status: RegistryEvent.statuses[:fresh]).find_each do |e|
Expand Down
13 changes: 10 additions & 3 deletions lib/portus/background/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ def work?
end

def enabled?
if APP_CONFIG("background.sync") == false
Rails.logger.warn("WARNING: Sync is disabled!")
if APP_CONFIG.enabled?("background.sync")
strategy = APP_CONFIG["background"]["sync"]["sync-strategy"]
if strategy == "initial" && Repository.any?
Rails.logger.info "`#{self}` was disabled because strategy is set to " \
"'initial' and the database is not empty"
false
else
true
end
else
APP_CONFIG("background.sync")
false
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/lib/portus/background/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
end
end

describe "#enabled?" do
it "returns true when enabled" do
APP_CONFIG["background"]["registry"] = { "enabled" => true }
expect(subject.enabled?).to be_truthy
end

it "returns false when not enabled" do
APP_CONFIG["background"]["registry"] = { "enabled" => false }
expect(subject.enabled?).to be_falsey
end
end

describe "#to_s" do
it "works" do
expect(subject.to_s).to eq "Registry events"
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/portus/background/security_scanning_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@
end
end

describe "#enabled?" do
it "returns true when enabled" do
expect(subject.enabled?).to be_truthy
end

it "returns false when not enabled" do
APP_CONFIG["security"] = {
"clair" => { "server" => "" },
"zypper" => { "server" => "" },
"dummy" => { "server" => "" }
}
expect(subject.enabled?).to be_falsey
end
end

describe "#to_s" do
it "works" do
expect(subject.to_s).to eq "Security scanning"
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/portus/background/sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ def update_registry!(catalog)
end
end

describe "#enabled?" do
it "returns true when enabled" do
APP_CONFIG["background"]["sync"] = { "enabled" => true }
expect(subject.enabled?).to be_truthy
end

it "returns false when not enabled" do
APP_CONFIG["background"]["sync"] = { "enabled" => false }
expect(subject.enabled?).to be_falsey
end

it "returns false on initial if there are repositories" do
APP_CONFIG["background"]["sync"] = {
"enabled" => true,
"sync-strategy" => "initial"
}

registry = create(:registry)
create(:user)
namespace = create(:namespace, registry: registry)
repo = create(:repository, name: "repo", namespace: namespace)
create(:tag, name: "tag", repository: repo)

expect(subject.enabled?).to be_falsey
end
end

describe "#to_s" do
it "works" do
expect(subject.to_s).to eq "Registry synchronization"
Expand Down

0 comments on commit d6f9beb

Please sign in to comment.