SystemWorkflowTests.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "--runs-on", "proxmox-node01"
  113. );
  114. Assert.Equal("System 'sys01' updated.\n", output);
  115. // Add second system
  116. (output, yaml) = await ExecuteAsync("systems", "add", "sys02");
  117. Assert.Equal("System 'sys02' added.\n", output);
  118. Assert.Contains("name: sys02", yaml);
  119. // Set sys02 to run on BOTH: hardware + sys01
  120. // NOTE: '--runs-on' accepts multiple values via repeated options.
  121. (output, yaml) = await ExecuteAsync(
  122. "systems", "set", "sys02",
  123. "--type", "VM",
  124. "--os", "debian-12",
  125. "--cores", "4",
  126. "--ram", "8",
  127. "--runs-on", "proxmox-node01",
  128. "--runs-on", "sys01"
  129. );
  130. Assert.Equal("System 'sys02' updated.\n", output);
  131. outputHelper.WriteLine(yaml);
  132. // Assert resulting YAML
  133. Assert.Equal("""
  134. version: 2
  135. resources:
  136. - kind: Server
  137. name: proxmox-node01
  138. - kind: System
  139. type: vm
  140. os: debian-12
  141. cores: 2
  142. ram: 4
  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. """, yaml);
  156. }
  157. }