def test_load_requests():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k.create_resource(open(TEST_DAEMONET).read())
    k._build_state()
    objects = filter(lambda x: isinstance(x, DaemonSet), k.state_objects)
    for p in objects:
        if p.metadata_name == "fluentd-elasticsearch":
            return
    raise ValueError("Could not find service loded")
Esempio n. 2
0
def test_queue_status():
    "test length and status of scheduler queue after load"
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k.create_resource(open(TEST_DAEMONET).read())
    k._build_state()
    scheduler = next(
        filter(lambda x: isinstance(x, Scheduler), k.state_objects))
    nodes = list(filter(lambda x: isinstance(x, Node), k.state_objects))
    assert scheduler.queueLength == len(nodes)
    assert scheduler.podQueue._get_value()
    assert scheduler.status == STATUS_SCHED["Changed"]
Esempio n. 3
0
def test_anyservice_interrupted_fromfiles():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k.create_resource(open(TEST_DAEMONET).read())
    k._build_state()
    mark_excluded_service(k.state_objects)
    p = AnyServiceInterrupted(k.state_objects)
    print_objects(k.state_objects)
    p.run(timeout=360, sessionName="test_anyservice_interrupted_fromfiles")
    if not p.plan:
        raise Exception("Could not solve %s" % p.__class__.__name__)
    print(Scenario(p.plan).asyaml())
Esempio n. 4
0
def test_load_pods_new():
    k = KubernetesCluster()
    k.load(open(TEST_PRIORITYCLASS).read())
    k.load(open(TEST_PODS).read())
    k._build_state()
    # TODO: check if pod is fully loaded
    pod = k.state_objects[2 + 3]
    assert isinstance(pod, Pod)
    assert len(pod.metadata_labels._get_value()) > 0
    assert pod.status == STATUS_POD["Running"]

    assert k.state_objects
def test_limits_for_pods_created():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k.create_resource(open(TEST_DAEMONET).read())
    k._build_state()
    objects = filter(lambda x: isinstance(x, Pod), k.state_objects)
    for p in objects:
        if str(p.metadata_name).startswith("fluentd-elasticsearch") and \
            p.cpuRequest > -1 and \
            p.memRequest > -1 and \
             p.memLimit > -1:
            return
    raise ValueError("Could not find service loded")
Esempio n. 6
0
def test_spec_selector_labels():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    for ds in filter(lambda x: isinstance(x, Service), k.state_objects):
        if labelFactory.get("app",
                            "redis-evict") in ds.spec_selector._get_value():
            return
    raise Exception("Can not check labels load")
Esempio n. 7
0
def test_load_folder_load_pod_labels():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    for ds in filter(lambda x: isinstance(x, Pod), k.state_objects):
        if labelFactory.get("app",
                            "redis-evict") in ds.metadata_labels._get_value():
            return
    raise Exception("Can not check labels load")
Esempio n. 8
0
def test_load_folder():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    # check that no pods are orphan
    pods = list(filter(lambda x: isinstance(x, Pod), k.state_objects))
    assert pods
    for pod in pods:
        assert pod.atNode._property_value != Node.NODE_NULL
Esempio n. 9
0
def test_service_load():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    objects = filter(lambda x: isinstance(x, Service), k.state_objects)
    for p in objects:
        if p.metadata_name == "redis-master-evict" and \
            labelFactory.get("app", "redis-evict") in p.metadata_labels._get_value():
            return
    raise ValueError("Could not find service loded")
Esempio n. 10
0
def test_load_folder_create_labels():
    k = KubernetesCluster()
    # k.load_dir(TEST_CLUSTER_FOLDER)
    k.create_resource(open(TEST_DAEMONET).read())
    k._build_state()
    for ds in filter(lambda x: isinstance(x, DaemonSet), k.state_objects):
        if labelFactory.get(
                "k8s-app",
                "fluentd-logging") in ds.metadata_labels._get_value():
            return
    raise Exception("Can not check labels load")
Esempio n. 11
0
def test_priority_is_loaded():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    priorityClasses = filter(lambda x: isinstance(x, PriorityClass),
                             k.state_objects)
    for p in priorityClasses:
        if p.metadata_name == "high-priority" and p.preemptionPolicy == POLICY["PreemptLowerPriority"]\
            and p.priority > 0:
            return
    raise ValueError("Could not find priority loded")
Esempio n. 12
0
def test_heapster_load():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    heapsterpod = next(filter(lambda x: isinstance(x, Pod) and \
        "heapster" in str(x.metadata_name), k.state_objects))
    criticalpc = next(filter(lambda x: isinstance(x, PriorityClass) \
        and x.metadata_name == "system-cluster-critical", k.state_objects))
    # print(heapsterpod.priorityClass._get_value(), criticalpc._get_value())
    assert heapsterpod.priorityClass._get_value() == criticalpc._get_value()
    assert heapsterpod.priorityClass == criticalpc
