Add function to check file size

Change-Id: I6ef32b1fc34fcf9fd8df87e6212e858a07733c3f
This commit is contained in:
Federico Ressi 2021-08-26 09:08:00 +02:00
parent 94cd681041
commit b661b8f91e
3 changed files with 96 additions and 2 deletions

View File

@ -99,4 +99,5 @@ SSHShellProcessFixture = _ssh.SSHShellProcessFixture
get_uptime = _uptime.get_uptime
assert_file_size = _wc.assert_file_size
get_file_size = _wc.get_file_size

View File

@ -15,16 +15,27 @@
# under the License.
from __future__ import absolute_import
import tobiko
from tobiko.shell import sh
from tobiko.shell import ssh
def get_file_size(filename: str,
def get_file_size(file_name: str,
ssh_client: ssh.SSHClientType = None,
sudo: bool = None) \
-> int:
output = sh.execute(f'wc -c "{filename}"',
output = sh.execute(f'wc -c "{file_name}"',
ssh_client=ssh_client,
sudo=sudo).stdout.strip()
size, _ = output.split(' ', 1)
return int(size)
def assert_file_size(file_size: int,
file_name: str,
ssh_client: ssh.SSHClientType = None,
sudo: bool = None):
size = get_file_size(file_name=file_name,
ssh_client=ssh_client,
sudo=sudo)
tobiko.get_test_case().assertEqual(file_size, size)

View File

@ -0,0 +1,82 @@
# Copyright (c) 2021 Red Hat, Inc.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import os
import testtools
from tobiko.shell import sh
class WcTest(testtools.TestCase):
def test_get_file_size(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
sh.execute(f'echo "{self.id()}" > "{file_name}"')
file_size = sh.get_file_size(file_name)
self.assertEqual(len(self.id()) + 1, file_size)
def test_get_file_size_when_empty(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
sh.execute(f'touch "{file_name}"')
file_size = sh.get_file_size(file_name)
self.assertEqual(0, file_size)
def test_get_file_size_when_not_found(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
ex = self.assertRaises(sh.ShellCommandFailed,
sh.get_file_size,
file_name)
self.assertEqual(1, ex.exit_status)
self.assertEqual(
f"wc: {file_name}: No such file or directory\n",
ex.stderr)
def test_assert_file_size(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
sh.execute(f'echo "{self.id()}" > "{file_name}"')
sh.assert_file_size(len(self.id()) + 1, file_name)
def test_assert_file_size_when_empty(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
sh.execute(f'touch "{file_name}"')
sh.assert_file_size(0, file_name)
def test_assert_file_size_when_not_found(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
ex = self.assertRaises(sh.ShellCommandFailed,
sh.assert_file_size,
1, file_name)
self.assertEqual(1, ex.exit_status)
self.assertEqual(
f"wc: {file_name}: No such file or directory\n",
ex.stderr)
def test_assert_file_size_when_mismatch(self):
file_name = os.path.join(sh.make_temp_dir(),
self.id())
sh.execute(f'echo "{self.id()}" > "{file_name}"')
ex = self.assertRaises(testtools.matchers.MismatchError,
sh.assert_file_size,
1, file_name)
self.assertEqual(f'1 != {len(self.id()) + 1}', str(ex))