def test_run_badblocks_outputs_failure(self): drive = { 'NAME': factory.make_name('NAME'), 'PATH': factory.make_name('PATH'), 'MODEL': factory.make_name('MODEL'), 'SERIAL': factory.make_name('SERIAL'), } self.patch(badblocks, 'list_drives').return_value = [drive] output = factory.make_string() self.patch(badblocks, 'check_output').return_value = '4096' mock_popen = self.patch(badblocks, 'Popen') mock_popen.return_value = Popen('echo -n %s; exit 1' % output, stdout=PIPE, shell=True) mock_print = self.patch(badblocks, 'print') self.assertEquals(1, badblocks.run_badblocks()) dashes = '-' * int((80.0 - (2 + len(drive['PATH']))) / 2) header = '%s %s %s' % (dashes, drive['PATH'], dashes) self.assertThat( mock_print, MockCallsMatch(call(header), call('Model: %s' % drive['MODEL']), call('Serial: %s' % drive['SERIAL']), call(), call('Badblocks exited with 1!'), call(), call(output)))
def test_run_badblocks_destructive(self): storage = factory.make_name("storage") blocksize = random.randint(512, 4096) self.patch(badblocks, "get_block_size").return_value = blocksize parallel_blocks = random.randint(1, 50000) self.patch(badblocks, "get_parallel_blocks").return_value = parallel_blocks cmd = [ "sudo", "-n", "badblocks", "-b", str(blocksize), "-c", str(parallel_blocks), "-v", "-f", "-s", "-w", storage, ] mock_popen = self.patch(badblocks, "Popen") proc = mock_popen.return_value proc.communicate.return_value = ( BADBLOCKS_OUTPUT.encode("utf-8"), None, ) proc.returncode = 0 self.assertEquals(1, badblocks.run_badblocks(storage, destructive=True)) self.assertThat(mock_popen, MockCalledOnceWith(cmd, stdout=PIPE, stderr=STDOUT))
def test_run_badblocks_destructive(self): storage = factory.make_name('storage') blocksize = random.randint(512, 4096) self.patch(badblocks, 'get_block_size').return_value = blocksize parallel_blocks = random.randint(1, 50000) self.patch(badblocks, 'get_parallel_blocks').return_value = (parallel_blocks) cmd = [ 'sudo', '-n', 'badblocks', '-b', str(blocksize), '-c', str(parallel_blocks), '-v', '-f', '-w', storage, ] mock_popen = self.patch(badblocks, "Popen") proc = mock_popen.return_value proc.communicate.return_value = (BADBLOCKS_OUTPUT.encode('utf-8'), None) proc.returncode = 0 self.assertEquals(0, badblocks.run_badblocks(storage, destructive=True)) self.assertThat(mock_popen, MockCalledOnceWith(cmd, stdout=PIPE, stderr=STDOUT))
def test_run_badblocks_nondestructive_and_writes_results_file(self): storage = factory.make_name('storage') blocksize = random.randint(512, 4096) self.patch(badblocks, 'get_block_size').return_value = blocksize parallel_blocks = random.randint(1, 50000) self.patch(badblocks, 'get_parallel_blocks').return_value = (parallel_blocks) self.patch(os, "environ", {"RESULT_PATH": factory.make_name()}) cmd = [ 'sudo', '-n', 'badblocks', '-b', str(blocksize), '-c', str(parallel_blocks), '-v', '-f', '-n', storage ] mock_popen = self.patch(badblocks, "Popen") proc = mock_popen.return_value proc.communicate.return_value = (BADBLOCKS_OUTPUT.encode('utf-8'), None) proc.returncode = 0 mock_open = self.patch(badblocks, "open") mock_open.return_value = io.StringIO() mock_yaml_safe_dump = self.patch(yaml, "safe_dump") results = { 'results': { 'badblocks': BADBLOCKS, } } self.assertEquals(0, badblocks.run_badblocks(storage)) self.assertThat(mock_popen, MockCalledOnceWith(cmd, stdout=PIPE, stderr=STDOUT)) self.assertThat(mock_open, MockCalledOnceWith(ANY, "w")) self.assertThat(mock_yaml_safe_dump, MockCalledOnceWith(results, mock_open.return_value))
def test_run_badblocks_nondestructive_and_writes_results_file(self): storage = factory.make_name("storage") blocksize = random.randint(512, 4096) self.patch(badblocks, "get_block_size").return_value = blocksize parallel_blocks = random.randint(1, 50000) self.patch(badblocks, "get_parallel_blocks").return_value = parallel_blocks self.patch(os, "environ", {"RESULT_PATH": factory.make_name()}) cmd = [ "sudo", "-n", "badblocks", "-b", str(blocksize), "-c", str(parallel_blocks), "-v", "-f", "-s", "-n", storage, ] mock_popen = self.patch(badblocks, "Popen") proc = mock_popen.return_value proc.communicate.return_value = ( BADBLOCKS_OUTPUT.encode("utf-8"), None, ) proc.returncode = 0 mock_open = self.patch(badblocks, "open") mock_open.return_value = io.StringIO() mock_yaml_safe_dump = self.patch(yaml, "safe_dump") results = { "results": { "badblocks": BADBLOCKS, "read_errors": READ_ERRORS, "write_errors": WRITE_ERRORS, "comparison_errors": COMPARISON_ERRORS, } } self.assertEquals(1, badblocks.run_badblocks(storage)) self.assertThat(mock_popen, MockCalledOnceWith(cmd, stdout=PIPE, stderr=STDOUT)) self.assertThat(mock_open, MockCalledOnceWith(ANY, "w")) self.assertThat( mock_yaml_safe_dump, MockCalledOnceWith(results, mock_open.return_value), )
def test_run_badblocks_exits_if_no_regex_match_found(self): storage = factory.make_name('storage') blocksize = random.randint(512, 4096) self.patch(badblocks, 'get_block_size').return_value = blocksize parallel_blocks = random.randint(1, 50000) self.patch(badblocks, 'get_parallel_blocks').return_value = (parallel_blocks) self.patch(os, "environ", {"RESULT_PATH": factory.make_name()}) mock_popen = self.patch(badblocks, "Popen") proc = mock_popen.return_value proc.communicate.return_value = (BADBLOCKS_OUTPUT.encode('utf-8'), None) proc.returncode = 0 mock_re_search = self.patch(re, "search") mock_re_search.return_value = None self.assertEquals(0, badblocks.run_badblocks(storage)) self.assertThat(mock_re_search, MockCalledOnce())