Esempio n. 13
0
def test_load_priorityclass_custom_high_priority():
    k = KubernetesCluster()
    k.load(open(TEST_PRIORITYCLASS).read())
    k._build_state()
    found = False
    for pc in filter(lambda x: isinstance(x, PriorityClass), k.state_objects):
        if pc.metadata_name == "high-priority":
            # print(pc.priority, pc.priority._get_value())
            assert pc.priority > 0
            found = True
    assert found
Esempio n. 14
0
def run(from_dir, output, create_from_file, timeout, exclude,
        ignore_nonexistent_exclusions, pipe):

    k = KubernetesCluster()

    click.echo(f"# Loading cluster definitions from {from_dir} ...")
    k.load_dir(from_dir)

    for f in create_from_file:
        click.echo(f"# Creating resource from {f} ...")
        k.create_resource(open(f).read())

    click.echo(f"# Building abstract state ...")
    k._build_state()
    if exclude != None:
        excludeList = []
        for kn in exclude.split(","):
            click.echo(f"# Exclude {kn} ...")
            excludeList.append(ExcludeDict(kn))
        mark_excluded(k.state_objects, excludeList,
                      ignore_nonexistent_exclusions)
    p = AnyServiceInterrupted(k.state_objects)
    # p.select_target_service()

    click.echo("# Solving ...")

    if stdout.isatty() and not pipe:
        with yaspin(Spinners.earth, text="") as sp:
            p.run(timeout=timeout, sessionName="cli_run")
            if not p.plan:
                sp.ok("✅ ")
                click.echo("# No scenario was found.")
            else:
                sp.fail("💥 ")
                click.echo("# Scenario found.")
                click.echo(Scenario(p.plan).asyaml())
    else:
        p.run(timeout=timeout, sessionName="cli_run")
        click.echo(Scenario(p.plan).asyaml())
Esempio n. 15
0
def test_eviction_fromfiles_strictgoal():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k.create_resource(open(TEST_DAEMONET).read())
    k._build_state()
    p = SingleGoalEvictionDetect(k.state_objects)
    p.select_target_service()
    p.run(timeout=360, sessionName="test_eviction_fromfiles_strictgoal")
    if not p.plan:
        raise Exception("Could not solve %s" % p.__class__.__name__)
    print(Scenario(p.plan).asyaml())
    if p.plan:
        i = 0
        for a in p.plan:
            i = i + 1
            print(
                i, ":", a.__class__.__name__, "\n",
                yaml.dump(
                    {
                        str(k):
                        repr(v._get_value()) if v else f"NONE_VALUE:{v}"
                        for (k, v) in a.kwargs.items()
                    },
                    default_flow_style=False))
Esempio n. 16
0
def test_service_status():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    objects = filter(lambda x: isinstance(x, Service), k.state_objects)
    service_found = False
    for p in objects:
        if p.metadata_name == "redis-master-evict" and \
            labelFactory.get("app", "redis-evict") in p.metadata_labels._get_value() and \
            labelFactory.get("app", "redis-evict") in p.spec_selector._get_value() and \
                p.status == STATUS_SERV["Pending"]:
            service_found = True
            break
    assert service_found

    objects = filter(lambda x: isinstance(x, Pod), k.state_objects)
    for p in objects:
        if p.targetService == Pod.TARGET_SERVICE_NULL and \
            labelFactory.get("app", "redis-evict") in p.metadata_labels._get_value():
            return

    raise ValueError("Could not find service loded")
Esempio n. 17
0
def test_service_active_pods():
    k = KubernetesCluster()
    k.load_dir(TEST_CLUSTER_FOLDER)
    k._build_state()
    p = StartServiceGoal(k.state_objects)
    p.select_target_service()
    global ALL_STATE
    ALL_STATE = k.state_objects
    # p.debug()
    try:
        p.xrun()
    except EmptyPlanError:
        return
    objects = filter(lambda x: isinstance(x, Service), k.state_objects)
    pods_active = False
    for p in objects:
        if p.metadata_name == "redis-master-evict" and \
            labelFactory.get("app", "redis-evict") in p.metadata_labels._get_value() and \
                p.status == STATUS_SERV["Started"] and\
                    p.amountOfActivePods > 0:
            pods_active = True
            break
    assert pods_active
Esempio n. 18
0
def test_load_pods():
    k = KubernetesCluster()
    k.load(open(TEST_PRIORITYCLASS).read())
    k.load(open(TEST_PODS).read())
    k._build_state()
    assert k.state_objects
