Skip to content

Commit

Permalink
Refactor: Added unit tests for Image class
Browse files Browse the repository at this point in the history
This is commit 4 of the refactor to move modules from utils
to classes

- Fixed the unit test for ImageLayer class
- Fixed the unit test for Package class
- Renamed a module in metadata

All the unit tests for the classes will pass but the tests for
the utilities will fail

Signed-off-by: Nisha K <[email protected]>
  • Loading branch information
Nisha K committed Jan 13, 2018
1 parent 8092d0f commit 7185c5c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
9 changes: 7 additions & 2 deletions classes/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SPDX-License-Identifier: BSD-2-Clause
'''

from utils import commands as cmds
from utils import container as cont
from utils import metadata as md
from .image_layer import ImageLayer

Expand Down Expand Up @@ -60,6 +60,10 @@ def config(self):
def layers(self):
return self.__layers

@property
def history(self):
return self.__history

def get_image_option(self):
'''Check to see which value was used to init the image object
Return the value that was used. If neither one was used raise
Expand All @@ -80,14 +84,15 @@ def load_image(self):
'''
try:
option = self.get_image_option()
if cmds.extract_image_metadata(option):
if cont.extract_image_metadata(option):
print('Image extracted')
else:
print('Failed to extract image')
self.__manifest = md.get_image_manifest()
self.__id = md.get_image_id(self.__manifest)
self.__repotags = md.get_image_repotags(self.__manifest)
self.__config = md.get_image_config(self.__manifest)
self.__history = md.get_image_history(self.__config)
layer_paths = md.get_image_layers(self.__manifest)
layer_diffs = md.get_diff_ids(self.__config)
while layer_diffs and layer_paths:
Expand Down
52 changes: 52 additions & 0 deletions tests/test_class_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: BSD-2-Clause
'''

import unittest
import subprocess

from classes.image import Image
from utils.container import docker_command
from utils.container import pull


class TestClassImage(unittest.TestCase):

def setUp(self):
'''Using a specific image here. If this test fails due to the image
not being found anymore, pick a different image to test against
For now use Docker to pull the image from Dockerhub'''
try:
docker_command(pull, 'vmware/photon:1.0')
except subprocess.CalledProcessError as error:
print(error.output)
self.image = Image('vmware/photon:1.0')
# constants for this image
self.id = ('25ebfa5ab0b7aee41b2d4fbdc675a39b13c4a5d69bd5c80a25674b8c18'
'24cbe1')
self.layer = ('00ab136ba3d1354c7ed46f67af6a7b3fbc849bcce56fee20c0bcf7d'
'b7e22f971')
self.no_layers = 1

def tearDown(self):
del self.image

def testInstance(self):
self.assertEqual(self.image.repotag, 'vmware/photon:1.0')
self.assertFalse(self.image.id)
self.assertFalse(self.image.manifest)
self.assertFalse(self.image.repotags)
self.assertFalse(self.image.config)
self.assertFalse(self.image.layers)
self.assertFalse(self.image.history)

def testLoadImage(self):
self.image.load_image()
self.assertEqual(self.image.id, self.id)
self.assertEqual(self.image.layers[0].diff_id, self.layer)
self.assertEqual(len(self.image.layers), self.no_layers)


if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions tests/test_class_image_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from classes.package import Package


class TestClassLayer(unittest.TestCase):
class TestClassImageLayer(unittest.TestCase):

def setUp(self):
self.layer = ImageLayer('123abc', 'path/to/tar')
Expand Down Expand Up @@ -46,7 +46,6 @@ def testToDict(self):
p1 = Package('x')
self.layer.add_package(p1)
a_dict = self.layer.to_dict()
print(a_dict)
self.assertTrue(a_dict['123abc'])
self.assertEqual(len(a_dict['123abc']['packages']), 1)
self.assertEqual(a_dict['123abc']['packages'][0]['name'], 'x')
Expand Down
14 changes: 7 additions & 7 deletions tests/test_class_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ def tearDown(self):

def testInstance(self):
self.assertEqual(self.package.name, 'x')
self.assertEqual(self.package.version, 0.0)
self.assertEqual(self.package.version, '')
self.assertFalse(self.package.src_url)
self.assertFalse(self.package.license)

def testSetters(self):
self.assertRaises(AttributeError, setattr, self.package, 'name', 'y')
self.package.version = 1.0
self.assertEqual(self.package.version, 1.0)
self.package.version = '1.0'
self.assertEqual(self.package.version, '1.0')
self.package.license = 'Apache 2.0'
self.assertEqual(self.package.license, 'Apache 2.0')
self.package.src_url = 'github.com'
self.assertEqual(self.package.src_url, 'github.com')

def testGetters(self):
self.package.version = 1.0
self.package.version = '1.0'
self.package.license = 'Apache 2.0'
self.package.src_url = 'github.com'
self.assertEqual(self.package.name, 'x')
self.assertEqual(self.package.version, 1.0)
self.assertEqual(self.package.version, '1.0')
self.assertEqual(self.package.license, 'Apache 2.0')
self.assertEqual(self.package.src_url, 'github.com')

def testToDict(self):
self.package.version = 1.0
self.package.version = '1.0'
self.package.license = 'Apache 2.0'
self.package.src_url = 'github.com'
a_dict = self.package.to_dict()
self.assertEqual(a_dict['name'], 'x')
self.assertEqual(a_dict['version'], 1.0)
self.assertEqual(a_dict['version'], '1.0')
self.assertEqual(a_dict['license'], 'Apache 2.0')
self.assertEqual(a_dict['src_url'], 'github.com')

Expand Down
16 changes: 6 additions & 10 deletions utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,12 @@ def get_nonempty_history(config):
return history


def get_history(config):
'''Given the image config, return the 'created_by' data. If it doesn't
exist return an empty string'''
history = []
for item in config['history']:
if 'created_by' in item.keys():
history.append(item['created_by'])
else:
history.append('')
return history
def get_image_history(config):
'''If the config has the image history return it. Else return None'''
if 'history' in config.keys():
return config['history']
else:
return None


def get_diff_ids(config):
Expand Down

0 comments on commit 7185c5c

Please sign in to comment.