SshExportWorkflowTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Tests.EndToEnd.Infra;
  2. using Xunit.Abstractions;
  3. namespace Tests.EndToEnd.ExporterTests;
  4. [Collection("Yaml CLI tests")]
  5. public class SshExportWorkflowTests(
  6. TempYamlCliFixture fs,
  7. ITestOutputHelper outputHelper)
  8. : IClassFixture<TempYamlCliFixture> {
  9. private async Task<(string output, string yaml)> ExecuteAsync(params string[] args) {
  10. outputHelper.WriteLine($"rpk {string.Join(" ", args)}");
  11. var output = await YamlCliTestHost.RunAsync(
  12. args,
  13. fs.Root,
  14. outputHelper,
  15. "config.yaml");
  16. outputHelper.WriteLine(output);
  17. var yaml = await File.ReadAllTextAsync(Path.Combine(fs.Root, "config.yaml"));
  18. return (output, yaml);
  19. }
  20. [Fact]
  21. public async Task ssh_export_basic_workflow_test() {
  22. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  23. version: 1
  24. resources:
  25. - kind: System
  26. type: vm
  27. name: vm-web01
  28. tags:
  29. - prod
  30. labels:
  31. ip: 192.168.1.10
  32. ssh_user: ubuntu
  33. - kind: System
  34. type: vm
  35. name: vm-db01
  36. labels:
  37. ip: 192.168.1.20
  38. ssh_user: postgres
  39. """);
  40. (var output, var _) = await ExecuteAsync(
  41. "ssh", "export"
  42. );
  43. Assert.Equal("""
  44. Generated SSH Config:
  45. Host vm-db01
  46. HostName 192.168.1.20
  47. User postgres
  48. Host vm-web01
  49. HostName 192.168.1.10
  50. User ubuntu
  51. """, output);
  52. }
  53. [Fact]
  54. public async Task ssh_export_with_defaults_test() {
  55. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  56. version: 1
  57. resources:
  58. - kind: System
  59. type: vm
  60. name: vm1
  61. labels:
  62. ip: 10.0.0.1
  63. """);
  64. (var output, var _) = await ExecuteAsync(
  65. "ssh", "export",
  66. "--default-user", "admin",
  67. "--default-port", "2222",
  68. "--default-identity", "~/.ssh/id_rsa"
  69. );
  70. Assert.Equal("""
  71. Generated SSH Config:
  72. Host vm1
  73. HostName 10.0.0.1
  74. User admin
  75. Port 2222
  76. IdentityFile ~/.ssh/id_rsa
  77. """, output);
  78. }
  79. [Fact]
  80. public async Task ssh_export_respects_tag_filter() {
  81. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  82. version: 1
  83. resources:
  84. - kind: System
  85. type: vm
  86. name: prod-vm
  87. tags:
  88. - prod
  89. labels:
  90. ip: 10.0.0.1
  91. - kind: System
  92. type: vm
  93. name: staging-vm
  94. tags:
  95. - staging
  96. labels:
  97. ip: 10.0.0.2
  98. """);
  99. (var output, var _) = await ExecuteAsync(
  100. "ssh", "export",
  101. "--include-tags", "prod"
  102. );
  103. Assert.Contains("Host prod-vm", output);
  104. Assert.DoesNotContain("Host staging-vm", output);
  105. }
  106. [Fact]
  107. public async Task ssh_export_is_sorted_by_name() {
  108. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  109. version: 1
  110. resources:
  111. - kind: System
  112. type: vm
  113. name: b-host
  114. labels:
  115. ip: 10.0.0.2
  116. - kind: System
  117. type: vm
  118. name: a-host
  119. labels:
  120. ip: 10.0.0.1
  121. """);
  122. (var output, var _) = await ExecuteAsync(
  123. "ssh", "export"
  124. );
  125. var aIndex = output.IndexOf("Host a-host", StringComparison.Ordinal);
  126. var bIndex = output.IndexOf("Host b-host", StringComparison.Ordinal);
  127. Assert.True(aIndex < bIndex);
  128. }
  129. [Fact]
  130. public async Task ssh_export_skips_resources_without_address() {
  131. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  132. version: 1
  133. resources:
  134. - kind: System
  135. type: vm
  136. name: vm-with-ip
  137. labels:
  138. ip: 10.0.0.1
  139. - kind: System
  140. type: vm
  141. name: vm-no-ip
  142. """);
  143. (var output, var _) = await ExecuteAsync(
  144. "ssh", "export"
  145. );
  146. Assert.Contains("Host vm-with-ip", output);
  147. Assert.DoesNotContain("vm-no-ip", output);
  148. }
  149. }