示例#1
0
def test_generate_token(gstate: GlobalState) -> None:

    nodemgr = NodeMgr(gstate)
    token = nodemgr._generate_token()
    assert len(token) > 0
    res = token.split("-")
    assert len(res) == 4
    for s in res:
        assert len(s) == 4
示例#2
0
async def test_deploy(gstate: GlobalState, mocker: MockerFixture,
                      nodemgr: NodeMgr) -> None:

    from gravel.controllers.auth import UserMgr, UserModel
    from gravel.controllers.inventory.disks import DiskDevice
    from gravel.controllers.nodes.deployment import DeploymentConfig
    from gravel.controllers.nodes.disks import DiskSolution
    from gravel.controllers.nodes.mgr import NodeInitStage

    called_mock_deploy = False

    def mock_solution(gstate: GlobalState) -> DiskSolution:
        return DiskSolution(
            systemdisk=DiskDevice(
                id="foo01",
                name="foo",
                path="/dev/foo",
                product="Foo",
                vendor="Foo Inc",
                size=1000,
                rotational=False,
                available=True,
                rejected_reasons=[],
            ),
            storage=[
                DiskDevice(
                    id="bar01",
                    name="bar",
                    path="/dev/bar",
                    product="Bar",
                    vendor="Bar LLC",
                    size=2000,
                    rotational=False,
                    available=True,
                    rejected_reasons=[],
                ),
                DiskDevice(
                    id="baz01",
                    name="baz",
                    path="/dev/baz",
                    product="Baz",
                    vendor="Baz Ltd",
                    size=2000,
                    rotational=False,
                    available=True,
                    rejected_reasons=[],
                ),
            ],
            storage_size=4000,
            possible=True,
        )

    async def mock_deploy(
        config: DeploymentConfig,
        post_bootstrap_cb: Callable[[bool, Optional[str]], Awaitable[None]],
        finisher: Callable[[bool, Optional[str]], Awaitable[None]],
    ) -> None:

        import inspect

        nonlocal called_mock_deploy
        called_mock_deploy = True

        assert config.hostname == "barbaz"
        assert config.address == "1.2.3.4"
        assert config.token == "751b-51fd-10d7-f7b4"
        assert config.ntp_addr == "my.ntp.addr"
        assert config.disks.system == "/dev/foo"
        assert len(config.disks.storage) == 2
        assert "/dev/bar" in config.disks.storage
        assert "/dev/baz" in config.disks.storage
        assert post_bootstrap_cb is not None
        assert finisher is not None
        assert inspect.iscoroutinefunction(post_bootstrap_cb)
        assert inspect.iscoroutinefunction(finisher)

    mocker.patch("gravel.controllers.nodes.disks.Disks.gen_solution",
                 new=mock_solution)

    nodemgr._init_stage = NodeInitStage.AVAILABLE

    nodemgr._generate_token = mocker.MagicMock(
        return_value="751b-51fd-10d7-f7b4")
    nodemgr._save_token = mocker.AsyncMock()
    nodemgr._deployment.deploy = mock_deploy

    await gstate.store.ensure_connection()

    await nodemgr.deploy(
        DeployParamsModel(hostname="barbaz", ntpaddr="my.ntp.addr"))

    assert called_mock_deploy
    assert nodemgr._token == "751b-51fd-10d7-f7b4"
    assert nodemgr._state.hostname == "barbaz"
    nodemgr._save_token.assert_called_once()  # type: ignore

    ntpaddr = await gstate.store.get("/nodes/ntp_addr")
    assert ntpaddr == "my.ntp.addr"

    usermgr = UserMgr(gstate.store)
    assert await usermgr.exists("admin")
    user: Optional[UserModel] = await usermgr.get("admin")
    assert user is not None
    assert user.username == "admin"
    # We can't test the plain password here because it will fail
    # and we don't care particularly about the password itself, just that
    # the user has been populated. We'll leave for the 'UserMgr' tests to
    # validate the correctness of its operations.
    assert len(user.password) > 0