Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / controller / settings / mail_settings.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11 import pprint
12
13 import vstf.controller.settings.settings as sets
14 import vstf.common.decorator as deco
15 from vstf.common.input import raw_choice
16
17 LOG = logging.getLogger(__name__)
18
19
20 class MailSettings(sets.Settings):
21
22     def __init__(
23             self,
24             path="/etc/vstf",
25             filename="reporters.mail.mail-settings",
26             mode=sets.SETS_DEFAULT):
27         super(MailSettings, self).__init__(path, filename, mode)
28
29     def _register_func(self):
30         super(MailSettings, self)._register_func()
31         body = set(
32             self._fset['body'].keys()
33         )
34         LOG.debug(body)
35         for item in body:
36             item = item.encode()
37             func_name = "set_%s" % item
38             setattr(
39                 self,
40                 func_name,
41                 self._setting_file(
42                     func_name,
43                     self._mset['body'],
44                     self._fset['body'],
45                     item))
46         other = {"attach", "content", "subtype"}
47         for item in other:
48             func_name = "mset_%s" % item
49             setattr(
50                 self,
51                 func_name,
52                 self._setting_memory(
53                     func_name,
54                     self._mset['body'],
55                     item))
56
57         LOG.debug(self.__dict__)
58
59     def sinput(self):
60         if raw_choice("if set mail server"):
61             server = self.raw_server()
62             self.set_server(server)
63
64         if raw_choice("if set mail body"):
65             body = self.raw_body()
66             self.set_body(body)
67         print "%s set finish: " % (self._filename)
68         print "+++++++++++++++++++++++++++++++++++++++++"
69         pprint.pprint(self.settings, indent=4)
70         print "+++++++++++++++++++++++++++++++++++++++++"
71
72     @deco.vstf_input("password", types=str)
73     @deco.vstf_input("username", types=str)
74     @deco.vstf_input('host', types=str)
75     def raw_server(self):
76         print "---------------------------------------"
77         print "Please vstf set mail server info like:"
78         print "    'host': 'localhost',"
79         print "    'username': 'user',['\\n' = None]"
80         print "    'password': '******',['\\n' = None]"
81         print "---------------------------------------"
82
83     @deco.vstf_input("subject", types=str, default='vstf mail')
84     @deco.vstf_input("bcc", types=list, default=[])
85     @deco.vstf_input("cc", types=list, default=[])
86     @deco.vstf_input("to", types=list, default=[])
87     @deco.vstf_input('from', types=list, default=['vstf_from@vstf.com'])
88     def raw_body(self):
89         print "----------------------------------------------------"
90         print "Please vstf set mail server info like:"
91         print "    'from': ['vstf_from@vstf.com'],"
92         print "    'to': ['vstf_to@vstf.com'],"
93         print "    'cc': ['vstf_cc@vstf.com']"
94         print "    'bcc': ['vstf_bcc@vstf.com']"
95         print "    'subject': Vstf Performance Test Report"
96         print "----------------------------------------------------"
97
98
99 def unit_test():
100     from vstf.common.log import setup_logging
101     setup_logging(
102         level=logging.DEBUG,
103         log_file="/var/log/vstf/vstf-mail-settings.log",
104         clevel=logging.INFO)
105
106     mail_settings = MailSettings()
107     mail_settings.sinput()
108
109     return
110
111     mail_server = {
112         "host": "localhost",
113         "username": None,
114         "password": None
115     }
116     mail_settings.set_server(mail_server)
117
118     subject = "Virtual Switching Performance Test Report"
119     mail_settings.set_subject(subject)
120
121     subtype = "plain"
122     mail_settings.mset_subtype(subtype)
123
124     attach_list = []
125     mail_settings.mset_attach(attach_list)
126
127     content = "this is a test"
128     mail_settings.mset_content(content)
129
130     LOG.info(mail_settings.settings)
131
132
133 if __name__ == '__main__':
134     unit_test()