def test_init_success1(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) monkeypatch.setenv("HOSTNAME", "fake-pod") def configmap_mock(unused1, configmap, unused2): c = yaml.safe_load(configmap.data["config"]) conf = config.build_config(c) pools = conf.get_pools() assert len(pools) == 3 assert "exclusive" in pools assert "shared" in pools assert "infra" in pools exl_pool = conf.get_pool("exclusive") cl_exclusive = [cl.core_id for cl in exl_pool.get_core_lists()] sha_pool = conf.get_pool("shared") cl_shared = [cl.core_id for cl in sha_pool.get_core_lists()] inf_pool = conf.get_pool("infra") cl_infra = [cl.core_id for cl in inf_pool.get_core_lists()] assert exl_pool.is_exclusive() assert not sha_pool.is_exclusive() assert not inf_pool.is_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 mock = MagicMock(name="mock") mock.side_effect = configmap_mock with patch('intel.k8s.create_config_map', new=mock): init.init(2, 1, "vertical", "vertical", "-1", "fake-namespace")
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_init_failure3(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) sockets = topology.Platform({ 0: topology.Socket(0, { 0: topology.Core(0, { 0: topology.CPU(0), 4: topology.CPU(4) }), 1: topology.Core(1, { 1: topology.CPU(1), 5: topology.CPU(5) }), 2: topology.Core(2, { 2: topology.CPU(2), 6: topology.CPU(6) }) }) }) with patch('intel.topology.parse', MagicMock(return_value=sockets)): temp_dir = tempfile.mkdtemp() with pytest.raises(RuntimeError) as err: init.init(os.path.join(temp_dir, "init"), 2, 1, "vertical", "vertical") assert err is not None expected_msg = "No more free cores left to assign for infra" assert err.value.args[0] == expected_msg
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_failure2(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) sockets = topology.Platform({ 0: topology.Socket( 0, { 0: topology.Core(0, { 0: topology.CPU(0), 4: topology.CPU(4) }), 1: topology.Core(1, { 1: topology.CPU(1), 5: topology.CPU(5) }) }) }) with patch('intel.topology.parse', MagicMock(return_value=sockets)): with pytest.raises(RuntimeError) as err: init.init(2, 1, "vertical", "vertical", "-1", "fake-namespace") assert err is not None expected_msg = "No more free cores left to assign for shared" assert err.value.args[0] == expected_msg
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_init_success_excl_non_isolcpus1(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("exclusive_non_isolcpus")) monkeypatch.setenv("HOSTNAME", "fake-pod") def configmap_mock(unused1, configmap, unused2): c = yaml.load(configmap.data["config"], Loader=yaml.FullLoader) conf = config.build_config(c) pools = conf.get_pools() assert len(pools) == 4 assert "exclusive" in pools assert "shared" in pools assert "infra" in pools assert "exclusive-non-isolcpus" in pools exl_pool = conf.get_pool("exclusive") cl_exclusive = [cl.core_id for cl in exl_pool.get_core_lists()] sha_pool = conf.get_pool("shared") cl_shared = [cl.core_id for cl in sha_pool.get_core_lists()] inf_pool = conf.get_pool("infra") cl_infra = [cl.core_id for cl in inf_pool.get_core_lists()] exl_non_pool = conf.get_pool("exclusive-non-isolcpus") cl_excl_non = [cl.core_id for cl in exl_non_pool.get_core_lists()] assert exl_pool.is_exclusive() assert not sha_pool.is_exclusive() assert not inf_pool.is_exclusive() assert exl_non_pool.is_exclusive() assert "1,9" in cl_exclusive assert "2,10" in cl_shared assert "3,11,4,12,5,13,6,14,7,15" in cl_infra assert "0,8" in cl_excl_non mock = MagicMock(name="mock") mock.side_effect = configmap_mock with patch('intel.k8s.create_config_map', new=mock): init.init(1, 1, "vertical", "vertical", "0", "fake-namespace")
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 main(): setup_logging() args = docopt(__doc__, version="CMK v1.4.1") if args["cluster-init"]: clusterinit.cluster_init(args["--host-list"], args["--all-hosts"], args["--cmk-cmd-list"], args["--cmk-img"], args["--cmk-img-pol"], args["--conf-dir"], args["--install-dir"], args["--num-exclusive-cores"], args["--num-shared-cores"], args["--pull-secret"], args["--saname"], args["--exclusive-mode"], args["--shared-mode"], args["--namespace"], args["--excl-non-isolcpus"]) return if args["init"]: init.init(args["--conf-dir"], int(args["--num-exclusive-cores"]), int(args["--num-shared-cores"]), args["--exclusive-mode"], args["--shared-mode"], args["--excl-non-isolcpus"]) return if args["discover"]: discover.discover(args["--conf-dir"]) return if args["describe"]: describe.describe(args["--conf-dir"]) return if args["isolate"]: isolate.isolate(args["--conf-dir"], args["--pool"], args["--no-affinity"], args["<command>"], args["<args>"], args["--socket-id"]) return if args["reconcile"]: reconcile.reconcile(args["--conf-dir"], int(args["--interval"]), args["--publish"]) return if args["install"]: install.install(args["--install-dir"]) return if args["uninstall"]: uninstall.uninstall(args["--install-dir"], args["--conf-dir"], args["--namespace"]) return if args["node-report"]: nodereport.nodereport(args["--conf-dir"], int(args["--interval"]), args["--publish"]) return if args["webhook"]: webhook.webhook(args["--conf-file"])
def test_init_failure_excl_non_isolcpus3(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("exclusive_non_isolcpus")) with pytest.raises(RuntimeError) as err: init.init(1, 1, "vertical", "vertical", "-2", "fake-namespace") assert err is not None expected_msg = "Invalid core ID: -2" assert err.value.args[0] == expected_msg
def test_init_check_assignment_error(monkeypatch, caplog): 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"), 2, 1, "vertical", "vertical", "-1") with pytest.raises(SystemExit): init.check_assignment(os.path.join(temp_dir, "init"), -1, -1)
def test_init_failure_excl_non_isolcpus5(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("isolcpus")) with pytest.raises(RuntimeError) as err: init.init(1, 1, "vertical", "vertical", "3", "fake-namespace") assert err is not None expected_msg = "Isolated cores [3] cannot be placed in"\ " exclusive-non-isolcpus pool" assert err.value.args[0] == expected_msg
def test_init_failure_excl_non_isolcpus2(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("exclusive_non_isolcpus")) with pytest.raises(RuntimeError) as err: init.init(1, 1, "vertical", "vertical", "0,1", "fake-namespace") assert err is not None expected_msg = "Core(s) have already been assigned to pool(s): [1]"\ ", cannot add them to exclusive-non-isolcpus pool" assert err.value.args[0] == expected_msg
def test_init_failure_excl_non_isolcpus4(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("exclusive_non_isolcpus")) with pytest.raises(RuntimeError) as err: init.init(1, 1, "vertical", "vertical", "20,21", "fake-namespace") assert err is not None expected_msg = "Following physical cores not on system: [20, 21];"\ " you may be including logical CPUs of each core" assert err.value.args[0] == expected_msg
def test_init_failure_excl_non_isolcpus5(monkeypatch): monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("isolcpus")) with patch(TOPOLOGY_ISCPU, MagicMock(return_value=quad_core_lscpu())): temp_dir = tempfile.mkdtemp() with pytest.raises(RuntimeError) as err: init.init(os.path.join(temp_dir, "init"), 1, 1, "vertical", "vertical", "3") assert err is not None expected_msg = "Isolated cores [3] cannot be placed in"\ " exclusive-non-isolcpus pool" assert err.value.args[0] == expected_msg
def test_init_failure_excl_non_isolcpus3(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() with pytest.raises(RuntimeError) as err: init.init(os.path.join(temp_dir, "init"), 1, 1, "vertical", "vertical", "-2") assert err is not None expected_msg = "Invalid core ID: -2" assert err.value.args[0] == expected_msg
def test_init_failure_excl_non_isolcpus4(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() with pytest.raises(RuntimeError) as err: init.init(os.path.join(temp_dir, "init"), 1, 1, "vertical", "vertical", "20,21") assert err is not None expected_msg = "Following physical cores not on system: [20, 21];"\ " you may be including logical CPUs of each core" assert err.value.args[0] == expected_msg
def test_init_failure_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() with pytest.raises(RuntimeError) as err: init.init(os.path.join(temp_dir, "init"), 1, 1, "vertical", "vertical", "0,1") assert err is not None expected_msg = "Core(s) have already been assigned to pool(s): [1]"\ ", cannot add them to exclusive-non-isolcpus pool" assert err.value.args[0] == expected_msg
def test_init_config_exists_error(monkeypatch, caplog): # 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.init(os.path.join(temp_dir, "init"), 2, 1, "vertical", "vertical") # Try to init again, configuration should already exist init.init(os.path.join(temp_dir, "init"), 2, 1, "vertical", "vertical") caplog_tuple = caplog.record_tuples assert caplog_tuple[-1][2] == "Configuration directory already exists."
def main(): setup_logging() args = docopt(__doc__, version="CMK v1.0.1") if args["cluster-init"]: clusterinit.cluster_init(args["--host-list"], args["--all-hosts"], args["--cmk-cmd-list"], args["--cmk-img"], args["--cmk-img-pol"], args["--conf-dir"], args["--install-dir"], args["--num-dp-cores"], args["--num-cp-cores"], args["--pull-secret"], args["--saname"]) return if args["init"]: init.init(args["--conf-dir"], int(args["--num-dp-cores"]), int(args["--num-cp-cores"])) return if args["discover"]: discover.discover(args["--conf-dir"]) return if args["describe"]: describe.describe(args["--conf-dir"]) return if args["isolate"]: isolate.isolate(args["--conf-dir"], args["--pool"], args["--no-affinity"], args["<command>"], args["<args>"]) return if args["reconcile"]: reconcile.reconcile(args["--conf-dir"], int(args["--interval"]), args["--publish"]) return if args["install"]: install.install(args["--install-dir"]) return if args["uninstall"]: uninstall.uninstall(args["--install-dir"], args["--conf-dir"]) return if args["node-report"]: nodereport.nodereport(args["--conf-dir"], int(args["--interval"]), args["--publish"]) return
def test_init_failure1(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) sockets = { 0: topology.Socket(0, { 0: topology.Core(0, { 0: topology.CPU(0), 2: topology.CPU(2) }), 1: topology.Core(1, { 1: topology.CPU(1), 3: topology.CPU(3) }) }) } with patch('intel.topology.parse', MagicMock(return_value=sockets)): temp_dir = tempfile.mkdtemp() with pytest.raises(RuntimeError): init.init(os.path.join(temp_dir, "init"), 2, 1)
def test_init_failure1(monkeypatch): # Set the procfs environment variable. monkeypatch.setenv(proc.ENV_PROC_FS, helpers.procfs_dir("ok")) sockets = topology.Platform({ 0: topology.Socket( 0, { 0: topology.Core(0, { 0: topology.CPU(0), 2: topology.CPU(2) }), 1: topology.Core(1, { 1: topology.CPU(1), 3: topology.CPU(3) }) }) }) with patch('intel.topology.parse', MagicMock(return_value=sockets)): with pytest.raises(RuntimeError): init.init(2, 1, "vertical", "vertical", "-1", "fake-namespace")
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