selftest: Pass realm-to-IP mapping to dns_hub as an argument
authorTim Beale <timbeale@catalyst.net.nz>
Wed, 20 Feb 2019 03:09:54 +0000 (16:09 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 5 Mar 2019 23:27:31 +0000 (23:27 +0000)
Instead of storing hashmaps in 2 different files, we can just convert a
perl hashmap into a string, pass it to dns_hub, and convert it back into
a python dictionary.

The main reason for doing this is the IP-to-testenv mapping now all
lives in a single file (Samba.pm). All this logic is right next to each
other rather than being split across multiple files. Hopefully this will
make it easier to keep it up to date as we add new testenvs.

Signed-off-by: Tim Beale <timbeale@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
selftest/target/Samba.pm
selftest/target/Samba4.pm
selftest/target/dns_hub.py

index 98aac656fd5efb669284c00091b17a2304007041..3f8a38fc4d915290ea49e02dc549d8ff32694eed 100644 (file)
@@ -385,6 +385,37 @@ sub mk_mitkdc_conf($$)
        close(KDCCONF);
 }
 
+sub realm_to_ip_mappings
+{
+       # this maps the SOCKET_WRAPPER_DEFAULT_IFACE value (which is the
+       # last byte of the IP address) for the various testenv PDCs to the
+       # corresponding DNS realm.
+       # This should always match the values in get_interface()
+       my %testenv_iface_mapping = (
+               'adnonssdom.samba.example.com'    => 17,    # addc_no_nss
+               'adnontlmdom.samba.example.com'   => 18,    # addc_no_ntlm
+               'samba2000.example.com'           => 25,    # dc5
+               'samba2003.example.com'           => 26,    # dc6
+               'samba2008r2.example.com'         => 27,    # dc7
+               'addom.samba.example.com'         => 30,    # addc
+               'sub.samba.example.com'           => 31,    # localsubdc
+               'chgdcpassword.samba.example.com' => 32,    # chgdcpass
+               'backupdom.samba.example.com'     => 40,    # backupfromdc
+               'renamedom.samba.example.com'     => 42,    # renamedc
+               'labdom.samba.example.com'        => 43,    # labdc
+               'samba.example.com'               => 21,    # localdc
+       );
+
+       my @mapping = ();
+
+       # convert the hashmap to a list of key=value strings
+       while (my ($key, $val) = each(%testenv_iface_mapping)) {
+               push(@mapping, "$key=$val");
+       }
+       # return the mapping as a single comma-separated string
+       return join(',', @mapping);
+}
+
 sub get_interface($)
 {
        my ($netbiosname) = @_;
@@ -394,7 +425,8 @@ sub get_interface($)
        # testenv to the DC's NETBIOS name. This value also corresponds to last
        # digit of the DC's IP address. Note that the NETBIOS name may differ from
        # the testenv name.
-       # Note that when adding a DC with a new realm, also update dns_hub.py.
+       # Note that when adding a DC with a new realm, also update
+       # get_realm_ip_mappings() above.
        my %testenv_iface_mapping = (
                localnt4dc2       => 3,
                localnt4member3   => 4,
index de7307d95c8e1b8e75afd26f35267bb30e8e1970..12f1a604c669d9c65947bf9e246c274b6d80f226 100755 (executable)
@@ -435,6 +435,7 @@ sub setup_dns_hub_internal($$$)
                $ENV{MAKE_TEST_BINARY} = "$self->{srcdir}/selftest/target/dns_hub.py";
                push (@args, "$self->{server_maxtime}");
                push (@args, "$env->{ipv4}");
+               push (@args, Samba::realm_to_ip_mappings());
                close($env->{STDIN_PIPE});
                open STDIN, ">&", $STDIN_READER or die "can't dup STDIN_READER to STDIN: $!";
 
index 4450993ecae7450509a69aa58e7d65b802715284..b0e2a2b7b670e559775bfc8c5b175fcc2a6fcd9d 100755 (executable)
@@ -72,24 +72,7 @@ class DnsHandler(sserver.BaseRequestHandler):
     def get_pdc_ipv4_addr(self, lookup_name):
         """Maps a DNS realm to the IPv4 address of the PDC for that testenv"""
 
-        # this maps the SOCKET_WRAPPER_DEFAULT_IFACE value (which is the
-        # last byte of the IP address) for the various testenv PDCs to the
-        # corresponding DNS realm.
-        # This should always match %testenv_iface_mapping in Samba.pm.
-        testenv_iface_mapping = {
-            'adnonssdom.samba.example.com': 17,     # addc_no_nss
-            'adnontlmdom.samba.example.com': 18,    # addc_no_ntlm
-            'samba2000.example.com': 25,            # dc5
-            'samba2003.example.com': 26,            # dc6
-            'samba2008r2.example.com': 27,          # dc7
-            'addom.samba.example.com': 30,          # addc
-            'sub.samba.example.com': 31,            # localsubdc
-            'chgdcpassword.samba.example.com': 32,  # chgdcpass
-            'backupdom.samba.example.com': 40,      # backupfromdc
-            'renamedom.samba.example.com': 42,      # renamedc
-            'labdom.samba.example.com': 43,         # labdc
-            'samba.example.com': 21,                # localdc
-        }
+        testenv_iface_mapping = self.server.realm_to_ip_mappings
 
         # sort the realms so we find the longest-match first
         testenv_realms = sorted(testenv_iface_mapping.keys(), key=len)
@@ -183,7 +166,14 @@ def main():
     timeout = int(sys.argv[1]) * 1000
     timeout = min(timeout, 2**31 - 1)  # poll with 32-bit int can't take more
     host = sys.argv[2]
+
     server = sserver.UDPServer((host, int(53)), DnsHandler)
+
+    # we pass in the realm-to-IP mappings as a comma-separated key=value
+    # string. Convert this back into a dictionary that the DnsHandler can use
+    realm_mapping = dict(kv.split('=') for kv in sys.argv[3].split(','))
+    server.realm_to_ip_mappings = realm_mapping
+
     t = server_thread(server)
     t.start()
     p = select.poll()