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 core;
18
 
19
use core\session\redis as redis_session;
20
use RedisClusterException;
21
 
22
/**
23
 * Unit tests for Redis cluster in the core/session/redis.php.
24
 *
25
 * NOTE: in order to execute this test you need to set up
26
 *       Redis cluster server and add configuration a constant
27
 *       to config.php or phpunit.xml configuration file:
28
 *
29
 * define('TEST_SESSION_REDIS_HOSTCLUSTER', '127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002');
30
 * define('TEST_SESSION_REDIS_AUTHCLUSTER', 'foobared');
31
 *
32
 * define('TEST_SESSION_REDIS_ENCRYPTCLUSTER', ['verify_peer' => false, 'verify_peer_name' => false]);
33
 * OR
34
 * define('TEST_SESSION_REDIS_ENCRYPTCLUSTER', ['cafile' => '/cafile/dir/ca.crt']);
35
 *
36
 * @package   core
37
 * @copyright 2024 Meirza <meirza.arson@moodle.com>
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 * @coversDefaultClass    \core\session\redis
40
 */
41
class session_redis_cluster_test extends \advanced_testcase {
42
 
43
    /**
44
     * Set up the test environment.
45
     */
46
    public function setUp(): void {
47
        global $CFG;
48
 
49
        if (!\cache_helper::is_cluster_available()) {
50
            $this->markTestSkipped('Could not test core_session with cluster, class RedisCluster is not available.');
51
        } else if (!defined('TEST_SESSION_REDIS_HOSTCLUSTER')) {
52
            $this->markTestSkipped('Could not test session_redis_cluster_test with cluster, missing configuration. ' .
53
                                  "Example: define('TEST_SESSION_REDIS_HOSTCLUSTER', " .
54
                                  "'localhost:7000,localhost:7001,localhost:7002');");
55
        }
56
        $this->resetAfterTest();
57
        $CFG->session_redis_host = TEST_SESSION_REDIS_HOSTCLUSTER;
58
        if (defined('TEST_SESSION_REDIS_ENCRYPTCLUSTER') && TEST_SESSION_REDIS_ENCRYPTCLUSTER) {
59
            $CFG->session_redis_encrypt = TEST_SESSION_REDIS_ENCRYPTCLUSTER;
60
        }
61
        if (defined('TEST_SESSION_REDIS_AUTHCLUSTER') && TEST_SESSION_REDIS_AUTHCLUSTER) {
62
            $CFG->session_redis_auth = TEST_SESSION_REDIS_AUTHCLUSTER;
63
        }
64
    }
65
 
66
    /**
67
     * Tests compression for session read and write operations.
68
     *
69
     * It covers the behavior of session read and write operations under different compression configurations.
70
     *
71
     * @runInSeparateProcess
72
     * @covers ::read
73
     * @covers ::write
74
     */
75
    public function test_read_and_write(): void {
76
        $rediscluster = new redis_session();
77
        $rediscluster->init();
78
        $this->assertTrue($rediscluster->write('sess1', 'DATA'));
79
        $this->assertSame('DATA', $rediscluster->read('sess1'));
80
        $this->assertTrue($rediscluster->close());
81
    }
82
 
83
    /**
84
     * Tests the behavior when connection attempts to Redis cluster are exceeded.
85
     *
86
     * It sets up the environment to simulate multiple failed connection attempts and
87
     * checks if the expected exception message is received.
88
     *
89
     * @runInSeparateProcess
90
     * @covers ::init
91
     */
92
    public function test_exception_when_connection_attempts_exceeded(): void {
93
        global $CFG;
94
 
95
        $CFG->session_redis_host = '127.0.0.1:1111111,127.0.0.1:1111112,127.0.0.1:1111113';
96
        $actual = '';
97
 
98
        $rediscluster = new redis_session();
99
        try {
100
            $rediscluster->init();
101
        } catch (RedisClusterException $e) {
102
            $actual = $e->getMessage();
103
        }
104
 
105
        $expected = "Failed to connect (try 5 out of 5) to Redis at";
106
        $this->assertDebuggingCalledCount(5);
107
        $this->assertStringContainsString($expected, $actual);
108
    }
109
}