Skip to content

Commit 9a0b65b

Browse files
committed
LNX-329 - Fix MethodNotFound error for profiler rum checking
1 parent 1db5c4d commit 9a0b65b

File tree

7 files changed

+129
-70
lines changed

7 files changed

+129
-70
lines changed

docker/stackify-python-api-test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ RUN mkdir /build
1515
COPY . /build/
1616

1717
RUN cat /build/requirements.txt | xargs -n 1 pip install; exit 0
18-
RUN if [ "${test}" = 1 ]; then pip install -i "${test_repo}" stackify-python-apm; fi; exit 0
18+
19+
ENV TEST="${test}"
20+
ENV TEST_REPO="${test_repo}"
1921

2022
CMD /bin/bash -c "cd /build && source test-docker-execute.sh"

stackify/rum.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
apm_installed = False
66

77
try:
8+
from stackifyapm import insert_rum_script as insert_rum_script_from_apm
89
apm_installed = True
9-
from stackifyapm import insert_rum_script as insert_rum_script_apm
10-
except ImportError:
10+
except (ImportError):
1111
pass
1212

1313

1414
def insert_rum_script():
15-
if apm_installed is True:
16-
return insert_rum_script_apm()
15+
apm_rum_script = insert_rum_script_apm()
16+
if apm_rum_script:
17+
return apm_rum_script
1718

1819
rum_key = config.rum_key
1920
rum_script_url = config.rum_script_url
@@ -72,3 +73,14 @@ def get_transaction_id():
7273

7374
def get_reporting_url():
7475
return ''
76+
77+
78+
def insert_rum_script_apm():
79+
if not is_apm_installed():
80+
return
81+
82+
return insert_rum_script_from_apm()
83+
84+
85+
def is_apm_installed():
86+
return apm_installed

test-docker-execute.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ function runPyTest() {
1414
echo '<--------------------------------------------->'
1515
echo "Python Version $(python --version)"
1616
echo 'Running pytest...'
17-
py.test
17+
py.test --ignore=tests/rum
18+
19+
if [ "${TEST}" = 1 ]; then
20+
pip install -i "${TEST_REPO}" stackify-python-apm;
21+
else
22+
pip install stackify-python-apm;
23+
py.test tests/rum
24+
fi
25+
26+
pip uninstall -y stackify-python-apm
1827
}
1928

2029
runFlake8

tests/rum/__init__.py

Whitespace-only changes.

tests/rum/test_rum_apm.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
try:
2+
from unittest import mock
3+
except Exception:
4+
import mock # noqa F401
5+
6+
import stackify
7+
import stackify.rum
8+
import base64
9+
import json
10+
from unittest import TestCase
11+
12+
apmExist = False
13+
try:
14+
from stackifyapm.base import Client
15+
apmExist = True
16+
except (ImportError):
17+
pass
18+
19+
APM_CONFIG = {
20+
"SERVICE_NAME": "service_name",
21+
"ENVIRONMENT": "production",
22+
"HOSTNAME": "sample_host",
23+
"FRAMEWORK_NAME": "framework",
24+
"FRAMEWORK_VERSION": "1.0",
25+
"APPLICATION_NAME": "sample_application",
26+
"BASE_DIR": "path/to/application/",
27+
"RUM_SCRIPT_URL": "https://test.com/test.js",
28+
"RUM_KEY": "LOREM123"
29+
}
30+
31+
32+
class RumTestApm(TestCase):
33+
def setUp(self):
34+
self.config_rum_key = stackify.config.rum_key
35+
self.config_rum_script_url = stackify.config.rum_script_url
36+
self.config_application = stackify.config.application
37+
self.config_environment = stackify.config.environment
38+
self.maxDiff = None
39+
40+
def shutDown(self):
41+
pass
42+
43+
def test_default_insert_rum_script_from_apm_with_transaction(self):
44+
if not apmExist:
45+
return
46+
47+
client = Client(APM_CONFIG)
48+
print('config')
49+
print(client.config.rum_key)
50+
51+
transaction = client.begin_transaction("transaction_test", client=client)
52+
rum_data = stackify.rum.insert_rum_script()
53+
54+
rum_settings = {
55+
"ID": transaction.get_trace_parent().trace_id,
56+
"Name": base64.b64encode(APM_CONFIG["APPLICATION_NAME"].encode("utf-8")).decode("utf-8"),
57+
"Env": base64.b64encode(APM_CONFIG["ENVIRONMENT"].encode("utf-8")).decode("utf-8"),
58+
"Trans": base64.b64encode('/'.encode("utf-8")).decode("utf-8")
59+
}
60+
61+
result_string = '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="{}" data-key="{}" async></script>'.format(
62+
json.dumps(rum_settings),
63+
APM_CONFIG["RUM_SCRIPT_URL"],
64+
APM_CONFIG["RUM_KEY"]
65+
)
66+
67+
assert rum_data
68+
assert rum_data == result_string
69+
client.end_transaction("transaction_test")
70+
71+
def test_default_insert_rum_script_from_apm_without_transaction(self):
72+
if not apmExist:
73+
return
74+
75+
rum_data = stackify.rum.insert_rum_script()
76+
assert not rum_data

