def test_cmk_isolate_sigterm(): temp_dir = tempfile.mkdtemp() conf_dir = os.path.join(temp_dir, "isolate") helpers.execute( "cp", ["-r", helpers.conf_dir("minimal"), "{}".format(conf_dir)]) c = config.Config(conf_dir) fifo = helpers.rand_str() helpers.execute("mkfifo", [fifo]) p = subprocess.Popen([ integration.cmk(), "isolate", CONF_DIR.format(conf_dir), POOL_SHARED, "echo 1 > {} && sleep 300".format(fifo) ]) cmk = psutil.Process(p.pid) # Wait for subprocess to exist helpers.execute(CAT.format(fifo)) clist = c.pool("shared").cpu_list("0", "0") assert cmk.pid in clist.tasks() # Send sigterm to cmk cmk.terminate() # Wait for cmk process to terminate cmk.wait() assert cmk.pid not in clist.tasks() helpers.execute("rm {}".format(fifo))
def test_cmk_isolate_pid_bookkeeping(): temp_dir = tempfile.mkdtemp() conf_dir = os.path.join(temp_dir, "isolate") helpers.execute( "cp", ["-r", helpers.conf_dir("minimal"), "{}".format(conf_dir)]) c = config.Config(conf_dir) fifo = helpers.rand_str() helpers.execute("mkfifo", [fifo]) p = subprocess.Popen([ integration.cmk(), "isolate", "--conf-dir={}".format(conf_dir), "--pool=shared", "echo 1 > {} && cat {}".format(fifo, fifo) ]) cmk = psutil.Process(p.pid) # Wait for subprocess to exist helpers.execute("cat {}".format(fifo)) clist = c.pool("shared").cpu_list("0", "0") assert cmk.pid in clist.tasks() # Signal subprocess to exit helpers.execute("echo 1 > {}".format(fifo)) # Wait for cmk process to terminate cmk.wait() assert cmk.pid not in clist.tasks() helpers.execute("rm {}".format(fifo))
def test_pool_exclusive(): c = config.Config(helpers.conf_dir("ok")) with c.lock(): pools = c.pools() assert not pools["controlplane"].exclusive() assert pools["dataplane"].exclusive() assert not pools["infra"].exclusive()
def test_init_success1(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) sockets = topology.Platform({0: quad_core()}) with patch(TOPOLOGY_PARSE, MagicMock(return_value=sockets)): temp_dir = tempfile.mkdtemp() init.init(os.path.join(temp_dir, "init"), 2, 1, "vertical", "vertical", "-1") c = config.Config(os.path.join(temp_dir, "init")) pools = c.pools() assert len(pools) == 3 assert "shared" in pools assert "exclusive" in pools assert "infra" in pools cl_exclusive = pools["exclusive"].cpu_lists() cl_shared = pools["shared"].cpu_lists() cl_infra = pools["infra"].cpu_lists() assert not pools["shared"].exclusive() assert pools["exclusive"].exclusive() assert not pools["infra"].exclusive() assert "0,4" in cl_exclusive assert "1,5" in cl_exclusive assert "2,6" in cl_shared assert "3,7" in cl_infra
def test_init_success_excl_non_isolcpus2(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("exclusive_non_isolcpus")) with patch(TOPOLOGY_ISCPU, MagicMock(return_value=eight_core_lscpu())): temp_dir = tempfile.mkdtemp() init.init(os.path.join(temp_dir, "init"), 1, 1, "vertical", "vertical", "0,3-5") c = config.Config(os.path.join(temp_dir, "init")) pools = c.pools() assert len(pools) == 4 assert "shared" in pools assert "exclusive" in pools assert "infra" in pools assert "exclusive-non-isolcpus" in pools cl_exclusive = pools["exclusive"].cpu_lists() cl_shared = pools["shared"].cpu_lists() cl_infra = pools["infra"].cpu_lists() cl_excl_non_isolcpus = pools["exclusive-non-isolcpus"].cpu_lists() assert not pools["shared"].exclusive() assert pools["exclusive"].exclusive() assert not pools["infra"].exclusive() assert pools["exclusive-non-isolcpus"].exclusive() assert cl_exclusive["1,9"] assert cl_shared["2,10"] assert "0,8" in cl_excl_non_isolcpus assert "3,11" in cl_excl_non_isolcpus assert "4,12" in cl_excl_non_isolcpus assert "5,13" in cl_excl_non_isolcpus assert cl_infra["6,14,7,15"]
def test_init_success1(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) sockets = topology.Platform({0: quad_core()}) with patch('intel.topology.parse', MagicMock(return_value=sockets)): temp_dir = tempfile.mkdtemp() init.init(os.path.join(temp_dir, "init"), 2, 1, "vertical", "vertical") c = config.Config(os.path.join(temp_dir, "init")) pools = c.pools() assert len(pools) == 3 assert "controlplane" in pools assert "dataplane" in pools assert "infra" in pools cldp = pools["dataplane"].cpu_lists() clcp = pools["controlplane"].cpu_lists() clinfra = pools["infra"].cpu_lists() assert not pools["controlplane"].exclusive() assert pools["dataplane"].exclusive() assert not pools["infra"].exclusive() assert "0,4" in cldp assert "1,5" in cldp assert "2,6" in clcp assert "3,7" in clinfra
def test_pool_exclusive(): c = config.Config(helpers.conf_dir("ok")) with c.lock(): pools = c.pools() assert not pools["shared"].exclusive() assert pools["exclusive"].exclusive() assert not pools["infra"].exclusive()
def test_pool_name(): c = config.Config(helpers.conf_dir("ok")) with c.lock(): pools = c.pools() assert pools["controlplane"].name() == "controlplane" assert pools["dataplane"].name() == "dataplane" assert pools["infra"].name() == "infra"
def test_init_success1_isolcpus(monkeypatch): # Set the procfs environment variable. This test kernel command line # sets isolcpus to lcpu IDs from cores 1, 2 and 3. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("isolcpus")) with patch("intel.topology.lscpu", MagicMock(return_value=quad_core_lscpu())): temp_dir = tempfile.mkdtemp() init.init(os.path.join(temp_dir, "init"), 2, 1, "vertical", "vertical") c = config.Config(os.path.join(temp_dir, "init")) pools = c.pools() assert len(pools) == 3 assert "controlplane" in pools assert "dataplane" in pools assert "infra" in pools cldp = pools["dataplane"].cpu_lists() clcp = pools["controlplane"].cpu_lists() clinfra = pools["infra"].cpu_lists() assert not pools["controlplane"].exclusive() assert pools["dataplane"].exclusive() assert not pools["infra"].exclusive() assert "1,5" in cldp assert "2,6" in cldp assert "3,7" in clcp assert "0,4" in clinfra
def test_config_pools(): c = config.Config(helpers.conf_dir("ok")) with c.lock(): pools = c.pools() assert len(pools) == 3 assert "shared" in pools assert "exclusive" in pools assert "infra" in pools
def test_config_pools(): c = config.Config(helpers.conf_dir("ok")) with c.lock(): pools = c.pools() assert len(pools) == 3 assert "controlplane" in pools assert "dataplane" in pools assert "infra" in pools
def test_pool_cpu_lists(): c = config.Config(helpers.conf_dir("ok")) with c.lock(): pools = c.pools() clists = pools["dataplane"].cpu_lists() assert len(clists) == 4 assert len(clists["4,12"].tasks()) == 1 assert 2000 in clists["4,12"].tasks()
def add_node_oir(namespace): pod_name = os.environ["HOSTNAME"] node_name = k8s.get_node_from_pod(None, pod_name) configmap_name = "cmk-config-{}".format(node_name) c = config.Config(configmap_name, pod_name, namespace) c.lock() num_excl_non_isolcpus = None if "exclusive" not in c.get_pools(): raise KeyError("Exclusive pool does not exist") num_slots = len(c.get_pool("exclusive").get_core_lists()) if "exclusive-non-isolcpus" in c.get_pools(): num_excl_non_isolcpus = len(c.get_pool("exclusive-non-isolcpus") .get_core_lists()) c.unlock() patch_path = ("/status/capacity/pod.alpha.kubernetes.io~1opaque-int-" "resource-cmk") patch_body = [{ "op": "add", "path": patch_path, "value": num_slots }] try: patch_k8s_node_status(patch_body) except K8sApiException as err: logging.error("Exception when patching node with OIR: {}" .format(err)) logging.error(ABORTING_DISCOVER) sys.exit(1) if num_excl_non_isolcpus: patch_path = ("/status/capacity/pod.alpha.kubernetes.io~1opaque-int-" "resource-cmk-excl-non-isolcpus") patch_body = [{ "op": "add", "path": patch_path, "value": num_excl_non_isolcpus }] try: patch_k8s_node_status(patch_body) except K8sApiException as err: logging.error("Exception when patching node with OIR: {}" .format(err)) logging.error(ABORTING_DISCOVER) sys.exit(1)
def add_node_er(conf_dir): c = config.Config(conf_dir) with c.lock(): if "exclusive" not in c.pools(): raise KeyError("Exclusive pool does not exist") num_slots = len(c.pool("exclusive").cpu_lists()) patch_path = ("/status/capacity/cmk.intel.com~1exclusive-cores") patch_body = [{"op": "add", "path": patch_path, "value": num_slots}] try: patch_k8s_node_status(patch_body) except K8sApiException as err: logging.error("Exception when patching node with OIR: {}".format(err)) logging.error("Aborting discover ...") sys.exit(1)
def add_node_oir(conf_dir): c = config.Config(conf_dir) with c.lock(): if "dataplane" not in c.pools(): raise KeyError("Dataplane pool does not exist") if len(c.pool("dataplane").cpu_lists()) == 0: raise KeyError("No CPU list in dataplane pool") num_slots = len(c.pool("dataplane").cpu_lists()) patch_path = ("/status/capacity/pod.alpha.kubernetes.io~1opaque-int-" "resource-cmk") patch_body = [{"op": "add", "path": patch_path, "value": num_slots}] try: patch_k8s_node_status(patch_body) except K8sApiException as err: logging.error("Exception when patching node with OIR: {}".format(err)) logging.error("Aborting discover ...") sys.exit(1)
def add_node_oir(conf_dir): c = config.Config(conf_dir) num_excl_non_isolcpus = None with c.lock(): if "exclusive" not in c.pools(): raise KeyError("Exclusive pool does not exist") num_slots = len(c.pool("exclusive").cpu_lists()) if "exclusive-non-isolcpus" in c.pools(): num_excl_non_isolcpus = len(c.pool("exclusive-non-isolcpus") .cpu_lists()) patch_path = ("/status/capacity/pod.alpha.kubernetes.io~1opaque-int-" "resource-cmk") patch_body = [{ "op": "add", "path": patch_path, "value": num_slots }] try: patch_k8s_node_status(patch_body) except K8sApiException as err: logging.error("Exception when patching node with OIR: {}" .format(err)) logging.error(ABORTING_DISCOVER) sys.exit(1) if num_excl_non_isolcpus: patch_path = ("/status/capacity/pod.alpha.kubernetes.io~1opaque-int-" "resource-cmk-excl-non-isolcpus") patch_body = [{ "op": "add", "path": patch_path, "value": num_excl_non_isolcpus }] try: patch_k8s_node_status(patch_body) except K8sApiException as err: logging.error("Exception when patching node with OIR: {}" .format(err)) logging.error(ABORTING_DISCOVER) sys.exit(1)
def test_init_success2_isolcpus(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("isolcpus")) with patch(TOPOLOGY_ISCPU, MagicMock(return_value=quad_core_lscpu())): temp_dir = tempfile.mkdtemp() init.init(os.path.join(temp_dir, "init"), 1, 2, "vertical", "vertical", "-1") c = config.Config(os.path.join(temp_dir, "init")) pools = c.pools() assert len(pools) == 3 assert "shared" in pools assert "exclusive" in pools assert "infra" in pools cl_exclusive = pools["exclusive"].cpu_lists() cl_shared = pools["shared"].cpu_lists() cl_infra = pools["infra"].cpu_lists() assert not pools["shared"].exclusive() assert pools["exclusive"].exclusive() assert not pools["infra"].exclusive() assert "1,5" in cl_exclusive assert "2,6,3,7" in cl_shared assert "0,4" in cl_infra
def test_cmk_reconcile(monkeypatch): temp_dir = tempfile.mkdtemp() helpers.execute("cp", [ "-r", helpers.conf_dir("ok"), "{}".format(os.path.join( temp_dir, "reconcile")) ]) c = config.Config(os.path.join(temp_dir, "reconcile")) pools = c.pools() cl_exclusive = pools["exclusive"].cpu_lists() cl_shared = pools["shared"].cpu_lists() cl_exclusive["5,13"].add_task(1) cl_exclusive["6,14"].add_task(1789101112) cl_shared["3,11"].add_task(1234561231) assert helpers.execute(integration.cmk(), [ "reconcile", "--conf-dir={}".format(os.path.join( temp_dir, "reconcile")) ], {proc.ENV_PROC_FS: helpers.procfs_dir("ok")}) == b"""{ "reclaimedCpuLists": [ { "cpus": "3,11", "pid": 1000, "pool": "shared" }, { "cpus": "3,11", "pid": 1001, "pool": "shared" }, { "cpus": "3,11", "pid": 1002, "pool": "shared" }, { "cpus": "3,11", "pid": 1003, "pool": "shared" }, { "cpus": "4,12", "pid": 2000, "pool": "exclusive" }, { "cpus": "5,13", "pid": 2001, "pool": "exclusive" }, { "cpus": "6,14", "pid": 2002, "pool": "exclusive" }, { "cpus": "7,15", "pid": 2003, "pool": "exclusive" }, { "cpus": "0-2,8-10", "pid": 3000, "pool": "infra" }, { "cpus": "0-2,8-10", "pid": 3001, "pool": "infra" }, { "cpus": "0-2,8-10", "pid": 3002, "pool": "infra" }, { "cpus": "3,11", "pid": 1234561231, "pool": "shared" }, { "cpus": "6,14", "pid": 1789101112, "pool": "exclusive" } ] } """ expected_output = """{ "path": """ + "\"" + temp_dir + """/reconcile", "pools": { "exclusive": { "cpuLists": { "4,12": { "cpus": "4,12", "tasks": [] }, "5,13": { "cpus": "5,13", "tasks": [ 1 ] }, "6,14": { "cpus": "6,14", "tasks": [] }, "7,15": { "cpus": "7,15", "tasks": [] } }, "exclusive": true, "name": "exclusive" }, "infra": { "cpuLists": { "0-2,8-10": { "cpus": "0-2,8-10", "tasks": [] } }, "exclusive": false, "name": "infra" }, "shared": { "cpuLists": { "3,11": { "cpus": "3,11", "tasks": [] } }, "exclusive": false, "name": "shared" } } } """ actual_output = helpers.execute(integration.cmk(), [ "describe", "--conf-dir={}".format(os.path.join(temp_dir, "reconcile")) ]) assert actual_output == expected_output.encode("UTF-8") helpers.execute("rm", ["-rf", "{}".format(os.path.join(temp_dir, "reconcile"))])
def test_config_lock_timeout(monkeypatch): max_wait = 2 monkeypatch.setenv(config.ENV_LOCK_TIMEOUT, 0.1) with pytest.raises(KeyboardInterrupt): with config.Config(helpers.conf_dir("ok")).lock(): time.sleep(max_wait)