Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/integration_tests/modules/test_combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
me: "127.0.0.1"
runcmd:
- echo 'hello world' > /var/tmp/runcmd_output

- #
- logger "My test log"
snap:
squashfuse_in_container: true
Expand Down
62 changes: 62 additions & 0 deletions tests/integration_tests/modules/test_growpart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
import pytest
import pathlib
import json
from uuid import uuid4
from pycloudlib.lxd.instance import LXDInstance

from cloudinit.subp import subp
from tests.integration_tests.instances import IntegrationInstance

DISK_PATH = '/tmp/test_disk_setup_{}'.format(uuid4())


def setup_and_mount_lxd_disk(instance: LXDInstance):
subp('lxc config device add {} test-disk-setup-disk disk source={}'.format(
instance.name, DISK_PATH).split())


@pytest.fixture(scope='class', autouse=True)
def create_disk():
"""Create 16M sparse file"""
pathlib.Path(DISK_PATH).touch()
os.truncate(DISK_PATH, 1 << 24)
yield
os.remove(DISK_PATH)


# Create undersized partition in bootcmd
ALIAS_USERDATA = """\
#cloud-config
bootcmd:
- parted /dev/sdb --script \
mklabel gpt \
mkpart primary 0 1MiB
- parted /dev/sdb --script print
growpart:
devices:
- "/"
- "/dev/sdb1"
runcmd:
- parted /dev/sdb --script print
"""


@pytest.mark.user_data(ALIAS_USERDATA)
@pytest.mark.lxd_setup.with_args(setup_and_mount_lxd_disk)
@pytest.mark.ubuntu
@pytest.mark.lxd_vm
class TestGrowPart:
"""Test growpart"""

def test_grow_part(self, client: IntegrationInstance):
"""Verify """
log = client.read_from_file('/var/log/cloud-init.log')
assert ("cc_growpart.py[INFO]: '/dev/sdb1' resized:"
" changed (/dev/sdb, 1) from") in log
Comment thread
holmanb marked this conversation as resolved.

lsblk = json.loads(client.execute('lsblk --json'))
sdb = [x for x in lsblk['blockdevices'] if x['name'] == 'sdb'][0]
assert len(sdb['children']) == 1
assert sdb['children'][0]['name'] == 'sdb1'
assert sdb['size'] == '16M'
69 changes: 68 additions & 1 deletion tests/unittests/test_handler/test_handler_growpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
from cloudinit import cloud
from cloudinit.config import cc_growpart
from cloudinit import subp
from cloudinit import temp_utils

from cloudinit.tests.helpers import TestCase

import errno
import logging
import os
import shutil
import re
import unittest
from contextlib import ExitStack
from unittest import mock
import stat

# growpart:
# mode: auto # off, on, auto, 'growpart'
Expand Down Expand Up @@ -58,6 +61,28 @@
"""


class Dir:
'''Stub object'''
def __init__(self, name):
self.name = name
self.st_mode = name

def is_dir(self, *args, **kwargs):
return True

def stat(self, *args, **kwargs):
return self


class Scanner:
'''Stub object'''
def __enter__(self):
return (Dir(''), Dir(''),)

def __exit__(self, *args):
pass


class TestDisabled(unittest.TestCase):
def setUp(self):
super(TestDisabled, self).setUp()
Expand Down Expand Up @@ -91,6 +116,13 @@ def setUp(self):

self.cloud_init = None
self.handle = cc_growpart.handle
self.tmppath = '/tmp/cloudinit-test-file'
self.tmpdir = os.scandir('/tmp')
self.tmpfile = open(self.tmppath, 'w')

def tearDown(self):
self.tmpfile.close()
os.remove(self.tmppath)

@mock.patch.dict("os.environ", clear=True)
def test_no_resizers_auto_is_fine(self):
Expand Down Expand Up @@ -130,7 +162,42 @@ def test_mode_auto_prefers_growpart(self):
mockobj.assert_called_once_with(
['growpart', '--help'], env={'LANG': 'C'})

@mock.patch.dict("os.environ", clear=True)
@mock.patch.dict("os.environ", {'LANG': 'cs_CZ.UTF-8'}, clear=True)
@mock.patch.object(temp_utils, 'mkdtemp', return_value='/tmp/much-random')
@mock.patch.object(stat, 'S_ISDIR', return_value=False)
@mock.patch.object(os.path, 'samestat', return_value=True)
@mock.patch.object(os.path, "join", return_value='/tmp')
@mock.patch.object(os, 'scandir', return_value=Scanner())
@mock.patch.object(os, 'mkdir')
@mock.patch.object(os, 'unlink')
@mock.patch.object(os, 'rmdir')
@mock.patch.object(os, 'open', return_value=1)
@mock.patch.object(os, 'close')
@mock.patch.object(shutil, 'rmtree')
@mock.patch.object(os, 'lseek', return_value=1024)
@mock.patch.object(os, 'lstat', return_value='interesting metadata')
def test_force_lang_check_tempfile(self, *args, **kwargs):
Comment thread
holmanb marked this conversation as resolved.
with mock.patch.object(
subp,
'subp',
return_value=(HELP_GROWPART_RESIZE, "")) as mockobj:

ret = cc_growpart.resizer_factory(mode="auto")
self.assertIsInstance(ret, cc_growpart.ResizeGrowPart)
diskdev = '/dev/sdb'
partnum = 1
partdev = '/dev/sdb'
ret.resize(diskdev, partnum, partdev)
mockobj.assert_has_calls([
mock.call(
["growpart", '--dry-run', diskdev, partnum],
env={'LANG': 'C', 'TMPDIR': '/tmp'}),
mock.call(
["growpart", diskdev, partnum],
env={'LANG': 'C', 'TMPDIR': '/tmp'}),
])

@mock.patch.dict("os.environ", {'LANG': 'cs_CZ.UTF-8'}, clear=True)
def test_mode_auto_falls_back_to_gpart(self):
with mock.patch.object(
subp, 'subp',
Expand Down