1
+ import sys
2
+ import os
3
+ import pytest
4
+ from unittest import mock
5
+ from host_modules import gcu
6
+
7
+ class TestGCU (object ):
8
+ @mock .patch ("dbus.SystemBus" )
9
+ @mock .patch ("dbus.service.BusName" )
10
+ @mock .patch ("dbus.service.Object.__init__" )
11
+ def test_apply_patch_db (self , MockInit , MockBusName , MockSystemBus ):
12
+ with mock .patch ("subprocess.run" ) as mock_run :
13
+ res_mock = mock .Mock ()
14
+ test_ret = 0
15
+ test_msg = b"Error: this is the test message\n Hello world\n "
16
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
17
+ res_mock .configure_mock (** attrs )
18
+ mock_run .return_value = res_mock
19
+ patch_file = "test.patch"
20
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
21
+ ret , msg = gcu_stub .apply_patch_db (patch_file )
22
+ call_args = mock_run .call_args [0 ][0 ]
23
+ assert "apply-patch" in call_args
24
+ assert "CONFIGDB" in call_args
25
+ assert patch_file in call_args
26
+ assert ret == test_ret , "Return value is wrong"
27
+ assert msg == "" , "Return message is wrong"
28
+ with mock .patch ("subprocess.run" ) as mock_run :
29
+ res_mock = mock .Mock ()
30
+ test_ret = 1
31
+ test_msg = b"Error: this is the test message\n Hello world\n "
32
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
33
+ res_mock .configure_mock (** attrs )
34
+ mock_run .return_value = res_mock
35
+ patch_file = "test.patch"
36
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
37
+ ret , msg = gcu_stub .apply_patch_db (patch_file )
38
+ call_args = mock_run .call_args [0 ][0 ]
39
+ assert "apply-patch" in call_args
40
+ assert "CONFIGDB" in call_args
41
+ assert patch_file in call_args
42
+ assert ret == test_ret , "Return value is wrong"
43
+ assert msg == "Error: this is the test message" , "Return message is wrong"
44
+
45
+ @mock .patch ("dbus.SystemBus" )
46
+ @mock .patch ("dbus.service.BusName" )
47
+ @mock .patch ("dbus.service.Object.__init__" )
48
+ def test_apply_patch_yang (self , MockInit , MockBusName , MockSystemBus ):
49
+ with mock .patch ("subprocess.run" ) as mock_run :
50
+ res_mock = mock .Mock ()
51
+ test_ret = 0
52
+ test_msg = b"Error: this is the test message\n Hello world\n "
53
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
54
+ res_mock .configure_mock (** attrs )
55
+ mock_run .return_value = res_mock
56
+ patch_file = "test.patch"
57
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
58
+ ret , msg = gcu_stub .apply_patch_yang (patch_file )
59
+ call_args = mock_run .call_args [0 ][0 ]
60
+ assert "apply-patch" in call_args
61
+ assert "SONICYANG" in call_args
62
+ assert patch_file in call_args
63
+ assert ret == test_ret , "Return value is wrong"
64
+ assert msg == "" , "Return message is wrong"
65
+ with mock .patch ("subprocess.run" ) as mock_run :
66
+ res_mock = mock .Mock ()
67
+ test_ret = 1
68
+ test_msg = b"Error: this is the test message\n Hello world\n "
69
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
70
+ res_mock .configure_mock (** attrs )
71
+ mock_run .return_value = res_mock
72
+ patch_file = "test.patch"
73
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
74
+ ret , msg = gcu_stub .apply_patch_yang (patch_file )
75
+ call_args = mock_run .call_args [0 ][0 ]
76
+ assert "apply-patch" in call_args
77
+ assert "SONICYANG" in call_args
78
+ assert patch_file in call_args
79
+ assert ret == test_ret , "Return value is wrong"
80
+ assert msg == "Error: this is the test message" , "Return message is wrong"
81
+
82
+ @mock .patch ("dbus.SystemBus" )
83
+ @mock .patch ("dbus.service.BusName" )
84
+ @mock .patch ("dbus.service.Object.__init__" )
85
+ def test_create_checkpoint (self , MockInit , MockBusName , MockSystemBus ):
86
+ with mock .patch ("subprocess.run" ) as mock_run :
87
+ res_mock = mock .Mock ()
88
+ test_ret = 0
89
+ test_msg = b"Error: this is the test message\n Hello world\n "
90
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
91
+ res_mock .configure_mock (** attrs )
92
+ mock_run .return_value = res_mock
93
+ cp_name = "test_name"
94
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
95
+ ret , msg = gcu_stub .create_checkpoint (cp_name )
96
+ call_args = mock_run .call_args [0 ][0 ]
97
+ assert "checkpoint" in call_args
98
+ assert "delete-checkpoint" not in call_args
99
+ assert cp_name in call_args
100
+ assert ret == test_ret , "Return value is wrong"
101
+ assert msg == "" , "Return message is wrong"
102
+ with mock .patch ("subprocess.run" ) as mock_run :
103
+ res_mock = mock .Mock ()
104
+ test_ret = 1
105
+ test_msg = b"Error: this is the test message\n Hello world\n "
106
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
107
+ res_mock .configure_mock (** attrs )
108
+ mock_run .return_value = res_mock
109
+ cp_name = "test_name"
110
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
111
+ ret , msg = gcu_stub .create_checkpoint (cp_name )
112
+ call_args = mock_run .call_args [0 ][0 ]
113
+ assert "checkpoint" in call_args
114
+ assert "delete-checkpoint" not in call_args
115
+ assert cp_name in call_args
116
+ assert ret == test_ret , "Return value is wrong"
117
+ assert msg == "Error: this is the test message" , "Return message is wrong"
118
+
119
+ @mock .patch ("dbus.SystemBus" )
120
+ @mock .patch ("dbus.service.BusName" )
121
+ @mock .patch ("dbus.service.Object.__init__" )
122
+ def test_delete_checkpoint (self , MockInit , MockBusName , MockSystemBus ):
123
+ with mock .patch ("subprocess.run" ) as mock_run :
124
+ res_mock = mock .Mock ()
125
+ test_ret = 0
126
+ test_msg = b"Error: this is the test message\n Hello world\n "
127
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
128
+ res_mock .configure_mock (** attrs )
129
+ mock_run .return_value = res_mock
130
+ cp_name = "test_name"
131
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
132
+ ret , msg = gcu_stub .delete_checkpoint (cp_name )
133
+ call_args = mock_run .call_args [0 ][0 ]
134
+ assert "delete-checkpoint" in call_args
135
+ assert cp_name in call_args
136
+ assert ret == test_ret , "Return value is wrong"
137
+ assert msg == "" , "Return message is wrong"
138
+ with mock .patch ("subprocess.run" ) as mock_run :
139
+ res_mock = mock .Mock ()
140
+ test_ret = 1
141
+ test_msg = b"Error: this is the test message\n Hello world\n "
142
+ attrs = {"returncode" : test_ret , "stderr" : test_msg }
143
+ res_mock .configure_mock (** attrs )
144
+ mock_run .return_value = res_mock
145
+ cp_name = "test_name"
146
+ gcu_stub = gcu .GCU (gcu .MOD_NAME )
147
+ ret , msg = gcu_stub .delete_checkpoint (cp_name )
148
+ call_args = mock_run .call_args [0 ][0 ]
149
+ assert "delete-checkpoint" in call_args
150
+ assert cp_name in call_args
151
+ assert ret == test_ret , "Return value is wrong"
152
+ assert msg == "Error: this is the test message" , "Return message is wrong"
153
+
0 commit comments