traffic_replay: Rework machine accounts to remove redundant code
authorTim Beale <timbeale@catalyst.net.nz>
Mon, 5 Nov 2018 22:27:42 +0000 (11:27 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 7 Nov 2018 16:55:09 +0000 (17:55 +0100)
generate_users_and_groups() now generates the machine acounts as well as
the user accounts, so it seems there's no need to also have
generate_traffic_accounts(), which does the same job.

Instead, we can just pass through the number of machine acounts to
generate_users_and_groups() and delete the other function.

Also updated generate_users_and_groups() so that machine_accounts is
no longer optional (we want to create machine accounts in all cases).

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

index 9ac8775ed6050400196db6bbf2cc388891951a20..069c4103a1504d804545943f4d13939829b5c23e 100644 (file)
@@ -1603,7 +1603,6 @@ class ConversationAccounts(object):
 def generate_replay_accounts(ldb, instance_id, number, password):
     """Generate a series of unique machine and user account names."""
 
-    generate_traffic_accounts(ldb, instance_id, number, password)
     accounts = []
     for i in range(1, number + 1):
         netbios_name = machine_name(instance_id, i)
@@ -1615,54 +1614,6 @@ def generate_replay_accounts(ldb, instance_id, number, password):
     return accounts
 
 
-def generate_traffic_accounts(ldb, instance_id, number, password):
-    """Create the specified number of user and machine accounts.
-
-    As accounts are not explicitly deleted between runs. This function starts
-    with the last account and iterates backwards stopping either when it
-    finds an already existing account or it has generated all the required
-    accounts.
-    """
-    print(("Generating machine and conversation accounts, "
-           "as required for %d conversations" % number),
-          file=sys.stderr)
-    added = 0
-    for i in range(number, 0, -1):
-        try:
-            netbios_name = machine_name(instance_id, i)
-            create_machine_account(ldb, instance_id, netbios_name, password)
-            added += 1
-            if added % 50 == 0:
-                LOGGER.info("Created %u/%u machine accounts" % (added, number))
-        except LdbError as e:
-            (status, _) = e.args
-            if status == 68:
-                break
-            else:
-                raise
-    if added > 0:
-        LOGGER.info("Added %d new machine accounts" % added)
-
-    added = 0
-    for i in range(number, 0, -1):
-        try:
-            username = user_name(instance_id, i)
-            create_user_account(ldb, instance_id, username, password)
-            added += 1
-            if added % 50 == 0:
-                LOGGER.info("Created %u/%u users" % (added, number))
-
-        except LdbError as e:
-            (status, _) = e.args
-            if status == 68:
-                break
-            else:
-                raise
-
-    if added > 0:
-        LOGGER.info("Added %d new user accounts" % added)
-
-
 def create_machine_account(ldb, instance_id, netbios_name, machinepass,
                            traffic_account=True):
     """Create a machine account via ldap."""
@@ -1813,7 +1764,7 @@ def clean_up_accounts(ldb, instance_id):
 
 def generate_users_and_groups(ldb, instance_id, password,
                               number_of_users, number_of_groups,
-                              group_memberships, machine_accounts=0,
+                              group_memberships, machine_accounts,
                               traffic_accounts=True):
     """Generate the required users and groups, allocating the users to
        those groups."""
@@ -1826,11 +1777,10 @@ def generate_users_and_groups(ldb, instance_id, password,
     LOGGER.info("Generating dummy user accounts")
     users_added = generate_users(ldb, instance_id, number_of_users, password)
 
-    if machine_accounts > 0:
-        LOGGER.info("Generating dummy machine accounts")
-        computers_added = generate_machine_accounts(ldb, instance_id,
-                                                    machine_accounts, password,
-                                                    traffic_accounts)
+    LOGGER.info("Generating dummy machine accounts")
+    computers_added = generate_machine_accounts(ldb, instance_id,
+                                                machine_accounts, password,
+                                                traffic_accounts)
 
     if number_of_groups > 0:
         LOGGER.info("Generating dummy groups")
index e8ba131445a6d909f92924fc3aad0def6b92fd26..991c9a9eb030324c5d16cd6df9234d285ca0ac8f 100755 (executable)
@@ -346,6 +346,7 @@ def main():
                                       number_of_users,
                                       opts.number_of_groups,
                                       opts.group_memberships,
+                                      machine_accounts=len(conversations),
                                       traffic_accounts=True)
 
     accounts = traffic.generate_replay_accounts(ldb,