4
4
5
5
from carbon .util import parseDestinations
6
6
from carbon .util import enableTcpKeepAlive
7
+ from carbon .util import TaggedSeries
7
8
8
9
9
10
class UtilTest (TestCase ):
@@ -21,6 +22,72 @@ def setTcpKeepAlive(self, value):
21
22
enableTcpKeepAlive (_Transport (), True , None )
22
23
self .assertEquals (s .getsockopt (socket .SOL_TCP , socket .SO_KEEPALIVE ), 1 )
23
24
25
+ def test_sanitizing_name_as_tag_value (self ):
26
+ test_cases = [
27
+ {
28
+ 'original' : "my~.test.abc" ,
29
+ 'expected' : "my~.test.abc" ,
30
+ }, {
31
+ 'original' : "a.b.c" ,
32
+ 'expected' : "a.b.c" ,
33
+ }, {
34
+ 'original' : "~~a~~.~~~b~~~.~~~c~~~" ,
35
+ 'expected' : "a~~.~~~b~~~.~~~c~~~" ,
36
+ }, {
37
+ 'original' : "a.b.c~" ,
38
+ 'expected' : "a.b.c~" ,
39
+ }, {
40
+ 'original' : "~a.b.c" ,
41
+ 'expected' : "a.b.c" ,
42
+ }, {
43
+ 'original' : "~a~" ,
44
+ 'expected' : "a~" ,
45
+ }, {
46
+ 'original' : "~~~" ,
47
+ 'raises' : True ,
48
+ }, {
49
+ 'original' : "~" ,
50
+ 'raises' : True ,
51
+ },
52
+ ]
53
+
54
+ for test_case in test_cases :
55
+ if test_case .get ('raises' , False ):
56
+ self .assertRaises (
57
+ Exception ,
58
+ TaggedSeries .sanitize_name_as_tag_value ,
59
+ test_case ['original' ],
60
+ )
61
+ else :
62
+ result = TaggedSeries .sanitize_name_as_tag_value (test_case ['original' ])
63
+ self .assertEquals (result , test_case ['expected' ])
64
+
65
+ def test_validate_tag_key_and_value (self ):
66
+ # assert that it raises exception when sanitized name is still not valid
67
+ with self .assertRaises (Exception ):
68
+ # sanitized name is going to be '', which is not a valid tag value
69
+ TaggedSeries .sanitize_name_as_tag_value ('~~~~' )
70
+
71
+ with self .assertRaises (Exception ):
72
+ # given tag value is invalid because it has length 0
73
+ TaggedSeries .validateTagAndValue ('metric.name;tag=' )
74
+
75
+ with self .assertRaises (Exception ):
76
+ # given tag key is invalid because it has length 0
77
+ TaggedSeries .validateTagAndValue ('metric.name;=value' )
78
+
79
+ with self .assertRaises (Exception ):
80
+ # given tag is missing =
81
+ TaggedSeries .validateTagAndValue ('metric.name;tagvalue' )
82
+
83
+ with self .assertRaises (Exception ):
84
+ # given tag value is invalid because it starts with ~
85
+ TaggedSeries .validateTagAndValue ('metric.name;tag=~value' )
86
+
87
+ with self .assertRaises (Exception ):
88
+ # given tag key is invalid because it contains !
89
+ TaggedSeries .validateTagAndValue ('metric.name;ta!g=value' )
90
+
24
91
25
92
# Destinations have the form:
26
93
# <host> ::= <string without colons> | "[" <string> "]"
0 commit comments