SystemWorkflowTests.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Tests.EndToEnd.Infra;
  2. using Xunit.Abstractions;
  3. namespace Tests.EndToEnd.SystemTests;
  4. [Collection("Yaml CLI tests")]
  5. public class SystemWorkflowTests(TempYamlCliFixture fs, ITestOutputHelper outputHelper)
  6. : IClassFixture<TempYamlCliFixture>
  7. {
  8. private async Task<(string, string)> ExecuteAsync(params string[] args)
  9. {
  10. outputHelper.WriteLine($"rpk {string.Join(" ", args)}");
  11. var output = await YamlCliTestHost.RunAsync(
  12. args,
  13. fs.Root,
  14. outputHelper,
  15. "config.yaml"
  16. );
  17. outputHelper.WriteLine(output);
  18. var yaml = await File.ReadAllTextAsync(Path.Combine(fs.Root, "config.yaml"));
  19. return (output, yaml);
  20. }
  21. [Fact]
  22. public async Task systems_cli_workflow_test()
  23. {
  24. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), "");
  25. await ExecuteAsync("servers", "add", "proxmox-node01");
  26. // Add system
  27. var (output, yaml) = await ExecuteAsync("systems", "add", "sys01");
  28. Assert.Equal("System 'sys01' added.\n", output);
  29. Assert.Contains("name: sys01", yaml);
  30. // Update system
  31. (output, yaml) = await ExecuteAsync(
  32. "systems", "set", "sys01",
  33. "--type", "VM",
  34. "--os", "debian-12",
  35. "--cores", "2",
  36. "--ram", "4",
  37. "--runs-on", "proxmox-node01"
  38. );
  39. Assert.Equal("System 'sys01' updated.\n", output);
  40. outputHelper.WriteLine(yaml);
  41. Assert.Equal("""
  42. version: 2
  43. resources:
  44. - kind: Server
  45. name: proxmox-node01
  46. - kind: System
  47. type: vm
  48. os: debian-12
  49. cores: 2
  50. ram: 4
  51. name: sys01
  52. runsOn:
  53. - proxmox-node01
  54. """, yaml);
  55. // Get system
  56. (output, yaml) = await ExecuteAsync("systems", "get", "sys01");
  57. Assert.Equal("sys01 Type: vm, OS: debian-12, Cores: 2, RAM: 4GB, Storage: 0GB, RunsOn: \nproxmox-node01\n", output);
  58. // List systems (strict table)
  59. (output, yaml) = await ExecuteAsync("systems", "list");
  60. Assert.Equal("""
  61. ╭───────┬──────┬───────────┬───────┬──────────┬──────────────┬────────────────╮
  62. │ Name │ Type │ OS │ Cores │ RAM (GB) │ Storage (GB) │ Runs On │
  63. ├───────┼──────┼───────────┼───────┼──────────┼──────────────┼────────────────┤
  64. │ sys01 │ vm │ debian-12 │ 2 │ 4 │ 0 │ proxmox-node01 │
  65. ╰───────┴──────┴───────────┴───────┴──────────┴──────────────┴────────────────╯
  66. """, output);
  67. // Summary (strict table)
  68. (output, yaml) = await ExecuteAsync("systems", "summary");
  69. Assert.Equal("""
  70. ╭───────┬──────┬───────────┬───────┬──────────┬──────────────┬────────────────╮
  71. │ Name │ Type │ OS │ Cores │ RAM (GB) │ Storage (GB) │ Runs On │
  72. ├───────┼──────┼───────────┼───────┼──────────┼──────────────┼────────────────┤
  73. │ sys01 │ vm │ debian-12 │ 2 │ 4 │ 0 │ proxmox-node01 │
  74. ╰───────┴──────┴───────────┴───────┴──────────┴──────────────┴────────────────╯
  75. """, output);
  76. // Describe (loose)
  77. (output, yaml) = await ExecuteAsync("systems", "describe", "sys01");
  78. Assert.Contains("sys01", output);
  79. Assert.Contains("vm", output);
  80. Assert.Contains("debian-12", output);
  81. Assert.Contains("Cores", output);
  82. Assert.Contains("RAM", output);
  83. Assert.Contains("Runs On", output);
  84. // Tree (loose)
  85. (output, yaml) = await ExecuteAsync("systems", "tree", "sys01");
  86. Assert.Contains("sys01", output);
  87. // ToDo add a service in the workflow to properly test the tree functionality
  88. //Assert.Contains("Service:", output);
  89. // Delete system
  90. (output, yaml) = await ExecuteAsync("systems", "del", "sys01");
  91. Assert.Equal("""
  92. System 'sys01' deleted.
  93. """, output);
  94. }
  95. [Fact]
  96. public async Task systems_cli_workflow_runs_on_hardware_and_systems_test()
  97. {
  98. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), "");
  99. // Create hardware (server)
  100. await ExecuteAsync("servers", "add", "proxmox-node01");
  101. // Add first system
  102. var (output, yaml) = await ExecuteAsync("systems", "add", "sys01");
  103. Assert.Equal("System 'sys01' added.\n", output);
  104. Assert.Contains("name: sys01", yaml);
  105. // Set sys01 to run on the created hardware
  106. (output, yaml) = await ExecuteAsync(
  107. "systems", "set", "sys01",
  108. "--type", "VM",
  109. "--os", "debian-12",
  110. "--cores", "2",
  111. "--ram", "4",
  112. "--ip", "10.0.20.10",
  113. "--runs-on", "proxmox-node01"
  114. );
  115. Assert.Equal("System 'sys01' updated.\n", output);
  116. // Add second system
  117. (output, yaml) = await ExecuteAsync("systems", "add", "sys02");
  118. Assert.Equal("System 'sys02' added.\n", output);
  119. Assert.Contains("name: sys02", yaml);
  120. // Set sys02 to run on BOTH: hardware + sys01
  121. // NOTE: '--runs-on' accepts multiple values via repeated options.
  122. (output, yaml) = await ExecuteAsync(
  123. "systems", "set", "sys02",
  124. "--type", "VM",
  125. "--os", "debian-12",
  126. "--cores", "4",
  127. "--ram", "8",
  128. "--runs-on", "proxmox-node01",
  129. "--runs-on", "sys01"
  130. );
  131. Assert.Equal("System 'sys02' updated.\n", output);
  132. outputHelper.WriteLine(yaml);
  133. // Assert resulting YAML
  134. Assert.Equal("""
  135. version: 2
  136. resources:
  137. - kind: Server
  138. name: proxmox-node01
  139. - kind: System
  140. type: vm
  141. os: debian-12
  142. cores: 2
  143. ram: 4
  144. ip: 10.0.20.10
  145. name: sys01
  146. runsOn:
  147. - proxmox-node01
  148. - kind: System
  149. type: vm
  150. os: debian-12
  151. cores: 4
  152. ram: 8
  153. name: sys02
  154. runsOn:
  155. - proxmox-node01
  156. - sys01
  157. """, yaml);
  158. }
  159. }