tests/test_compat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from unittest import TestCase
22

3-
from stackifyapm.utils.compat import b
4-
from stackifyapm.utils.compat import iterkeys
5-
from stackifyapm.utils.compat import iteritems
3+
from stackify.compat import b
4+
from stackify.compat import iterkeys
5+
from stackify.compat import iteritems
66

77

88
class CompatTest(TestCase):

tests/test_rum.py

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import stackify
77
import os
88
import stackify.rum
9-
import base64
109
import json
1110

1211
from .bases import ClearEnvTest
1312
from stackify.transport.application import ApiConfiguration
1413
from stackify.utils import arg_or_env
1514
from stackify.constants import DEFAULT_RUM_SCRIPT_URL
16-
from stackifyapm.base import Client
1715
from unittest import TestCase
1816

1917
APM_CONFIG = {
@@ -40,43 +38,13 @@ def setUp(self):
4038
def shutDown(self):
4139
pass
4240

43-
def test_default_insert_rum_script_from_apm_with_transaction(self):
44-
self.update_apm_installed(True)
45-
client = Client(APM_CONFIG)
46-
47-
transaction = client.begin_transaction("transaction_test", client=client)
48-
rum_data = stackify.rum.insert_rum_script()
49-
assert rum_data
50-
51-
rum_settings = {
52-
"ID": transaction.get_trace_parent().trace_id,
53-
"Name": base64.b64encode(APM_CONFIG["APPLICATION_NAME"].encode("utf-8")).decode("utf-8"),
54-
"Env": base64.b64encode(APM_CONFIG["ENVIRONMENT"].encode("utf-8")).decode("utf-8"),
55-
"Trans": base64.b64encode('/'.encode("utf-8")).decode("utf-8")
56-
}
57-
58-
result_string = '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="{}" data-key="{}" async></script>'.format(
59-
json.dumps(rum_settings),
60-
APM_CONFIG["RUM_SCRIPT_URL"],
61-
APM_CONFIG["RUM_KEY"]
62-
)
63-
64-
assert rum_data == result_string
65-
client.end_transaction("transaction_test")
66-
self.restore_apm_installed()
67-
68-
def test_default_insert_rum_script_from_apm_without_transaction(self):
69-
self.update_apm_installed(True)
70-
rum_data = stackify.rum.insert_rum_script()
71-
assert not rum_data
72-
self.restore_apm_installed()
73-
41+
@mock.patch('stackify.rum.is_apm_installed')
7442
@mock.patch('stackify.rum.get_reporting_url')
7543
@mock.patch('stackify.rum.get_transaction_id')
76-
def test_default_insert_rum_script(self, func, func_reporting_url):
44+
def test_default_insert_rum_script(self, func, func_reporting_url, func_apm):
7745
func.return_value = '123'
7846
func_reporting_url.return_value = 'test reporting url'
79-
self.update_apm_installed(False)
47+
func_apm.return_value = False
8048
self.update_common_config(
8149
rum_key='asd',
8250
application='app',
@@ -92,12 +60,12 @@ def test_default_insert_rum_script(self, func, func_reporting_url):
9260

9361
result = stackify.rum.insert_rum_script()
9462
self.reset_common_config()
95-
self.restore_apm_installed()
9663

9764
assert result == '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="https://stckjs.stackify.com/stckjs.js" data-key="asd" async></script>'.format(json.dumps(rum_settings))
9865

99-
def test_default_insert_rum_script_no_transaction_id(self):
100-
self.update_apm_installed(False)
66+
@mock.patch('stackify.rum.is_apm_installed')
67+
def test_default_insert_rum_script_no_transaction_id(self, func_apm):
68+
func_apm.return_value = False
10169
self.update_common_config(
10270
rum_key='asd',
10371
application='app',
@@ -106,12 +74,12 @@ def test_default_insert_rum_script_no_transaction_id(self):
10674

10775
result = stackify.rum.insert_rum_script()
10876
self.reset_common_config()
109-
self.restore_apm_installed()
11077

11178
assert result is None
11279

113-
def test_default_insert_rum_script_no_key(self):
114-
self.update_apm_installed(False)
80+
@mock.patch('stackify.rum.is_apm_installed')
81+
def test_default_insert_rum_script_no_key(self, func_apm):
82+
func_apm.return_value = False
11583
self.update_common_config(
11684
rum_key='',
11785
application='app',
@@ -122,24 +90,24 @@ def test_default_insert_rum_script_no_key(self):
12290
assert not result
12391

12492
self.reset_common_config()
125-
self.restore_apm_installed()
12693

127-
def test_default_insert_rum_script_no_details(self):
128-
self.update_apm_installed(False)
94+
@mock.patch('stackify.rum.is_apm_installed')
95+
def test_default_insert_rum_script_no_details(self, func_apm):
96+
func_apm.return_value = False
12997
self.update_common_config()
13098

13199
result = stackify.rum.insert_rum_script()
132100
assert not result
133101

134102
self.reset_common_config()
135-
self.restore_apm_installed()
136103

104+
@mock.patch('stackify.rum.is_apm_installed')
137105
@mock.patch('stackify.rum.get_reporting_url')
138106
@mock.patch('stackify.rum.get_transaction_id')
139-
def test_default_insert_rum_script_from_api(self, func, func_reporting_url):
107+
def test_default_insert_rum_script_from_api(self, func, func_reporting_url, func_apm):
140108
func.return_value = '123'
109+
func_apm.return_value = False
141110
func_reporting_url.return_value = 'test reporting url'
142-
self.update_apm_installed(False)
143111
self.create_config(
144112
rum_key='asd1',
145113
application='app1',
@@ -153,11 +121,11 @@ def test_default_insert_rum_script_from_api(self, func, func_reporting_url):
153121
}
154122
result = stackify.rum.insert_rum_script()
155123
self.reset_common_config()
156-
self.restore_apm_installed()
157124
assert result == '<script type="text/javascript">(window.StackifySettings || (window.StackifySettings = {}))</script><script src="https://stckjs.stackify.com/stckjs.js" data-key="asd1" async></script>'.format(json.dumps(rum_settings))
158125

159-
def test_default_insert_rum_script_no_key_from_api(self):
160-
self.update_apm_installed(False)
126+
@mock.patch('stackify.rum.is_apm_installed')
127+
def test_default_insert_rum_script_no_key_from_api(self, func_apm):
128+
func_apm.return_value = False
161129
self.create_config(
162130
rum_key=None,
163131
application='app2',
@@ -166,12 +134,12 @@ def test_default_insert_rum_script_no_key_from_api(self):
166134

167135
result = stackify.rum.insert_rum_script()
168136
self.reset_common_config()
169-
self.restore_apm_installed()
170137

171138
assert not result
172139

173-
def test_default_insert_rum_script_no_details_from_api(self):
174-
self.update_apm_installed(False)
140+
@mock.patch('stackify.rum.is_apm_installed')
141+
def test_default_insert_rum_script_no_details_from_api(self, func_apm):
142+
func_apm.return_value = False
175143
self.create_config(
176144
application=None,
177145
environment=None,
@@ -180,17 +148,9 @@ def test_default_insert_rum_script_no_details_from_api(self):
180148

181149
result = stackify.rum.insert_rum_script()
182150
self.reset_common_config()
183-
self.restore_apm_installed()
184151

185152
assert not result
186153

187-
def update_apm_installed(self, installed):
188-
self.apm_installed = stackify.rum.apm_installed
189-
stackify.rum.apm_installed = installed
190-
191-
def restore_apm_installed(self):
192-
stackify.rum.apm_installed = self.apm_installed
193-
194154
def update_common_config(self, rum_key=None, rum_script_url=None, application=None, environment=None):
195155
self.config_rum_key = stackify.config.rum_key
196156
self.config_rum_script_url = stackify.config.rum_script_url

0 commit comments

Comments
 (0)