Proyectos de Subversion Moodle

Rev

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