nmstatectl: deprecate "set" in favor of "apply"

nmstatectl should use the same names as libnmstate. In order to
accomplish that, "set" command is now an alias to "apply" and is raising
a warning when being used.

Integration test added.

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
This commit is contained in:
Fernando Fernandez Mancera 2021-01-20 14:56:42 +01:00 committed by Gris Ge
parent cbe5b57d0d
commit be8af1658c
3 changed files with 90 additions and 15 deletions

View File

@ -9,6 +9,8 @@ nmstatectl \- A nmstate command line tool
.br
.B nmstatectl set \fISTATE_FILE_PATH\fR [\fIOPTIONS\fR]
.br
.B nmstatectl apply \fISTATE_FILE_PATH\fR [\fIOPTIONS\fR]
.br
.B nmstatectl edit \fR[\fIINTERFACE_NAME\fR] [\fIOPTIONS\fR]
.br
.B nmstatectl rollback \fR[\fICHECKPOINT_PATH\fR]
@ -51,6 +53,16 @@ nmstatectl show eth\\*
.PP
.B set
.RS
"Set" command is deprecated. Please consider using "apply" instead.
Apply the network state from specified file in \fIYAML\fR or \fIJSON\fR format.
By default, if the network state after state applied is not identical to the
desired state, \fBnmstatectl\fR rollbacks to the state before \fBset\fR
command. Use the \fB--no-verify\fR argument to skip the verification.
.RE
.PP
.B apply
.RS
Apply the network state from specified file in \fIYAML\fR or \fIJSON\fR format.
By default, if the network state after state applied is not identical to the
desired state, \fBnmstatectl\fR rollbacks to the state before \fBset\fR

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2018-2020 Red Hat, Inc.
# Copyright (c) 2018-2021 Red Hat, Inc.
#
# This file is part of nmstate
#
@ -25,6 +25,7 @@ import os
import subprocess
import sys
import tempfile
import warnings
import yaml
@ -53,6 +54,7 @@ def main():
setup_subcommand_edit(subparsers)
setup_subcommand_rollback(subparsers)
setup_subcommand_set(subparsers)
setup_subcommand_apply(subparsers)
setup_subcommand_show(subparsers)
setup_subcommand_version(subparsers)
setup_subcommand_varlink(subparsers)
@ -124,8 +126,8 @@ def setup_subcommand_rollback(subparsers):
parser_rollback.set_defaults(func=rollback)
def setup_subcommand_set(subparsers):
parser_set = subparsers.add_parser("set", help="Set network state")
def setup_subcommand_apply(subparsers):
parser_set = subparsers.add_parser("apply", help="Apply network state")
parser_set.add_argument(
"file",
help="File containing desired state. "
@ -163,6 +165,51 @@ def setup_subcommand_set(subparsers):
parser_set.set_defaults(func=apply)
def setup_subcommand_set(subparsers):
parser_set = subparsers.add_parser(
"set",
help=(
"Set network state, deprecated please consider using"
"'apply' instead."
),
)
parser_set.add_argument(
"file",
help="File containing desired state. "
"stdin is used when no file is specified.",
nargs="*",
)
parser_set.add_argument(
"--no-verify",
action="store_false",
dest="verify",
default=True,
help="Do not verify that the state was completely set and disable "
"rollback to previous state",
)
parser_set.add_argument(
"--no-commit",
action="store_false",
dest="commit",
default=True,
help="Do not commit new state after verification",
)
parser_set.add_argument(
"--timeout",
type=int,
default=60,
help="Timeout in seconds before reverting uncommited changes.",
)
parser_set.add_argument(
"--memory-only",
action="store_false",
dest="save_to_disk",
default=True,
help="Do not make the state persistent.",
)
parser_set.set_defaults(func=set)
def setup_subcommand_show(subparsers):
parser_show = subparsers.add_parser("show", help="Show network state")
parser_show.set_defaults(func=show)
@ -263,6 +310,11 @@ def show(args):
print_state(state, use_yaml=args.yaml)
def set(args):
warnings.warn("Using 'set' is deprecated, use 'apply' instead.")
apply(args)
def apply(args):
if args.file:
for statefile in args.file:

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2018-2020 Red Hat, Inc.
# Copyright (c) 2018-2021 Red Hat, Inc.
#
# This file is part of nmstate
#
@ -32,6 +32,7 @@ from .testlib.examplelib import find_examples_dir
from .testlib.examplelib import load_example
APPLY_CMD = ["nmstatectl", "apply"]
SET_CMD = ["nmstatectl", "set"]
SHOW_CMD = ["nmstatectl", "show"]
CONFIRM_CMD = ["nmstatectl", "commit"]
@ -93,17 +94,19 @@ ETH1_YAML_CONFIG = b"""interfaces:
mtu: 1500
"""
SET_WARNING = "Using 'set' is deprecated, use 'apply' instead."
EXAMPLES = find_examples_dir()
CONFIRMATION_INTERFACE = "eth1.101"
CONFIRMATION_CLEAN = "vlan101_eth1_absent.yml"
CONFIRMATION_TEST = "vlan101_eth1_up.yml"
CONFIRMATION_TEST_STATE = load_example(CONFIRMATION_TEST)
CONFIRMATION_SET = SET_CMD + [
CONFIRMATION_APPLY = APPLY_CMD + [
"--no-commit",
os.path.join(EXAMPLES, CONFIRMATION_TEST),
]
CONFIRMATION_TIMEOUT = 5
CONFIRMATION_TIMOUT_COMMAND = SET_CMD + [
CONFIRMATION_TIMOUT_COMMAND = APPLY_CMD + [
"--no-commit",
"--timeout",
str(CONFIRMATION_TIMEOUT),
@ -160,16 +163,24 @@ def test_show_command_only_non_existing():
assert len(state[Constants.INTERFACES]) == 0
def test_set_command_with_yaml_format():
ret = cmdlib.exec_cmd(SET_CMD, stdin=ETH1_YAML_CONFIG)
def test_apply_command_with_yaml_format():
ret = cmdlib.exec_cmd(APPLY_CMD, stdin=ETH1_YAML_CONFIG)
rc, out, err = ret
assert rc == cmdlib.RC_SUCCESS, cmdlib.format_exec_cmd_result(ret)
def test_set_command_with_two_states():
def test_set_command_with_yaml_deprecated():
ret = cmdlib.exec_cmd(SET_CMD, stdin=ETH1_YAML_CONFIG)
rc, out, err = ret
assert rc == cmdlib.RC_SUCCESS, cmdlib.format_exec_cmd_result(ret)
assert SET_WARNING in err.rstrip()
def test_apply_command_with_two_states():
examples = find_examples_dir()
cmd = SET_CMD + [
cmd = APPLY_CMD + [
os.path.join(examples, "linuxbrige_eth1_up.yml"),
os.path.join(examples, "linuxbrige_eth1_absent.yml"),
]
@ -185,7 +196,7 @@ def test_manual_confirmation(eth1_up):
with example_state(CONFIRMATION_CLEAN, CONFIRMATION_CLEAN):
assert_command(CONFIRMATION_SET)
assert_command(CONFIRMATION_APPLY)
assertlib.assert_state(CONFIRMATION_TEST_STATE)
assert_command(CONFIRM_CMD)
assertlib.assert_state(CONFIRMATION_TEST_STATE)
@ -196,7 +207,7 @@ def test_manual_rollback(eth1_up):
with example_state(CONFIRMATION_CLEAN, CONFIRMATION_CLEAN) as clean_state:
assert_command(CONFIRMATION_SET)
assert_command(CONFIRMATION_APPLY)
assertlib.assert_state(CONFIRMATION_TEST_STATE)
assert_command(ROLLBACK_CMD)
assertlib.assert_state(clean_state)
@ -204,16 +215,16 @@ def test_manual_rollback(eth1_up):
def test_dual_change(eth1_up):
"""
I cannot set a state without confirming/rolling back the state change.
I cannot apply a state without confirming/rolling back the state change.
"""
with example_state(CONFIRMATION_CLEAN, CONFIRMATION_CLEAN) as clean_state:
assert_command(CONFIRMATION_SET)
assert_command(CONFIRMATION_APPLY)
assertlib.assert_state(CONFIRMATION_TEST_STATE)
try:
cmdlib.exec_cmd(CONFIRMATION_SET)
cmdlib.exec_cmd(CONFIRMATION_APPLY)
except Exception as e:
assert isinstance(e, NmstateConflictError)
finally: