Repository: StackStorm/st2

Issue:
`st2 pack search` is broken in 2.7
EDIT: Added full debug output
```shell
root@EWC:~/.ssh# st2 --debug pack search clicrud
2018-04-13 17:12:20,760  DEBUG - Using cached token from file "/root/.st2/token-st2admin"
ERROR: local variable 'response' referenced before assignment

CLI settings:
----------------
Config file path: /root/.st2/config
Client settings:
----------------
ST2_BASE_URL: http://127.0.0.1
ST2_AUTH_URL: http://127.0.0.1:9100
ST2_API_URL: http://127.0.0.1:9101/v1
ST2_STREAM_URL: http://127.0.0.1:9102/v1
ST2_AUTH_TOKEN: e67164f509424e70a6244e97059cf76d

Proxy settings:
---------------
HTTP_PROXY:
HTTPS_PROXY:

Traceback (most recent call last):
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/shell.py", line 343, in run
    func(args)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/commands/resource.py", line 259, in run_and_print
    instances = self.run(args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/commands/resource.py", line 47, in decorate
    return func(*args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/commands/pack.py", line 359, in run
    return self.manager.search(args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/models/core.py", line 40, in decorate
    return func(*args, **kwargs)
  File "/opt/stackstorm/st2/local/lib/python2.7/site-packages/st2client/models/core.py", line 511, in search
    if response.status_code != http_client.OK:
UnboundLocalError: local variable 'response' referenced before assignment```


Discussion:

Resolving patch:
diff --git a/st2client/st2client/models/core.py b/st2client/st2client/models/core.py
--- a/st2client/st2client/models/core.py
+++ b/st2client/st2client/models/core.py
@@ -506,7 +506,7 @@ def search(self, args, ignore_errors=False, **kwargs):
         else:
             payload = {'pack': args.pack}
 
-            response = self.client.post(url, payload, **kwargs)
+        response = self.client.post(url, payload, **kwargs)
 
         if response.status_code != http_client.OK:
             if ignore_errors:


Test patch:
diff --git a/st2api/tests/unit/controllers/v1/test_packs.py b/st2api/tests/unit/controllers/v1/test_packs.py
--- a/st2api/tests/unit/controllers/v1/test_packs.py
+++ b/st2api/tests/unit/controllers/v1/test_packs.py
@@ -180,8 +180,7 @@ def test_uninstall(self, _handle_schedule_execution):
 
     @mock.patch.object(pack_service, 'fetch_pack_index',
                        mock.MagicMock(return_value=(PACK_INDEX, {})))
-    def test_search(self):
-
+    def test_search_with_query(self):
         test_scenarios = [
             {
                 'input': {'query': 'test'},
@@ -234,11 +233,26 @@ def test_search(self):
                 'expected_result': []
             }
         ]
+
         for scenario in test_scenarios:
             resp = self.app.post_json('/v1/packs/index/search', scenario['input'])
             self.assertEqual(resp.status_int, scenario['expected_code'])
             self.assertEqual(resp.json, scenario['expected_result'])
 
+    @mock.patch.object(pack_service, 'get_pack_from_index',
+                       mock.MagicMock(return_value=PACK_INDEX['test']))
+    def test_search_with_pack_has_result(self):
+        resp = self.app.post_json('/v1/packs/index/search', {'pack': 'st2-dev'})
+        self.assertEqual(resp.status_int, 200)
+        self.assertEqual(resp.json, PACK_INDEX['test'])
+
+    @mock.patch.object(pack_service, 'get_pack_from_index',
+                       mock.MagicMock(return_value=None))
+    def test_search_with_pack_no_result(self):
+        resp = self.app.post_json('/v1/packs/index/search', {'pack': 'not-found'})
+        self.assertEqual(resp.status_int, 200)
+        self.assertEqual(resp.json, [])
+
     @mock.patch.object(pack_service, 'fetch_pack_index',
                        mock.MagicMock(return_value=(PACK_INDEX, {})))
     def test_show(self):