Esempio n. 1
0
    def test_timestamp(self):
        """Test timestamp generation."""
        self.assertEqual('y.x', hostmanager.generate_hostname('x', 'y'))
        self.assertEqual('y-dl7tpestdee.x',
                         hostmanager.generate_hostname('x', 'y-{time}'))

        time.time.return_value = 1538429096.025851
        h1 = hostmanager.generate_hostname('x', 'y-{time}')

        time.time.return_value = 1538429096.02586
        h2 = hostmanager.generate_hostname('x', 'y-{time}')

        time.time.return_value = 1538429096.03
        h3 = hostmanager.generate_hostname('x', 'y-{time}')

        self.assertTrue(h1 < h2)
        self.assertTrue(h2 < h3)
Esempio n. 2
0
def _generate_hostnames(domain,
                        cell,
                        partition,
                        count,
                        min_on_demand=None,
                        max_on_demand=None):
    hostname_template = '{}-{}-{}'.format(cell, partition, '{time}')

    # Sanitize DNS name.
    hostname_template = re.sub(r'[^a-zA-Z0-9-{}]', '', hostname_template)

    # All hosts are on-demand by default.
    if min_on_demand is None and max_on_demand is None:
        min_on_demand = max_on_demand = count

    hostnames = []
    # Generate hostnames in a loop, append seq. no. in case time is not unique.
    # For each generated hostname, figure instance lifecycle (on-demand/spot):
    # - First min_on_demand are on-demand.
    # - Next max_on_demand - min_on_demand are spot, can fallback to on-demand.
    # - Remaining ones are spot.
    for i in range(count):
        hostname = hostmanager.generate_hostname(
            domain, '{}{}'.format(hostname_template, i))
        if min_on_demand:
            try_spot = False
            try_on_demand = True
            min_on_demand -= 1
            if max_on_demand:
                max_on_demand -= 1
        elif max_on_demand:
            try_spot = try_on_demand = True
            max_on_demand -= 1
        else:
            try_spot = True
            try_on_demand = False
        hostnames.append((hostname, try_spot, try_on_demand))
    return hostnames