executor_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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(values, a1, "")
  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(values, a1, "")
  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. cfg.Sanitize()
  103. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  104. logEntries, remaining := e.GetLogTrackingIds(0, 10)
  105. assert.Equal(t, 0, len(logEntries), "There should be 0 logs")
  106. assert.Zero(t, remaining, "There should be no remaining logs")
  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. execNewReqAndWait(e, "blat", cfg)
  114. logEntries, remaining = e.GetLogTrackingIds(0, 10)
  115. assert.Equal(t, 7, len(logEntries), "There should be 7 logs")
  116. assert.Zero(t, remaining, "There should be no remaining logs")
  117. execNewReqAndWait(e, "blat", cfg)
  118. execNewReqAndWait(e, "blat", cfg)
  119. execNewReqAndWait(e, "blat", cfg)
  120. execNewReqAndWait(e, "blat", cfg)
  121. execNewReqAndWait(e, "blat", cfg)
  122. logEntries, remaining = e.GetLogTrackingIds(0, 10)
  123. assert.Equal(t, 10, len(logEntries), "There should be 10 logs")
  124. assert.Equal(t, int64(2), remaining, "There should be 1 remaining logs")
  125. }
  126. func execNewReqAndWait(e *Executor, title string, cfg *config.Config) {
  127. req := &ExecutionRequest{
  128. ActionTitle: title,
  129. Cfg: cfg,
  130. }
  131. wg, _ := e.ExecRequest(req)
  132. wg.Wait()
  133. }
  134. func TestGetPagingIndexes(t *testing.T) {
  135. assert.Zero(t, getPagingStartIndex(5, 0), "Testing start index from empty list")
  136. assert.Equal(t, int64(4), getPagingStartIndex(5, 10), "Testing start index from mid point")
  137. assert.Equal(t, int64(9), getPagingStartIndex(-1, 10), "Testing start index with negative offset")
  138. assert.Equal(t, int64(0), getPagingStartIndex(15, 10), "Testing start index with large offset")
  139. assert.Equal(t, int64(9), getPagingStartIndex(0, 10), "Testing start index with zero count")
  140. }
  141. func TestUnsetRequiredArgument(t *testing.T) {
  142. a1 := &config.Action{
  143. Title: "Print your name",
  144. Shell: "echo 'Your name is: {{ name }}'",
  145. Arguments: []config.ActionArgument{
  146. {
  147. Name: "name",
  148. Type: "ascii",
  149. },
  150. },
  151. }
  152. values := map[string]string{}
  153. out, err := parseActionArguments(values, a1, "")
  154. assert.Equal(t, "", out)
  155. assert.NotNil(t, err)
  156. }
  157. func TestUnusedArgumentStillPassesTypeSafetyCheck(t *testing.T) {
  158. a1 := &config.Action{
  159. Title: "Print your name",
  160. Shell: "echo 'Your name is: {{ name }}'",
  161. Arguments: []config.ActionArgument{
  162. {
  163. Name: "name",
  164. Type: "ascii",
  165. },
  166. {
  167. Name: "age",
  168. Type: "int",
  169. },
  170. },
  171. }
  172. values := map[string]string{
  173. "name": "Fred",
  174. "age": "Not an integer",
  175. }
  176. out, err := parseActionArguments(values, a1, "")
  177. assert.Equal(t, "", out)
  178. assert.NotNil(t, err)
  179. }
  180. // https://github.com/OliveTin/OliveTin/issues/564
  181. func TestMangleInvalidArgumentValues(t *testing.T) {
  182. e, cfg := testingExecutor()
  183. a1 := &config.Action{
  184. Title: "Validate my date without seconds because I am from an Android phone",
  185. Shell: "echo 'The date is: {{ date }}'",
  186. Arguments: []config.ActionArgument{
  187. {
  188. Name: "date",
  189. Type: "datetime",
  190. },
  191. },
  192. }
  193. cfg.Actions = append(cfg.Actions, a1)
  194. cfg.Sanitize()
  195. req := ExecutionRequest{
  196. Action: a1,
  197. AuthenticatedUser: acl.UserFromSystem(cfg, "testuser"),
  198. Cfg: cfg,
  199. Arguments: map[string]string{
  200. "date": "1990-01-10T12:00", // Invalid format, should be without seconds
  201. },
  202. }
  203. wg, _ := e.ExecRequest(&req)
  204. wg.Wait()
  205. assert.NotNil(t, req.logEntry, "Log entry should not be nil")
  206. assert.Equal(t, req.logEntry.Output, "The date is: 1990-01-10T12:00:00\n", "Date should be mangled to a valid format")
  207. }