Esempio n. 19
0
def test_4_1():
    # Initialize scheduler, globalvar
    k = KubernetesCluster()
    scheduler = next(
        filter(lambda x: isinstance(x, Scheduler), k.state_objects))
    # initial node state
    i = 0
    j = 0
    nodes = []
    pods = []

    # Service to detecte eviction
    s1 = Service()
    s1.metadata_name = "test-service"
    s1.amountOfActivePods = 0
    s1.antiaffinity = True
    s1.targetAmountOfPodsOnDifferentNodes = 4
    s1.isSearched = True
    services = []
    services.append(s1)
    s2 = Service()
    s2.metadata_name = "test-service2"
    s2.amountOfActivePods = 0
    # create Deploymnent that we're going to detect failure of...
    d = Deployment()
    d.spec_replicas = 2
    node_item = Node()
    node_item.metadata_name = "node 1"
    node_item.cpuCapacity = 8
    node_item.memCapacity = 8
    node_item.isNull = False
    node_item.status = STATUS_NODE["Active"]
    nodes.append(node_item)

    pod = build_running_pod_with_d(1, 2, 2, node_item, None, None, s1, pods)
    pod.nodeSelectorSet = True
    pod.nodeSelectorList.add(node_item)
    pod = build_running_pod_with_d(2, 2, 2, node_item, None, None, s1, pods)
    pod.nodeSelectorSet = True
    pod.nodeSelectorList.add(node_item)
    pod = build_running_pod_with_d(3, 2, 2, node_item, None, None, None, pods)
    pod = build_running_pod_with_d(4, 2, 2, node_item, None, None, None, pods)

    node_item = Node()
    node_item.metadata_name = "node 2"
    node_item.cpuCapacity = 8
    node_item.memCapacity = 8
    node_item.isNull = False
    node_item.status = STATUS_NODE["Active"]
    nodes.append(node_item)

    pod = build_running_pod_with_d(5, 2, 2, node_item, None, None, s1, pods)
    pod = build_running_pod_with_d(6, 2, 2, node_item, None, None, s1, pods)
    pod = build_running_pod_with_d(7, 2, 2, node_item, None, None, s2, pods)
    pod = build_running_pod_with_d(8, 2, 2, node_item, None, None, s2, pods)

    node_item = Node()
    node_item.metadata_name = "node 3"
    node_item.cpuCapacity = 4
    node_item.memCapacity = 4
    node_item.isNull = False
    node_item.status = STATUS_NODE["Active"]
    nodes.append(node_item)

    pod = build_running_pod_with_d(9, 2, 2, node_item, None, None, None, pods)

    node_item = Node()
    node_item.metadata_name = "node 4"
    node_item.cpuCapacity = 8
    node_item.memCapacity = 8
    node_item.isNull = False
    node_item.status = STATUS_NODE["Active"]
    nodes.append(node_item)

    node_item = Node()
    node_item.metadata_name = "node 5"
    node_item.cpuCapacity = 8
    node_item.memCapacity = 8
    node_item.isNull = False
    node_item.status = STATUS_NODE["Active"]
    nodes.append(node_item)

    node_item = Node()
    node_item.metadata_name = "node 6"
    node_item.cpuCapacity = 8
    node_item.memCapacity = 8
    node_item.isNull = False
    node_item.status = STATUS_NODE["New"]
    nodes.append(node_item)

    for node in nodes:
        for pod in pods:
            if not pod.nodeSelectorSet: pod.nodeSelectorList.add(node)
        for node2 in nodes:
            if node != node2:
                node2.different_than.add(node)

    # priority for pod-to-evict
    pc = PriorityClass()
    pc.priority = 10
    pc.metadata_name = "high-prio-test"

    k.state_objects.extend(nodes)
    k.state_objects.extend(pods)
    k.state_objects.extend([pc, s1, s2])
    create_objects = []
    k._build_state()
    globalVar = next(
        filter(lambda x: isinstance(x, GlobalVar), k.state_objects))
    scheduler = next(
        filter(lambda x: isinstance(x, Scheduler), k.state_objects))

    class Antiaffinity_prefered_k1(Antiaffinity_prefered):
        def goal(self):
            assert services[0].antiaffinity_prefered_policy_met == True

    p = Antiaffinity_prefered_k1(k.state_objects)
    Antiaffinity_prefered_k1.__name__ = inspect.stack()[0].function
    assert_conditions = ["manually_initiate_killing_of_podt",\
                        "Not_at_same_node",\
                        "Add_node"]
    not_assert_conditions = []
    print_objects(k.state_objects)
    test_case = StateSet()
    test_case.scheduler = scheduler
    test_case.globalVar = globalVar
    test_case.pods = pods
    test_case.nodes = nodes
    services = [s1, s2]
    test_case.services = services
    assert_brake = checks_assert_conditions_in_one_mode(
        k, p, assert_conditions, not_assert_conditions, "functional test",
        DEBUG_MODE)
Esempio n. 20
0
def test_load_nodes():
    k = KubernetesCluster()
    k.load(open(TEST_NODES).read())
    k._build_state()
    assert k.state_objects
Esempio n. 21
0
def test_load_deployments():
    k = KubernetesCluster()
    k.load(open(TEST_PRIORITYCLASS).read())
    k.load(open(TEST_NODES).read())
    k.load(open(TEST_PODS).read())
    k.load(open(TEST_DEPLOYMENTS).read())
    k._build_state()
    assert k.state_objects