mirror of
https://github.com/nix-community/home-manager
synced 2025-01-09 18:49:51 +01:00
faa4b16358
This module is a continuation of #2630 by MaeIsBad. It also adds a module `virtualisation.oci-containers` that is equivalent to the one in NixOS. Basically it allows a simple toggle to activate oci-container services and commands. We also support Podman on mac. Note, Podman requires a VM on mac, which has to be started before any Podman commands can be executed. Users might sometimes require VMs that use different architectures than the default VM started by Podman. Thus, they get the option to define the VM(s) that will be initialized and started by podman. Since Podman has to start a machine, it's best to do it using launchd. The configuration of the machines requires a JSON, generated from an attrset in Home Manager, which is where Python script comes into play to take care of diff-ing the `podman machine list` to CRUD them. PR #4331 Co-authored-by: MaeIsBad <26093674+MaeIsBad@users.noreply.github.com>
232 lines
6.7 KiB
Python
232 lines
6.7 KiB
Python
import argparse
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
|
|
from podmactl import (
|
|
Machine,
|
|
MachineDict,
|
|
diff_machines,
|
|
PodmanMachineCommander,
|
|
)
|
|
|
|
|
|
class MachineTestCase(unittest.TestCase):
|
|
def test_from_list_dict(self):
|
|
"""Ensure that dicts from `podman machine list` can create a machine object"""
|
|
self.assertEqual(
|
|
Machine(
|
|
cpus=2, disk_size=100, memory=2048, name="indie-machine", active=True
|
|
),
|
|
Machine.from_dict(
|
|
dict(
|
|
CPUs=2,
|
|
DiskSize=100,
|
|
Memory=2048,
|
|
Name="indie-machine",
|
|
)
|
|
),
|
|
)
|
|
|
|
def test_from_extra_dict(self):
|
|
self.assertEqual(
|
|
Machine(
|
|
cpus=2, disk_size=100, memory=2048, name="indie-machine", active=True
|
|
),
|
|
Machine.from_dict(
|
|
dict(
|
|
cpus=2,
|
|
disk_size=100,
|
|
memory=2048,
|
|
name="indie-machine",
|
|
active=True,
|
|
new=True,
|
|
dont_exist="something",
|
|
something_else="ladidah",
|
|
)
|
|
),
|
|
)
|
|
|
|
def test_from_bad_dict(self):
|
|
"""Will pass the wrong number of args to the __init__"""
|
|
self.assertRaises(
|
|
TypeError,
|
|
Machine.from_dict,
|
|
dict(
|
|
cpus=2,
|
|
),
|
|
)
|
|
|
|
def test_make_cli_args(self):
|
|
args = PodmanMachineCommander.make_cli_args(
|
|
Machine(cpus=2, disk_size=50, memory=4096, name="manjaro", active=False)
|
|
)
|
|
self.assertEqual(
|
|
args,
|
|
[
|
|
"--cpus",
|
|
"2",
|
|
"--disk-size",
|
|
"50",
|
|
"--memory",
|
|
"4096",
|
|
],
|
|
)
|
|
|
|
def test_make_optional_cli_args(self):
|
|
machine = Machine(
|
|
cpus=2,
|
|
disk_size=50,
|
|
memory=4096,
|
|
name="manjaro",
|
|
active=False,
|
|
image_path="somewhere.qcow2.xz",
|
|
)
|
|
self.assertEqual(
|
|
PodmanMachineCommander.make_cli_args(machine),
|
|
[
|
|
"--cpus",
|
|
"2",
|
|
"--disk-size",
|
|
"50",
|
|
"--memory",
|
|
"4096",
|
|
],
|
|
)
|
|
self.assertEqual(
|
|
PodmanMachineCommander.make_cli_args(
|
|
machine, PodmanMachineCommander.MACHINE_CLI_ARGS + ("image_path",)
|
|
),
|
|
[
|
|
"--cpus",
|
|
"2",
|
|
"--disk-size",
|
|
"50",
|
|
"--memory",
|
|
"4096",
|
|
"--image-path",
|
|
"somewhere.qcow2.xz",
|
|
],
|
|
)
|
|
|
|
|
|
class MachineDictTestCase(unittest.TestCase):
|
|
def test_load(self):
|
|
machine_json = {
|
|
"cpus": 2,
|
|
"disk_size": 100,
|
|
"memory": 2048,
|
|
"active": False,
|
|
}
|
|
machines_json = {"default": machine_json}
|
|
with tempfile.NamedTemporaryFile(mode="w") as fp:
|
|
json.dump(machines_json, fp)
|
|
fp.seek(0)
|
|
machines = MachineDict(fp.name)
|
|
self.assertDictEqual(
|
|
machines,
|
|
{
|
|
"default": Machine(
|
|
cpus=2,
|
|
disk_size=100,
|
|
memory=2048,
|
|
name="default",
|
|
active=False,
|
|
)
|
|
},
|
|
)
|
|
|
|
def test_bad_machine_load(self):
|
|
with self.assertRaises(argparse.ArgumentTypeError):
|
|
with tempfile.NamedTemporaryFile(mode="w") as fp:
|
|
json.dump({"default": {}}, fp)
|
|
fp.seek(0)
|
|
MachineDict(fp.name)
|
|
|
|
def test_bad_json_load(self):
|
|
with self.assertRaises(argparse.ArgumentTypeError):
|
|
with tempfile.NamedTemporaryFile(mode="w") as fp:
|
|
fp.write("this is definitely not a json")
|
|
fp.seek(0)
|
|
MachineDict(fp.name)
|
|
|
|
|
|
class DiffTestCase(unittest.TestCase):
|
|
def test_new_machines(self):
|
|
diff = diff_machines(
|
|
{
|
|
"new": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="new", active=True
|
|
),
|
|
"old": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="old", active=False
|
|
),
|
|
},
|
|
{
|
|
"old": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="old", active=False
|
|
),
|
|
},
|
|
)
|
|
self.assertListEqual(
|
|
diff.new,
|
|
[Machine(cpus=1, disk_size=100, memory=1024, name="new", active=False)],
|
|
)
|
|
self.assertListEqual(
|
|
diff.same,
|
|
[Machine(cpus=1, disk_size=100, memory=1024, name="old", active=False)],
|
|
)
|
|
self.assertListEqual(diff.removed, [])
|
|
self.assertListEqual(diff.modified, [])
|
|
|
|
def test_update_machine(self):
|
|
diff = diff_machines(
|
|
{
|
|
"changed": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="changed", active=True
|
|
),
|
|
},
|
|
{
|
|
"changed": Machine(
|
|
cpus=2, disk_size=100, memory=2048, name="changed", active=False
|
|
),
|
|
},
|
|
)
|
|
self.assertListEqual(diff.new, [])
|
|
self.assertListEqual(diff.same, [])
|
|
self.assertListEqual(diff.removed, [])
|
|
self.assertListEqual(
|
|
diff.modified,
|
|
[Machine(cpus=1, disk_size=100, memory=1024, name="changed", active=False)],
|
|
)
|
|
|
|
def test_remove_machine(self):
|
|
diff = diff_machines(
|
|
{
|
|
"same": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="same", active=True
|
|
),
|
|
},
|
|
{
|
|
"same": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="same", active=False
|
|
),
|
|
"removed": Machine(
|
|
cpus=1, disk_size=100, memory=1024, name="removed", active=True
|
|
),
|
|
},
|
|
)
|
|
self.assertListEqual(diff.new, [])
|
|
self.assertListEqual(
|
|
diff.same,
|
|
[Machine(cpus=1, disk_size=100, memory=1024, name="same", active=False)],
|
|
)
|
|
self.assertListEqual(
|
|
diff.removed,
|
|
[Machine(cpus=1, disk_size=100, memory=1024, name="removed", active=False)],
|
|
)
|
|
self.assertListEqual(diff.modified, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|