traffic_replay: Make sure naming assumptions are in a single place
authorTim Beale <timbeale@catalyst.net.nz>
Mon, 5 Nov 2018 20:35:41 +0000 (09:35 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 7 Nov 2018 16:55:09 +0000 (17:55 +0100)
The traffic_replay group/user/machine account names follow a standard
format. This adds a function to generate the machine-name. It also makes
sure the existing user_name() function gets called in all applicable
places.

Signed-off-by: Tim Beale <timbeale@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/emulate/traffic.py

index af99e66..77a1862 100644 (file)
@@ -1606,8 +1606,8 @@ def generate_replay_accounts(ldb, instance_id, number, password):
     generate_traffic_accounts(ldb, instance_id, number, password)
     accounts = []
     for i in range(1, number + 1):
-        netbios_name = "STGM-%d-%d" % (instance_id, i)
-        username     = "STGU-%d-%d" % (instance_id, i)
+        netbios_name = machine_name(instance_id, i)
+        username = user_name(instance_id, i)
 
         account = ConversationAccounts(netbios_name, password, username,
                                        password)
@@ -1629,7 +1629,7 @@ def generate_traffic_accounts(ldb, instance_id, number, password):
     added = 0
     for i in range(number, 0, -1):
         try:
-            netbios_name = "STGM-%d-%d" % (instance_id, i)
+            netbios_name = machine_name(instance_id, i)
             create_machine_account(ldb, instance_id, netbios_name, password)
             added += 1
             if added % 50 == 0:
@@ -1646,7 +1646,7 @@ def generate_traffic_accounts(ldb, instance_id, number, password):
     added = 0
     for i in range(number, 0, -1):
         try:
-            username = "STGU-%d-%d" % (instance_id, i)
+            username = user_name(instance_id, i)
             create_user_account(ldb, instance_id, username, password)
             added += 1
             if added % 50 == 0:
@@ -1747,15 +1747,19 @@ def generate_users(ldb, instance_id, number, password):
     return users
 
 
+def machine_name(instance_id, i):
+    """Generate a machine account name from instance id."""
+    return "STGM-%d-%d" % (instance_id, i)
+
+
 def generate_machine_accounts(ldb, instance_id, number, password,
                               traffic_account=True):
     """Add machine accounts to the server"""
     existing_objects = search_objectclass(ldb, objectclass='computer')
     added = 0
     for i in range(number, 0, -1):
-        name = "STGM-%d-%d$" % (instance_id, i)
-        if name not in existing_objects:
-            name = "STGM-%d-%d" % (instance_id, i)
+        name = machine_name(instance_id, i)
+        if name + "$" not in existing_objects:
             create_machine_account(ldb, instance_id, name, password,
                                    traffic_account)
             added += 1