build-unit-test-docker: handle existing user/group

Ubuntu Lunar docker images contain a 'ubuntu' user and group by
default at 1000.  The current code will skip creating a user if
the gid/uid already exist but then attempt to `chown` a path to
the `{username}:{username}` that it didn't create (which fails).

Rather than do-nothing if the gid/uid already exist, use groupmod
and usermod to modify those as appropriate.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ie07146be1bd2ce9bce2aac818da2f01665dba890
diff --git a/scripts/build-unit-test-docker b/scripts/build-unit-test-docker
index c4903bb..75b463e 100755
--- a/scripts/build-unit-test-docker
+++ b/scripts/build-unit-test-docker
@@ -874,11 +874,19 @@
 # NOTE: The file is sorted to ensure the ordering is stable.
 RUN echo '{Package.depcache()}' > /tmp/depcache
 
-# Final configuration for the workspace
-RUN grep -q {gid} /etc/group || groupadd -f -g {gid} {username}
+# Ensure the group, user, and home directory are created (or rename them if
+# they already exist).
+RUN if grep -q ":{gid}:" /etc/group ; then \
+        groupmod -n {username} $(awk -F : '{{ if ($3 == {gid}) {{ print $1 }} }}' /etc/group) ; \
+    else \
+        groupadd -f -g {gid} {username} ; \
+    fi
 RUN mkdir -p "{os.path.dirname(homedir)}"
-RUN grep -q {uid} /etc/passwd || \
-        useradd -d {homedir} -m -u {uid} -g {gid} {username}
+RUN if grep -q ":{uid}:" /etc/passwd ; then \
+        usermod -l {username} -d {homedir} $(awk -F : '{{ if ($3 == {uid}) {{ print $1 }} }}' /etc/passwd) ; \
+    else \
+        useradd -d {homedir} -m -u {uid} -g {gid} {username} ; \
+    fi
 RUN sed -i '1iDefaults umask=000' /etc/sudoers
 RUN echo "{username} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers