executor_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  88. func TestGetLogsEmpty(t *testing.T) {
  89. e, cfg := testingExecutor()
  90. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  91. logs, remaining := e.GetLogTrackingIds(0, 10)
  92. assert.NotNil(t, logs, "Logs should not be nil")
  93. assert.Equal(t, 0, len(logs), "No logs yet")
  94. assert.Equal(t, int64(0), remaining, "There should be no remaining logs")
  95. }
  96. func TestGetLogsLessThanPageSize(t *testing.T) {
  97. e, cfg := testingExecutor()
  98. cfg.Actions = append(cfg.Actions, &config.Action{
  99. Title: "blat",
  100. Shell: "date",
  101. })
  102. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  103. logEntries, remaining := e.GetLogTrackingIds(0, 10)
  104. assert.Equal(t, 0, len(logEntries), "There should be 0 logs")
  105. assert.Zero(t, remaining, "There should be no remaining logs")
  106. execNewReqAndWait(e, "blat", cfg)
  107. execNewReqAndWait(e, "blat", cfg)
  108. execNewReqAndWait(e, "blat", cfg)
  109. execNewReqAndWait(e, "blat", cfg)
  110. execNewReqAndWait(e, "blat", cfg)
  111. execNewReqAndWait(e, "blat", cfg)
  112. execNewReqAndWait(e, "blat", cfg)
  113. logEntries, remaining = e.GetLogTrackingIds(0, 10)
  114. assert.Equal(t, 7, len(logEntries), "There should be 7 logs")
  115. assert.Zero(t, remaining, "There should be no remaining logs")
  116. execNewReqAndWait(e, "blat", cfg)
  117. execNewReqAndWait(e, "blat", cfg)
  118. execNewReqAndWait(e, "blat", cfg)
  119. execNewReqAndWait(e, "blat", cfg)
  120. execNewReqAndWait(e, "blat", cfg)
  121. logEntries, remaining = e.GetLogTrackingIds(0, 10)
  122. assert.Equal(t, 10, len(logEntries), "There should be 10 logs")
  123. assert.Equal(t, int64(2), remaining, "There should be 1 remaining logs")
  124. }
  125. func execNewReqAndWait(e *Executor, title string, cfg *config.Config) {
  126. req := &ExecutionRequest{
  127. ActionTitle: title,
  128. Cfg: cfg,
  129. }
  130. wg, _ := e.ExecRequest(req)
  131. wg.Wait()
  132. }
  133. func TestGetPagingIndexes(t *testing.T) {
  134. assert.Zero(t, getPagingStartIndex(5, 0, 5), "Testing start index from empty list")
  135. assert.Equal(t, int64(4), getPagingStartIndex(5, 10, 5), "Testing start index from mid point")
  136. assert.Equal(t, int64(9), getPagingStartIndex(-1, 10, 5), "Testing start index with negative offset")
  137. assert.Equal(t, int64(0), getPagingStartIndex(15, 10, 5), "Testing start index with large offset")
  138. assert.Equal(t, int64(9), getPagingStartIndex(0, 10, 0), "Testing start index with zero count")
  139. }