Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace cachestore_redis;
18
 
1441 ariadna 19
use core_cache\definition;
20
use core_cache\helper;
21
use core_cache\store;
1 efrain 22
use cachestore_redis;
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
require_once(__DIR__ . '/../../../tests/fixtures/stores.php');
27
require_once(__DIR__ . '/../lib.php');
28
 
29
/**
30
 * Redis cluster test.
31
 *
32
 * If you wish to use these unit tests all you need to do is add the following definition to
33
 * your config.php file:
34
 *
35
 * define('TEST_CACHESTORE_REDIS_SERVERSCLUSTER', 'localhost:7000,localhost:7001');
36
 * define('TEST_CACHESTORE_REDIS_ENCRYPTCLUSTER', true);
37
 * define('TEST_CACHESTORE_REDIS_AUTHCLUSTER', 'foobared');
38
 * define('TEST_CACHESTORE_REDIS_CASCLUSTER', '/cafile/dir/ca.crt');
39
 *
40
 * @package   cachestore_redis
41
 * @author    Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
42
 * @copyright 2017 Catalyst IT Australia {@link http://www.catalyst-au.net}
43
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 *
45
 * @coversDefaultClass \cachestore_redis
46
 */
1441 ariadna 47
final class cachestore_cluster_redis_test extends \advanced_testcase {
1 efrain 48
    /**
49
     * Create a cache store for testing the Redis cluster.
50
     *
51
     * @param string|null $seed The redis cluster servers.
52
     * @return cachestore_redis The created cache store instance.
53
     */
54
    public function create_store(?string $seed = null): cachestore_redis {
55
        global $DB;
56
 
1441 ariadna 57
        $definition = definition::load_adhoc(
58
            mode: store::MODE_APPLICATION,
1 efrain 59
            component: 'cachestore_redis',
60
            area: 'phpunit_test',
61
        );
62
 
63
        $servers = $seed ?? str_replace(",", "\n", TEST_CACHESTORE_REDIS_SERVERSCLUSTER);
64
 
65
        $config = [
66
            'server'      => $servers,
67
            'prefix'      => $DB->get_prefix(),
68
            'clustermode' => true,
69
        ];
70
 
71
        if (defined('TEST_CACHESTORE_REDIS_ENCRYPTCLUSTER') && TEST_CACHESTORE_REDIS_ENCRYPTCLUSTER === true) {
72
            $config['encryption'] = true;
73
        }
74
        if (defined('TEST_CACHESTORE_REDIS_AUTHCLUSTER') && TEST_CACHESTORE_REDIS_AUTHCLUSTER) {
75
            $config['password'] = TEST_CACHESTORE_REDIS_AUTHCLUSTER;
76
        }
77
        if (defined('TEST_CACHESTORE_REDIS_CASCLUSTER') && TEST_CACHESTORE_REDIS_CASCLUSTER) {
78
            $config['cafile'] = TEST_CACHESTORE_REDIS_CASCLUSTER;
79
        }
80
 
81
        $store = new cachestore_redis('TestCluster', $config);
82
        $store->initialise($definition);
83
        $store->purge();
84
 
85
        return $store;
86
    }
87
 
88
    /**
89
     * Set up the test environment.
90
     */
91
    public function setUp(): void {
1441 ariadna 92
        parent::setUp();
1 efrain 93
        if (!cachestore_redis::are_requirements_met()) {
94
            $this->markTestSkipped('Could not test cachestore_redis with cluster, missing requirements.');
1441 ariadna 95
        } else if (!helper::is_cluster_available()) {
1 efrain 96
            $this->markTestSkipped('Could not test cachestore_redis with cluster, class RedisCluster is not available.');
97
        } else if (!defined('TEST_CACHESTORE_REDIS_SERVERSCLUSTER')) {
98
            $this->markTestSkipped('Could not test cachestore_redis with cluster, missing configuration. ' .
99
                                  "Example: define('TEST_CACHESTORE_REDIS_SERVERSCLUSTER', " .
100
                                  "'localhost:7000,localhost:7001,localhost:7002');");
101
        }
102
    }
103
 
104
    /**
105
     * Test if the cache store can be created successfully.
106
     *
107
     * @covers ::is_ready
108
     */
109
    public function test_it_can_create(): void {
110
        $store = $this->create_store();
111
        $this->assertNotNull($store);
112
        $this->assertTrue($store->is_ready());
113
    }
114
 
115
    /**
116
     * Test if the cache store trims server names correctly.
117
     *
118
     * @covers ::new_redis
119
     */
120
    public function test_it_trims_server_names(): void {
121
        // Add a time before and spaces after the first server. Also adds a blank line before second server.
122
        $servers = explode(',', TEST_CACHESTORE_REDIS_SERVERSCLUSTER);
123
        $servers[0] = "\t" . $servers[0] . "  \n";
124
        $servers = implode("\n", $servers);
125
 
126
        $store = $this->create_store($servers);
127
 
128
        $this->assertTrue($store->is_ready());
129
    }
130
 
131
    /**
132
     * Test if the cache store can successfully set and get a value.
133
     *
134
     * @covers ::set
135
     * @covers ::get
136
     */
137
    public function test_it_can_setget(): void {
138
        $store = $this->create_store();
139
        $store->set('the key', 'the value');
140
        $actual = $store->get('the key');
141
 
142
        $this->assertSame('the value', $actual);
143
    }
144
 
145
    /**
146
     * Test if the cache store can successfully set and get multiple values.
147
     *
148
     * @covers ::set_many
149
     * @covers ::get_many
150
     */
151
    public function test_it_can_setget_many(): void {
152
        $store = $this->create_store();
153
 
154
        // Create values.
155
        $values = [];
156
        $keys = [];
157
        $expected = [];
158
        for ($i = 0; $i < 10; $i++) {
159
            $key = "getkey_{$i}";
160
            $value = "getvalue #{$i}";
161
            $keys[] = $key;
162
            $values[] = [
163
                'key'   => $key,
164
                'value' => $value,
165
            ];
166
            $expected[$key] = $value;
167
        }
168
 
169
        $store->set_many($values);
170
        $actual = $store->get_many($keys);
171
        $this->assertSame($expected, $actual);
172
    }
173
 
174
    /**
175
     * Test if the cache store is marked as not ready if it fails to connect.
176
     *
177
     * @covers ::is_ready
178
     */
179
    public function test_it_is_marked_not_ready_if_failed_to_connect(): void {
180
        global $DB;
181
 
182
        $config = [
183
            'server'      => "abc:123",
184
            'prefix'      => $DB->get_prefix(),
185
            'clustermode' => true,
186
        ];
187
        $store = new cachestore_redis('TestCluster', $config);
188
        $debugging = $this->getDebuggingMessages();
189
        // Failed to connect should show a debugging message.
190
        $this->assertCount(1, \phpunit_util::get_debugging_messages() );
191
        $this->assertStringContainsString('Couldn\'t map cluster keyspace using any provided seed', $debugging[0]->message);
192
        $this->resetDebugging();
193
        $this->assertFalse($store->is_ready());
194
    }
195
}