command_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package command
  15. import (
  16. "testing"
  17. "time"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. )
  21. func TestCommandConfig(t *testing.T) {
  22. require.Equal(t, defaultTimeout, config.Timeout)
  23. cfg := Config{
  24. Timeout: 10,
  25. Env: []string{"a=b"},
  26. }
  27. err := cfg.Initialize()
  28. require.NoError(t, err)
  29. assert.Equal(t, cfg.Timeout, config.Timeout)
  30. assert.Equal(t, cfg.Env, config.Env)
  31. assert.Len(t, cfg.Commands, 0)
  32. timeout, env, args := GetConfig("cmd", "")
  33. assert.Equal(t, time.Duration(config.Timeout)*time.Second, timeout)
  34. assert.Contains(t, env, "a=b")
  35. assert.Len(t, args, 0)
  36. cfg.Commands = []Command{
  37. {
  38. Path: "cmd1",
  39. Timeout: 30,
  40. Env: []string{"c=d"},
  41. Args: []string{"1", "", "2"},
  42. },
  43. {
  44. Path: "cmd2",
  45. Timeout: 0,
  46. Env: []string{"e=f"},
  47. },
  48. }
  49. err = cfg.Initialize()
  50. require.NoError(t, err)
  51. assert.Equal(t, cfg.Timeout, config.Timeout)
  52. assert.Equal(t, cfg.Env, config.Env)
  53. if assert.Len(t, config.Commands, 2) {
  54. assert.Equal(t, cfg.Commands[0].Path, config.Commands[0].Path)
  55. assert.Equal(t, cfg.Commands[0].Timeout, config.Commands[0].Timeout)
  56. assert.Equal(t, cfg.Commands[0].Env, config.Commands[0].Env)
  57. assert.Equal(t, cfg.Commands[0].Args, config.Commands[0].Args)
  58. assert.Equal(t, cfg.Commands[1].Path, config.Commands[1].Path)
  59. assert.Equal(t, cfg.Timeout, config.Commands[1].Timeout)
  60. assert.Equal(t, cfg.Commands[1].Env, config.Commands[1].Env)
  61. assert.Equal(t, cfg.Commands[1].Args, config.Commands[1].Args)
  62. }
  63. timeout, env, args = GetConfig("cmd1", "")
  64. assert.Equal(t, time.Duration(config.Commands[0].Timeout)*time.Second, timeout)
  65. assert.Contains(t, env, "a=b")
  66. assert.Contains(t, env, "c=d")
  67. assert.NotContains(t, env, "e=f")
  68. if assert.Len(t, args, 3) {
  69. assert.Equal(t, "1", args[0])
  70. assert.Empty(t, args[1])
  71. assert.Equal(t, "2", args[2])
  72. }
  73. timeout, env, args = GetConfig("cmd2", "")
  74. assert.Equal(t, time.Duration(config.Timeout)*time.Second, timeout)
  75. assert.Contains(t, env, "a=b")
  76. assert.NotContains(t, env, "c=d")
  77. assert.Contains(t, env, "e=f")
  78. assert.Len(t, args, 0)
  79. cfg.Commands = []Command{
  80. {
  81. Path: "cmd1",
  82. Timeout: 30,
  83. Env: []string{"c=d"},
  84. Args: []string{"1", "", "2"},
  85. Hook: HookCheckPassword,
  86. },
  87. {
  88. Path: "cmd1",
  89. Timeout: 0,
  90. Env: []string{"e=f"},
  91. Hook: HookExternalAuth,
  92. },
  93. }
  94. err = cfg.Initialize()
  95. require.NoError(t, err)
  96. timeout, env, args = GetConfig("cmd1", "")
  97. assert.Equal(t, time.Duration(config.Timeout)*time.Second, timeout)
  98. assert.Contains(t, env, "a=b")
  99. assert.NotContains(t, env, "c=d")
  100. assert.NotContains(t, env, "e=f")
  101. assert.Len(t, args, 0)
  102. timeout, env, args = GetConfig("cmd1", HookCheckPassword)
  103. assert.Equal(t, time.Duration(config.Commands[0].Timeout)*time.Second, timeout)
  104. assert.Contains(t, env, "a=b")
  105. assert.Contains(t, env, "c=d")
  106. assert.NotContains(t, env, "e=f")
  107. if assert.Len(t, args, 3) {
  108. assert.Equal(t, "1", args[0])
  109. assert.Empty(t, args[1])
  110. assert.Equal(t, "2", args[2])
  111. }
  112. timeout, env, args = GetConfig("cmd1", HookExternalAuth)
  113. assert.Equal(t, time.Duration(cfg.Timeout)*time.Second, timeout)
  114. assert.Contains(t, env, "a=b")
  115. assert.NotContains(t, env, "c=d")
  116. assert.Contains(t, env, "e=f")
  117. assert.Len(t, args, 0)
  118. }
  119. func TestConfigErrors(t *testing.T) {
  120. c := Config{}
  121. err := c.Initialize()
  122. if assert.Error(t, err) {
  123. assert.Contains(t, err.Error(), "invalid timeout")
  124. }
  125. c.Timeout = 10
  126. c.Env = []string{"a"}
  127. err = c.Initialize()
  128. if assert.Error(t, err) {
  129. assert.Contains(t, err.Error(), "invalid env var")
  130. }
  131. c.Env = nil
  132. c.Commands = []Command{
  133. {
  134. Path: "",
  135. },
  136. }
  137. err = c.Initialize()
  138. if assert.Error(t, err) {
  139. assert.Contains(t, err.Error(), "invalid path")
  140. }
  141. c.Commands = []Command{
  142. {
  143. Path: "path",
  144. Timeout: 10000,
  145. },
  146. }
  147. err = c.Initialize()
  148. if assert.Error(t, err) {
  149. assert.Contains(t, err.Error(), "invalid timeout")
  150. }
  151. c.Commands = []Command{
  152. {
  153. Path: "path",
  154. Timeout: 30,
  155. Env: []string{"b"},
  156. },
  157. }
  158. err = c.Initialize()
  159. if assert.Error(t, err) {
  160. assert.Contains(t, err.Error(), "invalid env var")
  161. }
  162. c.Commands = []Command{
  163. {
  164. Path: "path",
  165. Timeout: 30,
  166. Env: []string{"a=b"},
  167. Hook: "invali",
  168. },
  169. }
  170. err = c.Initialize()
  171. if assert.Error(t, err) {
  172. assert.Contains(t, err.Error(), "invalid hook name")
  173. }
  174. }