SystemWorkflowTests.cs 7.8 KB

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