def test_generate_name_with_suffix(self): "Test length of generated name is equal to maximum defined in config + 1 for suffix." suffix = "prefix" name = generate_name(self.dc, suffix=suffix).split("-") self.assertEqual(len(name), self.dc.max_name_words + 1) self.assertNotEqual(name[0], suffix) self.assertEqual(name[-1], suffix)
def generate_tags_and_instance_id(key, config, prefix="", suffix="", dynamic=True): """Generate properly formatted Azure tags and instance_id. Args: config.id_labels = {(resource_id, node_name): tags} or None Returns: tags (list), instance_id (str) """ if not config.get("id_labels"): keys = TAG_KEYS.get(key) tags = [dicta(key=key, v=generate_name(config)) for key in keys] instance_id = generate_instance_id(key, config) else: id_label_key = random.choice(list(config.id_labels.keys())) tag_key_list = random.choice(config.id_labels.get(id_label_key)) SEEN_KEYS = set() tags = [] for key, value in tag_key_list: if key not in SEEN_KEYS: tags.append(dicta(key=key, v=value)) SEEN_KEYS.update([key]) _, node_name = id_label_key instance_id = generate_instance_id(key, config, node_name) return tags, instance_id
def test_generate_name_with_prefix_and_suffix(self): "Test length of generated name is equal to maximum defined in config + 2." prefix = "prefix" suffix = "suffix" name = generate_name(self.dc, prefix=prefix, suffix=suffix).split("-") self.assertEqual(len(name), self.dc.max_name_words + 2) self.assertEqual(name[0], prefix) self.assertEqual(name[-1], suffix)
def generate_tags(key, config, prefix="", suffix="", dynamic=True): """Generate properly formatted AWS tags. Returns: list """ keys = RESOURCE_TAG_COLS.get(key) return [dicta(key=key, v=generate_name(config)) for key in keys]
def test_generate_name_not_dynamic_with_prefix_and_suffix(self): "Test length of generated name is equal to 2 (prefix + suffix)" prefix = "prefix" suffix = "suffix" name = generate_name(self.dc, prefix=prefix, suffix=suffix, dynamic=False).split("-") self.assertEqual(len(name), 2) self.assertEqual(name[0], prefix) self.assertEqual(name[-1], suffix)
def build_data(self, config, _random=False): # noqa: C901 """ Build a structure to fill out a nise yaml template. Struture has the form of: {start_date: date, (config.start_date) ens_date: date, (config.end_date) nodes: [ (number of nodes controlled by config.max_nodes) {node_name: str, (dynamic) cpu_cores: int, (config.max_node_cpu_cores) memory_gig: int, (config.max_node_memory_gig) resource_id: str, (dynamic) namespaces: [ (number of namespaces controlled by config.max_node_namespaces) {namespace: str, (dynamic) pods: [ (number of pods controlled by config.max_node_namespace_pods) pod_name: str, (dynamic) cpu_request: int, (config.max_node_namespace_pod_cpu_request) mem_request_gig: int, (config.max_node_namespace_pod_mem_request_gig) cpu_limit: int, (config.max_node_namespace_pod_cpu_limit) mem_limit_gig: int, (config.max_node_namespace_pod_mem_limit_gig) pod_seconds: int, (config.max_node_namespace_pod_seconds) labels: str (dynamic) ], volumes: [ volume_name: str, storage_class: str, volume_request_gig: int, labels: str, volume_claims: [ volume_claim_name: str, pod_name: str, labels: str, capacity_gig: int ] ]} ]} ]} Parameters: config : dicta Returns: dicta """ LOG.info("Data build starting") data = dicta(start_date=str(config.start_date), end_date=str(config.end_date), nodes=[], resourceid_labels=None) resourceid_labels = {} if _random: max_nodes = FAKER.random_int(1, config.max_nodes) else: max_nodes = config.max_nodes for node_ix in range(max_nodes): LOG.info(f"Building node {node_ix + 1}/{max_nodes}...") if _random: cores = FAKER.random_int(1, config.max_node_cpu_cores) memory = FAKER.random_int(1, config.max_node_memory_gig) else: cores = config.max_node_cpu_cores memory = config.max_node_memory_gig resource_id = generate_resource_id(config) node_name = generate_name(config) id_label_key = (resource_id, node_name) resourceid_labels[id_label_key] = [] node = dicta(name=node_name, cpu_cores=cores, memory_gig=memory, resource_id=resource_id, namespaces=[]) data.nodes.append(node) if _random: max_namespaces = FAKER.random_int(1, config.max_node_namespaces) else: max_namespaces = config.max_node_namespaces for namespace_ix in range(max_namespaces): LOG.info( f"Building node {node_ix + 1}/{max_nodes}; namespace {namespace_ix + 1}/{max_namespaces}..." ) namespace = dicta(name=generate_name(config, prefix=node.name), pods=[], volumes=[]) node.namespaces.append(namespace) if _random: max_pods = FAKER.random_int(1, config.max_node_namespace_pods) else: max_pods = config.max_node_namespace_pods LOG.info(f"Building {max_pods} pods...") for pod_ix in range(max_pods): if _random: cpu_req = FAKER.random_int(1, node.cpu_cores) mem_req = FAKER.random_int(1, node.memory_gig) cpu_lim = FAKER.random_int(1, node.cpu_cores) mem_lim = FAKER.random_int(1, node.memory_gig) pod_sec = FAKER.random_int( config.min_node_namespace_pod_seconds, config.max_node_namespace_pod_seconds, step=(config.max_node_namespace_pod_seconds // 10) or 1800, ) else: cpu_lim = cpu_req = node.cpu_cores mem_lim = mem_req = node.memory_gig pod_sec = config.max_node_namespace_pod_seconds pod_labels = generate_labels( config.max_node_namespace_pod_labels) resourceid_labels[id_label_key].append(pod_labels) pod = dicta( name=generate_name(config, prefix=namespace.name + "-pod", suffix=str(pod_ix), dynamic=False), cpu_request=cpu_req, mem_request_gig=mem_req, cpu_limit=cpu_lim, mem_limit_gig=mem_lim, pod_seconds=pod_sec, labels=pod_labels, ) namespace.pods.append(pod) if _random: max_volumes = FAKER.random_int( 1, config.max_node_namespace_volumes) else: max_volumes = config.max_node_namespace_volumes LOG.info(f"Building {max_volumes} volumes...") for volume_ix in range(max_volumes): if _random: storage_cls = config.storage_classes[FAKER.random_int( 0, len(config.storage_classes) - 1)] vol_req = FAKER.random_int( 1, config.max_node_namespace_volume_request_gig) else: storage_cls = config.storage_classes[0] vol_req = config.max_node_namespace_volume_request_gig volume_labels = generate_labels( config.max_node_namespace_volume_labels) resourceid_labels[id_label_key].append(volume_labels) volume = dicta( name=generate_name(config, prefix=namespace.name + "-vol", suffix=str(volume_ix), dynamic=False), storage_class=storage_cls, volume_request_gig=vol_req, labels=volume_labels, volume_claims=[], ) namespace.volumes.append(volume) if _random: max_volume_claims = FAKER.random_int( 1, config.max_node_namespace_volume_volume_claims) else: max_volume_claims = config.max_node_namespace_volume_volume_claims for volume_claim_ix in range(max_volume_claims): if _random: cap = FAKER.random_int( 1, config. max_node_namespace_volume_volume_claim_capacity_gig ) else: cap = config.max_node_namespace_volume_volume_claim_capacity_gig pod_name = namespace.pods[-1 if volume_claim_ix >= len( namespace.pods) else volume_claim_ix].name volume_claim_labels = generate_labels( config. max_node_namespace_volume_volume_claim_labels) resourceid_labels[id_label_key].append( volume_claim_labels) volume_claim = dicta( name=generate_name( config, prefix=namespace.name + "-vol-claim", suffix=str(volume_claim_ix), dynamic=False, ), pod_name=pod_name, labels=volume_claim_labels, capacity_gig=cap, ) volume.volume_claims.append(volume_claim) data.resourceid_labels = resourceid_labels return data
def test_generate_name(self): "Test length of generated name is equal to maximum defined in config." name = generate_name(self.dc).split("-") self.assertEqual(len(name), self.dc.max_name_words)