test(utils): add additional coverage (#4751)
parent
98604dc7eb
commit
1cf9e6f3ef
|
@ -0,0 +1,47 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestShouldEncryptAndDecriptUsingAES(t *testing.T) {
|
||||
var key [32]byte = sha256.Sum256([]byte("the key"))
|
||||
|
||||
var secret = "the secret"
|
||||
|
||||
encryptedSecret, err := Encrypt([]byte(secret), &key)
|
||||
assert.NoError(t, err, "")
|
||||
|
||||
decryptedSecret, err := Decrypt(encryptedSecret, &key)
|
||||
|
||||
assert.NoError(t, err, "")
|
||||
assert.Equal(t, secret, string(decryptedSecret))
|
||||
}
|
||||
|
||||
func TestShouldFailDecryptOnInvalidKey(t *testing.T) {
|
||||
var key [32]byte = sha256.Sum256([]byte("the key"))
|
||||
|
||||
var secret = "the secret"
|
||||
|
||||
encryptedSecret, err := Encrypt([]byte(secret), &key)
|
||||
assert.NoError(t, err, "")
|
||||
|
||||
key = sha256.Sum256([]byte("the key 2"))
|
||||
|
||||
_, err = Decrypt(encryptedSecret, &key)
|
||||
|
||||
assert.Error(t, err, "message authentication failed")
|
||||
}
|
||||
|
||||
func TestShouldFailDecryptOnInvalidCypherText(t *testing.T) {
|
||||
var key [32]byte = sha256.Sum256([]byte("the key"))
|
||||
|
||||
encryptedSecret := []byte("abc123")
|
||||
|
||||
_, err := Decrypt(encryptedSecret, &key)
|
||||
|
||||
assert.Error(t, err, "message authentication failed")
|
||||
}
|
|
@ -7,6 +7,8 @@ import (
|
|||
|
||||
// CheckUntil regularly check a predicate until it's true or time out is reached.
|
||||
func CheckUntil(interval time.Duration, timeout time.Duration, predicate func() (bool, error)) error {
|
||||
timeoutCh := time.After(timeout)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(interval):
|
||||
|
@ -18,7 +20,7 @@ func CheckUntil(interval time.Duration, timeout time.Duration, predicate func()
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-time.After(timeout):
|
||||
case <-timeoutCh:
|
||||
return fmt.Errorf("timeout of %ds reached", int64(timeout/time.Second))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckUntilPredicateOk(t *testing.T) {
|
||||
interval := time.Second * 1
|
||||
timeout := time.Second * 5
|
||||
|
||||
err := CheckUntil(interval, timeout, func() (bool, error) {
|
||||
return true, nil
|
||||
})
|
||||
assert.NoError(t, err, "")
|
||||
}
|
||||
|
||||
func TestCheckUntilPredicateError(t *testing.T) {
|
||||
interval := time.Second * 1
|
||||
timeout := time.Second * 5
|
||||
|
||||
theError := errors.New("some error")
|
||||
|
||||
err := CheckUntil(interval, timeout, func() (bool, error) {
|
||||
return false, theError
|
||||
})
|
||||
assert.ErrorIs(t, err, theError)
|
||||
}
|
||||
|
||||
func TestCheckUntilPredicateTimeout(t *testing.T) {
|
||||
interval := 1 * time.Second
|
||||
timeout := 3 * time.Second
|
||||
|
||||
err := CheckUntil(interval, timeout, func() (bool, error) {
|
||||
return false, nil
|
||||
})
|
||||
assert.ErrorContains(t, err, "timeout of 3s reached")
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"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")
|
||||
|
||||
fmt.Println(string(result))
|
||||
|
||||
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")
|
||||
}
|
Loading…
Reference in New Issue