This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathtest_options.py
More file actions
174 lines (134 loc) · 6.35 KB
/
test_options.py
File metadata and controls
174 lines (134 loc) · 6.35 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright 2019, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
import mock
from opencensus.ext.azure import common
class TestOptions(unittest.TestCase):
def setUp(self):
os.environ.clear()
def test_process_options_ikey_code_cs(self):
options = common.Options()
options.connection_string = 'Authorization=ikey;InstrumentationKey=123'
options.instrumentation_key = '456'
os.environ['APPLICATIONINSIGHTS_CONNECTION_STRING'] = \
'Authorization=ikey;InstrumentationKey=789'
os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] = '101112'
common.process_options(options)
self.assertEqual(options.instrumentation_key, '123')
def test_process_options_ikey_code_ikey(self):
options = common.Options()
options.connection_string = None
options.instrumentation_key = '456'
os.environ['APPLICATIONINSIGHTS_CONNECTION_STRING'] = \
'Authorization=ikey;InstrumentationKey=789'
os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] = '101112'
common.process_options(options)
self.assertEqual(options.instrumentation_key, '456')
def test_process_options_ikey_env_cs(self):
options = common.Options()
options.connection_string = None
options.instrumentation_key = None
os.environ['APPLICATIONINSIGHTS_CONNECTION_STRING'] = \
'Authorization=ikey;InstrumentationKey=789'
os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] = '101112'
common.process_options(options)
self.assertEqual(options.instrumentation_key, '789')
def test_process_options_ikey_env_ikey(self):
options = common.Options()
options.connection_string = None
options.instrumentation_key = None
os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] = '101112'
common.process_options(options)
self.assertEqual(options.instrumentation_key, '101112')
def test_process_options_endpoint_code_cs(self):
options = common.Options()
options.connection_string = 'Authorization=ikey;IngestionEndpoint=123'
os.environ['APPLICATIONINSIGHTS_CONNECTION_STRING'] = \
'Authorization=ikey;IngestionEndpoint=456'
common.process_options(options)
self.assertEqual(options.endpoint, '123')
def test_process_options_endpoint_env_cs(self):
options = common.Options()
options.connection_string = None
os.environ['APPLICATIONINSIGHTS_CONNECTION_STRING'] = \
'Authorization=ikey;IngestionEndpoint=456'
common.process_options(options)
self.assertEqual(options.endpoint, '456')
def test_process_options_endpoint_default(self):
options = common.Options()
options.connection_string = None
common.process_options(options)
self.assertEqual(options.endpoint,
'https://dc.services.visualstudio.com')
def test_process_options_proxies_default(self):
options = common.Options()
options.proxies = "{}"
common.process_options(options)
self.assertEqual(options.proxies, "{}")
def test_process_options_proxies_set_proxies(self):
options = common.Options()
options.connection_string = None
options.proxies = '{"https": "https://test-proxy.com"}'
common.process_options(options)
self.assertEqual(
options.proxies,
'{"https": "https://test-proxy.com"}'
)
@mock.patch("opencensus.ext.azure.common.tempfile")
def test_process_options_enable_local_storage(self, mock_tempfile):
options = common.Options()
self.assertTrue(options.enable_local_storage)
self.assertIsNotNone(options.storage_path)
mock_tempfile.gettempdir.assert_called_once()
@mock.patch("opencensus.ext.azure.common.tempfile")
def test_process_options_disable_local_storage(self, mock_tempfile):
options = common.Options(enable_local_storage = False)
self.assertFalse(options.enable_local_storage, False)
self.assertIsNone(options.storage_path)
mock_tempfile.gettempdir.assert_not_called()
def test_parse_connection_string_none(self):
cs = None
result = common.parse_connection_string(cs)
self.assertEqual(result, {})
def test_parse_connection_string_invalid(self):
cs = 'asd'
self.assertRaises(ValueError,
lambda: common.parse_connection_string(cs))
def test_parse_connection_string_default_auth(self):
cs = 'InstrumentationKey=123'
result = common.parse_connection_string(cs)
self.assertEqual(result['instrumentationkey'], '123')
def test_parse_connection_string_invalid_auth(self):
cs = 'Authorization=asd'
self.assertRaises(ValueError,
lambda: common.parse_connection_string(cs))
def test_parse_connection_string_explicit_endpoint(self):
cs = 'Authorization=ikey;IngestionEndpoint=123;' \
'Location=us;EndpointSuffix=suffix'
result = common.parse_connection_string(cs)
self.assertEqual(result['ingestionendpoint'], '123')
def test_parse_connection_string_default(self):
cs = 'Authorization=ikey;Location=us'
result = common.parse_connection_string(cs)
self.assertEqual(result['ingestionendpoint'],
None)
def test_parse_connection_string_no_location(self):
cs = 'Authorization=ikey;EndpointSuffix=suffix'
result = common.parse_connection_string(cs)
self.assertEqual(result['ingestionendpoint'], 'https://dc.suffix')
def test_parse_connection_string_location(self):
cs = 'Authorization=ikey;EndpointSuffix=suffix;Location=us'
result = common.parse_connection_string(cs)
self.assertEqual(result['ingestionendpoint'], 'https://us.dc.suffix')