Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace /dev/stdin #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions host_modules/config_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from host_modules import host_service
import subprocess
import os

MOD_NAME = 'config'
DEFAULT_CONFIG = '/etc/sonic/config_db.json'
Expand All @@ -15,11 +16,16 @@ def reload(self, config_db_json):

cmd = ['/usr/local/bin/config', 'reload', '-y']
if config_db_json and len(config_db_json.strip()):
cmd.append('/dev/stdin')
input_bytes = (config_db_json + '\n').encode('utf-8')
result = subprocess.run(cmd, input=input_bytes, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
config_file = '/tmp/config_db.json'
try:
if (os.path.exists(config_file)):
os.remove(config_file)
with open(config_file, 'w') as fp:
fp.write(config_db_json)
except Exception as err:
return -1, "Fail to create config file: %s"%str(err)
cmd.append(config_file)
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
msg = ''
if result.returncode:
lines = result.stderr.decode().split('\n')
Expand Down
24 changes: 18 additions & 6 deletions host_modules/gcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ class GCU(host_service.HostModule):
"""
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is')
def apply_patch_db(self, patch_text):
input_bytes = (patch_text + '\n').encode('utf-8')
cmd = ['/usr/local/bin/config', 'apply-patch', '-f', 'CONFIGDB', '/dev/stdin']
patch_file_path = '/tmp/config_db.patch'
try:
with open(patch_file_path, 'w') as fp:
fp.write(patch_text)
except Exception as err:
return -1, "Fail to create patch file: %s"%str(err)

result = subprocess.run(cmd, input=input_bytes, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd = ['/usr/local/bin/config', 'apply-patch', '-f', 'CONFIGDB', patch_file_path]

result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
msg = ''
if result.returncode:
lines = result.stderr.decode().split('\n')
Expand All @@ -26,10 +32,16 @@ def apply_patch_db(self, patch_text):

@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is')
def apply_patch_yang(self, patch_text):
input_bytes = (patch_text + '\n').encode('utf-8')
cmd = ['/usr/local/bin/config', 'apply-patch', '-f', 'SONICYANG', '/dev/stdin']
patch_file_path = '/tmp/config_yang.patch'
try:
with open(patch_file_path, 'w') as fp:
fp.write(patch_text)
except Exception as err:
return -1, "Fail to create patch file: %s"%str(err)

result = subprocess.run(cmd, input=input_bytes, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd = ['/usr/local/bin/config', 'apply-patch', '-f', 'SONICYANG', patch_file_path]

result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
msg = ''
if result.returncode:
lines = result.stderr.decode().split('\n')
Expand Down
11 changes: 9 additions & 2 deletions tests/host_modules/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TestConfigEngine(object):
@mock.patch("dbus.service.BusName")
@mock.patch("dbus.service.Object.__init__")
def test_reload(self, MockInit, MockBusName, MockSystemBus):
config_file = "/tmp/config_db.json"
with mock.patch("subprocess.run") as mock_run:
res_mock = mock.Mock()
test_ret = 0
Expand All @@ -21,7 +22,7 @@ def test_reload(self, MockInit, MockBusName, MockSystemBus):
ret, msg = config_stub.reload(config_db_json)
call_args = mock_run.call_args[0][0]
assert "reload" in call_args
assert "/dev/stdin" in call_args
assert config_file in call_args
assert ret == test_ret, "Return value is wrong"
assert msg == "", "Return message is wrong"
with mock.patch("subprocess.run") as mock_run:
Expand All @@ -36,9 +37,15 @@ def test_reload(self, MockInit, MockBusName, MockSystemBus):
ret, msg = config_stub.reload(config_db_json)
call_args = mock_run.call_args[0][0]
assert "reload" in call_args
assert "/dev/stdin" in call_args
assert config_file in call_args
assert ret == test_ret, "Return value is wrong"
assert msg == "Error: this is the test message", "Return message is wrong"
with mock.patch("builtins.open") as mock_open:
mock_open.side_effect = Exception('Boom!')
config_db_json = "{}"
config_stub = config_engine.Config(config_engine.MOD_NAME)
ret, msg = config_stub.reload(config_db_json)
assert ret != 0, "Should fail for exception"

@mock.patch("dbus.SystemBus")
@mock.patch("dbus.service.BusName")
Expand Down
20 changes: 16 additions & 4 deletions tests/host_modules/gcu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_apply_patch_db(self, MockInit, MockBusName, MockSystemBus):
call_args = mock_run.call_args[0][0]
assert "apply-patch" in call_args
assert "CONFIGDB" in call_args
assert '/dev/stdin' in call_args
assert '/tmp/config_db.patch' in call_args
assert ret == test_ret, "Return value is wrong"
assert msg == "", "Return message is wrong"
with mock.patch("subprocess.run") as mock_run:
Expand All @@ -38,9 +38,15 @@ def test_apply_patch_db(self, MockInit, MockBusName, MockSystemBus):
call_args = mock_run.call_args[0][0]
assert "apply-patch" in call_args
assert "CONFIGDB" in call_args
assert '/dev/stdin' in call_args
assert '/tmp/config_db.patch' in call_args
assert ret == test_ret, "Return value is wrong"
assert msg == "Error: this is the test message", "Return message is wrong"
with mock.patch("builtins.open") as mock_open:
mock_open.side_effect = Exception('Boom!')
patch_text = "{}"
gcu_stub = gcu.GCU(gcu.MOD_NAME)
ret, msg = gcu_stub.apply_patch_db(patch_text)
assert ret != 0, "Should fail for exception"

@mock.patch("dbus.SystemBus")
@mock.patch("dbus.service.BusName")
Expand All @@ -59,7 +65,7 @@ def test_apply_patch_yang(self, MockInit, MockBusName, MockSystemBus):
call_args = mock_run.call_args[0][0]
assert "apply-patch" in call_args
assert "SONICYANG" in call_args
assert '/dev/stdin' in call_args
assert '/tmp/config_yang.patch' in call_args
assert ret == test_ret, "Return value is wrong"
assert msg == "", "Return message is wrong"
with mock.patch("subprocess.run") as mock_run:
Expand All @@ -75,9 +81,15 @@ def test_apply_patch_yang(self, MockInit, MockBusName, MockSystemBus):
call_args = mock_run.call_args[0][0]
assert "apply-patch" in call_args
assert "SONICYANG" in call_args
assert '/dev/stdin' in call_args
assert '/tmp/config_yang.patch' in call_args
assert ret == test_ret, "Return value is wrong"
assert msg == "Error: this is the test message", "Return message is wrong"
with mock.patch("builtins.open") as mock_open:
mock_open.side_effect = Exception('Boom!')
patch_text = "{}"
gcu_stub = gcu.GCU(gcu.MOD_NAME)
ret, msg = gcu_stub.apply_patch_db(patch_text)
assert ret != 0, "Should fail for exception"

@mock.patch("dbus.SystemBus")
@mock.patch("dbus.service.BusName")
Expand Down