Bug 12800: running unit tests that use Koha::Cache breaks cache usage from Apache
authorTomas Cohen Arazi <tomascohen@gmail.com>
Thu, 21 Aug 2014 18:08:15 +0000 (15:08 -0300)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Sun, 24 Aug 2014 15:48:34 +0000 (12:48 -0300)
If the user runs:

 $ prove t/Cache.t

with it's system user, two situations can happen:

1) If MEMCACHED_NAMESPACE is set on koha-httpd.xml other than the default 'koha', then
 Apache sets /tmp/sharefile-koha-<namespace> and the problem is not present: running
 the test creates /tmp/sharefile-koha-koha != /tmp/sharefile-koha-<namespace>
=> SUCCESS: no problem

2) If MEMCACHED_NAMESPACE is not set (or eq 'koha'), then there is a permission problem
either running the unit tests, or when using any funcitonality on the UI that needs
Koha::Cache.
Explanation: the one that is run first will set the /tmp/sharefile-koha-koha ownership
so it will be either the dev's sys user, or www-data (or whatever apache is using).

This patch sets a namespace for the unit tests, so there is no collision.

To test:
- On your dev setup, having MEMCACHED_NAMESPACE unset on koha-httpd.conf
- Edit a marc framework. If it fails, remove /tmp/sharefile-koha-koha, and try again
  -> fixed. Now try running
  $ prove t/Cache.t
=> FAIL: test fails because of permission problem
- Apply the patch
- Re-run the test
=> SUCCESS: test passes
Try changing the order, etc.

The temporary file that is used is deleted after the tests are run.

Sponsored-by: Universidad Nacional de Cordoba
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
t/Cache.t

index 65cf52b..4c50a2b 100644 (file)
--- a/t/Cache.t
+++ b/t/Cache.t
@@ -1,9 +1,21 @@
 #!/usr/bin/perl
 
-# Tests Koha::Cache and whichever type of cache is enabled (through Koha::Cache)
-
-use strict;
-use warnings;
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
 
 use Test::More tests => 32;
 
@@ -16,6 +28,9 @@ BEGIN {
 }
 
 SKIP: {
+    # Set a special namespace for testing, to avoid breaking
+    # if test is run with a different user than Apache's.
+    $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
     my $cache = Koha::Cache->get_instance();
 
     skip "Cache not enabled", 28
@@ -151,9 +166,14 @@ SKIP: {
 
 END {
   SKIP: {
+        $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
         my $cache = Koha::Cache->get_instance();
         skip "Cache not enabled", 1
           unless ( $cache->is_cache_active() );
         is( $destructorcount, 1, 'Destructor run exactly once' );
+        # cleanup temporary file
+        my $tmp_file = $cache->{ fastmmap_cache }->{ share_file };
+        unlink $tmp_file if defined $tmp_file;
+
     }
 }