AnsibleInventoryWorkflowTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Tests.EndToEnd.Infra;
  2. using Xunit.Abstractions;
  3. namespace Tests.EndToEnd.ExporterTests;
  4. [Collection("Yaml CLI tests")]
  5. public class AnsibleInventoryWorkflowTests(
  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 ansible_inventory_cli_workflow_test() {
  22. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  23. version: 1
  24. resources:
  25. - kind: System
  26. type: vm
  27. os: ubuntu-22.04
  28. cores: 2
  29. ram: 4
  30. name: vm-web01
  31. tags:
  32. - prod
  33. labels:
  34. ansible_host: 192.168.1.10
  35. ansible_user: ubuntu
  36. env: prod
  37. - kind: System
  38. type: vm
  39. os: debian-12
  40. cores: 2
  41. ram: 2
  42. name: vm-staging01
  43. tags:
  44. - staging
  45. labels:
  46. ansible_host: 192.168.1.20
  47. ansible_user: debian
  48. env: staging
  49. """);
  50. (var output, var yaml) = await ExecuteAsync(
  51. "ansible", "inventory",
  52. "--group-tags", "prod,staging",
  53. "--group-labels", "env",
  54. "--global-var", "ansible_user=ansible",
  55. "--global-var", "ansible_python_interpreter=/usr/bin/python3"
  56. );
  57. Assert.Equal("""
  58. Generated Inventory:
  59. [all:vars]
  60. ansible_python_interpreter=/usr/bin/python3
  61. ansible_user=ansible
  62. [env_prod]
  63. vm-web01 ansible_host=192.168.1.10 ansible_user=ubuntu
  64. [env_staging]
  65. vm-staging01 ansible_host=192.168.1.20 ansible_user=debian
  66. [prod]
  67. vm-web01 ansible_host=192.168.1.10 ansible_user=ubuntu
  68. [staging]
  69. vm-staging01 ansible_host=192.168.1.20 ansible_user=debian
  70. """, output);
  71. }
  72. [Fact]
  73. public async Task a_system_with_only_its_ip_field_is_still_addressable() {
  74. // Discovered hosts carry an ip but no address labels; like the ssh and hosts
  75. // exporters, the inventory reads the System's own address. An explicit
  76. // ansible_host label still wins when both are present.
  77. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  78. version: 4
  79. resources:
  80. - kind: System
  81. name: scanned-host
  82. ip: 10.0.20.150
  83. tags:
  84. - lan
  85. - kind: System
  86. name: labelled-host
  87. ip: 10.0.20.151
  88. tags:
  89. - lan
  90. labels:
  91. ansible_host: vpn.example.com
  92. """);
  93. (var output, var _) = await ExecuteAsync("ansible", "inventory", "--group-tags", "lan");
  94. Assert.Contains("scanned-host ansible_host=10.0.20.150", output);
  95. Assert.Contains("labelled-host ansible_host=vpn.example.com", output);
  96. }
  97. [Fact]
  98. public async Task ansible_inventory_yaml_output_test() {
  99. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  100. version: 1
  101. resources:
  102. - kind: System
  103. type: vm
  104. os: ubuntu-22.04
  105. cores: 2
  106. ram: 4
  107. name: vm-web01
  108. tags:
  109. - prod
  110. labels:
  111. ansible_host: 192.168.1.10
  112. ansible_user: ubuntu
  113. env: prod
  114. """);
  115. (var output, var _) = await ExecuteAsync(
  116. "ansible", "inventory",
  117. "--format", "yaml",
  118. "--group-tags", "prod",
  119. "--group-labels", "env"
  120. );
  121. Assert.Equal("""
  122. Generated Inventory:
  123. all:
  124. children:
  125. env_prod:
  126. hosts:
  127. vm-web01:
  128. ansible_host: 192.168.1.10
  129. ansible_user: ubuntu
  130. prod:
  131. hosts:
  132. vm-web01:
  133. ansible_host: 192.168.1.10
  134. ansible_user: ubuntu
  135. """, output);
  136. }
  137. [Fact]
  138. public async Task ansible_inventory_groups_are_sorted() {
  139. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  140. version: 1
  141. resources:
  142. - kind: System
  143. type: vm
  144. name: b-host
  145. tags: [zeta]
  146. labels:
  147. ansible_host: 10.0.0.2
  148. - kind: System
  149. type: vm
  150. name: a-host
  151. tags: [alpha]
  152. labels:
  153. ansible_host: 10.0.0.1
  154. """);
  155. (var output, var _) = await ExecuteAsync(
  156. "ansible", "inventory",
  157. "--group-tags", "alpha,zeta"
  158. );
  159. var alphaIndex = output.IndexOf("[alpha]", StringComparison.Ordinal);
  160. var zetaIndex = output.IndexOf("[zeta]", StringComparison.Ordinal);
  161. Assert.True(alphaIndex < zetaIndex);
  162. }
  163. [Fact]
  164. public async Task yaml_format_does_not_emit_ini_sections() {
  165. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  166. version: 1
  167. resources:
  168. - kind: System
  169. type: vm
  170. name: vm1
  171. labels:
  172. ansible_host: 10.0.0.1
  173. """);
  174. (var output, var _) = await ExecuteAsync(
  175. "ansible", "inventory",
  176. "--format", "yaml"
  177. );
  178. Assert.DoesNotContain("[", output);
  179. Assert.Contains("all:", output);
  180. }
  181. }