def test_rm(self): new_sample = os.path.join(os.path.dirname(__file__), "sample.rm") shutil.copy(sample, new_sample) cli.main(["ssh_config", "-f", new_sample, "rm", "-y", "server1"]) sshconfig = SSHConfig.load(new_sample) host = sshconfig.get("server1", raise_exception=False) self.assertIsNone(host) os.remove(new_sample)
def test_cli(self): f = StringIO() with redirect_stdout(f): try: cli.main(["ssh_config", "-v"]) except SystemExit: pass output = f.getvalue().strip() self.assertTrue(output.startswith("ssh_config"))
def test_import(self): new_sample = os.path.join(os.path.dirname(__file__), "sample.import") shutil.copy(sample, new_sample) import_csv = os.path.join(os.path.dirname(__file__), "import.csv") cli.main(["ssh_config", "-f", new_sample, "import", "-q", "-y", import_csv]) sshconfig = SSHConfig.load(new_sample) import_1 = sshconfig.remove("import1") import_2 = sshconfig.remove("import2") sshconfig.write() self.assertTrue(import_1) self.assertTrue(import_2) os.remove(new_sample)
def test_bastion(self): self.maxDiff = None new_sample = os.path.join(os.path.dirname(__file__), "sample.update") shutil.copy(sample, new_sample) cli.main( [ "ssh_config", "-f", new_sample, "add", "-b", "-y", "bastion1", "HostName=bastion.example.com", "User=ssh_user", ] ) configs = SSHConfig.load(new_sample) self.assertEqual( configs.get("bastion1").attributes(), { "HostName": "bastion.example.com", "User": "******", "ProxyCommand": "none", "ForwardAgent": "yes", }, ) cli.main( [ "ssh_config", "-f", new_sample, "bastion", "-y", "bastion1", "server1", ] ) configs = SSHConfig.load(new_sample) self.assertEqual( configs.get("bastion1").attributes(), { "HostName": "bastion.example.com", "User": "******", "ProxyCommand": "none", "ForwardAgent": "yes", }, ) os.remove(new_sample)
def test_ls_with_pattern(self): expect = u"""\ Host HostName User Port IdentityFile ======================================================== server_cmd_1 203.0.113.76 None 2202 None server_cmd_2 203.0.113.76 user 22 None server_cmd_3 203.0.113.76 user 2202 None """ f = StringIO() with redirect_stdout(f): cli.main(["ssh_config", "-f", sample, "ls", "server_*"]) output = f.getvalue() self.maxDiff = None self.assertEqual(expect, output)
def test_add(self): sample_add = os.path.join(os.path.dirname(__file__), "sample.add") shutil.copy(sample, sample_add) cli.main([ "ssh_config", "-f", sample_add, "add", "-y", "test_add", "HostName=238.0.4.1", ]) sshconfig = SSHConfig.load(sample_add) host = sshconfig.get("test_add", raise_exception=False) self.assertIsNotNone(host) self.assertEqual(host.HostName, "238.0.4.1") os.remove(sample_add)
def test_update(self): new_sample = os.path.join(os.path.dirname(__file__), "sample.update") shutil.copy(sample, new_sample) cli.main([ "ssh_config", "-f", new_sample, "update", "-y", "server1", "IdentityFile=~/.ssh/id_rsa_test", ]) sshconfig = SSHConfig.load(new_sample) host = sshconfig.get("server1", raise_exception=False) self.assertEqual("203.0.113.76", host.HostName) self.assertEqual("~/.ssh/id_rsa_test", host.IdentityFile) os.remove(new_sample)
def test_update_error(self): new_sample = os.path.join(os.path.dirname(__file__), "sample.update") shutil.copy(sample, new_sample) with self.assertRaises(Exception) as e: cli.main([ "ssh_config", "-f", new_sample, "update", "-y", "server1", "IdentityFile", ]) self.assertTrue( str(e.exception).startswith( "<attribute=value> like options aren't provided, list index out of range, ['IdentityFile']" )) os.remove(new_sample)
def test_update_with_pattern(self): new_sample = os.path.join(os.path.dirname(__file__), "sample.update") shutil.copy(sample, new_sample) cli.main([ "ssh_config", "-f", new_sample, "add", "-y", "-p", "server_*", "IdentityFile=~/.ssh/id_rsa_test", ]) sshconfig = SSHConfig.load(new_sample) for host in sshconfig: if "server_cmd" in host.name: self.assertEqual("203.0.113.76", host.HostName) self.assertEqual("~/.ssh/id_rsa_test", host.IdentityFile) os.remove(new_sample)
def test_export(self): self.maxDiff = None outfile = os.path.join(os.path.dirname(__file__), "sample.out") cli.main(["ssh_config", "-f", sample, "export", "-x", "csv", outfile]) with open(outfile, "r") as f: self.assertEqual( u"""\ Name,HostName,User,Port,IdentityFile *,,,, host_1 host_2,%h.test.com,user,2202, server1,203.0.113.76,,, server_cmd_1,203.0.113.76,,2202, server_cmd_2,203.0.113.76,user,22, server_cmd_3,203.0.113.76,user,2202, """, f.read(), ) os.remove(outfile) cli.main( [ "ssh_config", "-f", sample, "export", "-c", "HostName,User,Port,IdentityFile,ServerAliveInterval", "csv", outfile, ] ) with open(outfile, "r") as f: self.assertEqual( u"""\ Name,HostName,User,Port,IdentityFile,ServerAliveInterval *,,,,,40 host_1 host_2,%h.test.com,user,2202,, server1,203.0.113.76,,,,200 server_cmd_1,203.0.113.76,,2202,, server_cmd_2,203.0.113.76,user,22,, server_cmd_3,203.0.113.76,user,2202,, """, f.read(), ) os.remove(outfile) cli.main( ["ssh_config", "-f", sample, "export", "-g", "linux", "ansible", outfile] ) with open(outfile, "r") as f: self.assertEqual( u"""\ [linux] host_1 host_2 ansible_host=%h.test.com ansible_user=user server1 ansible_host=203.0.113.76 server_cmd_1 ansible_host=203.0.113.76 server_cmd_2 ansible_host=203.0.113.76 ansible_user=user server_cmd_3 ansible_host=203.0.113.76 ansible_user=user """, f.read(), ) os.remove(outfile)
def test_export(self): self.maxDiff = None outfile = os.path.join(os.path.dirname(__file__), "sample.out") cli.main(["ssh_config", "-f", sample, "export", outfile]) with open(outfile, "r") as f: self.assertEqual( u"""\ Name,HostName,User,Port,IdentityFile,ProxyCommand,LocalCommand,LocalForward,Match,AddKeysToAgent,AddressFamily,BatchMode,BindAddress,BindInterface,CanonialDomains,CnonicalizeFallbackLocal,IdentityAgent,LogLevel,PreferredAuthentications,ServerAliveInterval,ForwardAgent *,,,,,,,,,,,,,,,,,,,40, server1,203.0.113.76,,,,,,,,,,,,,,,,,,200, server_cmd_1,203.0.113.76,,2202,,,,,,,,,,,,,,,,, server_cmd_2,203.0.113.76,user,22,,,,,,,,,,,,,,,,, server_cmd_3,203.0.113.76,user,2202,,,,,,,,,,,,,,,,, """, f.read(), ) os.remove(outfile) cli.main(["ssh_config", "-f", sample, "export", "csv", outfile]) with open(outfile, "r") as f: self.assertEqual( u"""\ Name,HostName,User,Port,IdentityFile,ProxyCommand,LocalCommand,LocalForward,Match,AddKeysToAgent,AddressFamily,BatchMode,BindAddress,BindInterface,CanonialDomains,CnonicalizeFallbackLocal,IdentityAgent,LogLevel,PreferredAuthentications,ServerAliveInterval,ForwardAgent *,,,,,,,,,,,,,,,,,,,40, server1,203.0.113.76,,,,,,,,,,,,,,,,,,200, server_cmd_1,203.0.113.76,,2202,,,,,,,,,,,,,,,,, server_cmd_2,203.0.113.76,user,22,,,,,,,,,,,,,,,,, server_cmd_3,203.0.113.76,user,2202,,,,,,,,,,,,,,,,, """, f.read(), ) os.remove(outfile) cli.main(["ssh_config", "-f", sample, "export", "-x", "csv", outfile]) with open(outfile, "r") as f: self.assertEqual( u"""\ Name,HostName,User,Port,IdentityFile *,,,, server1,203.0.113.76,,, server_cmd_1,203.0.113.76,,2202, server_cmd_2,203.0.113.76,user,22, server_cmd_3,203.0.113.76,user,2202, """, f.read(), ) os.remove(outfile) cli.main([ "ssh_config", "-f", sample, "export", "-c", "HostName,User,Port,IdentityFile,ServerAliveInterval", "csv", outfile, ]) with open(outfile, "r") as f: self.assertEqual( u"""\ Name,HostName,User,Port,IdentityFile,ServerAliveInterval *,,,,,40 server1,203.0.113.76,,,,200 server_cmd_1,203.0.113.76,,2202,, server_cmd_2,203.0.113.76,user,22,, server_cmd_3,203.0.113.76,user,2202,, """, f.read(), ) os.remove(outfile) cli.main([ "ssh_config", "-f", sample, "export", "-g", "linux", "ansible", outfile ]) with open(outfile, "r") as f: self.assertEqual( u"""\ [linux] server1 ansible_host=203.0.113.76 server_cmd_1 ansible_host=203.0.113.76 server_cmd_2 ansible_host=203.0.113.76 ansible_user=user server_cmd_3 ansible_host=203.0.113.76 ansible_user=user """, f.read(), ) os.remove(outfile)