command_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (C) 2019-2022 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 := GetConfig("cmd")
  33. assert.Equal(t, time.Duration(config.Timeout)*time.Second, timeout)
  34. assert.Contains(t, env, "a=b")
  35. cfg.Commands = []Command{
  36. {
  37. Path: "cmd1",
  38. Timeout: 30,
  39. Env: []string{"c=d"},
  40. },
  41. {
  42. Path: "cmd2",
  43. Timeout: 0,
  44. Env: []string{"e=f"},
  45. },
  46. }
  47. err = cfg.Initialize()
  48. require.NoError(t, err)
  49. assert.Equal(t, cfg.Timeout, config.Timeout)
  50. assert.Equal(t, cfg.Env, config.Env)
  51. if assert.Len(t, config.Commands, 2) {
  52. assert.Equal(t, cfg.Commands[0].Path, config.Commands[0].Path)
  53. assert.Equal(t, cfg.Commands[0].Timeout, config.Commands[0].Timeout)
  54. assert.Equal(t, cfg.Commands[0].Env, config.Commands[0].Env)
  55. assert.Equal(t, cfg.Commands[1].Path, config.Commands[1].Path)
  56. assert.Equal(t, cfg.Timeout, config.Commands[1].Timeout)
  57. assert.Equal(t, cfg.Commands[1].Env, config.Commands[1].Env)
  58. }
  59. timeout, env = GetConfig("cmd1")
  60. assert.Equal(t, time.Duration(config.Commands[0].Timeout)*time.Second, timeout)
  61. assert.Contains(t, env, "a=b")
  62. assert.Contains(t, env, "c=d")
  63. assert.NotContains(t, env, "e=f")
  64. timeout, env = GetConfig("cmd2")
  65. assert.Equal(t, time.Duration(config.Timeout)*time.Second, timeout)
  66. assert.Contains(t, env, "a=b")
  67. assert.NotContains(t, env, "c=d")
  68. assert.Contains(t, env, "e=f")
  69. }
  70. func TestConfigErrors(t *testing.T) {
  71. c := Config{}
  72. err := c.Initialize()
  73. if assert.Error(t, err) {
  74. assert.Contains(t, err.Error(), "invalid timeout")
  75. }
  76. c.Timeout = 10
  77. c.Env = []string{"a"}
  78. err = c.Initialize()
  79. if assert.Error(t, err) {
  80. assert.Contains(t, err.Error(), "invalid env var")
  81. }
  82. c.Env = nil
  83. c.Commands = []Command{
  84. {
  85. Path: "",
  86. },
  87. }
  88. err = c.Initialize()
  89. if assert.Error(t, err) {
  90. assert.Contains(t, err.Error(), "invalid path")
  91. }
  92. c.Commands = []Command{
  93. {
  94. Path: "path",
  95. Timeout: 10000,
  96. },
  97. }
  98. err = c.Initialize()
  99. if assert.Error(t, err) {
  100. assert.Contains(t, err.Error(), "invalid timeout")
  101. }
  102. c.Commands = []Command{
  103. {
  104. Path: "path",
  105. Timeout: 30,
  106. Env: []string{"b"},
  107. },
  108. }
  109. err = c.Initialize()
  110. if assert.Error(t, err) {
  111. assert.Contains(t, err.Error(), "invalid env var")
  112. }
  113. }