UpsPortWorkflowTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Tests.EndToEnd.Infra;
  2. using Xunit.Abstractions;
  3. namespace Tests.EndToEnd.UpsTests;
  4. [Collection("Yaml CLI tests")]
  5. public class UpsPortWorkflowTests(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. outputHelper.WriteLine(output);
  15. var yaml = await File.ReadAllTextAsync(Path.Combine(fs.Root, "config.yaml"));
  16. return (output, yaml);
  17. }
  18. [Fact]
  19. public async Task ups_port_cli_workflow_test() {
  20. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), "");
  21. await ExecuteAsync("ups", "add", "ups01");
  22. await ExecuteAsync("ups", "set", "ups01", "--model", "APC-BGM2200", "--va", "2200");
  23. // A UPS data port that is physically RJ45-shaped but enumerates as USB.
  24. (var output, var yaml) = await ExecuteAsync(
  25. "ups", "port", "add", "ups01",
  26. "--type", "usb",
  27. "--speed", "0.48",
  28. "--count", "1"
  29. );
  30. Assert.Equal("Port added to UPS 'ups01'.\n", output);
  31. // The dataline surge pass-through pair.
  32. (output, yaml) = await ExecuteAsync(
  33. "ups", "port", "add", "ups01",
  34. "--type", "rj45",
  35. "--speed", "1",
  36. "--count", "2"
  37. );
  38. Assert.Equal("Port added to UPS 'ups01'.\n", output);
  39. Assert.Equal("""
  40. version: 4
  41. resources:
  42. - kind: Ups
  43. model: APC-BGM2200
  44. va: 2200
  45. ports:
  46. - type: usb
  47. speed: 0.48
  48. count: 1
  49. - type: rj45
  50. speed: 1
  51. count: 2
  52. name: ups01
  53. connections: []
  54. """, yaml);
  55. // Update the second group in place.
  56. (output, yaml) = await ExecuteAsync(
  57. "ups", "port", "set", "ups01",
  58. "--index", "1",
  59. "--type", "rj45",
  60. "--speed", "1",
  61. "--count", "4"
  62. );
  63. Assert.Equal("Port #1 updated on UPS 'ups01'.\n", output);
  64. Assert.Contains("count: 4", yaml);
  65. // Describe surfaces the port summary.
  66. (output, yaml) = await ExecuteAsync("ups", "describe", "ups01");
  67. Assert.Contains("Ports:", output);
  68. Assert.Contains("usb: 1", output);
  69. Assert.Contains("rj45: 4", output);
  70. // Remove the pass-through pair again.
  71. (output, yaml) = await ExecuteAsync("ups", "port", "del", "ups01", "--index", "1");
  72. Assert.Equal("Port #1 removed from UPS 'ups01'.\n", output);
  73. Assert.Equal("""
  74. version: 4
  75. resources:
  76. - kind: Ups
  77. model: APC-BGM2200
  78. va: 2200
  79. ports:
  80. - type: usb
  81. speed: 0.48
  82. count: 1
  83. name: ups01
  84. connections: []
  85. """, yaml);
  86. (output, yaml) = await ExecuteAsync("ups", "describe", "ups01");
  87. Assert.Contains("usb: 1", output);
  88. Assert.DoesNotContain("rj45", output);
  89. }
  90. [Fact]
  91. public async Task describe_reports_none_when_ups_has_no_ports() {
  92. await File.WriteAllTextAsync(Path.Combine(fs.Root, "config.yaml"), "");
  93. await ExecuteAsync("ups", "add", "ups-bare");
  94. (var output, var _) = await ExecuteAsync("ups", "describe", "ups-bare");
  95. Assert.Contains("Ports:", output);
  96. Assert.Contains("None", output);
  97. }
  98. }