github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/pfs_middleware/tests/test_rpc.py (about) 1 # Copyright (c) 2016 SwiftStack, Inc. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 # implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 import unittest 17 18 import pfs_middleware.rpc as rpc 19 20 21 class TestAddressParsing(unittest.TestCase): 22 def test_ipv4(self): 23 resp = {"IsBimodal": True, 24 "ActivePeerPrivateIPAddr": "10.2.3.4"} 25 26 _, parsed_ip = rpc.parse_is_account_bimodal_response(resp) 27 self.assertEqual(parsed_ip, "10.2.3.4") 28 29 def test_ipv6_no_brackets(self): 30 addr = "fc00:df02:c928:4ef2:f085:8af2:cf1b:6b4" 31 resp = {"IsBimodal": True, 32 "ActivePeerPrivateIPAddr": addr} 33 34 _, parsed_ip = rpc.parse_is_account_bimodal_response(resp) 35 self.assertEqual(parsed_ip, addr) 36 37 def test_ipv6_brackets(self): 38 addr = "fc00:bbb9:a634:aa7d:8cb1:1c1e:d1cb:519c" 39 resp = {"IsBimodal": True, 40 "ActivePeerPrivateIPAddr": "[{0}]".format(addr)} 41 42 _, parsed_ip = rpc.parse_is_account_bimodal_response(resp) 43 self.assertEqual(parsed_ip, addr) 44 45 46 class TestResponseParsing(unittest.TestCase): 47 # These maybe aren't great, but they're more than what we had 48 def test_get_object(self): 49 resp = { 50 "ReadEntsOut": 'a', 51 "Metadata": 'Yg==', # 'b' 52 "FileSize": 'c', 53 "ModificationTime": 'd', 54 "AttrChangeTime": 'e', 55 "IsDir": 'f', 56 "InodeNumber": 'g', 57 "NumWrites": 'h', 58 "LeaseId": 'i', 59 } 60 self.assertEqual(rpc.parse_get_object_response(resp), ( 61 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i')) 62 63 # older proxyfsd didn't send IsDir, so it will not be present in 64 # older responses, but older GET would fail on a directory object 65 # so False is correct if IsDir is not present. 66 del resp["IsDir"] 67 self.assertEqual(rpc.parse_get_object_response(resp), ( 68 'a', 'b', 'c', 'e', False, 'g', 'h', 'i')) 69 70 # Old proxyfsd didn't send AttrChangeTime, but we've always had 71 # ModificationTime available, which is the next-best option 72 del resp["AttrChangeTime"] 73 self.assertEqual(rpc.parse_get_object_response(resp), ( 74 'a', 'b', 'c', 'd', False, 'g', 'h', 'i')) 75 76 def test_coalesce_object(self): 77 resp = { 78 "ModificationTime": 'a', 79 "AttrChangeTime": 'b', 80 "InodeNumber": 'c', 81 "NumWrites": 'd', 82 } 83 self.assertEqual(rpc.parse_coalesce_object_response(resp), ( 84 'b', 'c', 'd')) 85 # Old proxyfsd didn't send AttrChangeTime, but we've always had 86 # ModificationTime available, which is the next-best option 87 del resp["AttrChangeTime"] 88 self.assertEqual(rpc.parse_coalesce_object_response(resp), ( 89 'a', 'c', 'd')) 90 91 def test_put_complete(self): 92 resp = { 93 "ModificationTime": 'a', 94 "AttrChangeTime": 'b', 95 "InodeNumber": 'c', 96 "NumWrites": 'd', 97 } 98 self.assertEqual(rpc.parse_put_complete_response(resp), ( 99 'b', 'c', 'd')) 100 # Old proxyfsd didn't send AttrChangeTime, but we've always had 101 # ModificationTime available, which is the next-best option 102 del resp["AttrChangeTime"] 103 self.assertEqual(rpc.parse_put_complete_response(resp), ( 104 'a', 'c', 'd')) 105 106 def test_mkdir(self): 107 resp = { 108 "ModificationTime": 'a', 109 "AttrChangeTime": 'b', 110 "InodeNumber": 'c', 111 "NumWrites": 'd', 112 } 113 self.assertEqual(rpc.parse_middleware_mkdir_response(resp), ( 114 'b', 'c', 'd')) 115 # Old proxyfsd didn't send AttrChangeTime, but we've always had 116 # ModificationTime available, which is the next-best option 117 del resp["AttrChangeTime"] 118 self.assertEqual(rpc.parse_middleware_mkdir_response(resp), ( 119 'a', 'c', 'd')) 120 121 def test_get_account(self): 122 resp = { 123 "ModificationTime": 'a', 124 "AttrChangeTime": 'b', 125 "AccountEntries": ['c'], 126 } 127 self.assertEqual(rpc.parse_get_account_response(resp), ('b', ['c'])) 128 del resp["AttrChangeTime"] 129 self.assertEqual(rpc.parse_get_account_response(resp), ('a', ['c'])) 130 131 resp = { 132 "ModificationTime": 'a', 133 "AttrChangeTime": 'b', 134 "AccountEntries": None, 135 } 136 self.assertEqual(rpc.parse_get_account_response(resp), ('b', [])) 137 del resp["AttrChangeTime"] 138 self.assertEqual(rpc.parse_get_account_response(resp), ('a', [])) 139 140 def test_head(self): 141 resp = { 142 "Metadata": 'YQ==', # 'a' 143 "ModificationTime": 'b', 144 "AttrChangeTime": 'c', 145 "FileSize": 'd', 146 "IsDir": 'e', 147 "InodeNumber": 'f', 148 "NumWrites": 'g', 149 } 150 self.assertEqual(rpc.parse_head_response(resp), ( 151 'a', 'c', 'd', 'e', 'f', 'g')) 152 153 # Old proxyfsd didn't send AttrChangeTime, but we've always had 154 # ModificationTime available, which is the next-best option 155 del resp["AttrChangeTime"] 156 self.assertEqual(rpc.parse_head_response(resp), ( 157 'a', 'b', 'd', 'e', 'f', 'g'))