sanitize_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package config
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func TestSanitizeConfig(t *testing.T) {
  7. c := DefaultConfig()
  8. a := &Action{
  9. Title: "Mr Waffles",
  10. Arguments: []ActionArgument{
  11. {
  12. Name: "Carrots",
  13. Choices: []ActionArgumentChoice{
  14. {
  15. Value: "Waffle",
  16. },
  17. },
  18. },
  19. {
  20. Name: "foobar",
  21. },
  22. },
  23. }
  24. c.Actions = append(c.Actions, a)
  25. c.Sanitize()
  26. a2 := c.findAction("Mr Waffles")
  27. assert.NotNil(t, a2, "Found action after adding it")
  28. assert.Equal(t, 3, a2.Timeout, "Default timeout is set")
  29. assert.Equal(t, "😀", a2.Icon, "Default icon is a smiley")
  30. assert.Equal(t, "Carrots", a2.Arguments[0].Title, "Arg title is set to name")
  31. assert.Equal(t, "Waffle", a2.Arguments[0].Choices[0].Title, "Choice title is set to name")
  32. }
  33. func TestSanitizeConfigInlineDashboardActions(t *testing.T) {
  34. c := DefaultConfig()
  35. inline := &Action{
  36. Shell: "date",
  37. }
  38. dashboardActionTitle := "Inline Dashboard Action"
  39. c.Dashboards = []*DashboardComponent{
  40. {
  41. Title: "My Dashboard",
  42. Contents: []*DashboardComponent{
  43. {
  44. Title: dashboardActionTitle,
  45. InlineAction: inline,
  46. },
  47. },
  48. },
  49. }
  50. c.Sanitize()
  51. // Inline action should have been appended to the global Actions slice.
  52. assert.GreaterOrEqual(t, len(c.Actions), 1, "At least one action should exist after sanitization")
  53. // It should be discoverable by the dashboard component title when no explicit title was set.
  54. found := c.findAction(dashboardActionTitle)
  55. if assert.NotNil(t, found, "Inline dashboard action should be discoverable by title") {
  56. assert.Equal(t, dashboardActionTitle, found.Title, "Inline action title should default from dashboard component title")
  57. assert.Equal(t, 3, found.Timeout, "Inline action should have default timeout applied")
  58. assert.NotEmpty(t, found.Icon, "Inline action should have default icon applied")
  59. assert.NotEmpty(t, found.ID, "Inline action should have a generated ID")
  60. }
  61. }
  62. func TestSanitizeActionExecutionMode(t *testing.T) {
  63. t.Run("execTool with shell clears shell and exec", func(t *testing.T) {
  64. a := &Action{Title: "Mixed", Shell: "echo hi", ExecTool: &ExecToolConfig{Name: "k8s", Config: map[string]any{"image": "busybox"}}}
  65. sanitizeActionExecutionMode(a)
  66. assert.Empty(t, a.Shell)
  67. assert.Nil(t, a.Exec)
  68. assert.NotNil(t, a.ExecTool)
  69. })
  70. t.Run("shell and exec keeps exec only", func(t *testing.T) {
  71. a := &Action{Title: "Both", Shell: "echo hi", Exec: []string{"echo", "hi"}}
  72. sanitizeActionExecutionMode(a)
  73. assert.Empty(t, a.Shell)
  74. assert.NotEmpty(t, a.Exec)
  75. })
  76. t.Run("execTool with empty name cleared", func(t *testing.T) {
  77. a := &Action{Title: "Bad", ExecTool: &ExecToolConfig{Name: "", Config: map[string]any{}}}
  78. sanitizeActionExecutionMode(a)
  79. assert.Nil(t, a.ExecTool)
  80. })
  81. }