Skip to content

Commit

Permalink
Beautify code
Browse files Browse the repository at this point in the history
(cherry picked from commit a403d5f)
(cherry picked from commit 509e522)
Signed-off-by: Volker Theile <votdev@gmx.de>
  • Loading branch information
votdev committed Nov 4, 2018
1 parent 30e253b commit 5b5ce95
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ def test_argparse_is_json_fail(self):
self.command_helper.argparse_is_json("abc")
self.assertEqual(str(ctx.exception), "No valid JSON.")

@mock.patch("sys.stdin.read", return_value='{"foo": "bar"}')
def test_argparse_is_json_stdin(self, mock_read):
self.assertDictEqual({
"foo": "bar"
}, self.command_helper.argparse_is_json_stdin("-"))

def test_argparse_is_datamodel_id_fail(self):
with self.assertRaises(Exception) as ctx:
self.command_helper.argparse_is_datamodel_id("xyz")
self.assertEqual(str(ctx.exception), "No valid data model ID.")

def test_argparse_is_datamodel_id_1(self):
self.assertEqual(
self.command_helper.argparse_is_datamodel_id("conf"), "conf"
)

@mock.patch("openmediavault.config.Datamodel")
def test_argparse_is_datamodel_id_2(self, mock_datamodel):
self.assertEqual(
self.command_helper.argparse_is_datamodel_id("conf.service.ssh"),
"conf.service.ssh"
)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
#
# This file is part of OpenMediaVault.
#
# @license http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2018 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.
import unittest
import mock
import openmediavault.mkrrdgraph


class MkRrdGraphTestCase(unittest.TestCase):
@mock.patch("os.system")
def test_call_rrdtool_graph(self, mock_system):
args = []
# yapf: disable
args.append('--slope-mode')
args.extend(['--lower-limit', '0'])
args.extend(['--units-exponent', '0'])
args.append('GPRINT:lmax:MAX:"%4.2lf Max"')
args.append('GPRINT:lavg:LAST:"%4.2lf Last\l"')
# yapf: enable
openmediavault.mkrrdgraph.call_rrdtool_graph(args)
mock_system.assert_called_with(
"rrdtool graph --slope-mode "
"--lower-limit 0 --units-exponent 0 "
"GPRINT:lmax:MAX:\"%4.2lf Max\" "
"GPRINT:lavg:LAST:\"%4.2lf Last\\l\" "
">/dev/null"
)

@mock.patch("shutil.copyfile")
def test_copy_placeholder_image(self, mock_copyfile):
openmediavault.setenv("OMV_RRDGRAPH_ERROR_IMAGE", "foo.png")
openmediavault.mkrrdgraph.copy_placeholder_image("bar.png")
mock_copyfile.assert_called_with("foo.png", "bar.png")

@mock.patch("os.path.exists", return_value=True)
@mock.patch("builtins.open", new_callable=mock.mock_open)
def test_load_collectd_config(self, mock_open, mock_exists):
# mock_open doesn't properly handle iterating over the open file
# with for "line in file".
# https://bugs.python.org/issue32933
mock_open.return_value.__iter__.return_value = [
"LoadPlugin interface", "<Plugin interface>", " Interface 'ens6'",
" Interface \"ens7\"", " IgnoreSelected false", "</Plugin>"
]
openmediavault.setenv("OMV_COLLECTD_CONFIG_DIR", "/x/y/z")
result = openmediavault.mkrrdgraph.load_collectd_config(
"interface", "Interface"
)
mock_exists.assert_called_with("/x/y/z/interface.conf")
mock_open.assert_called_with("/x/y/z/interface.conf", "r")
self.assertListEqual(result, ["ens6", "ens7"])


if __name__ == "__main__":
unittest.main()

0 comments on commit 5b5ce95

Please sign in to comment.