AnsibleInventoryWorkflowTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Tests.EndToEnd.Infra;
  2. using Xunit.Abstractions;
  3. namespace Tests.EndToEnd.AnsibleTests;
  4. [Collection("Yaml CLI tests")]
  5. public class AnsibleInventoryWorkflowTests(
  6. TempYamlCliFixture fs,
  7. ITestOutputHelper outputHelper)
  8. : IClassFixture<TempYamlCliFixture>
  9. {
  10. private async Task<(string output, string yaml)> ExecuteAsync(params string[] args)
  11. {
  12. outputHelper.WriteLine($"rpk {string.Join(" ", args)}");
  13. var output = await YamlCliTestHost.RunAsync(
  14. args,
  15. fs.Root,
  16. outputHelper,
  17. "config.yaml");
  18. outputHelper.WriteLine(output);
  19. var yaml = await File.ReadAllTextAsync(Path.Combine(fs.Root, "config.yaml"));
  20. return (output, yaml);
  21. }
  22. [Fact]
  23. public async Task ansible_inventory_cli_workflow_test()
  24. {
  25. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), """
  26. version: 1
  27. resources:
  28. - kind: System
  29. type: vm
  30. os: ubuntu-22.04
  31. cores: 2
  32. ram: 4
  33. name: vm-web01
  34. tags:
  35. - prod
  36. labels:
  37. ansible_host: 192.168.1.10
  38. ansible_user: ubuntu
  39. env: prod
  40. - kind: System
  41. type: vm
  42. os: debian-12
  43. cores: 2
  44. ram: 2
  45. name: vm-staging01
  46. tags:
  47. - staging
  48. labels:
  49. ansible_host: 192.168.1.20
  50. ansible_user: debian
  51. env: staging
  52. """);
  53. var (output, yaml) = await ExecuteAsync(
  54. "ansible", "inventory",
  55. "--group-tags", "prod,staging",
  56. "--group-labels", "env",
  57. "--global-var", "ansible_user=ansible",
  58. "--global-var", "ansible_python_interpreter=/usr/bin/python3"
  59. );
  60. Assert.Equal("""
  61. Generated Inventory:
  62. [all:vars]
  63. ansible_python_interpreter=/usr/bin/python3
  64. ansible_user=ansible
  65. [env_prod]
  66. vm-web01 ansible_host=192.168.1.10 ansible_user=ubuntu
  67. [env_staging]
  68. vm-staging01 ansible_host=192.168.1.20 ansible_user=debian
  69. [prod]
  70. vm-web01 ansible_host=192.168.1.10 ansible_user=ubuntu
  71. [staging]
  72. vm-staging01 ansible_host=192.168.1.20 ansible_user=debian
  73. """, output);
  74. }
  75. }