Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
spec/integration: added integration testing
Browse files Browse the repository at this point in the history
This commit is the foundation for integration testing in the Portus repository.
It uses docker containers to setup everything, and a lot of magic has been
written around RSpec so it integrates with the style of the other tests.

This commit does not handle on purpose the following (which have been submitted
as separate issues):

  - Test multiple versions of Docker.
  - Test crono
  - Test portusctl
  - Cover as much of the registry client as possible
  - Add integration testing into CI

Fixes #745

Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
  • Loading branch information
mssola committed Apr 29, 2016
1 parent bfdfcea commit f93f3f0
Show file tree
Hide file tree
Showing 12 changed files with 775 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/bin/portus
/docker-compose.yml
/docker-compose.overlay.yml
/spec/integration/fixtures/config.yml
/spec/integration/fixtures/data
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ rvm:
matrix:
allow_failures:
- rvm: ruby-head

before_install:
- |
bash -c " # Install MariaDB
sudo bash .travis_mariadb.sh
"
before_script:
- mysql -e 'create database portus_test;'

script:
- bundle exec rspec spec
- bundle exec rspec packaging/suse/portusctl/spec
- bundle exec rubocop -V
- bundle exec rubocop -F
- bundle exec brakeman -z -A -x CheckUnscopedFind,CheckRender

env:
global:
# OSC_CREDENTIALS are kept secret
Expand Down
6 changes: 3 additions & 3 deletions .travis_mariadb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ set -e
OS_CODENAME=$(lsb_release --codename --short)

service mysql stop
apt-get purge '^mysql*' 'libmysql*'
apt-get install python-software-properties
apt-get -y purge '^mysql*' 'libmysql*'
apt-get -y install python-software-properties
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db

add-apt-repository "deb http://ftp.osuosl.org/pub/mariadb/repo/10.0/ubuntu $OS_CODENAME main"
apt-get update -qq
apt-get -y update -qq

apt-get -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold -y install mariadb-server libmariadbd-dev
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ unless ENV["PACKAGING"] && ENV["PACKAGING"] == "yes"
gem "json-schema"
gem "timecop"
gem "codeclimate-test-reporter", group: :test, require: nil
gem "docker-api", "~> 1.28.0"
end
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ GEM
warden (~> 1.2.3)
diff-lcs (1.2.5)
docile (1.1.5)
docker-api (1.28.0)
excon (>= 0.38.0)
json
erubis (2.7.0)
excon (0.49.0)
execjs (2.2.2)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -398,6 +402,7 @@ DEPENDENCIES
crono
database_cleaner
devise
docker-api (~> 1.28.0)
factory_girl_rails
ffaker
font-awesome-rails
Expand Down
14 changes: 7 additions & 7 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
create_table "crono_jobs", force: :cascade do |t|
t.string "job_id", limit: 255, null: false
t.datetime "last_performed_at"
t.boolean "healthy", limit: 1
t.boolean "healthy"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand All @@ -66,9 +66,9 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "team_id", limit: 4
t.boolean "public", limit: 1, default: false
t.boolean "public", default: false
t.integer "registry_id", limit: 4, null: false
t.boolean "global", limit: 1, default: false
t.boolean "global", default: false
t.text "description", limit: 65535
end

Expand All @@ -82,7 +82,7 @@
t.string "hostname", limit: 255, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "use_ssl", limit: 1
t.boolean "use_ssl"
end

add_index "registries", ["hostname"], name: "index_registries_on_hostname", unique: true, using: :btree
Expand Down Expand Up @@ -137,7 +137,7 @@
t.string "name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "hidden", limit: 1, default: false
t.boolean "hidden", default: false
t.text "description", limit: 65535
end

Expand All @@ -157,8 +157,8 @@
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "admin", limit: 1, default: false
t.boolean "enabled", limit: 1, default: true
t.boolean "admin", default: false
t.boolean "enabled", default: true
t.string "ldap_name", limit: 255
t.integer "failed_attempts", limit: 4, default: 0
t.datetime "locked_at"
Expand Down
40 changes: 40 additions & 0 deletions lib/tasks/portus.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
require "pty"

# Spawn a new command and return its exit status. It will print to stdout on
# real time.
def spawn_cmd(cmd)
status = 0

PTY.spawn(cmd) do |stdout, _, pid|
# rubocop:disable Lint/HandleExceptions
begin
stdout.each { |line| print line }
rescue Errno::EIO
# End of output
end
# rubocop:enable Lint/HandleExceptions

Process.wait(pid)
status = $CHILD_STATUS.exitstatus
end
status
end

namespace :portus do
desc "Create the account used by Portus to talk with Registry's API"
task create_api_account: :environment do
Expand Down Expand Up @@ -89,4 +111,22 @@ HERE
end
puts
end

desc "Properly test Portus"
task :test do |_, args|
tags = args.extras.map { |a| "--tag #{a}" }
tags << "--tag ~integration" if ENV["TRAVIS"] == "true"

# Run normal tests + integration.
ENV["INTEGRATION_LDAP"] = nil
status = spawn_cmd("rspec spec #{tags.join(" ")}")
exit(status) if status != 0
exit(0) if ENV["TRAVIS"] == "true"

# Run LDAP integration tests.
ENV["INTEGRATION_LDAP"] = "t"
tags << "--tag integration" unless args.extras.include?("integration")
status = spawn_cmd("rspec spec #{tags.join(" ")}")
exit(status)
end
end
63 changes: 63 additions & 0 deletions spec/integration/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "integration/helper"
require "portus/registry_client"

def push_test_images
name = ldap? ? "johnldap" : "john"
email = "john@example.com"
password = "12341234"
create_user(name, email, password, true)
expect { login(name, password, email) }.not_to raise_error

# Pulling images that we should already have (so we go faster :P). Then
# re-tag it so they can be pushed. The re-tag has another name, to avoid
# clashes with other tests.
name = "registry"
retag = "registre"
pulled_tags = ["2.3.1", "2.4.0", "latest"]
pulled_tags.each do |tag|
base = "#{name}:#{tag}"
tgt = "#{retag}:#{tag}"
img = "library/#{base}"
pull(img)
system("docker tag #{img} #{registry_hostname}/#{tgt}")
end

expect(push("#{registry_hostname}/#{retag}")).to be_truthy

# Wait until the registry has everything we need.
eventually_expect(3) do
tags = rails_exec("Tag.where(repository: Repository.find_by(name: '#{retag}')).to_json")
tags.size
end
end

integration "Client" do
it "tells that a registry is reachable" do
client = Portus::RegistryClient.new(registry_hostname)
expect(client.reachable?).to be_truthy
end

it "fetches the catalog of pushed repositories" do
push_test_images

client = Portus::RegistryClient.new(registry_hostname)
cat = client.catalog
expected = { "registre" => ["2.3.1", "2.4.0", "latest"] }

cat.each do |r|
key = expected[r["name"]]
next if key.nil?
expect(key).to eq r["tags"]
end
end

it "fetches the manifest of the given repo/tag" do
push_test_images

client = Portus::RegistryClient.new(registry_hostname)
manifest = client.manifest("registre", "2.3.1")

expect(manifest["name"]).to eq "registre"
expect(manifest["tag"]).to eq "2.3"
end
end
23 changes: 23 additions & 0 deletions spec/integration/fixtures/config.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 0.1
storage:
filesystem:
rootdirectory: /registry_data
delete:
enabled: true
http:
addr: 0.0.0.0:5000
debug:
addr: 0.0.0.0:5001
auth:
token:
realm: http://<%= @ip %>:3000/v2/token
service: <%= @ip %>:5000
issuer: <%= @ip %>
rootcertbundle: /etc/docker/registry/portus.crt
notifications:
endpoints:
- name: portus
url: http://<%= @ip %>:3000/v2/webhooks/events
timeout: 500ms
threshold: 5
backoff: 1s
32 changes: 32 additions & 0 deletions spec/integration/fixtures/portus.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-----BEGIN CERTIFICATE-----
MIIFejCCA2ICAQEwDQYJKoZIh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