~mysql/mysql-utilities/1.3.2

« back to all changes in this revision

Viewing changes to mysql-test/t/is_alias.py

  • Committer: Paulo Jesus
  • Date: 2013-04-19 18:31:54 UTC
  • mfrom: (289.1.47 mysql-utilities-1.2.2)
  • Revision ID: paulo.jesus@oracle.com-20130419183154-g8rkvs2oezbdvrud
Release-1.3.1 Merge

This patch merges version 1.2.2 to 1.3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 
3
#
 
4
# This program 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; version 2 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
16
#
 
17
 
 
18
import socket
 
19
import mutlib
 
20
from mysql.utilities.exception import MUTLibError
 
21
 
 
22
_IPv6_LOOPBACK = "::1"
 
23
 
 
24
_DEFAULT_MYSQL_OPTS = ('"--log-bin=mysql-bin  --report-host=%s '
 
25
                       '--report-port=%s --bind-address=:: "')
 
26
_PASS = "Pass\n"
 
27
_FAIL = "Failed\n\n"
 
28
_BAD_RESULT_MSG = ("Got wrong result for test case {0}. \n"
 
29
                   " Expected: {1}, got: {2}.")
 
30
_test_case_name = "test_case_name"
 
31
_aliases = "aliases"
 
32
_host_name = "host_name" 
 
33
_result = "result"
 
34
_desc = "description"
 
35
 
 
36
_alias_reuseness = 'alias_reuseness'
 
37
_mock_no_local_host = 'mock_no_local_host'
 
38
# this address is used just to obtained his IP
 
39
_oracle_com = 'oracle.com'
 
40
_oracle_ip = socket.getaddrinfo(_oracle_com, None)[0][4][0]
 
41
# this address must return the same host_name using his IP
 
42
_python_org = "python.org"
 
43
_python_ip = socket.getaddrinfo(_python_org, None)[0][4][0]
 
44
 
 
45
_special_test_cases = [{_desc: "This test reuse of aliases",
 
46
                        _test_case_name: _alias_reuseness,
 
47
                       _aliases: [_alias_reuseness], 
 
48
                       _host_name: _alias_reuseness,
 
49
                       _result: True},
 
50
 
 
51
                      {_desc: "this test addition of  lookup to aliases",
 
52
                       _test_case_name: _mock_no_local_host,
 
53
                       _aliases: [], 
 
54
                       _host_name: _mock_no_local_host,
 
55
                       _result: True},
 
56
 
 
57
                      {_desc: "This test Negative host added to aliases",
 
58
                       _test_case_name: _alias_reuseness,
 
59
                       _aliases: [], 
 
60
                       _host_name: _mock_no_local_host,
 
61
                       _result: False},
 
62
 
 
63
                      {_desc: ("This test non local server host name,"
 
64
                               " lookup of his aliases"),
 
65
                       _test_case_name: _oracle_com,
 
66
                       _aliases: [], 
 
67
                       _host_name: _python_org,
 
68
                       _result: False},
 
69
 
 
70
                      {_desc: ("This test non local server, lookup"
 
71
                               " of aliases for the given hostname"), 
 
72
                      _test_case_name: _oracle_com,
 
73
                       _aliases: [_mock_no_local_host], 
 
74
                       _host_name: _mock_no_local_host,
 
75
                       _result: False},
 
76
 
 
77
                      {_desc: ("This test non local server,"
 
78
                               "lookup of aliases for the given ip"), 
 
79
                       _test_case_name: _python_ip,
 
80
                       _aliases: [_mock_no_local_host], 
 
81
                       _host_name: _mock_no_local_host,
 
82
                       _result: False},
 
83
 
 
84
                      {_desc: ("This test lookup of aliases for non "
 
85
                               "local server by IP."),
 
86
                       _test_case_name: _oracle_ip,
 
87
                       _aliases: [], 
 
88
                       _host_name: _oracle_com,
 
89
                       _result: True},
 
90
 
 
91
                      {_desc: ("This test lookups of aliases for non "
 
92
                               "local server by hostname."),
 
93
                       _test_case_name: _python_org,
 
94
                       _aliases: [], 
 
95
                       _host_name: _python_ip,
 
96
                       _result: True},
 
97
 
 
98
                      {_desc: ("It test the reuse of aliases for the "
 
99
                               "given non local server by hostname."),
 
100
                       _test_case_name: _python_org,
 
101
                       _aliases: [_python_ip], 
 
102
                       _host_name: _python_ip,
 
103
                       _result: True}]
 
104
 
 
105
 
 
106
class test(mutlib.System_test):
 
107
    """test Server.is_alias() method.
 
108
    This test, test the result from Server.is_alias()
 
109
    Note: this test requires server version 5.5.30 and Internet connection.
 
110
    """
 
