From b4c1d73ad56e0705d1c5dd1fc4f1d89b09fceff1 Mon Sep 17 00:00:00 2001
From: Christian Schwede <cschwede@redhat.com>
Date: Mon, 1 Jun 2015 06:50:33 +0000
Subject: [PATCH] Make swift-recon compatible for servers without storage
 policies

Swift recon introduced a new key for storage policies, and the CLI expected this
key in the server response. However, if one updates the CLI but not yet the
server an exception will be raised, because there is no default value and no
check if the key is included in the response.

This change checks if the policies key is included in the response and updates
one test to ensure backward compability.

Closes-Bug: 1453599

Change-Id: I7c7a90f9933bec2ab45595df9dc600a6cba65666
---
 swift/cli/recon.py          |  9 ++++-----
 test/unit/cli/test_recon.py | 13 ++++++++-----
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/swift/cli/recon.py b/swift/cli/recon.py
index a0fcdf7835..f57f75c22a 100755
--- a/swift/cli/recon.py
+++ b/swift/cli/recon.py
@@ -770,11 +770,10 @@ class SwiftRecon(object):
                 objq[url] = response['objects']
                 conq[url] = response['containers']
                 acctq[url] = response['accounts']
-                if response['policies']:
-                    for key in response['policies']:
-                        pkey = "objects_%s" % key
-                        stats.setdefault(pkey, {})
-                        stats[pkey][url] = response['policies'][key]['objects']
+                for key in response.get('policies', {}):
+                    pkey = "objects_%s" % key
+                    stats.setdefault(pkey, {})
+                    stats[pkey][url] = response['policies'][key]['objects']
         stats.update({"objects": objq, "containers": conq, "accounts": acctq})
         for item in stats:
             if len(stats[item]) > 0:
diff --git a/test/unit/cli/test_recon.py b/test/unit/cli/test_recon.py
index 0b6ffd7a33..dd53ae9d54 100644
--- a/test/unit/cli/test_recon.py
+++ b/test/unit/cli/test_recon.py
@@ -246,7 +246,8 @@ class TestRecon(unittest.TestCase):
 
     def test_quarantine_check(self):
         hosts = [('127.0.0.1', 6010), ('127.0.0.1', 6020),
-                 ('127.0.0.1', 6030), ('127.0.0.1', 6040)]
+                 ('127.0.0.1', 6030), ('127.0.0.1', 6040),
+                 ('127.0.0.1', 6050)]
         # sample json response from http://<host>:<port>/recon/quarantined
         responses = {6010: {'accounts': 0, 'containers': 0, 'objects': 1,
                             'policies': {'0': {'objects': 0},
@@ -259,13 +260,15 @@ class TestRecon(unittest.TestCase):
                                          '1': {'objects': 3}}},
                      6040: {'accounts': 3, 'containers': 3, 'objects': 7,
                             'policies': {'0': {'objects': 3},
-                                         '1': {'objects': 4}}}}
+                                         '1': {'objects': 4}}},
+                     # A server without storage policies enabled
+                     6050: {'accounts': 0, 'containers': 0, 'objects': 4}}
         # <low> <high> <avg> <total> <Failed> <no_result> <reported>
         expected = {'objects_0': (0, 3, 1.5, 6, 0.0, 0, 4),
                     'objects_1': (1, 4, 2.5, 10, 0.0, 0, 4),
-                    'objects': (1, 7, 4.0, 16, 0.0, 0, 4),
-                    'accounts': (0, 3, 1.5, 6, 0.0, 0, 4),
-                    'containers': (0, 3, 1.5, 6, 0.0, 0, 4)}
+                    'objects': (1, 7, 4.0, 20, 0.0, 0, 5),
+                    'accounts': (0, 3, 1.2, 6, 0.0, 0, 5),
+                    'containers': (0, 3, 1.2, 6, 0.0, 0, 5)}
 
         def mock_scout_quarantine(app, host):
             url = 'http://%s:%s/recon/quarantined' % host