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