111
 
 
112
    def check_prerequisites(self):
 
113
        if not self.servers.get_server(0).check_version_compat(5, 5, 30):
 
114
            raise MUTLibError("Test requires server version 5.5.30")
 
115
        return self.check_num_servers(1)
 
116
 
 
117
    def setup(self):
 
118
        self.res_fname = "result.txt"
 
119
 
 
120
        # Spawn servers
 
121
        # Change clone Server_List host value
 
122
        self.old_cloning_host = self.servers.cloning_host
 
123
        self.servers.cloning_host = _IPv6_LOOPBACK
 
124
 
 
125
        self.server0 = self.servers.get_server(0)
 
126
        mysqld = _DEFAULT_MYSQL_OPTS % (_IPv6_LOOPBACK,
 
127
                                        self.servers.view_next_port())
 
128
        self.server1_name = "server_1"
 
129
        res = self.servers.spawn_new_server(self.server0, "1001",
 
130
                                            self.server1_name, mysqld)
 
131
        if not res:
 
132
            raise MUTLibError("Cannot spawn server '{0}'."
 
133
                              "".format(name))
 
134
        self.server1 = res[0]
 
135
 
 
136
        self.host_name = socket.gethostname()
 
137
 
 
138
        # Duplicated values added to test reuse of calculated values.
 
139
        # All elements of List of good test cases are expected to return True.
 
140
        self.good_test_cases = ["127.0.0.1", "[::1]", "localhost",
 
141
                                self.host_name, "::1", "0:0:0:0:0:0:0:1",
 
142
                                "0::0:1", "[0::1]", "0::0:0:1"]
 
143
        # All elements of List of bad test cases are expected to return False.
 
144
        self.bad_test_cases = ["0.0.0.2", "[::2]", "host_local", "::2", 
 
145
                               "0:0:0:0:0:0:0:2", "oracle.com", "python.org"]
 
146
 
 
147
        return True
 
148
 
 
149
    def run_is_alias_test(self, server, test_num, test_case,
 
150
                                    exp_res=True):
 
151
        NOT = ""
 
152
        if not exp_res:
 
153
            NOT = "not "
 
154
        comment = ("test case {0} - test alias: {1} is {2}alias for "
 
155
                   "server {3}".format(test_num, test_case, NOT, server.host))
 
156
        if self.debug:
 
157
                print(comment)
 
158
        self.results.append("{0}\n".format(comment))
 
159
        res = server.is_alias(test_case)
 
160
        if not res == exp_res:
 
161
            msg = _BAD_RESULT_MSG.format(test_num, repr(exp_res), repr(res))
 
162
            if self.debug:
 
163
                print("{0} {1}".format(_FAIL, msg))
 
164
            raise MUTLibError("{0}: {1} {2}".format(comment, _FAIL, msg))
 
165
        else:
 
166
            if self.debug:
 
167
                print(_PASS)
 
168
            self.results.append(_PASS)
 
169
        self.results.append("\n")
 
170
        server.aliases = []
 
171
 
 
172
    def run_is_alias_test_cases(self, server, test_num):
 
173
        for test_case in self.good_test_cases:
 
174
            test_num += 1
 
175
            self.run_is_alias_test(server, test_num, test_case)
 
176
        for test_case in self.bad_test_cases:
 
177
            test_num += 1
 
178
            self.run_is_alias_test(server, test_num, test_case, False)
 
179
        return test_num
 
180
 
 
181
    def run(self):
 
182
        test_num = 0
 
183
        if self.debug:
 
184
                print("\n")
 
185
        test_num = self.run_is_alias_test_cases(self.server0, test_num)
 
186
        test_num = self.run_is_alias_test_cases(self.server1, test_num)
 
187
 
 
188
        for dict in _special_test_cases:
 
189
            test_num += 1
 
190
            self.server1.aliases = dict[_aliases]
 
191
            self.server1.host = dict[_host_name]
 
192
            self.run_is_alias_test(self.server1, test_num, dict[_test_case_name],
 
193
                               dict[_result])
 
194
 
 
195
        # cleanup name_host
 
196
        self.server1.host = "localhost"
 
197
        # Mask Known results values.
 
198
        self.replace_substring(self.host_name, "<computer_name>")
 
199
 
 
200
        return True
 
201
 
 
202
    def get_result(self):
 
203
        return self.compare(__name__, self.results)
 
204
 
 
205
    def record(self):
 
206
        return self.save_result_file(__name__, self.results)
 
207
 
 
208
    def cleanup(self):
 
209
        # Restoring clone Server_List host value
 
210
        self.servers.cloning_host = self.old_cloning_host
 
211
        # Kill the servers that are only for this test.
 
212
        self.servers.stop_server(self.server1)
 
213
        return True
 
214