Commit 3bc0dd76 authored by Jérome Perrin's avatar Jérome Perrin

cli/boot: Read partition base name from config

also add missing test for slapos node boot

/reviewed-on nexedi/slapos.core!187
parent be2cbf5b
......@@ -42,11 +42,14 @@ from slapos.cli.entry import SlapOSApp
from slapos.cli.config import ConfigCommand
from slapos.format import isGlobalScopeAddress
def _removeTimestamp(instancehome):
def _removeTimestamp(instancehome, partition_base_name):
"""
Remove .timestamp from all partitions
"""
timestamp_glob_path = "%s/slappart*/.timestamp" % instancehome
timestamp_glob_path = os.path.join(
instancehome,
"%s*" % partition_base_name,
".timestamp")
for timestamp_path in glob.glob(timestamp_glob_path):
print("Removing %s" % timestamp_path)
os.remove(timestamp_path)
......@@ -157,6 +160,7 @@ class BootCommand(ConfigCommand):
def take_action(self, args):
configp = self.fetch_config(args)
instance_root = configp.get('slapos','instance_root')
partition_base_name = configp.get('slapformat', 'partition_base_name')
master_url = urlparse(configp.get('slapos','master_url'))
master_hostname = master_url.hostname
......@@ -187,4 +191,4 @@ class BootCommand(ConfigCommand):
print("[BOOT] [ERROR] Fail to bang, try again in 15 seconds...")
sleep(15)
_removeTimestamp(instance_root)
_removeTimestamp(instance_root, partition_base_name)
......@@ -29,6 +29,9 @@ import logging
import pprint
import unittest
import tempfile
import shutil
import socket
import textwrap
import sys
import os
import pkg_resources
......@@ -282,6 +285,60 @@ class TestCliProxyShow(CliMixin):
self.assertIn('287375f0cba269902ba1bc50242839d7', f.read())
class TestCliBoot(CliMixin):
def test_node_boot(self):
# initialize instance root, with a timestamp in a partition
temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, temp_dir)
instance_root = os.path.join(temp_dir, 'instance')
partition_base_name = 'partition'
os.mkdir(instance_root)
os.mkdir(os.path.join(instance_root, partition_base_name + '1'))
timestamp = os.path.join(
instance_root, partition_base_name + '1', '.timestamp')
with open(timestamp, 'w') as f:
f.write("1578552471")
# make a config file using this instance root
with tempfile.NamedTemporaryFile(mode='w') as slapos_conf:
slapos_conf.write(
textwrap.dedent(
"""\
[slapos]
instance_root = {instance_root}
master_url = https://slap.vifib.com/
[slapformat]
partition_base_name = {partition_base_name}
interface_name = interface_name_from_config
""").format(**locals()))
slapos_conf.flush()
# run slapos node boot
app = slapos.cli.entry.SlapOSApp()
with patch('slapos.cli.command.check_root_user', return_value=True) as check_root_user,\
patch('slapos.cli.boot.SlapOSApp') as SlapOSApp,\
patch('slapos.cli.boot.ConfigCommand.config_path', return_value=slapos_conf.name), \
patch(
'slapos.cli.boot.netifaces.ifaddresses',
return_value={socket.AF_INET6: ({'addr': '2000::1'},),},) as ifaddresses,\
patch('slapos.cli.boot._ping_hostname', return_value=1) as _ping_hostname:
app.run(('node', 'boot'))
# boot command runs as root
check_root_user.assert_called_once()
# it waits for interface to have an IPv6 address
ifaddresses.assert_called_once_with('interface_name_from_config')
# then ping master hostname to wait for connectivity
_ping_hostname.assert_called_once_with('slap.vifib.com')
# then format and bang
SlapOSApp().run.assert_any_call(['node', 'format', '--now', '--verbose'])
SlapOSApp().run.assert_any_call(['node', 'bang', '-m', 'Reboot'])
# timestamp files have been removed
self.assertFalse(os.path.exists(timestamp))
class TestCliNode(CliMixin):
def test_node_software(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment