executor_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package executor
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. acl "github.com/OliveTin/OliveTin/internal/acl"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. )
  8. func testingExecutor() (*Executor, *config.Config) {
  9. cfg := config.DefaultConfig()
  10. e := DefaultExecutor(cfg)
  11. a1 := &config.Action{
  12. Title: "Do some tickles",
  13. Shell: "echo 'Tickling {{ person }}'",
  14. Arguments: []config.ActionArgument{
  15. {
  16. Name: "person",
  17. Type: "ascii",
  18. },
  19. },
  20. }
  21. cfg.Actions = append(cfg.Actions, a1)
  22. cfg.Sanitize()
  23. return e, cfg
  24. }
  25. func TestCreateExecutorAndExec(t *testing.T) {
  26. e, cfg := testingExecutor()
  27. req := ExecutionRequest{
  28. ActionTitle: "Do some tickles",
  29. AuthenticatedUser: &acl.AuthenticatedUser{Username: "Mr Tickle"},
  30. Cfg: cfg,
  31. Arguments: map[string]string{
  32. "person": "yourself",
  33. },
  34. }
  35. assert.NotNil(t, e, "Create an executor")
  36. wg, _ := e.ExecRequest(&req)
  37. wg.Wait()
  38. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  39. }
  40. func TestExecNonExistant(t *testing.T) {
  41. e, cfg := testingExecutor()
  42. req := ExecutionRequest{
  43. ActionTitle: "Waffles",
  44. logEntry: &InternalLogEntry{},
  45. Cfg: cfg,
  46. }
  47. wg, _ := e.ExecRequest(&req)
  48. wg.Wait()
  49. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  50. assert.Equal(t, "💩", req.logEntry.ActionIcon, "Log entry icon is a poop (not found)")
  51. }
  52. func TestArgumentNameCamelCase(t *testing.T) {
  53. a1 := &config.Action{
  54. Title: "Do some tickles",
  55. Shell: "echo 'Tickling {{ personName }}'",
  56. Arguments: []config.ActionArgument{
  57. {
  58. Name: "personName",
  59. Type: "ascii",
  60. },
  61. },
  62. }
  63. values := map[string]string{
  64. "personName": "Fred",
  65. }
  66. out, err := parseActionArguments(a1.Shell, values, a1, a1.Title, "")
  67. assert.Equal(t, "echo 'Tickling Fred'", out)
  68. assert.Nil(t, err)
  69. }
  70. func TestArgumentNameSnakeCase(t *testing.T) {
  71. a1 := &config.Action{
  72. Title: "Do some tickles",
  73. Shell: "echo 'Tickling {{ person_name }}'",
  74. Arguments: []config.ActionArgument{
  75. {
  76. Name: "person_name",
  77. Type: "ascii",
  78. },
  79. },
  80. }
  81. values := map[string]string{
  82. "person_name": "Fred",
  83. }
  84. out, err := parseActionArguments(a1.Shell, values, a1, a1.Title, "")
  85. assert.Equal(t, "echo 'Tickling Fred'", out)
  86. assert.Nil(t, err)
  87. }