AnsibleInventoryWorkflowTests.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ansible_inventory_yaml_output_test() {
  74. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  75. version: 1
  76. resources:
  77. - kind: System
  78. type: vm
  79. os: ubuntu-22.04
  80. cores: 2
  81. ram: 4
  82. name: vm-web01
  83. tags:
  84. - prod
  85. labels:
  86. ansible_host: 192.168.1.10
  87. ansible_user: ubuntu
  88. env: prod
  89. """);
  90. (var output, var _) = await ExecuteAsync(
  91. "ansible", "inventory",
  92. "--format", "yaml",
  93. "--group-tags", "prod",
  94. "--group-labels", "env"
  95. );
  96. Assert.Equal("""
  97. Generated Inventory:
  98. all:
  99. children:
  100. env_prod:
  101. hosts:
  102. vm-web01:
  103. ansible_host: 192.168.1.10
  104. ansible_user: ubuntu
  105. prod:
  106. hosts:
  107. vm-web01:
  108. ansible_host: 192.168.1.10
  109. ansible_user: ubuntu
  110. """, output);
  111. }
  112. [Fact]
  113. public async Task ansible_inventory_groups_are_sorted() {
  114. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  115. version: 1
  116. resources:
  117. - kind: System
  118. type: vm
  119. name: b-host
  120. tags: [zeta]
  121. labels:
  122. ansible_host: 10.0.0.2
  123. - kind: System
  124. type: vm
  125. name: a-host
  126. tags: [alpha]
  127. labels:
  128. ansible_host: 10.0.0.1
  129. """);
  130. (var output, var _) = await ExecuteAsync(
  131. "ansible", "inventory",
  132. "--group-tags", "alpha,zeta"
  133. );
  134. var alphaIndex = output.IndexOf("[alpha]", StringComparison.Ordinal);
  135. var zetaIndex = output.IndexOf("[zeta]", StringComparison.Ordinal);
  136. Assert.True(alphaIndex < zetaIndex);
  137. }
  138. [Fact]
  139. public async Task yaml_format_does_not_emit_ini_sections() {
  140. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  141. version: 1
  142. resources:
  143. - kind: System
  144. type: vm
  145. name: vm1
  146. labels:
  147. ansible_host: 10.0.0.1
  148. """);
  149. (var output, var _) = await ExecuteAsync(
  150. "ansible", "inventory",
  151. "--format", "yaml"
  152. );
  153. Assert.DoesNotContain("[", output);
  154. Assert.Contains("all:", output);
  155. }
  156. }