Best Python code snippet using localstack_python
test_nxos_upgrade.py
Source:test_nxos_upgrade.py
...38class NxosUpgradeTestCase(TestCase, LoaderModuleMockMixin):39 """ Test cases for salt.modules.nxos_upgrade """40 platform_list = None41 @staticmethod42 def assert_platform_upgrade(condition, platform):43 """ Assert platform upgrade condition and display appropriate chassis & images upon assertion failure """44 assert bool(condition), "{0}: Upgrade {1} -> {2}".format(45 platform.chassis, platform.cimage, platform.nimage46 )47 def setup_loader_modules(self):48 """ Define list of platforms for Unit Test """49 self.platform_list = [50 N3KPlatform(cimage="nxos.7.0.3.F3.3.bin", nimage="nxos.9.2.1.255.bin"),51 N36KPlatform(cimage="nxos.9.1.2.50.bin", nimage="nxos.9.2.2.50.bin"),52 N5KPlatform(53 ckimage="n6000-uk9-kickstart.7.3.0.N1.1.bin",54 cimage="n6000-uk9.7.3.0.N1.1.bin",55 nkimage="n6000-uk9-kickstart.7.3.3.N2.1.bin",56 nimage="n6000-uk9.7.3.3.N2.1.bin",57 ),58 N7KPlatform(59 ckimage="n7000-s2-kickstart.7.3.0.D1.1.bin",60 cimage="n7000-s2-dk9.7.3.0.D1.1.bin",61 nkimage="n7000-s2-kickstart.8.3.1.112.gbin",62 nimage="n7000-s2-dk9.8.3.1.112.gbin",63 ),64 N93KPlatform(cimage="nxos.7.0.3.I7.4.bin", nimage="nxos.7.0.3.I7.5.bin"),65 N93KPlatform(cimage="nxos.7.0.3.I7.5.bin", nimage="nxos.7.0.3.I7.5.bin"),66 N93KLXCPlatform(cimage="nxos.7.0.3.I7.4.bin", nimage="nxos.7.0.3.I7.5.bin"),67 N95KPlatform(cimage="nxos.7.0.3.I7.4.bin", nimage="nxos.9.2.2.14.bin"),68 ]69 return {nxos_upgrade: {}}70 def tearDown(self):71 del self.platform_list72 @staticmethod73 def test_check_upgrade_impact_input_validation():74 """ UT: nxos_upgrade module:check_upgrade_impact method - input validation """75 result = nxos_upgrade.check_upgrade_impact("dummy-platform-image.bin", issu=1)76 assert "Input Error" in result77 @staticmethod78 def test_upgrade_input_validation():79 """ UT: nxos_upgrade module:upgrade method - input validation """80 result = nxos_upgrade.upgrade("dummy-platform-image.bin", issu=1)81 assert "Input Error" in result82 def test_check_upgrade_impact_backend_processing_error_500(self):83 """ UT: nxos_upgrade module:check_upgrade_impact method - error HTTP code 500 """84 for platform in self.platform_list:85 if platform.backend_processing_error_500:86 with patch.dict(87 nxos_upgrade.__salt__,88 {89 "nxos.sendline": MagicMock(90 return_value=platform.backend_processing_error_50091 )92 },93 ):94 result = nxos_upgrade.check_upgrade_impact(platform.nimage)95 self.assert_platform_upgrade(96 result["backend_processing_error"], platform97 )98 self.assert_platform_upgrade(not result["succeeded"], platform)99 def test_check_upgrade_impact_internal_server_error_400_invalid_command(self):100 """ UT: nxos_upgrade module:check_upgrade_impact method - invalid command error HTTP code 400 """101 for platform in self.platform_list:102 if platform.bad_request_client_error_400_invalid_command_dict:103 with patch.dict(104 nxos_upgrade.__salt__,105 {106 "nxos.sendline": MagicMock(107 return_value=platform.bad_request_client_error_400_invalid_command_dict108 )109 },110 ):111 result = nxos_upgrade.check_upgrade_impact(platform.nimage)112 self.assert_platform_upgrade(result["invalid_command"], platform)113 self.assert_platform_upgrade(not result["succeeded"], platform)114 def test_check_upgrade_impact_internal_server_error_400_in_progress(self):115 """ UT: nxos_upgrade module:check_upgrade_impact method - in-progress error HTTP code 400 """116 for platform in self.platform_list:117 if platform.bad_request_client_error_400_in_progress_dict:118 with patch.dict(119 nxos_upgrade.__salt__,120 {121 "nxos.sendline": MagicMock(122 return_value=platform.bad_request_client_error_400_in_progress_dict123 )124 },125 ):126 result = nxos_upgrade.check_upgrade_impact(platform.nimage)127 self.assert_platform_upgrade(result["installing"], platform)128 self.assert_platform_upgrade(not result["succeeded"], platform)129 def test_check_upgrade_impact_internal_server_error_500(self):130 """ UT: nxos_upgrade module:check_upgrade_impact method - internal server error HTTP code 500 """131 for platform in self.platform_list:132 if platform.internal_server_error_500:133 with patch.dict(134 nxos_upgrade.__salt__,135 {136 "nxos.sendline": MagicMock(137 return_value=platform.internal_server_error_500138 )139 },140 ):141 result = nxos_upgrade.check_upgrade_impact(platform.nimage)142 self.assert_platform_upgrade(143 platform.internal_server_error_500 in result["error_data"],144 platform,145 )146 self.assert_platform_upgrade(147 result["backend_processing_error"], platform148 )149 self.assert_platform_upgrade(not result["succeeded"], platform)150 def test_check_upgrade_impact_non_disruptive_success(self):151 """ UT: nxos_upgrade module:check_upgrade_impact method - non-disruptive success """152 for platform in self.platform_list:153 if platform.install_all_non_disruptive_success:154 with patch.dict(155 nxos_upgrade.__salt__,156 {157 "nxos.sendline": MagicMock(158 return_value=platform.install_all_non_disruptive_success159 )160 },161 ):162 result = nxos_upgrade.check_upgrade_impact(platform.nimage)163 self.assert_platform_upgrade(164 result["upgrade_non_disruptive"], platform165 )166 self.assert_platform_upgrade(result["succeeded"], platform)167 self.assert_platform_upgrade(result["module_data"], platform)168 def test_check_upgrade_impact_disruptive_success(self):169 """ UT: nxos_upgrade module:check_upgrade_impact method - disruptive success """170 for platform in self.platform_list:171 if platform.install_all_disruptive_success:172 with patch.dict(173 nxos_upgrade.__salt__,174 {175 "nxos.sendline": MagicMock(176 return_value=platform.install_all_disruptive_success177 )178 },179 ):180 result = nxos_upgrade.check_upgrade_impact(platform.nimage)181 self.assert_platform_upgrade(182 result["upgrade_required"] == platform.upgrade_required,183 platform,184 )185 self.assert_platform_upgrade(186 not result["upgrade_non_disruptive"], platform187 )188 self.assert_platform_upgrade(not result["succeeded"], platform)189 self.assert_platform_upgrade(190 result["upgrade_in_progress"], platform191 )192 self.assert_platform_upgrade(result["module_data"], platform)193 def test_upgrade_show_install_all_impact_no_module_data(self):194 """ UT: nxos_upgrade module: upgrade method - no module data """195 for platform in self.platform_list:196 if platform.show_install_all_impact_no_module_data:197 with patch.dict(198 nxos_upgrade.__salt__,199 {200 "nxos.sendline": MagicMock(201 return_value=platform.show_install_all_impact_no_module_data202 )203 },204 ):205 result = nxos_upgrade.upgrade(platform.nimage, issu=False)206 self.assert_platform_upgrade(not result["succeeded"], platform)207 self.assert_platform_upgrade(208 result["error_data"] == result["upgrade_data"], platform209 )210 def test_upgrade_invalid_command(self):211 """ UT: nxos_upgrade module:upgrade method - invalid command """212 for platform in self.platform_list:213 if platform.invalid_command:214 with patch.dict(215 nxos_upgrade.__salt__,216 {"nxos.sendline": MagicMock(return_value=platform.invalid_command)},217 ):218 result = nxos_upgrade.upgrade(platform.nimage, platform.nkimage)219 self.assert_platform_upgrade(result["error_data"], platform)220 self.assert_platform_upgrade(not result["succeeded"], platform)221 def test_upgrade_install_in_progress(self):222 """ UT: nxos_upgrade module:upgrade method - in-progress """223 for platform in self.platform_list:224 if platform.show_install_all_impact_in_progress:225 with patch.dict(226 nxos_upgrade.__salt__,227 {228 "nxos.sendline": MagicMock(229 return_value=platform.show_install_all_impact_in_progress230 )231 },232 ):233 result = nxos_upgrade.upgrade(platform.nimage, platform.nkimage)234 self.assert_platform_upgrade(result["error_data"], platform)235 self.assert_platform_upgrade(not result["succeeded"], platform)236 def test_upgrade_install_in_progress_terminal_dont_ask(self):237 """ UT: nxos_upgrade module:upgrade method - in-progress (terminal don't-ask) """238 for platform in self.platform_list:239 if platform.invalid_command:240 with patch.dict(241 nxos_upgrade.__salt__,242 {243 "nxos.sendline": MagicMock(244 return_value=[245 {},246 platform.show_install_all_impact_in_progress,247 ]248 )249 },250 ):251 result = nxos_upgrade.upgrade(platform.nimage, platform.nkimage)252 self.assert_platform_upgrade(result["error_data"], platform)253 self.assert_platform_upgrade(not result["succeeded"], platform)254 def test_upgrade_install_in_progress_sans_terminal_dont_ask(self):255 """ UT: nxos_upgrade module:upgrade method - in-progress (sans terminal don't-ask) """256 for platform in self.platform_list:257 if platform.invalid_command:258 with patch.dict(259 nxos_upgrade.__salt__,260 {261 "nxos.sendline": MagicMock(262 return_value=[platform.show_install_all_impact_in_progress]263 )264 },265 ):266 result = nxos_upgrade.upgrade(platform.nimage, platform.nkimage)267 self.assert_platform_upgrade(result["error_data"], platform)268 self.assert_platform_upgrade(not result["succeeded"], platform)269 def test_upgrade_internal_server_error_500(self):270 """ UT: nxos_upgrade module:upgrade method - internal server error 500 """271 for platform in self.platform_list:272 if platform.backend_processing_error_500:273 with patch.dict(274 nxos_upgrade.__salt__,275 {276 "nxos.sendline": MagicMock(277 return_value=platform.internal_server_error_500278 )279 },280 ):281 result = nxos_upgrade.upgrade(platform.nimage)282 self.assert_platform_upgrade(result["error_data"], platform)283 self.assert_platform_upgrade(284 result["backend_processing_error"], platform285 )286 self.assert_platform_upgrade(not result["succeeded"], platform)287 def test_upgrade_install_all_disruptive(self):288 """ UT: nxos_upgrade module:upgrade method - install all disruptive """289 for platform in self.platform_list:290 if platform.show_install_all_impact:291 with patch.dict(292 nxos_upgrade.__salt__,293 {294 "nxos.sendline": MagicMock(295 side_effect=[296 platform.show_install_all_impact,297 platform.install_all_disruptive_success,298 ]299 )300 },301 ):302 result = nxos_upgrade.upgrade(303 platform.nimage, platform.nkimage, issu=False304 )305 self.assert_platform_upgrade(not result["error_data"], platform)306 if platform.upgrade_required:307 self.assert_platform_upgrade(308 result["upgrade_in_progress"], platform309 )310 else:311 self.assert_platform_upgrade(312 not result["upgrade_in_progress"], platform313 )314 def test_upgrade_install_all_non_disruptive(self):315 """ UT: nxos_upgrade module:upgrade method - install all non-disruptive """316 for platform in self.platform_list:317 if platform.show_install_all_impact_non_disruptive:318 with patch.dict(319 nxos_upgrade.__salt__,320 {321 "nxos.sendline": MagicMock(322 side_effect=[323 platform.show_install_all_impact_non_disruptive,324 platform.install_all_non_disruptive_success,325 ]326 )327 },328 ):329 result = nxos_upgrade.upgrade(330 platform.nimage, platform.nkimage, issu=True331 )332 self.assert_platform_upgrade(not result["error_data"], platform)333 self.assert_platform_upgrade(result["succeeded"], platform)334 def test_upgrade_CommandExecutionError_Exception(self):335 """ UT: nxos_upgrade module:upgrade method - raise CommandExecutionError exception #1 """336 for platform in self.platform_list:337 if platform.invalid_command:338 with patch.dict(339 nxos_upgrade.__salt__,340 {341 "nxos.sendline": MagicMock(342 side_effect=CommandExecutionError(343 {344 "rejected_input": "invalid CLI command",345 "message": "CLI excution error",346 "code": "400",347 "cli_error": platform.invalid_command,348 }349 )350 )351 },352 ):353 result = nxos_upgrade.upgrade(354 platform.nimage, platform.nkimage, issu=False355 )356 self.assert_platform_upgrade(result["error_data"], platform)357 self.assert_platform_upgrade(result["invalid_command"], platform)358 self.assert_platform_upgrade(not result["succeeded"], platform)359 def test_upgrade_CommandExecutionError_Exception2(self):360 """ UT: nxos_upgrade module:upgrade method - raise CommandExecutionError exception #2 """361 for platform in self.platform_list:362 if platform.invalid_command:363 with patch.dict(364 nxos_upgrade.__salt__,365 {366 "nxos.sendline": MagicMock(367 side_effect=[368 platform.show_install_all_impact,369 CommandExecutionError(370 {371 "rejected_input": "invalid CLI command",372 "message": "CLI excution error",373 "code": "400",374 "cli_error": platform.invalid_command,375 }376 ),377 ]378 )379 },380 ):381 result = nxos_upgrade.upgrade(382 platform.nimage, platform.nkimage, issu=False383 )384 if platform.upgrade_required:385 self.assert_platform_upgrade(result["error_data"], platform)386 self.assert_platform_upgrade(387 result["invalid_command"], platform388 )389 self.assert_platform_upgrade(not result["succeeded"], platform)390 else:391 self.assert_platform_upgrade(result["succeeded"], platform)392 def test_upgrade_NxosError_Exception(self):393 """ UT: nxos_upgrade module:upgrade method - raise NxosError exception """394 for platform in self.platform_list:395 if platform.internal_server_error_500:396 with patch.dict(397 nxos_upgrade.__salt__,398 {399 "nxos.sendline": MagicMock(400 side_effect=[401 platform.show_install_all_impact,402 NxosError(platform.internal_server_error_500),403 ]404 )405 },406 ):407 result = nxos_upgrade.upgrade(408 platform.nimage, platform.nkimage, issu=False409 )410 if platform.upgrade_required:411 self.assert_platform_upgrade(412 result["upgrade_in_progress"], platform413 )414 self.assert_platform_upgrade(not result["succeeded"], platform)415 else:416 self.assert_platform_upgrade(417 not result["upgrade_in_progress"], platform418 )419 self.assert_platform_upgrade(result["succeeded"], platform)420 def test_upgrade_NxosError_Exception2(self):421 """ UT: nxos_upgrade module:upgrade method - raise NxosError exception #2 """422 for platform in self.platform_list:423 if platform.internal_server_error_500:424 with patch.dict(425 nxos_upgrade.__salt__,426 {427 "nxos.sendline": MagicMock(428 side_effect=[429 platform.show_install_all_impact,430 NxosError(431 "{'Error Message': 'Not Found', 'Code': 404}"432 ),433 ]434 )435 },436 ):437 result = nxos_upgrade.upgrade(438 platform.nimage, platform.nkimage, issu=False439 )440 if platform.upgrade_required:441 self.assert_platform_upgrade(442 result["upgrade_in_progress"], platform443 )444 self.assert_platform_upgrade(not result["succeeded"], platform)445 else:446 self.assert_platform_upgrade(447 not result["upgrade_in_progress"], platform448 )...
test_handshake.py
Source:test_handshake.py
1#!/usr/bin/env python2#3# Copyright 2009, Google Inc.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are8# met:9#10# * Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# * Redistributions in binary form must reproduce the above13# copyright notice, this list of conditions and the following disclaimer14# in the documentation and/or other materials provided with the15# distribution.16# * Neither the name of Google Inc. nor the names of its17# contributors may be used to endorse or promote products derived from18# this software without specific prior written permission.19#20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31"""Tests for handshake module."""32import unittest33import config # This must be imported before mod_pywebsocket.34from mod_pywebsocket import handshake35import mock36_GOOD_REQUEST = (37 80,38 '/demo',39 {40 'Upgrade':'WebSocket',41 'Connection':'Upgrade',42 'Host':'example.com',43 'Origin':'http://example.com',44 'WebSocket-Protocol':'sample',45 }46)47_GOOD_RESPONSE_DEFAULT_PORT = (48 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'49 'Upgrade: WebSocket\r\n'50 'Connection: Upgrade\r\n'51 'WebSocket-Origin: http://example.com\r\n'52 'WebSocket-Location: ws://example.com/demo\r\n'53 'WebSocket-Protocol: sample\r\n'54 '\r\n')55_GOOD_RESPONSE_SECURE = (56 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'57 'Upgrade: WebSocket\r\n'58 'Connection: Upgrade\r\n'59 'WebSocket-Origin: http://example.com\r\n'60 'WebSocket-Location: wss://example.com/demo\r\n'61 'WebSocket-Protocol: sample\r\n'62 '\r\n')63_GOOD_REQUEST_NONDEFAULT_PORT = (64 8081,65 '/demo',66 {67 'Upgrade':'WebSocket',68 'Connection':'Upgrade',69 'Host':'example.com:8081',70 'Origin':'http://example.com',71 'WebSocket-Protocol':'sample',72 }73)74_GOOD_RESPONSE_NONDEFAULT_PORT = (75 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'76 'Upgrade: WebSocket\r\n'77 'Connection: Upgrade\r\n'78 'WebSocket-Origin: http://example.com\r\n'79 'WebSocket-Location: ws://example.com:8081/demo\r\n'80 'WebSocket-Protocol: sample\r\n'81 '\r\n')82_GOOD_RESPONSE_SECURE_NONDEF = (83 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'84 'Upgrade: WebSocket\r\n'85 'Connection: Upgrade\r\n'86 'WebSocket-Origin: http://example.com\r\n'87 'WebSocket-Location: wss://example.com:8081/demo\r\n'88 'WebSocket-Protocol: sample\r\n'89 '\r\n')90_GOOD_REQUEST_NO_PROTOCOL = (91 80,92 '/demo',93 {94 'Upgrade':'WebSocket',95 'Connection':'Upgrade',96 'Host':'example.com',97 'Origin':'http://example.com',98 }99)100_GOOD_RESPONSE_NO_PROTOCOL = (101 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'102 'Upgrade: WebSocket\r\n'103 'Connection: Upgrade\r\n'104 'WebSocket-Origin: http://example.com\r\n'105 'WebSocket-Location: ws://example.com/demo\r\n'106 '\r\n')107_GOOD_REQUEST_WITH_OPTIONAL_HEADERS = (108 80,109 '/demo',110 {111 'Upgrade':'WebSocket',112 'Connection':'Upgrade',113 'Host':'example.com',114 'Origin':'http://example.com',115 'WebSocket-Protocol':'sample',116 'AKey':'AValue',117 'EmptyValue':'',118 }119)120_BAD_REQUESTS = (121 ( # HTTP request122 80,123 '/demo',124 {125 'Host':'www.google.com',126 'User-Agent':'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5;'127 ' en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'128 ' GTB6 GTBA',129 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,'130 '*/*;q=0.8',131 'Accept-Language':'en-us,en;q=0.5',132 'Accept-Encoding':'gzip,deflate',133 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7',134 'Keep-Alive':'300',135 'Connection':'keep-alive',136 }137 ),138 ( # Missing Upgrade139 80,140 '/demo',141 {142 'Connection':'Upgrade',143 'Host':'example.com',144 'Origin':'http://example.com',145 'WebSocket-Protocol':'sample',146 }147 ),148 ( # Wrong Upgrade149 80,150 '/demo',151 {152 'Upgrade':'NonWebSocket',153 'Connection':'Upgrade',154 'Host':'example.com',155 'Origin':'http://example.com',156 'WebSocket-Protocol':'sample',157 }158 ),159 ( # Empty WebSocket-Protocol160 80,161 '/demo',162 {163 'Upgrade':'WebSocket',164 'Connection':'Upgrade',165 'Host':'example.com',166 'Origin':'http://example.com',167 'WebSocket-Protocol':'',168 }169 ),170 ( # Wrong port number format171 80,172 '/demo',173 {174 'Upgrade':'WebSocket',175 'Connection':'Upgrade',176 'Host':'example.com:0x50',177 'Origin':'http://example.com',178 'WebSocket-Protocol':'sample',179 }180 ),181 ( # Header/connection port mismatch182 8080,183 '/demo',184 {185 'Upgrade':'WebSocket',186 'Connection':'Upgrade',187 'Host':'example.com',188 'Origin':'http://example.com',189 'WebSocket-Protocol':'sample',190 }191 ),192 ( # Illegal WebSocket-Protocol193 80,194 '/demo',195 {196 'Upgrade':'WebSocket',197 'Connection':'Upgrade',198 'Host':'example.com',199 'Origin':'http://example.com',200 'WebSocket-Protocol':'illegal\x09protocol',201 }202 ),203)204_STRICTLY_GOOD_REQUESTS = (205 (206 'GET /demo HTTP/1.1\r\n',207 'Upgrade: WebSocket\r\n',208 'Connection: Upgrade\r\n',209 'Host: example.com\r\n',210 'Origin: http://example.com\r\n',211 '\r\n',212 ),213 ( # WebSocket-Protocol214 'GET /demo HTTP/1.1\r\n',215 'Upgrade: WebSocket\r\n',216 'Connection: Upgrade\r\n',217 'Host: example.com\r\n',218 'Origin: http://example.com\r\n',219 'WebSocket-Protocol: sample\r\n',220 '\r\n',221 ),222 ( # WebSocket-Protocol and Cookie223 'GET /demo HTTP/1.1\r\n',224 'Upgrade: WebSocket\r\n',225 'Connection: Upgrade\r\n',226 'Host: example.com\r\n',227 'Origin: http://example.com\r\n',228 'WebSocket-Protocol: sample\r\n',229 'Cookie: xyz\r\n'230 '\r\n',231 ),232 ( # Cookie233 'GET /demo HTTP/1.1\r\n',234 'Upgrade: WebSocket\r\n',235 'Connection: Upgrade\r\n',236 'Host: example.com\r\n',237 'Origin: http://example.com\r\n',238 'Cookie: abc/xyz\r\n'239 'Cookie2: $Version=1\r\n'240 'Cookie: abc\r\n'241 '\r\n',242 ),243 (244 'GET / HTTP/1.1\r\n',245 'Upgrade: WebSocket\r\n',246 'Connection: Upgrade\r\n',247 'Host: example.com\r\n',248 'Origin: http://example.com\r\n',249 '\r\n',250 ),251)252_NOT_STRICTLY_GOOD_REQUESTS = (253 ( # Extra space after GET254 'GET /demo HTTP/1.1\r\n',255 'Upgrade: WebSocket\r\n',256 'Connection: Upgrade\r\n',257 'Host: example.com\r\n',258 'Origin: http://example.com\r\n',259 '\r\n',260 ),261 ( # Resource name doesn't stat with '/'262 'GET demo HTTP/1.1\r\n',263 'Upgrade: WebSocket\r\n',264 'Connection: Upgrade\r\n',265 'Host: example.com\r\n',266 'Origin: http://example.com\r\n',267 '\r\n',268 ),269 ( # No space after :270 'GET /demo HTTP/1.1\r\n',271 'Upgrade:WebSocket\r\n',272 'Connection: Upgrade\r\n',273 'Host: example.com\r\n',274 'Origin: http://example.com\r\n',275 '\r\n',276 ),277 ( # Lower case Upgrade header278 'GET /demo HTTP/1.1\r\n',279 'upgrade: WebSocket\r\n',280 'Connection: Upgrade\r\n',281 'Host: example.com\r\n',282 'Origin: http://example.com\r\n',283 '\r\n',284 ),285 ( # Connection comes before Upgrade286 'GET /demo HTTP/1.1\r\n',287 'Connection: Upgrade\r\n',288 'Upgrade: WebSocket\r\n',289 'Host: example.com\r\n',290 'Origin: http://example.com\r\n',291 '\r\n',292 ),293 ( # Origin comes before Host294 'GET /demo HTTP/1.1\r\n',295 'Upgrade: WebSocket\r\n',296 'Connection: Upgrade\r\n',297 'Origin: http://example.com\r\n',298 'Host: example.com\r\n',299 '\r\n',300 ),301 ( # Host continued to the next line302 'GET /demo HTTP/1.1\r\n',303 'Upgrade: WebSocket\r\n',304 'Connection: Upgrade\r\n',305 'Host: example\r\n',306 ' .com\r\n',307 'Origin: http://example.com\r\n',308 '\r\n',309 ),310 ( # Cookie comes before WebSocket-Protocol311 'GET /demo HTTP/1.1\r\n',312 'Upgrade: WebSocket\r\n',313 'Connection: Upgrade\r\n',314 'Host: example.com\r\n',315 'Origin: http://example.com\r\n',316 'Cookie: xyz\r\n'317 'WebSocket-Protocol: sample\r\n',318 '\r\n',319 ),320 ( # Unknown header321 'GET /demo HTTP/1.1\r\n',322 'Upgrade: WebSocket\r\n',323 'Connection: Upgrade\r\n',324 'Host: example.com\r\n',325 'Origin: http://example.com\r\n',326 'Content-Type: text/html\r\n'327 '\r\n',328 ),329 ( # Cookie with continuation lines330 'GET /demo HTTP/1.1\r\n',331 'Upgrade: WebSocket\r\n',332 'Connection: Upgrade\r\n',333 'Host: example.com\r\n',334 'Origin: http://example.com\r\n',335 'Cookie: xyz\r\n',336 ' abc\r\n',337 ' defg\r\n',338 '\r\n',339 ),340 ( # Wrong-case cookie341 'GET /demo HTTP/1.1\r\n',342 'Upgrade: WebSocket\r\n',343 'Connection: Upgrade\r\n',344 'Host: example.com\r\n',345 'Origin: http://example.com\r\n',346 'cookie: abc/xyz\r\n'347 '\r\n',348 ),349 ( # Cookie, no space after colon350 'GET /demo HTTP/1.1\r\n',351 'Upgrade: WebSocket\r\n',352 'Connection: Upgrade\r\n',353 'Host: example.com\r\n',354 'Origin: http://example.com\r\n',355 'Cookie:abc/xyz\r\n'356 '\r\n',357 ),358)359def _create_request(request_def):360 conn = mock.MockConn('')361 conn.local_addr = ('0.0.0.0', request_def[0])362 return mock.MockRequest(363 uri=request_def[1],364 headers_in=request_def[2],365 connection=conn)366def _create_get_memorized_lines(lines):367 def get_memorized_lines():368 return lines369 return get_memorized_lines370def _create_requests_with_lines(request_lines_set):371 requests = []372 for lines in request_lines_set:373 request = _create_request(_GOOD_REQUEST)374 request.connection.get_memorized_lines = _create_get_memorized_lines(375 lines)376 requests.append(request)377 return requests378class HandshakerTest(unittest.TestCase):379 def test_validate_protocol(self):380 handshake._validate_protocol('sample') # should succeed.381 handshake._validate_protocol('Sample') # should succeed.382 handshake._validate_protocol('sample\x20protocol') # should succeed.383 handshake._validate_protocol('sample\x7eprotocol') # should succeed.384 self.assertRaises(handshake.HandshakeError,385 handshake._validate_protocol,386 '')387 self.assertRaises(handshake.HandshakeError,388 handshake._validate_protocol,389 'sample\x19protocol')390 self.assertRaises(handshake.HandshakeError,391 handshake._validate_protocol,392 'sample\x7fprotocol')393 self.assertRaises(handshake.HandshakeError,394 handshake._validate_protocol,395 # "Japan" in Japanese396 u'\u65e5\u672c')397 def test_good_request_default_port(self):398 request = _create_request(_GOOD_REQUEST)399 handshaker = handshake.Handshaker(request,400 mock.MockDispatcher())401 handshaker.do_handshake()402 self.assertEqual(_GOOD_RESPONSE_DEFAULT_PORT,403 request.connection.written_data())404 self.assertEqual('/demo', request.ws_resource)405 self.assertEqual('http://example.com', request.ws_origin)406 self.assertEqual('ws://example.com/demo', request.ws_location)407 self.assertEqual('sample', request.ws_protocol)408 def test_good_request_secure_default_port(self):409 request = _create_request(_GOOD_REQUEST)410 request.connection.local_addr = ('0.0.0.0', 443)411 request.is_https_ = True412 handshaker = handshake.Handshaker(request,413 mock.MockDispatcher())414 handshaker.do_handshake()415 self.assertEqual(_GOOD_RESPONSE_SECURE,416 request.connection.written_data())417 self.assertEqual('sample', request.ws_protocol)418 def test_good_request_nondefault_port(self):419 request = _create_request(_GOOD_REQUEST_NONDEFAULT_PORT)420 handshaker = handshake.Handshaker(request,421 mock.MockDispatcher())422 handshaker.do_handshake()423 self.assertEqual(_GOOD_RESPONSE_NONDEFAULT_PORT,424 request.connection.written_data())425 self.assertEqual('sample', request.ws_protocol)426 def test_good_request_secure_non_default_port(self):427 request = _create_request(_GOOD_REQUEST_NONDEFAULT_PORT)428 request.is_https_ = True429 handshaker = handshake.Handshaker(request,430 mock.MockDispatcher())431 handshaker.do_handshake()432 self.assertEqual(_GOOD_RESPONSE_SECURE_NONDEF,433 request.connection.written_data())434 self.assertEqual('sample', request.ws_protocol)435 def test_good_request_default_no_protocol(self):436 request = _create_request(_GOOD_REQUEST_NO_PROTOCOL)437 handshaker = handshake.Handshaker(request,438 mock.MockDispatcher())439 handshaker.do_handshake()440 self.assertEqual(_GOOD_RESPONSE_NO_PROTOCOL,441 request.connection.written_data())442 self.assertEqual(None, request.ws_protocol)443 def test_good_request_optional_headers(self):444 request = _create_request(_GOOD_REQUEST_WITH_OPTIONAL_HEADERS)445 handshaker = handshake.Handshaker(request,446 mock.MockDispatcher())447 handshaker.do_handshake()448 self.assertEqual('AValue',449 request.headers_in['AKey'])450 self.assertEqual('',451 request.headers_in['EmptyValue'])452 def test_bad_requests(self):453 for request in map(_create_request, _BAD_REQUESTS):454 handshaker = handshake.Handshaker(request,455 mock.MockDispatcher())456 self.assertRaises(handshake.HandshakeError, handshaker.do_handshake)457 def test_strictly_good_requests(self):458 for request in _create_requests_with_lines(_STRICTLY_GOOD_REQUESTS):459 strict_handshaker = handshake.Handshaker(request,460 mock.MockDispatcher(),461 True)462 strict_handshaker.do_handshake()463 def test_not_strictly_good_requests(self):464 for request in _create_requests_with_lines(_NOT_STRICTLY_GOOD_REQUESTS):465 strict_handshaker = handshake.Handshaker(request,466 mock.MockDispatcher(),467 True)468 self.assertRaises(handshake.HandshakeError,469 strict_handshaker.do_handshake)470if __name__ == '__main__':471 unittest.main()...
nxos_upgrade.py
Source:nxos_upgrade.py
...105 impact_check = __salt__["nxos.sendline"](cmd, **kwargs)106 except CommandExecutionError as e:107 impact_check = ast.literal_eval(e.message)108 return _parse_upgrade_data(impact_check)109def upgrade(system_image, kickstart_image=None, issu=True, **kwargs):110 """111 Upgrade NX-OS switch.112 system_image (Mandatory Option)113 Path on bootflash: to system image upgrade file.114 kickstart_image115 Path on bootflash: to kickstart image upgrade file.116 (Not required if using combined system/kickstart image file)117 Default: None118 issu119 Set this option to True when an In Service Software Upgrade or120 non-disruptive upgrade is required. The upgrade will abort if issu is121 not possible.122 Default: True123 timeout124 Timeout in seconds for long running 'install all' upgrade command.125 Default: 900126 error_pattern127 Use the option to pass in a regular expression to search for in the128 output of the 'install all upgrade command that indicates an error129 has occurred. This option is only used when proxy minion connection130 type is ssh and otherwise ignored.131 .. code-block:: bash132 salt 'n9k' nxos.upgrade system_image=nxos.9.2.1.bin133 salt 'n7k' nxos.upgrade system_image=n7000-s2-dk9.8.1.1.bin \\134 kickstart_image=n7000-s2-kickstart.8.1.1.bin issu=False135 """136 # Input Validation137 if not isinstance(issu, bool):138 return "Input Error: The [issu] parameter must be either True or False"139 impact = None140 upgrade = None141 maxtry = 60142 for attempt in range(1, maxtry):143 # Gather impact data first. It's possible to loose upgrade data144 # when the switch reloads or switches over to the inactive supervisor.145 # The impact data will be used if data being collected during the146 # upgrade is lost.147 if impact is None:148 log.info("Gathering impact data")149 impact = check_upgrade_impact(system_image, kickstart_image, issu, **kwargs)150 if impact["installing"]:151 log.info("Another show impact in progress... wait and retry")152 time.sleep(30)153 continue154 # If we are upgrading from a system running a separate system and155 # kickstart image to a combined image or vice versa then the impact156 # check will return a syntax error as it's not supported.157 # Skip the impact check in this case and attempt the upgrade.158 if impact["invalid_command"]:159 impact = False160 continue161 log.info("Impact data gathered:\n{}".format(impact))162 # Check to see if conditions are sufficent to return the impact163 # data and not proceed with the actual upgrade.164 #165 # Impact data indicates the upgrade or downgrade will fail166 if impact["error_data"]:167 return impact168 # Requested ISSU but ISSU is not possible169 if issu and not impact["upgrade_non_disruptive"]:170 impact["error_data"] = impact["upgrade_data"]171 return impact172 # Impact data indicates a failure and no module_data collected173 if not impact["succeeded"] and not impact["module_data"]:174 impact["error_data"] = impact["upgrade_data"]175 return impact176 # Impact data indicates switch already running desired image177 if not impact["upgrade_required"]:178 impact["succeeded"] = True179 return impact180 # If we get here, impact data indicates upgrade is needed.181 upgrade = _upgrade(system_image, kickstart_image, issu, **kwargs)182 if upgrade["installing"]:183 log.info("Another install is in progress... wait and retry")184 time.sleep(30)185 continue186 # If the issu option is False and this upgrade request includes a187 # kickstart image then the 'force' option is used. This option is188 # only available in certain image sets.189 if upgrade["invalid_command"]:190 log_msg = "The [issu] option was set to False for this upgrade."191 log_msg = log_msg + " Attempt was made to ugrade using the force"192 log_msg = log_msg + " keyword which is not supported in this"193 log_msg = log_msg + " image. Set [issu=True] and re-try."194 upgrade["upgrade_data"] = log_msg195 break196 break197 # Check for errors and return upgrade result:198 if upgrade["backend_processing_error"]:199 # This means we received a backend processing error from the transport200 # and lost the upgrade data. This also indicates that the upgrade201 # is in progress so use the impact data for logging purposes.202 impact["upgrade_in_progress"] = True203 return impact204 return upgrade205def _upgrade(system_image, kickstart_image, issu, **kwargs):206 """207 Helper method that does the heavy lifting for upgrades.208 """209 si = system_image210 ki = kickstart_image211 dev = "bootflash"212 cmd = "terminal dont-ask ; install all"213 if ki is None:214 logmsg = "Upgrading device using combined system/kickstart image."215 logmsg += "\nSystem Image: {}".format(si)216 cmd = cmd + " nxos {0}:{1}".format(dev, si)217 if issu:218 cmd = cmd + " non-disruptive"219 else:...
update.py
Source:update.py
...38 })39 if conf.get('release_bench'):40 print 'Release bench, cannot update'41 sys.exit(1)42 version_upgrade = is_version_upgrade()43 if version_upgrade[0] and not upgrade:44 print45 print46 print "This update will cause a major version change in Frappe/ERPNext from {0} to {1}.".format(*version_upgrade[1:])47 print "This would take significant time to migrate and might break custom apps. Please run `bench update --upgrade` to confirm."48 print49 print "You can stay on the latest stable release by running `bench switch-to-master` or pin your bench to {0} by running `bench switch-to-v{0}`".format(version_upgrade[1])50 sys.exit(1)51 _update(pull, patch, build, bench, auto, restart_supervisor, requirements, no_backup, upgrade, force=force)52def _update(pull=False, patch=False, build=False, update_bench=False, auto=False, restart_supervisor=False, requirements=False, no_backup=False, upgrade=False, bench_path='.', force=False):53 conf = get_config(bench_path=bench_path)54 version_upgrade = is_version_upgrade(bench_path=bench_path)55 if version_upgrade[0] and not upgrade:56 raise Exception("Major Version Upgrade")57 if upgrade and (version_upgrade[0] or (not version_upgrade[0] and force)):58 validate_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path)59 before_update(bench_path=bench_path, requirements=requirements)60 if pull:61 pull_all_apps(bench_path=bench_path)62 if requirements:63 update_requirements(bench_path=bench_path)64 if upgrade and (version_upgrade[0] or (not version_upgrade[0] and force)):65 pre_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path)66 import bench.utils, bench.app67 reload(bench.utils)68 reload(bench.app)69 if patch:70 if not no_backup:71 backup_all_sites(bench_path=bench_path)72 patch_sites(bench_path=bench_path)73 if build:74 build_assets(bench_path=bench_path)75 if upgrade and (version_upgrade[0] or (not version_upgrade[0] and force)):76 post_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path)77 if restart_supervisor or conf.get('restart_supervisor_on_update'):78 restart_supervisor_processes(bench_path=bench_path)79 print "_"*8080 print "Bench: Open source installer + admin for Frappe and ERPNext (https://erpnext.com)"81 print82@click.command('retry-upgrade')83@click.option('--version', default=5)84def retry_upgrade(version):85 pull_all_apps()86 patch_sites()87 build_assets()88 post_upgrade(version-1, version)89def restart_update(kwargs):90 args = ['--'+k for k, v in kwargs.items() if v]91 os.execv(sys.argv[0], sys.argv[:2] + args)92@click.command('switch-to-branch')93@click.argument('branch')94@click.argument('apps', nargs=-1)95@click.option('--upgrade',is_flag=True)96def switch_to_branch(branch, apps, upgrade=False):97 "Switch all apps to specified branch, or specify apps separated by space"98 from bench.app import switch_to_branch99 switch_to_branch(branch=branch, apps=list(apps), upgrade=upgrade)100 print 'Switched to ' + branch 101 print 'Please run `bench update --patch` to be safe from any differences in database schema'102@click.command('switch-to-master')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!