def test_direct_cycle(self): context = test_context() context.registry.register_collector("direct-cyc1", DirectCycle1()) context.registry.register_collector("direct-cyc2", DirectCycle2()) self.assertRaises(CyclicDependencyError, lambda *args: ExecutionGraph(context))
def test_reaction_to_downgrade_required(self): reactor = Python3InstallReactor() commands = reactor.react( validation_result_with(status=Status.DOWNGRADE_REQUIRED), ctx=test_context()) self.assertEqual(0, len(commands))
def test_validate_ok(self): validator = GCloudConfigValidator() result = validator.validate(input_data=GCloudConfig( account="*****@*****.**", auth_ok=True, docker_ok=True), ctx=test_context()) self.assertEqual(Status.OK, result.status)
def test_command_with_no_user_input(self): expected_command = ["cmd", "--arg"] command = ReactorCommand(cmd=expected_command) self.assertEqual( expected_command, command.resolve(ctx=test_context(mode=Mode.INTERACTIVE)))
def test_reaction_to_status_ok(self): reactor = BazelInstallReactor() commands = reactor.react(validation_result_with(status=Status.OK), ctx=test_context()) self.assertEqual(len(commands), 1) self.assertEqual("brew upgrade bazelbuild/tap/bazelisk", str(commands[0]))
def test_validate_auth_not_ok(self): validator = GCloudConfigValidator() result = validator.validate(GCloudConfig(account="*****@*****.**", auth_ok=False, docker_ok=True), ctx=test_context()) self.assertEqual(Status.NOT_FOUND, result.status)
def test_validate_low_space(self): validator = DiskInfoValidator() result = validator.validate(DiskInfo(filesystem="", total=100, used=91, free=9), ctx=test_context()) self.assertEqual(Status.WARNING, result.status)
def test_validate_ok(self): validator = DiskInfoValidator() result = validator.validate(DiskInfo(filesystem="", total=1, used=1, free=1), ctx=test_context()) self.assertEqual(Status.OK, result.status)
def test_reaction_to_upgrade_required(self): reactor = PythonInstallReactor() commands = reactor.react( validation_result_with(status=Status.UPGRADE_REQUIRED), ctx=test_context()) self.assertEqual(len(commands), 1) upgrade_command = commands[0] self.assertIn(" upgrade", str(upgrade_command))
def test_dry_run_sanity(self): context = test_context() context.registry = _inspection_context().registry context.flags.dryrun = True summary = run_embedded(context) # just checking that something has been executed and that we got an expected result structure self.assertGreater(summary.total_count, 0) self.assertGreaterEqual(summary.problem_count, 0)
def test_not_found_reaction(self): reactor = XcodeInstallReactor() commands = reactor.react( validation_result_with(status=Status.NOT_FOUND), ctx=test_context(mode=Mode.INTERACTIVE)) self.assertEqual(1, len(commands)) install_command = commands[0] self.assertEqual("xcode-select --install", str(install_command))
def test_reaction_to_not_found_docker(self): reactor = GCloudConfigInstallReactor() commands = reactor.react(data=validation_result_with( status=Status.NOT_FOUND, docker_ok=False), ctx=test_context(mode=Mode.INTERACTIVE)) self.assertEqual(1, len(commands)) config_docker_command = commands[0] self.assertIn("auth configure-docker", str(config_docker_command))
def test_reaction_to_not_found(self): reactor = BazelInstallReactor() commands = reactor.react( validation_result_with(status=Status.NOT_FOUND), ctx=test_context()) self.assertEqual(len(commands), 3) self.assertEqual("brew tap bazelbuild/tap", str(commands[0])) self.assertEqual("brew tap-pin bazelbuild/tap", str(commands[1])) self.assertEqual("brew install bazelbuild/tap/bazelisk", str(commands[2]))
def test_reaction_to_downgrade_required(self): reactor = PythonInstallReactor() commands = reactor.react( validation_result_with(status=Status.DOWNGRADE_REQUIRED), ctx=test_context()) self.assertEqual(len(commands), 2) uninstall_command, install_command = commands self.assertIn(" uninstall", str(uninstall_command)) self.assertIn(" install", str(install_command))
def test_reaction_to_not_found(self): reactor = PythonInstallReactor() commands = reactor.react( validation_result_with(status=Status.NOT_FOUND), ctx=test_context()) self.assertEqual(len(commands), 1) install_command = commands[0] self.assertIn(" install", str(install_command))
def test_interactive_component_not_executed_in_non_interactive_mode(self): ctx = test_context(mode=Mode.BACKGROUND) executor = Executor() collector = InteractiveMockCollector() ctx.registry.register_collector("id", collector) executor.execute(ctx) self.assertFalse(collector.called)
def test_platform_incompatible_component_not_executed(self): ctx = test_context() executor = Executor() collector = PlatformInCompatible() ctx.registry.register_collector("id", collector) executor.execute(ctx) self.assertFalse(collector.called)
def test_experimental_component_not_executed(self): ctx = test_context() ctx.flags.experimental = False executor = Executor() collector = ExperimentalMockCollector() ctx.registry.register_collector("id", collector) executor.execute(ctx) self.assertFalse(collector.called)
def test_reaction_to_not_found(self): reactor = GCloudInstallReactor() commands = reactor.react( data=validation_result_with(status=Status.NOT_FOUND), ctx=test_context(mode=Mode.INTERACTIVE) ) self.assertEqual(1, len(commands)) install_command = commands[0] self.assertIn(" install", str(install_command))
def test_simple(self): context = test_context() context.registry.register_collector("c1", Comp1()) context.registry.register_collector("c2", Comp2()) context.registry.register_collector("c3", Comp3()) context.registry.register_collector("c4", Comp4()) nodes = list(ExecutionGraph(context).topologically_ordered_comp_ids()) self.assertTrue( list(["c1", "c2", "c3", "c4"]) == nodes or list(["c2", "c1", "c3", "c4"]) == nodes)
def test_execute_collection_only(self): ctx = test_context() executor = Executor() collector = MockCollector("data") ctx.registry.register_collector("id", collector) self.assertEqual(ExecutionSummary(total_count=1, problem_count=0), executor.execute(ctx)) self.assertTrue(collector.called)
def test_experimental_component_executed_when_experimental_flag_is_on( self): ctx = test_context() ctx.flags.experimental = True executor = Executor() collector = ExperimentalMockCollector() ctx.registry.register_collector("id", collector) executor.execute(ctx) self.assertTrue(collector.called)
def context_with_unavailable_address(): from uuid import uuid4 ctx = test_context() ctx.config = { "network": { "check_specs": [{ "address": "http://localhost/{}".format(uuid4()), "failure_message": "dummy" }] } } return ctx
def test_reaction_to_upgrade_required(self): reactor = BazelInstallReactor() commands = reactor.react( validation_result_with(status=Status.UPGRADE_REQUIRED), ctx=test_context()) self.assertEqual(4, len(commands)) self.assertEqual("brew uninstall bazelbuild/tap/bazel", str(commands[0])) self.assertEqual("brew tap bazelbuild/tap", str(commands[1])) self.assertEqual("brew tap-pin bazelbuild/tap", str(commands[2])) self.assertEqual("brew install bazelbuild/tap/bazelisk", str(commands[3]))
def test_execute_validation_only(self): ctx = test_context() executor = Executor() collector = MockCollector("data") validator = MockValidator(result=validation_result_with("data")) ctx.registry.register_collector("id", collector) ctx.registry.register_validator("id", validator) self.assertEqual(ExecutionSummary(total_count=1, problem_count=1), executor.execute(ctx)) self.assertTrue(collector.called) self.assertTrue(validator.called)
def test_execute_flow_with_multiple_components(self): ctx = test_context() handler = RecordingHandler(fail=True) executor = Executor() collector1 = MockCollector("comp1_data") collector2 = MockCollector("comp2_data") ctx.registry.register_collector("comp_1", collector1) ctx.registry.register_collector("comp_2", collector2) self.assertEqual(ExecutionSummary(total_count=2, problem_count=0), executor.execute(ctx, get_handler=handler.get)) self.assertTrue(collector1.called) self.assertTrue(collector2.called)
def test_reaction_to_not_found_auth(self): reactor = GCloudConfigInstallReactor() commands = reactor.react(data=validation_result_with( status=Status.NOT_FOUND, auth_ok=False), ctx=test_context(mode=Mode.INTERACTIVE)) self.assertEqual(2, len(commands)) login_command = commands[0] self.assertIn("auth login <user_input:gcloud_email>", str(login_command)) app_default_login_command = commands[1] self.assertIn("auth application-default login", str(app_default_login_command))
def test_execute_flow_with_empty_reactor(self): ctx = test_context() handler = RecordingHandler() executor = Executor() reactor = MockReactor() ctx.registry.register_collector("id", MockCollector("data")) ctx.registry.register_validator( "id", MockValidator(result=validation_result_with("data"))) ctx.registry.register_reactor("id", reactor) self.assertEqual(ExecutionSummary(total_count=1, problem_count=1), executor.execute(ctx, get_handler=handler.get)) self.assertTrue(reactor.called) self.assertEqual(0, len(handler.recorded))
def test_snapshot(self): env = EnvDataCollector(test_context(), "", "") snapshot = env.snapshot().data self.assertIsNotNone(snapshot) self.assert_non_empty_key(snapshot, "timestamp_utc") self.assert_non_empty_key(snapshot, "user") self.assert_non_empty_key(snapshot, "hostname") self.assert_non_empty_key(snapshot, "cpu_count") self.assert_non_empty_key(snapshot, "total_ram") self.assert_non_empty_key(snapshot, "os") os_info = snapshot["os"] self.assert_non_empty_key(os_info, "name") self.assert_non_empty_key(os_info, "version") self.assert_non_empty_key(snapshot, "bazel") bazel_info = snapshot["bazel"] self.assert_non_empty_key(bazel_info, "path") self.assert_non_empty_key(bazel_info, "bazelisk") self.assert_non_empty_key(bazel_info, "version") self.assert_non_empty_key(bazel_info, "env.USE_BAZEL_VERSION") self.assert_non_empty_key(snapshot, "python") python_info = snapshot["python"] self.assert_non_empty_key(python_info, "version") self.assert_non_empty_key(snapshot, "disk") disk_info = snapshot["disk"] self.assert_non_empty_key(disk_info, "filesystem") self.assert_non_empty_key(disk_info, "free") self.assert_non_empty_key(disk_info, "used") self.assert_non_empty_key(disk_info, "total") self.assert_non_empty_key(snapshot, "gcloud") gcloud_info = snapshot["gcloud"] self.assert_non_empty_key(gcloud_info, "configured") self.assert_non_empty_key(snapshot, "docker") docker_info = snapshot["docker"] self.assert_non_empty_key(docker_info, "server_installed") self.assert_non_empty_key(docker_info, "configured") self.assert_non_empty_key(snapshot, "network") network_info = snapshot["network"] self.assert_non_empty_key(network_info, "connectivity_checks")
def test_filtered_component_list(self): ctx = test_context(mode=Mode.BACKGROUND) ctx.components = ["id1", "id3"] executor = Executor() collector1 = MockCollector("data") collector2 = MockCollector("data") collector3 = MockCollector("data") ctx.registry.register_collector("id1", collector1) ctx.registry.register_collector("id2", collector2) ctx.registry.register_collector("id3", collector3) executor.execute(ctx) self.assertTrue(collector1.called) self.assertFalse(collector2.called) self.assertTrue(collector3.called)