nodepool/nodepool/tests/test_builder.py

86 lines
3.2 KiB
Python

# Copyright (C) 2015 Hewlett-Packard Development Company, L.P.
#
# 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.
import os
import fixtures
from nodepool import builder, exceptions, tests
class TestNodepoolBuilderDibImage(tests.BaseTestCase):
def test_from_path(self):
image = builder.DibImageFile.from_path(
'/foo/bar/myid1234.qcow2')
self.assertEqual(image.image_id, 'myid1234')
self.assertEqual(image.extension, 'qcow2')
def test_from_image_id(self):
tempdir = fixtures.TempDir()
self.useFixture(tempdir)
image_path = os.path.join(tempdir.path, 'myid1234.qcow2')
open(image_path, 'w')
images = builder.DibImageFile.from_image_id(tempdir.path, 'myid1234')
self.assertEqual(len(images), 1)
image = images[0]
self.assertEqual(image.image_id, 'myid1234')
self.assertEqual(image.extension, 'qcow2')
def test_from_id_multiple(self):
tempdir = fixtures.TempDir()
self.useFixture(tempdir)
image_path_1 = os.path.join(tempdir.path, 'myid1234.qcow2')
image_path_2 = os.path.join(tempdir.path, 'myid1234.raw')
open(image_path_1, 'w')
open(image_path_2, 'w')
images = builder.DibImageFile.from_image_id(tempdir.path, 'myid1234')
images = sorted(images, key=lambda x: x.extension)
self.assertEqual(len(images), 2)
self.assertEqual(images[0].extension, 'qcow2')
self.assertEqual(images[1].extension, 'raw')
def test_from_images_dir(self):
tempdir = fixtures.TempDir()
self.useFixture(tempdir)
image_path_1 = os.path.join(tempdir.path, 'myid1234.qcow2')
image_path_2 = os.path.join(tempdir.path, 'myid1234.raw')
open(image_path_1, 'w')
open(image_path_2, 'w')
images = builder.DibImageFile.from_images_dir(tempdir.path)
images = sorted(images, key=lambda x: x.extension)
self.assertEqual(len(images), 2)
self.assertEqual(images[0].image_id, 'myid1234')
self.assertEqual(images[0].extension, 'qcow2')
self.assertEqual(images[1].image_id, 'myid1234')
self.assertEqual(images[1].extension, 'raw')
def test_to_path(self):
image = builder.DibImageFile('myid1234', 'qcow2')
self.assertEqual(image.to_path('/imagedir'),
'/imagedir/myid1234.qcow2')
self.assertEqual(image.to_path('/imagedir/'),
'/imagedir/myid1234.qcow2')
self.assertEqual(image.to_path('/imagedir/', False),
'/imagedir/myid1234')
image = builder.DibImageFile('myid1234')
self.assertRaises(exceptions.BuilderError, image.to_path, '/imagedir/')