summaryrefslogtreecommitdiff
path: root/internal/utils/exec_test.go
blob: d15fb454fda954fce339c94fd755970524ab572c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package utils

import (
	"errors"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestShouldExecCommandOnAutheliaRootPath(t *testing.T) {
	cmd := Command("pwd")
	result, err := cmd.CombinedOutput()
	assert.NoError(t, err, "")

	str := strings.Trim(string(result), "\n")

	assert.NoError(t, err, "")
	assert.Equal(t, true, strings.HasSuffix(str, "authelia"))
}

func TestCommandShouldOutputResult(t *testing.T) {
	output, exitcode, err := RunCommandAndReturnOutput("echo hello")

	assert.NoError(t, err)
	assert.Equal(t, 0, exitcode)
	assert.Equal(t, "hello", output)
}

func TestShouldWaitUntilCommandEnds(t *testing.T) {
	cmd := Command("sleep", "2")

	err := RunCommandWithTimeout(cmd, 3*time.Second)
	assert.NoError(t, err, "")
}

func TestShouldTimeoutWaitingCommand(t *testing.T) {
	cmd := Command("sleep", "3")

	err := RunCommandWithTimeout(cmd, 2*time.Second)
	assert.Error(t, err)
}

func TestShouldRunFuncUntilNoError(t *testing.T) {
	counter := 0

	err := RunFuncWithRetry(3, 500*time.Millisecond, func() error {
		counter++
		if counter < 3 {
			return errors.New("not ready")
		}

		return nil
	})
	assert.NoError(t, err, "")
}

func TestShouldFailAfterMaxAttemps(t *testing.T) {
	counter := 0

	err := RunFuncWithRetry(3, 500*time.Millisecond, func() error {
		counter++
		if counter < 4 {
			return errors.New("not ready")
		}

		return nil
	})
	assert.ErrorContains(t, err, "not ready")
}