1
1
#!/usr/bin/env python3
2
+
3
+ from fabric import Result
4
+
2
5
import os
3
6
import sys
4
7
import re
5
- import time
8
+ import time
6
9
import utils as u
7
10
11
+
12
+ DIAGNOSE_PASSED_RE = re .compile (r'All \d+ checks passed' )
13
+
14
+ def _diagnose_result_is_successful (result : Result ) -> bool :
15
+ return DIAGNOSE_PASSED_RE .search (result .stdout ) is not None
16
+
8
17
@u .print_test_decorator
9
- def run_test_windows (remote_host : u .Host , env_vars : dict ) -> None :
18
+ def run_test_windows (remote_host : u .Host , env_vars : dict ) -> None :
10
19
11
20
"""
12
- Test to validate connection of observe-agent to Observe
21
+ Test to validate connection of observe-agent to Observe
13
22
14
23
Args:
15
- remote_host (Host): instance to ssh into
24
+ remote_host (Host): instance to ssh into
16
25
env_vars (dict): environment variables passed into for testing
17
26
18
27
Raises:
19
- ValueError: Something failed with initial config or observe-agent -> observe connection
28
+ ValueError: Something failed with initial config or observe-agent -> observe connection
20
29
"""
21
-
30
+
22
31
init_command = 'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent init-config --token {} --observe_url {}' .format (env_vars ["observe_token" ], env_vars ["observe_url" ])
23
32
diagnose_command = 'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent diagnose'
24
-
25
- #Set up correct config with observe url and token
33
+
34
+ #Set up correct config with observe url and token
26
35
result = remote_host .run_command (init_command )
27
36
28
37
#Check diagnose command
29
38
result = remote_host .run_command (diagnose_command )
30
- observe_val = False
31
- for line in result .stdout .splitlines ():
32
- if "Request to test URL responded with response code 200" in line :
33
- print (" ✅ observe-agent -> observe validation passed! " )
34
- observe_val = True
35
- break
36
- if not observe_val :
39
+ if _diagnose_result_is_successful (result ):
40
+ print (" ✅ observe-agent -> observe validation passed! " )
41
+ else :
37
42
print (result )
38
43
raise ValueError (f"❌ Failed: observe-agent -> observe validation" )
39
-
40
- pass
44
+
45
+ pass
41
46
42
47
@u .print_test_decorator
43
- def run_test_docker (remote_host : u .Host , env_vars : dict ) -> None :
48
+ def run_test_docker (remote_host : u .Host , env_vars : dict ) -> None :
44
49
docker_prefix = 'sudo docker run \
45
50
--mount type=bind,source=/proc,target=/hostfs/proc,readonly \
46
51
--mount type=bind,source=/snap,target=/hostfs/snap,readonly \
@@ -50,71 +55,63 @@ def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
50
55
--mount type=bind,source=$(pwd)/observe-agent.yaml,target=/etc/observe-agent/observe-agent.yaml \
51
56
--pid host \
52
57
$(sudo docker images --format "{{.Repository}}:{{.Tag}}" | grep SNAPSHOT)'
53
-
58
+
54
59
init_command = 'init-config --token {} --observe_url {}' .format (env_vars ["observe_token" ], env_vars ["observe_url" ])
55
60
diagnose_command = 'diagnose'
56
61
57
- #Set up correct config with observe url and token
62
+ #Set up correct config with observe url and token
58
63
result = remote_host .run_command (docker_prefix + ' ' + init_command )
59
64
60
65
#Check diagnose command
61
66
result = remote_host .run_command (docker_prefix + ' ' + diagnose_command )
62
- observe_val = False
63
- for line in result .stdout .splitlines ():
64
- if "Request to test URL responded with response code 200" in line :
65
- print (" ✅ observe-agent -> observe validation passed! " )
66
- observe_val = True
67
- break
68
- if not observe_val :
67
+ if _diagnose_result_is_successful (result ):
68
+ print (" ✅ observe-agent -> observe validation passed! " )
69
+ else :
69
70
print (result )
70
71
raise ValueError (f"❌ Failed: observe-agent -> observe validation" )
71
72
72
73
73
74
pass
74
75
75
76
@u .print_test_decorator
76
- def run_test_linux (remote_host : u .Host , env_vars : dict ) -> None :
77
+ def run_test_linux (remote_host : u .Host , env_vars : dict ) -> None :
77
78
78
79
"""
79
- Test to validate connection of observe-agent to Observe
80
+ Test to validate connection of observe-agent to Observe
80
81
81
82
Args:
82
- remote_host (Host): instance to ssh into
83
+ remote_host (Host): instance to ssh into
83
84
env_vars (dict): environment variables passed into for testing
84
85
85
86
Raises:
86
- ValueError: Something failed with initial config or observe-agent -> observe connection
87
+ ValueError: Something failed with initial config or observe-agent -> observe connection
87
88
"""
88
89
89
90
init_command = 'sudo observe-agent init-config --token {} --observe_url {}' .format (env_vars ["observe_token" ], env_vars ["observe_url" ])
90
91
diagnose_command = 'observe-agent diagnose'
91
92
92
- #Set up correct config with observe url and token
93
+ #Set up correct config with observe url and token
93
94
result = remote_host .run_command (init_command )
94
95
95
96
#Check diagnose command
96
97
result = remote_host .run_command (diagnose_command )
97
- observe_val = False
98
- for line in result .stdout .splitlines ():
99
- if "Request to test URL responded with response code 200" in line :
100
- print (" ✅ observe-agent -> observe validation passed! " )
101
- observe_val = True
102
- break
103
- if not observe_val :
98
+ if _diagnose_result_is_successful (result ):
99
+ print (" ✅ observe-agent -> observe validation passed! " )
100
+ else :
104
101
print (result )
105
102
raise ValueError (f"❌ Failed: observe-agent -> observe validation" )
106
-
103
+
107
104
108
105
if __name__ == '__main__' :
109
106
110
107
env_vars = u .get_env_vars (need_observe = True )
111
108
remote_host = u .Host (host_ip = env_vars ["host" ],
112
109
username = env_vars ["user" ],
113
110
key_file_path = env_vars ["key_filename" ],
114
- password = env_vars ["password" ])
115
-
116
- #Test SSH Connection before starting test of interest
117
- remote_host .test_conection (int (env_vars ["machine_config" ]["sleep" ]))
111
+ password = env_vars ["password" ])
112
+
113
+ #Test SSH Connection before starting test of interest
114
+ remote_host .test_conection (int (env_vars ["machine_config" ]["sleep" ]))
118
115
119
116
if "redhat" in env_vars ["machine_config" ]["distribution" ] or "debian" in env_vars ["machine_config" ]["distribution" ]:
120
117
run_test_linux (remote_host , env_vars )
@@ -123,6 +120,6 @@ def run_test_linux(remote_host: u.Host, env_vars: dict) -> None:
123
120
elif "docker" in env_vars ["machine_config" ]["distribution" ]:
124
121
run_test_docker (remote_host , env_vars )
125
122
126
- pass
123
+ pass
127
124
128
125
0 commit comments