Best Python code snippet using autotest_python
test_agent.py
Source:test_agent.py
...29 "key_data": "ssh-rsa asdf",30 "os_type": "asdf",31 }32class AgentTestCaseBase(test.NoDBTestCase):33 def _create_agent(self, instance):34 self.session = "session"35 self.virtapi = "virtapi"36 self.vm_ref = "vm_ref"37 return agent.XenAPIBasedAgent(self.session, self.virtapi,38 instance, self.vm_ref)39class AgentImageFlagsTestCase(AgentTestCaseBase):40 def test_agent_is_present(self):41 self.flags(xenapi_use_agent_default=False)42 instance = {"system_metadata":43 [{"key": "image_xenapi_use_agent", "value": "true"}]}44 self.assertTrue(agent.should_use_agent(instance))45 def test_agent_is_disabled(self):46 self.flags(xenapi_use_agent_default=True)47 instance = {"system_metadata":48 [{"key": "image_xenapi_use_agent", "value": "false"}]}49 self.assertFalse(agent.should_use_agent(instance))50 def test_agent_uses_deafault_when_prop_invalid(self):51 self.flags(xenapi_use_agent_default=True)52 instance = {"system_metadata":53 [{"key": "image_xenapi_use_agent", "value": "bob"}],54 "uuid": "uuid"}55 self.assertTrue(agent.should_use_agent(instance))56 def test_agent_default_not_present(self):57 self.flags(xenapi_use_agent_default=False)58 instance = {"system_metadata": []}59 self.assertFalse(agent.should_use_agent(instance))60 def test_agent_default_present(self):61 self.flags(xenapi_use_agent_default=True)62 instance = {"system_metadata": []}63 self.assertTrue(agent.should_use_agent(instance))64class SysMetaKeyTestBase():65 key = None66 def _create_agent_with_value(self, value):67 kwargs = {self.key: value}68 instance = _get_fake_instance(**kwargs)69 return self._create_agent(instance)70 def test_get_sys_meta_key_true(self):71 agent = self._create_agent_with_value("true")72 self.assertTrue(agent._get_sys_meta_key(self.key))73 def test_get_sys_meta_key_false(self):74 agent = self._create_agent_with_value("False")75 self.assertFalse(agent._get_sys_meta_key(self.key))76 def test_get_sys_meta_key_invalid_is_false(self):77 agent = self._create_agent_with_value("invalid")78 self.assertFalse(agent._get_sys_meta_key(self.key))79 def test_get_sys_meta_key_missing_is_false(self):80 instance = _get_fake_instance()81 agent = self._create_agent(instance)82 self.assertFalse(agent._get_sys_meta_key(self.key))83class SkipSshFlagTestCase(SysMetaKeyTestBase, AgentTestCaseBase):84 key = "image_xenapi_skip_agent_inject_ssh"85 def test_skip_ssh_key_inject(self):86 agent = self._create_agent_with_value("True")87 self.assertTrue(agent._skip_ssh_key_inject())88class SkipFileInjectAtBootFlagTestCase(SysMetaKeyTestBase, AgentTestCaseBase):89 key = "image_xenapi_skip_agent_inject_files_at_boot"90 def test_skip_inject_files_at_boot(self):91 agent = self._create_agent_with_value("True")92 self.assertTrue(agent._skip_inject_files_at_boot())93class InjectSshTestCase(AgentTestCaseBase):94 def test_inject_ssh_key_succeeds(self):95 instance = _get_fake_instance()96 agent = self._create_agent(instance)97 self.mox.StubOutWithMock(agent, "inject_file")98 agent.inject_file("/root/.ssh/authorized_keys",99 "\n# The following ssh key was injected by Nova"100 "\nssh-rsa asdf\n")101 self.mox.ReplayAll()102 agent.inject_ssh_key()103 def _test_inject_ssh_key_skipped(self, instance):104 agent = self._create_agent(instance)105 # make sure its not called106 self.mox.StubOutWithMock(agent, "inject_file")107 self.mox.ReplayAll()108 agent.inject_ssh_key()109 def test_inject_ssh_key_skipped_no_key_data(self):110 instance = _get_fake_instance()111 instance["key_data"] = None112 self._test_inject_ssh_key_skipped(instance)113 def test_inject_ssh_key_skipped_windows(self):114 instance = _get_fake_instance()115 instance["os_type"] = "windows"116 self._test_inject_ssh_key_skipped(instance)117 def test_inject_ssh_key_skipped_cloud_init_present(self):118 instance = _get_fake_instance(119 image_xenapi_skip_agent_inject_ssh="True")120 self._test_inject_ssh_key_skipped(instance)121class FileInjectionTestCase(AgentTestCaseBase):122 def test_inject_file(self):123 instance = _get_fake_instance()124 agent = self._create_agent(instance)125 self.mox.StubOutWithMock(agent, "_call_agent")126 b64_path = base64.b64encode('path')127 b64_contents = base64.b64encode('contents')128 agent._call_agent('inject_file',129 {'b64_contents': b64_contents,130 'b64_path': b64_path})131 self.mox.ReplayAll()132 agent.inject_file("path", "contents")133 def test_inject_files(self):134 instance = _get_fake_instance()135 agent = self._create_agent(instance)136 self.mox.StubOutWithMock(agent, "inject_file")137 files = [("path1", "content1"), ("path2", "content2")]138 agent.inject_file(*files[0])139 agent.inject_file(*files[1])140 self.mox.ReplayAll()141 agent.inject_files(files)142 def test_inject_files_skipped_when_cloud_init_installed(self):143 instance = _get_fake_instance(144 image_xenapi_skip_agent_inject_files_at_boot="True")145 agent = self._create_agent(instance)146 self.mox.StubOutWithMock(agent, "inject_file")147 files = [("path1", "content1"), ("path2", "content2")]148 self.mox.ReplayAll()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!