[ovs-dev] [PATCH 01/11] ovn-nbctl: Don't die in pg_by_name_or_uuid().

Jakub Sitnicki jkbs at redhat.com
Tue Jul 17 13:34:05 UTC 2018


Let the caller handle the error. This prepares us for reporting errors
in daemon mode.

Also, extend the tests to cover this error path.

Signed-off-by: Jakub Sitnicki <jkbs at redhat.com>
---
 ovn/utilities/ovn-nbctl.c | 23 ++++++++++++++++-------
 tests/ovn-nbctl.at        |  5 +++++
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index d07524697..0cd11e845 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -609,10 +609,12 @@ lb_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist,
     return NULL;
 }
 
-static const struct nbrec_port_group *
-pg_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist)
+static char * OVS_WARN_UNUSED_RESULT
+pg_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist,
+                   const struct nbrec_port_group **pg_p)
 {
     const struct nbrec_port_group *pg = NULL;
+    *pg_p = NULL;
 
     struct uuid pg_uuid;
     bool is_uuid = uuid_from_string(&pg_uuid, id);
@@ -632,11 +634,12 @@ pg_by_name_or_uuid(struct ctl_context *ctx, const char *id, bool must_exist)
     }
 
     if (!pg && must_exist) {
-        ctl_fatal("%s: port group %s not found", id,
-                  is_uuid ? "UUID" : "name");
+        return xasprintf("%s: port group %s not found", id,
+                         is_uuid ? "UUID" : "name");
     }
 
-    return pg;
+    *pg_p = pg;
+    return NULL;
 }
 
 static void
@@ -1559,7 +1562,10 @@ acl_cmd_get_pg_or_ls(struct ctl_context *ctx,
     char *error;
 
     if (!opt_type) {
-        *pg = pg_by_name_or_uuid(ctx, ctx->argv[1], false);
+        error = pg_by_name_or_uuid(ctx, ctx->argv[1], false, pg);
+        if (error) {
+            return error;
+        }
         error = ls_by_name_or_uuid(ctx, ctx->argv[1], false, ls);
         if (error) {
             return error;
@@ -1574,7 +1580,10 @@ acl_cmd_get_pg_or_ls(struct ctl_context *ctx,
                              ctx->argv[1]);
         }
     } else if (!strcmp(opt_type, "port-group")) {
-        *pg = pg_by_name_or_uuid(ctx, ctx->argv[1], true);
+        error = pg_by_name_or_uuid(ctx, ctx->argv[1], true, pg);
+        if (error) {
+            return error;
+        }
         *ls = NULL;
     } else if (!strcmp(opt_type, "switch")) {
         error = ls_by_name_or_uuid(ctx, ctx->argv[1], true, ls);
diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index 73a61a4be..9f6a2783b 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -234,6 +234,11 @@ OVN_NBCTL_TEST_ACL([ls1], [--type=switch])
 AT_CHECK([ovn-nbctl create port_group name=pg0], [0], [ignore])
 OVN_NBCTL_TEST_ACL([pg0], [--type=port-group])
 
+dnl Test when port group doesn't exist
+AT_CHECK([ovn-nbctl --type=port-group acl-add pg1 to-lport 100 ip drop], [1], [], [dnl
+ovn-nbctl: pg1: port group name not found
+])
+
 dnl Test when same name exists in logical switches and portgroups
 AT_CHECK([ovn-nbctl create port_group name=ls0], [0], [ignore])
 AT_CHECK([ovn-nbctl acl-add ls0 to-lport 100 ip drop], [1], [], [stderr])
-- 
2.14.4



More information about the dev mailing list