From bef746ae7b215db78be088657bbfaf47c774f943 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 8 Aug 2016 10:25:54 +0200 Subject: [ci] setup couch for gitlab Couch docker image will be available on the host called couchdb. So we use curl to check for it and display the version string. And then we move a couchdb.yml config file into place so the right host will be used. --- .gitlab-ci.yml | 16 +++++++++------- .travis.yml | 8 ++++---- test/config/couchdb.admin.yml | 6 ------ test/config/couchdb.yml | 5 ----- test/config/gitlab/couchdb.yml | 4 ++++ test/config/travis/couchdb.admin.yml | 6 ++++++ test/config/travis/couchdb.yml | 5 +++++ test/config/travis/ruby-version | 1 + test/config/travis/setup_couch.sh | 17 +++++++++++++++++ test/travis/ruby-version | 1 - test/travis/setup_couch.sh | 17 ----------------- 11 files changed, 46 insertions(+), 40 deletions(-) delete mode 100644 test/config/couchdb.admin.yml delete mode 100644 test/config/couchdb.yml create mode 100644 test/config/gitlab/couchdb.yml create mode 100644 test/config/travis/couchdb.admin.yml create mode 100644 test/config/travis/couchdb.yml create mode 100644 test/config/travis/ruby-version create mode 100755 test/config/travis/setup_couch.sh delete mode 100644 test/travis/ruby-version delete mode 100755 test/travis/setup_couch.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index df920d0..01d8d52 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,7 +8,7 @@ image: "ruby:2.1" # Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-service services: - couchdb:1.6.1 - + # Cache gems in between builds cache: paths: @@ -17,13 +17,15 @@ cache: # This is a basic example for a gem or script which doesn't use # services such as redis or postgres before_script: - - ruby -v # Print out ruby version for debugging - - gem install bundler --no-ri --no-rdoc # Bundler is not installed with the image - - bundle install -j $(nproc) --path vendor # Install dependencies into ./vendor/ruby - - export RAILS_ENV=test + - ruby -v + - curl -s couchdb:5984 + - cp test/config/gitlab/couchdb.yml config/couchdb.admin.yml + - cp test/config/gitlab/couchdb.yml config + - gem install bundler --no-ri --no-rdoc + - bundle install -j $(nproc) --path vendor --without development debug + - bundle exec rake RAILS_ENV=test db:rotate + - bundle exec rake RAILS_ENV=test db:migrate rails: script: - - bundle exec rake db:rotate - - bundle exec rake db:migrate - bundle exec rake test diff --git a/.travis.yml b/.travis.yml index 4e7aad0..2681951 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,10 +6,10 @@ before_install: - "gem install bundler --version 1.11.2" before_script: - "rm .ruby-version" - - "mv test/travis/ruby-version .ruby-version" - - "test/travis/setup_couch.sh" - - "mv test/config/couchdb.admin.yml config/couchdb.admin.yml" - - "mv test/config/couchdb.yml config/couchdb.yml" + - "mv test/config/travis/ruby-version .ruby-version" + - "test/config/travis/setup_couch.sh" + - "mv test/config/travis/couchdb.admin.yml config/couchdb.admin.yml" + - "mv test/config/travis/couchdb.yml config/couchdb.yml" - "bundle exec rake RAILS_ENV=test db:rotate" - "bundle exec rake RAILS_ENV=test db:migrate" after_script: diff --git a/test/config/couchdb.admin.yml b/test/config/couchdb.admin.yml deleted file mode 100644 index 0988bc1..0000000 --- a/test/config/couchdb.admin.yml +++ /dev/null @@ -1,6 +0,0 @@ -test: - auto_update_design_doc: false - username: "anna" - password: "secret" - prefix: "" - diff --git a/test/config/couchdb.yml b/test/config/couchdb.yml deleted file mode 100644 index 9c8b67b..0000000 --- a/test/config/couchdb.yml +++ /dev/null @@ -1,5 +0,0 @@ -test: - auto_update_design_doc: false - username: "me" - password: "pwd" - prefix: "" diff --git a/test/config/gitlab/couchdb.yml b/test/config/gitlab/couchdb.yml new file mode 100644 index 0000000..5d4f71f --- /dev/null +++ b/test/config/gitlab/couchdb.yml @@ -0,0 +1,4 @@ +test: + auto_update_design_doc: false + host: "couchdb" + prefix: "" diff --git a/test/config/travis/couchdb.admin.yml b/test/config/travis/couchdb.admin.yml new file mode 100644 index 0000000..7c9584c --- /dev/null +++ b/test/config/travis/couchdb.admin.yml @@ -0,0 +1,6 @@ +test: + auto_update_design_doc: false + username: "anna" + password: "secret" + prefix: "" + diff --git a/test/config/travis/couchdb.yml b/test/config/travis/couchdb.yml new file mode 100644 index 0000000..9c8b67b --- /dev/null +++ b/test/config/travis/couchdb.yml @@ -0,0 +1,5 @@ +test: + auto_update_design_doc: false + username: "me" + password: "pwd" + prefix: "" diff --git a/test/config/travis/ruby-version b/test/config/travis/ruby-version new file mode 100644 index 0000000..68b3a4c --- /dev/null +++ b/test/config/travis/ruby-version @@ -0,0 +1 @@ +1.9.3-p551 diff --git a/test/config/travis/setup_couch.sh b/test/config/travis/setup_couch.sh new file mode 100755 index 0000000..0502c12 --- /dev/null +++ b/test/config/travis/setup_couch.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +HOST="http://localhost:5984" +echo "couch version :" +curl -X GET $HOST +echo "creating user :" +curl -HContent-Type:application/json -XPUT $HOST/_users/org.couchdb.user:me --data-binary '{"_id": "org.couchdb.user:me","name": "me","roles": [],"type": "user","password": "pwd"}' +echo "creating databases :" +curl -X PUT $HOST/sessions +curl -X PUT $HOST/users +curl -X PUT $HOST/tickets +echo "restricting database access :" +curl -X PUT $HOST/sessions/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}' +curl -X PUT $HOST/users/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}' +curl -X PUT $HOST/tickets/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}' +echo "adding admin :" +curl -X PUT $HOST/_config/admins/anna -d '"secret"' diff --git a/test/travis/ruby-version b/test/travis/ruby-version deleted file mode 100644 index 68b3a4c..0000000 --- a/test/travis/ruby-version +++ /dev/null @@ -1 +0,0 @@ -1.9.3-p551 diff --git a/test/travis/setup_couch.sh b/test/travis/setup_couch.sh deleted file mode 100755 index 0502c12..0000000 --- a/test/travis/setup_couch.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -HOST="http://localhost:5984" -echo "couch version :" -curl -X GET $HOST -echo "creating user :" -curl -HContent-Type:application/json -XPUT $HOST/_users/org.couchdb.user:me --data-binary '{"_id": "org.couchdb.user:me","name": "me","roles": [],"type": "user","password": "pwd"}' -echo "creating databases :" -curl -X PUT $HOST/sessions -curl -X PUT $HOST/users -curl -X PUT $HOST/tickets -echo "restricting database access :" -curl -X PUT $HOST/sessions/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}' -curl -X PUT $HOST/users/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}' -curl -X PUT $HOST/tickets/_security -Hcontent-type:application/json --data-binary '{"admins":{"names":[],"roles":[]},"members":{"names":["me"],"roles":[]}}' -echo "adding admin :" -curl -X PUT $HOST/_config/admins/anna -d '"secret"' -- cgit v1.2.3 From b4ff3b959d4dd6a7561ac3be063a43619c0bd89c Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 11:34:01 +0200 Subject: move temporary_user into lib - fix load issue We already did the same for other concerns. The way we load models for couchrest migrations does not work well with concerns in the model directory as they will be loaded twice. --- app/models/temporary_user.rb | 93 -------------------------------------------- app/models/user.rb | 1 + lib/temporary_user.rb | 93 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 93 deletions(-) delete mode 100644 app/models/temporary_user.rb create mode 100644 lib/temporary_user.rb diff --git a/app/models/temporary_user.rb b/app/models/temporary_user.rb deleted file mode 100644 index 2afae15..0000000 --- a/app/models/temporary_user.rb +++ /dev/null @@ -1,93 +0,0 @@ -# -# For users with login '*test_user*', we don't want to store these documents in -# the main users db. This is because we create and destroy a lot of test -# users. This weirdness of using a different db for some users breaks a lot of -# things, such as associations. However, this is OK for now since we don't need -# those for running the frequent nagios tests. -# -# This module is included in user.rb. This will only work if it is included -# after designs are defined, otherwise, the design definition will overwrite -# find_by_login(). -# - -module TemporaryUser - extend ActiveSupport::Concern - include CouchRest::Model::DatabaseMethod - - USER_DB = 'users' - TMP_USER_DB = 'tmp_users' - TMP_LOGIN = 'tmp_user' # created and deleted frequently - TEST_LOGIN = 'test_user' # created, rarely deleted - - included do - use_database_method :db_name - - # since the original find_by_login is dynamically created with - # instance_eval, it appears that we also need to use instance eval to - # override it. - instance_eval <<-EOS, __FILE__, __LINE__ + 1 - def find_by_login(*args) - if args.grep(/^#{TMP_LOGIN}/).any? - by_login.database(tmp_database).key(*args).first() - else - by_login.key(*args).first() - end - end - EOS - end - - module ClassMethods - def get(id, db = database) - super(id, db) || super(id, tmp_database) - end - alias :find :get - - # calls db_name(TMP_LOGIN), then creates a CouchRest::Database - # from the name - def tmp_database - choose_database(TMP_LOGIN) - end - - def db_name(login=nil) - if !login.nil? && login.include?(TMP_LOGIN) - TMP_USER_DB - else - USER_DB - end - end - - # create the tmp db if it doesn't exist. - # requires admin access. - def create_tmp_database! - design_doc.sync!(tmp_database.tap{|db|db.create!}) - end - - def is_tmp?(login) - !login.nil? && login =~ /^#{TMP_LOGIN}/ - end - - def is_test?(login) - !login.nil? && (login =~ /^#{TMP_LOGIN}/ || login =~ /^#{TEST_LOGIN}/) - end - end - - # - # this gets called each and every time a User object needs to - # access the database. - # - def db_name - self.class.db_name(self.login) - end - - # returns true if this User instance is stored in tmp db. - def is_tmp? - self.class.is_tmp?(self.login) - end - - # returns true if this user is used for testing purposes - # (either a temporary or long lived) - def is_test? - self.class.is_test?(self.login) - end - -end diff --git a/app/models/user.rb b/app/models/user.rb index 93830cc..1116e6c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ require 'login_format_validation' require 'local_email' +require 'temporary_user' class User < CouchRest::Model::Base include LoginFormatValidation diff --git a/lib/temporary_user.rb b/lib/temporary_user.rb new file mode 100644 index 0000000..2afae15 --- /dev/null +++ b/lib/temporary_user.rb @@ -0,0 +1,93 @@ +# +# For users with login '*test_user*', we don't want to store these documents in +# the main users db. This is because we create and destroy a lot of test +# users. This weirdness of using a different db for some users breaks a lot of +# things, such as associations. However, this is OK for now since we don't need +# those for running the frequent nagios tests. +# +# This module is included in user.rb. This will only work if it is included +# after designs are defined, otherwise, the design definition will overwrite +# find_by_login(). +# + +module TemporaryUser + extend ActiveSupport::Concern + include CouchRest::Model::DatabaseMethod + + USER_DB = 'users' + TMP_USER_DB = 'tmp_users' + TMP_LOGIN = 'tmp_user' # created and deleted frequently + TEST_LOGIN = 'test_user' # created, rarely deleted + + included do + use_database_method :db_name + + # since the original find_by_login is dynamically created with + # instance_eval, it appears that we also need to use instance eval to + # override it. + instance_eval <<-EOS, __FILE__, __LINE__ + 1 + def find_by_login(*args) + if args.grep(/^#{TMP_LOGIN}/).any? + by_login.database(tmp_database).key(*args).first() + else + by_login.key(*args).first() + end + end + EOS + end + + module ClassMethods + def get(id, db = database) + super(id, db) || super(id, tmp_database) + end + alias :find :get + + # calls db_name(TMP_LOGIN), then creates a CouchRest::Database + # from the name + def tmp_database + choose_database(TMP_LOGIN) + end + + def db_name(login=nil) + if !login.nil? && login.include?(TMP_LOGIN) + TMP_USER_DB + else + USER_DB + end + end + + # create the tmp db if it doesn't exist. + # requires admin access. + def create_tmp_database! + design_doc.sync!(tmp_database.tap{|db|db.create!}) + end + + def is_tmp?(login) + !login.nil? && login =~ /^#{TMP_LOGIN}/ + end + + def is_test?(login) + !login.nil? && (login =~ /^#{TMP_LOGIN}/ || login =~ /^#{TEST_LOGIN}/) + end + end + + # + # this gets called each and every time a User object needs to + # access the database. + # + def db_name + self.class.db_name(self.login) + end + + # returns true if this User instance is stored in tmp db. + def is_tmp? + self.class.is_tmp?(self.login) + end + + # returns true if this user is used for testing purposes + # (either a temporary or long lived) + def is_test? + self.class.is_test?(self.login) + end + +end -- cgit v1.2.3 From f0d2f44ea3babdafc793856082d8c532b05187ae Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 11:59:05 +0200 Subject: [db] remove DatabaseMethod import from TemporaryUser It was causing 404s on initializing the database with db:migrate. DatabaseMethods rewrite of #database does not call prepare_database and thus does not create the database when needed. DatabaseMethod is also quite a bit too much for a model that has two databases that can be memoized easily. There's no way to have a separate database instance per record. --- lib/temporary_user.rb | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/lib/temporary_user.rb b/lib/temporary_user.rb index 2afae15..e41add5 100644 --- a/lib/temporary_user.rb +++ b/lib/temporary_user.rb @@ -12,16 +12,12 @@ module TemporaryUser extend ActiveSupport::Concern - include CouchRest::Model::DatabaseMethod - USER_DB = 'users' TMP_USER_DB = 'tmp_users' TMP_LOGIN = 'tmp_user' # created and deleted frequently TEST_LOGIN = 'test_user' # created, rarely deleted included do - use_database_method :db_name - # since the original find_by_login is dynamically created with # instance_eval, it appears that we also need to use instance eval to # override it. @@ -45,15 +41,7 @@ module TemporaryUser # calls db_name(TMP_LOGIN), then creates a CouchRest::Database # from the name def tmp_database - choose_database(TMP_LOGIN) - end - - def db_name(login=nil) - if !login.nil? && login.include?(TMP_LOGIN) - TMP_USER_DB - else - USER_DB - end + @tmp_database ||= prepare_database TMP_USER_DB end # create the tmp db if it doesn't exist. @@ -71,12 +59,12 @@ module TemporaryUser end end - # - # this gets called each and every time a User object needs to - # access the database. - # - def db_name - self.class.db_name(self.login) + def database + if login.present? && login.include?(TMP_LOGIN) + self.class.tmp_database + else + self.class.database + end end # returns true if this User instance is stored in tmp db. -- cgit v1.2.3 From 8196f46b2c4d1d0dce7e4eafcc299879ec0d894b Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 12:22:32 +0200 Subject: [ci] checkout submodule on gitlab --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 01d8d52..01c4103 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -19,6 +19,7 @@ cache: before_script: - ruby -v - curl -s couchdb:5984 + - git submodule update --init - cp test/config/gitlab/couchdb.yml config/couchdb.admin.yml - cp test/config/gitlab/couchdb.yml config - gem install bundler --no-ri --no-rdoc -- cgit v1.2.3 From 8fbe70729da1d308a118c930e8f938837484a61c Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 17:26:51 +0200 Subject: [db] def database on users instead of use_database use_database affects all uses of prepare_database - so also the one in tmp_database. In order to avoid that we do not use_database but just overwrite the database method itself. --- app/models/user.rb | 2 -- lib/temporary_user.rb | 8 ++++++-- test/unit/temporary_user_test.rb | 28 ++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 1116e6c..9cebbca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,8 +5,6 @@ require 'temporary_user' class User < CouchRest::Model::Base include LoginFormatValidation - use_database :users - property :login, String, :accessible => true property :password_verifier, String, :accessible => true property :password_salt, String, :accessible => true diff --git a/lib/temporary_user.rb b/lib/temporary_user.rb index e41add5..d0db1c4 100644 --- a/lib/temporary_user.rb +++ b/lib/temporary_user.rb @@ -13,6 +13,7 @@ module TemporaryUser extend ActiveSupport::Concern + USER_DB = 'users' TMP_USER_DB = 'tmp_users' TMP_LOGIN = 'tmp_user' # created and deleted frequently TEST_LOGIN = 'test_user' # created, rarely deleted @@ -38,12 +39,15 @@ module TemporaryUser end alias :find :get - # calls db_name(TMP_LOGIN), then creates a CouchRest::Database - # from the name + def database + @database ||= prepare_database USER_DB + end + def tmp_database @tmp_database ||= prepare_database TMP_USER_DB end + # create the tmp db if it doesn't exist. # requires admin access. def create_tmp_database! diff --git a/test/unit/temporary_user_test.rb b/test/unit/temporary_user_test.rb index 38ccd67..2c9e70f 100644 --- a/test/unit/temporary_user_test.rb +++ b/test/unit/temporary_user_test.rb @@ -6,16 +6,37 @@ class TemporaryUserTest < ActiveSupport::TestCase InviteCodeValidator.any_instance.stubs(:validate) end - test "tmp_user saved to tmp_users" do - begin - assert User.ancestors.include?(TemporaryUser) + test "TemporaryUser concern is applied" do + assert User.ancestors.include?(TemporaryUser) + end + + test "temporary user has tmp_users as db" do + tmp_user = User.new :login => 'tmp_user_'+SecureRandom.hex(5).downcase + assert_equal 'leap_web_tmp_users', tmp_user.database.name + end + test "normal user has users as db" do + user = User.new :login => 'a'+SecureRandom.hex(5).downcase + assert_equal 'leap_web_users', user.database.name + end + + test "user saved to users" do + begin assert_difference('User.database.info["doc_count"]') do normal_user = User.create!(:login => 'a'+SecureRandom.hex(5).downcase, :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF') refute normal_user.database.to_s.include?('tmp') end + ensure + begin + normal_user.destroy + rescue + end + end + end + test "tmp_user saved to tmp_users" do + begin assert_difference('User.tmp_database.info["doc_count"]') do tmp_user = User.create!(:login => 'tmp_user_'+SecureRandom.hex(5).downcase, :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF') @@ -23,7 +44,6 @@ class TemporaryUserTest < ActiveSupport::TestCase end ensure begin - normal_user.destroy tmp_user.destroy rescue end -- cgit v1.2.3 From 75c037e5d6d8b8f2b98477bdd0359f5687bd7517 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 12 Aug 2016 23:10:50 +0200 Subject: [ci] use default db prefix on gitlab and travis This makes writing the tests for database names easier as they should be the same strings on gitlab, travis and locally. --- test/config/gitlab/couchdb.yml | 1 - test/config/travis/couchdb.admin.yml | 1 - test/config/travis/couchdb.yml | 1 - 3 files changed, 3 deletions(-) diff --git a/test/config/gitlab/couchdb.yml b/test/config/gitlab/couchdb.yml index 5d4f71f..68761dc 100644 --- a/test/config/gitlab/couchdb.yml +++ b/test/config/gitlab/couchdb.yml @@ -1,4 +1,3 @@ test: auto_update_design_doc: false host: "couchdb" - prefix: "" diff --git a/test/config/travis/couchdb.admin.yml b/test/config/travis/couchdb.admin.yml index 7c9584c..a7677f9 100644 --- a/test/config/travis/couchdb.admin.yml +++ b/test/config/travis/couchdb.admin.yml @@ -2,5 +2,4 @@ test: auto_update_design_doc: false username: "anna" password: "secret" - prefix: "" diff --git a/test/config/travis/couchdb.yml b/test/config/travis/couchdb.yml index 9c8b67b..a57b888 100644 --- a/test/config/travis/couchdb.yml +++ b/test/config/travis/couchdb.yml @@ -2,4 +2,3 @@ test: auto_update_design_doc: false username: "me" password: "pwd" - prefix: "" -- cgit v1.2.3