Repository: fedora-infra/bodhi

Issue:
bodhi-check-policies only operates on unpushed updates
I noticed today when working on #1514 that the update query that ```bodhi-check-policies``` performs only selects [unpushed updates](https://github.com/fedora-infra/bodhi/blob/3.1.0/bodhi/server/scripts/check_policies.py#L40-L42):

```
updates = models.Update.query.filter(models.Update.pushed == false())\
        .filter(models.Update.status.in_(
                [models.UpdateStatus.pending, models.UpdateStatus.testing]))
```

I think this is probably not what we want, as [testing updates are marked pushed](https://github.com/fedora-infra/bodhi/blob/3b655f2/bodhi/server/consumers/masher.py#L845-L857). This means we will only consult Greenwave about pending updates.


Discussion:
Interesting!  Good catch.

Resolving patch:
diff --git a/bodhi/server/scripts/check_policies.py b/bodhi/server/scripts/check_policies.py
--- a/bodhi/server/scripts/check_policies.py
+++ b/bodhi/server/scripts/check_policies.py
@@ -24,7 +24,6 @@
 messages in the message bus yet.
 """
 import click
-from sqlalchemy.sql.expression import false
 
 from bodhi.server import config, initialize_db, models, Session
 
@@ -36,9 +35,8 @@ def check():
     initialize_db(config.config)
     session = Session()
 
-    updates = models.Update.query.filter(models.Update.pushed == false())\
-        .filter(models.Update.status.in_(
-                [models.UpdateStatus.pending, models.UpdateStatus.testing]))
+    updates = models.Update.query.filter(models.Update.status.in_(
+        [models.UpdateStatus.pending, models.UpdateStatus.testing]))
     for update in updates:
         try:
             update.update_test_gating_status()


Test patch:
diff --git a/bodhi/tests/server/scripts/test_check_policies.py b/bodhi/tests/server/scripts/test_check_policies.py
--- a/bodhi/tests/server/scripts/test_check_policies.py
+++ b/bodhi/tests/server/scripts/test_check_policies.py
@@ -160,6 +160,37 @@ def test_no_policies_enforced(self):
         mock_greenwave.assert_called_once_with(config['greenwave_api_url'] + '/decision',
                                                expected_query)
 
+    @patch.dict(config, [('greenwave_api_url', 'http://domain.local')])
+    def test_pushed_update(self):
+        """Assert that check() operates on pushed updates."""
+        runner = testing.CliRunner()
+        update = self.db.query(models.Update).all()[0]
+        update.status = models.UpdateStatus.testing
+        update.pushed = True
+        self.db.commit()
+        with patch('bodhi.server.models.util.greenwave_api_post') as mock_greenwave:
+            greenwave_response = {
+                'policies_satisfied': False,
+                'summary': 'it broke',
+                'applicable_policies': ['bodhi-unrestricted'],
+            }
+            mock_greenwave.return_value = greenwave_response
+
+            result = runner.invoke(check_policies.check, [])
+
+        self.assertEqual(result.exit_code, 0)
+        update = self.db.query(models.Update).filter(models.Update.id == update.id).one()
+        self.assertEqual(update.test_gating_status, models.TestGatingStatus.failed)
+        self.assertEqual(update.greenwave_summary_string, 'it broke')
+        expected_query = {
+            'product_version': 'fedora-17', 'decision_context': 'bodhi_update_push_stable',
+            'subject': [{'item': u'bodhi-2.0-1.fc17', 'type': 'koji_build'},
+                        {'original_spec_nvr': u'bodhi-2.0-1.fc17'},
+                        {'item': u'FEDORA-{}-a3bbe1a8f2'.format(datetime.datetime.utcnow().year),
+                         'type': 'bodhi_update'}]}
+        mock_greenwave.assert_called_once_with(config['greenwave_api_url'] + '/decision',
+                                               expected_query)
+
     @patch.dict(config, [('greenwave_api_url', 'http://domain.local')])
     def test_unrestricted_policy(self):
         """Assert correct behavior when an unrestricted policy is applied"""