Skip to content

Commit

Permalink
fix(provision): encode power env fields on python 2
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Styk <mart.styk@gmail.com>
  • Loading branch information
StykMartin authored and JohnVillalovos committed Aug 3, 2024
1 parent aa77288 commit 4fa92c5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
24 changes: 19 additions & 5 deletions LabController/src/bkr/labcontroller/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,27 @@ def find_power_script(power_type):
raise ValueError("Invalid power type %r" % power_type)


def _decode(value):
# Decode if we are running python2 and value is unicode
if six.PY2 and isinstance(value, six.text_type):
return value.encode("utf8")
return value


def build_power_env(command):
env = dict(os.environ)
env["power_address"] = (command["power"].get("address") or "").encode("utf8")
env["power_id"] = (command["power"].get("id") or "").encode("utf8")
env["power_user"] = (command["power"].get("user") or "").encode("utf8")
env["power_pass"] = (command["power"].get("passwd") or "").encode("utf8")
env["power_mode"] = command["action"].encode("utf8")
power_mapping = {
"address": "power_address",
"id": "power_id",
"user": "power_user",
"passwd": "power_pass",
}

for k, v in six.iteritems(power_mapping):
env[v] = _decode(command["power"].get(k, ""))

env["power_mode"] = _decode(command["action"])

return env


Expand Down
54 changes: 54 additions & 0 deletions LabController/src/bkr/labcontroller/test_provision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright Contributors to the Beaker project.
# SPDX-License-Identifier: GPL-2.0-or-later

import unittest

import six

from bkr.common.helpers import SensitiveUnicode
from bkr.labcontroller.provision import build_power_env


class TestBuildPowerEnv(unittest.TestCase):
def test_build_power_env(self):
t_command = {
"power": {
"address": u"192.168.1.1",
"id": u"42",
"user": u"root",
"passwd": SensitiveUnicode(u"toor"),
},
"action": u"reboot",
}

expected = {
"power_address": "192.168.1.1",
"power_id": "42",
"power_user": "root",
"power_pass": "toor",
"power_mode": "reboot",
}

actual = build_power_env(t_command)

for key, value in six.iteritems(expected):
self.assertEqual(expected[key], actual[key])

def test_build_power_env_with_missing_fields(self):
t_command = {
"power": {"address": u"192.168.1.1", "passwd": SensitiveUnicode(u"toor")},
"action": u"reboot",
}

expected = {
"power_address": "192.168.1.1",
"power_id": "",
"power_user": "",
"power_pass": "toor",
"power_mode": "reboot",
}

actual = build_power_env(t_command)

for key, value in six.iteritems(expected):
self.assertEqual(expected[key], actual[key])
2 changes: 2 additions & 0 deletions beaker.spec
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,13 @@ BuildRequires: python3-gevent
BuildRequires: python3-lxml
BuildRequires: python3-werkzeug
BuildRequires: python3-psutil
BuildRequires: python3-daemon
%else
# python2-gevent112 is a special build created for labcontroller. It includes backports to ensure compatibility with py2.7.9 SSL backport.
BuildRequires: python2-gevent112
BuildRequires: python2-psutil
BuildRequires: python-lxml
BuildRequires: python-daemon
%endif

# Syslinux is only available on x86_64. This package is used to provide pxelinux.0, which is then copied to the TFTP directory.
Expand Down

0 comments on commit 4fa92c5

Please sign in to comment.