2021-02-10 14:03:27 +01:00
|
|
|
# Copyright 2021 Google 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.
|
2021-04-06 14:45:41 +02:00
|
|
|
import gzip
|
2021-02-10 14:03:27 +01:00
|
|
|
import unittest
|
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
# pylint: disable=import-error
|
2021-02-10 14:03:27 +01:00
|
|
|
from pyfakefs import fake_filesystem_unittest
|
|
|
|
|
|
|
|
import bloaty_merger
|
|
|
|
import file_sections_pb2
|
|
|
|
|
|
|
|
|
|
|
|
class BloatyMergerTestCase(fake_filesystem_unittest.TestCase):
|
2021-08-18 19:46:46 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.setUpPyfakefs()
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
def test_parse_csv(self):
|
|
|
|
csv_content = "sections,vmsize,filesize\nsection1,2,3\n"
|
|
|
|
self.fs.create_file("file1.bloaty.csv", contents=csv_content)
|
|
|
|
pb = bloaty_merger.parse_csv("file1.bloaty.csv")
|
|
|
|
self.assertEqual(pb.path, "file1")
|
|
|
|
self.assertEqual(len(pb.sections), 1)
|
|
|
|
s = pb.sections[0]
|
|
|
|
self.assertEqual(s.name, "section1")
|
|
|
|
self.assertEqual(s.vm_size, 2)
|
|
|
|
self.assertEqual(s.file_size, 3)
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
def test_missing_file(self):
|
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
bloaty_merger.parse_csv("missing.bloaty.csv")
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
def test_malformed_csv(self):
|
|
|
|
csv_content = "header1,heaVder2,header3\n4,5,6\n"
|
|
|
|
self.fs.create_file("file1.bloaty.csv", contents=csv_content)
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
bloaty_merger.parse_csv("file1.bloaty.csv")
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
def test_create_file_metrics(self):
|
|
|
|
file_list = "file1.bloaty.csv file2.bloaty.csv"
|
|
|
|
file1_content = "sections,vmsize,filesize\nsection1,2,3\nsection2,7,8"
|
|
|
|
file2_content = "sections,vmsize,filesize\nsection1,4,5\n"
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
self.fs.create_file("files.lst", contents=file_list)
|
|
|
|
self.fs.create_file("file1.bloaty.csv", contents=file1_content)
|
|
|
|
self.fs.create_file("file2.bloaty.csv", contents=file2_content)
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
bloaty_merger.create_file_size_metrics("files.lst", "output.pb.gz")
|
2021-02-10 14:03:27 +01:00
|
|
|
|
2021-08-18 19:46:46 +02:00
|
|
|
metrics = file_sections_pb2.FileSizeMetrics()
|
|
|
|
with gzip.open("output.pb.gz", "rb") as output:
|
|
|
|
metrics.ParseFromString(output.read())
|
2021-02-10 14:03:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-08-18 19:46:46 +02:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(BloatyMergerTestCase)
|
|
|
|
unittest.TextTestRunner(verbosity=2).run(suite)
|