executor_dashboards_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package executor
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. "github.com/OliveTin/OliveTin/internal/entities"
  8. )
  9. func TestResolveOnDashboardsDefaultsToActionsDashboard(t *testing.T) {
  10. index := buildDashboardTargetIndex(&config.Config{})
  11. targets := resolveOnDashboards(index, "Lonely Action", "")
  12. require.Len(t, targets, 1)
  13. assert.Equal(t, "Actions", targets[0].Title)
  14. assert.Equal(t, "/", targets[0].Path)
  15. }
  16. func TestResolveOnDashboardsConfiguredDashboard(t *testing.T) {
  17. cfg := &config.Config{
  18. Actions: []*config.Action{
  19. {Title: "Restart"},
  20. },
  21. Dashboards: []*config.DashboardComponent{
  22. {
  23. Title: "Operations",
  24. Contents: []*config.DashboardComponent{
  25. {Title: "Restart"},
  26. },
  27. },
  28. },
  29. }
  30. index := buildDashboardTargetIndex(cfg)
  31. targets := resolveOnDashboards(index, "Restart", "")
  32. require.Len(t, targets, 1)
  33. assert.Equal(t, "Operations", targets[0].Title)
  34. assert.Equal(t, "/dashboards/Operations", targets[0].Path)
  35. }
  36. func TestResolveOnDashboardsEntityDirectory(t *testing.T) {
  37. cfg := &config.Config{
  38. Actions: []*config.Action{
  39. {Title: "Reboot", Entity: "host"},
  40. },
  41. Dashboards: []*config.DashboardComponent{
  42. {
  43. Title: "Servers",
  44. Contents: []*config.DashboardComponent{
  45. {
  46. Type: "fieldset",
  47. Entity: "host",
  48. Contents: []*config.DashboardComponent{
  49. {
  50. Type: "directory",
  51. Title: "Host Details",
  52. Contents: []*config.DashboardComponent{
  53. {Title: "Reboot"},
  54. },
  55. },
  56. },
  57. },
  58. },
  59. },
  60. },
  61. }
  62. entities.ClearEntitiesOfType("host")
  63. defer entities.ClearEntitiesOfType("host")
  64. entities.AddEntity("host", "host-1", map[string]any{"title": "Host 1"})
  65. index := buildDashboardTargetIndex(cfg)
  66. targets := resolveOnDashboards(index, "Reboot", "host-1")
  67. require.Len(t, targets, 1)
  68. assert.Equal(t, "Host Details", targets[0].Title)
  69. assert.Equal(t, "host", targets[0].EntityType)
  70. assert.Equal(t, "host-1", targets[0].EntityKey)
  71. assert.Equal(t, "/dashboards/Host Details/host/host-1", targets[0].Path)
  72. }
  73. func TestRebuildActionMapStoresOnDashboards(t *testing.T) {
  74. cfg := &config.Config{
  75. Actions: []*config.Action{
  76. {Title: "Only Default"},
  77. {Title: "Configured"},
  78. },
  79. Dashboards: []*config.DashboardComponent{
  80. {
  81. Title: "Custom",
  82. Contents: []*config.DashboardComponent{
  83. {Title: "Configured"},
  84. },
  85. },
  86. },
  87. }
  88. ex := DefaultExecutor(cfg)
  89. ex.RebuildActionMap()
  90. defaultBinding := ex.FindBindingWithNoEntity(cfg.Actions[0])
  91. require.NotNil(t, defaultBinding)
  92. require.Len(t, defaultBinding.OnDashboards, 1)
  93. assert.Equal(t, "Actions", defaultBinding.OnDashboards[0].Title)
  94. assert.False(t, defaultBinding.IsOnConfiguredDashboard())
  95. configuredBinding := ex.FindBindingWithNoEntity(cfg.Actions[1])
  96. require.NotNil(t, configuredBinding)
  97. require.Len(t, configuredBinding.OnDashboards, 1)
  98. assert.Equal(t, "Custom", configuredBinding.OnDashboards[0].Title)
  99. assert.True(t, configuredBinding.IsOnConfiguredDashboard())
  100. }
  101. func TestResolveOnDashboardsMultipleDashboards(t *testing.T) {
  102. cfg := &config.Config{
  103. Actions: []*config.Action{
  104. {Title: "Shared"},
  105. },
  106. Dashboards: []*config.DashboardComponent{
  107. {
  108. Title: "One",
  109. Contents: []*config.DashboardComponent{
  110. {Title: "Shared"},
  111. },
  112. },
  113. {
  114. Title: "Two",
  115. Contents: []*config.DashboardComponent{
  116. {Title: "Shared"},
  117. },
  118. },
  119. },
  120. }
  121. index := buildDashboardTargetIndex(cfg)
  122. targets := resolveOnDashboards(index, "Shared", "")
  123. require.Len(t, targets, 2)
  124. assert.Equal(t, "One", targets[0].Title)
  125. assert.Equal(t, "Two", targets[1].Title)
  126. }