~verterok/landscape-charm/support-postgresql-charm-v2-protocol

« back to all changes in this revision

Viewing changes to tests/basic/test_service.py

  • Committer: Christopher Glass
  • Date: 2015-07-09 09:00:17 UTC
  • mto: This revision was merged to the branch mainline in revision 326.
  • Revision ID: christopher.glass@canonical.com-20150709090017-q470u36tw766w3a1
This refactors cron script helper functions into the Environment instead,
to make it part of the general set of helpers so it can be reused elsewhere.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
FIXME: revert to using ssh -q, stderr=STDOUT instead of 2>&1, stderr=PIPE once
5
5
       lp:1281577 is addressed.
6
6
"""
7
 
from subprocess import check_output, CalledProcessError, PIPE, STDOUT
 
7
from subprocess import check_output, PIPE
8
8
 
9
9
from helpers import IntegrationTest
10
10
from layers import OneLandscapeUnitLayer, OneLandscapeUnitNoCronLayer
139
139
    """
140
140
    layer = OneLandscapeUnitNoCronLayer
141
141
 
142
 
    def _sanitize_ssh_output(self, output,
143
 
                             remove_text=["sudo: unable to resolve",
144
 
                                          "Warning: Permanently added",
145
 
                                          "Connection to"]):
146
 
        """Strip some common warning messages from ssh output.
147
 
 
148
 
        @param output: output to sanitize
149
 
        @param remove_text: list of text that, if found at the beginning of
150
 
                            the a output line, will have that line removed
151
 
                            entirely.
152
 
        """
153
 
        new_output = []
154
 
        for line in output.split("\n"):
155
 
            if any(line.startswith(remove) for remove in remove_text):
156
 
                continue
157
 
            new_output.append(line)
158
 
        return "\n".join(new_output)
159
 
 
160
 
    def _run_cron(self, script):
161
 
        status = 0
162
 
        cmd = ["juju", "ssh", self.layer.cron_unit, "sudo", "-u landscape",
163
 
               script, "2>&1"]
164
 
        try:
165
 
            # The sanitize is a workaround for lp:1328269
166
 
            output = self._sanitize_ssh_output(
167
 
                check_output(
168
 
                    cmd, stderr=STDOUT, stdin=PIPE).decode("utf-8").strip())
169
 
        except CalledProcessError as e:
170
 
            output = e.output.decode("utf-8").strip()
171
 
            status = e.returncode
172
 
        # these jobs currently don't set their exit status to non-zero
173
 
        # if they fail, they just print things to stdout/stderr
174
 
        return (output, status)
175
 
 
176
142
    def test_maintenance_cron(self):
177
143
        """Verify that the maintenance cron job runs without errors."""
178
144
        script = "/opt/canonical/landscape/scripts/maintenance.sh"
179
 
        output, status = self._run_cron(script)
 
145
        output, status = self.environment.run_script_on_cron_unit(
 
146
            script, self.layer)
180
147
        self.assertEqual(output, "")
181
148
        self.assertEqual(status, 0)
182
149
 
183
150
    def test_update_security_db_cron(self):
184
151
        """Verify that the update_security_db cron job runs without errors."""
185
152
        script = "/opt/canonical/landscape/scripts/update_security_db.sh"
186
 
        output, status = self._run_cron(script)
 
153
        output, status = self.environment.run_script_on_cron_unit(
 
154
            script, self.layer)
187
155
        self.assertEqual(output, "")
188
156
        self.assertEqual(status, 0)
189
157
 
190
158
    def test_update_alerts_cron(self):
191
159
        """Verify that the update_alerts cron job runs without errors."""
192
160
        script = "/opt/canonical/landscape/scripts/update_alerts.sh"
193
 
        output, status = self._run_cron(script)
 
161
        output, status = self.environment.run_script_on_cron_unit(
 
162
            script, self.layer)
194
163
        self.assertEqual(output, "")
195
164
        self.assertEqual(status, 0)
196
165
 
204
173
        cmd = ["juju", "run", "--unit", self.layer.cron_unit, find_cmd]
205
174
        script = check_output(cmd, stderr=PIPE).decode("utf-8").strip()
206
175
 
207
 
        output, status = self._run_cron(script)
 
176
        output, status = self.environment.run_script_on_cron_unit(
 
177
            script, self.layer)
208
178
        self.assertEqual(output, "")
209
179
        self.assertEqual(status, 0)
210
180
 
211
181
    def test_process_alerts_cron(self):
212
182
        """Verify that the process_alerts cron job runs without errors."""
213
183
        script = "/opt/canonical/landscape/scripts/process_alerts.sh"
214
 
        output, status = self._run_cron(script)
 
184
        output, status = self.environment.run_script_on_cron_unit(
 
185
            script, self.layer)
215
186
        self.assertEqual(output, "")
216
187
        self.assertEqual(status, 0)
217
188
 
218
189
    def test_hash_id_databases_cron(self):
219
190
        """Verify that the hash_id_databases cron job runs without errors."""
220
191
        script = "/opt/canonical/landscape/scripts/hash_id_databases.sh"
221
 
        output, status = self._run_cron(script)
 
192
        output, status = self.environment.run_script_on_cron_unit(
 
193
            script, self.layer)
222
194
        self.assertEqual(output, "")
223
195
        self.assertEqual(status, 0)
224
196
 
225
197
    def test_meta_releases_cron(self):
226
198
        """Verify that the meta_releases cron job runs without errors."""
227
199
        script = "/opt/canonical/landscape/scripts/meta_releases.sh"
228
 
        output, status = self._run_cron(script)
 
200
        output, status = self.environment.run_script_on_cron_unit(
 
201
            script, self.layer)
229
202
        self.assertEqual(output, "")
230
203
        self.assertEqual(status, 0)
231
204
 
232
205
    def test_sync_lds_releases_cron(self):
233
206
        """Verify that the sync_lds_releases cron job runs without errors."""
234
207
        script = "/opt/canonical/landscape/scripts/sync_lds_releases.sh"
235
 
        output, status = self._run_cron(script)
 
208
        output, status = self.environment.run_script_on_cron_unit(
 
209
            script, self.layer)
236
210
        self.assertEqual(output, "")
237
211
        self.assertEqual(status, 0)
238
212