File: test_instance_id.py

package info (click to toggle)
cloud-init 25.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,412 kB
  • sloc: python: 135,894; sh: 3,883; makefile: 141; javascript: 30; xml: 22
file content (99 lines) | stat: -rw-r--r-- 2,925 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import uuid
from typing import cast

import pytest
from pycloudlib.lxd.instance import LXDInstance

from cloudinit import subp
from tests.integration_tests.instances import IntegrationInstance
from tests.integration_tests.integration_settings import PLATFORM
from tests.integration_tests.releases import CURRENT_RELEASE, FOCAL

_INSTANCE_ID = uuid.uuid4()


def setup_meta_data(instance: LXDInstance):
    """Increment the instance id and apply it to the instance."""
    global _INSTANCE_ID
    _INSTANCE_ID = uuid.UUID(int=(1 + _INSTANCE_ID.int))
    command = [
        "lxc",
        "config",
        "set",
        instance.name,
        f"volatile.cloud-init.instance-id={_INSTANCE_ID}",
    ]
    assert subp.subp(command)


# class TestInstanceID:
@pytest.mark.skipif(
    PLATFORM not in ["lxd_container", "lxd_vm"] or CURRENT_RELEASE == FOCAL,
    reason="Uses lxd-specific behavior.",
)
@pytest.mark.lxd_setup.with_args(setup_meta_data)
@pytest.mark.lxd_use_exec
def test_instance_id_changes(client: IntegrationInstance):
    """Verify instance id change behavior

    If the id from the datasource changes, cloud-init should update the
    instance id link.
    """
    client.execute("cloud-init status --wait")
    # check that instance id is the one we set
    assert (
        str(_INSTANCE_ID)
        == client.execute("cloud-init query instance-id").stdout.rstrip()
    )
    assert (
        f"/var/lib/cloud/instances/{_INSTANCE_ID}"
        == client.execute(
            "readlink -f /var/lib/cloud/instance"
        ).stdout.rstrip()
    )

    instance = cast(LXDInstance, client.instance)
    setup_meta_data(instance)
    client.restart()
    client.execute("cloud-init status --wait")
    # check that instance id is the one we reset
    assert (
        str(_INSTANCE_ID)
        == client.execute("cloud-init query instance-id").stdout.rstrip()
    )
    assert (
        f"/var/lib/cloud/instances/{_INSTANCE_ID}"
        == client.execute(
            "readlink -f /var/lib/cloud/instance"
        ).stdout.rstrip()
    )


@pytest.mark.lxd_use_exec
def test_instance_id_no_changes(client: IntegrationInstance):
    """Verify instance id no change behavior

    If the id from the datasource does not change, cloud-init should not
    update the instance id link.
    """
    instance_id = client.execute(
        "cloud-init query instance-id"
    ).stdout.rstrip()
    assert (
        f"/var/lib/cloud/instances/{instance_id}"
        == client.execute(
            "readlink -f /var/lib/cloud/instance"
        ).stdout.rstrip()
    )
    client.restart()
    client.execute("cloud-init status --wait")
    assert (
        instance_id
        == client.execute("cloud-init query instance-id").stdout.rstrip()
    )
    assert (
        f"/var/lib/cloud/instances/{instance_id}"
        == client.execute(
            "readlink -f /var/lib/cloud/instance"
        ).stdout.rstrip()
    )