2021-04-06 10:08:43 +00:00
|
|
|
package adminmonitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
i "github.com/portainer/portainer/api/internal/testhelpers"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_stopWithoutStarting(t *testing.T) {
|
|
|
|
monitor := New(1*time.Minute, nil, nil)
|
|
|
|
monitor.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_stopCouldBeCalledMultipleTimes(t *testing.T) {
|
|
|
|
monitor := New(1*time.Minute, nil, nil)
|
|
|
|
monitor.Stop()
|
|
|
|
monitor.Stop()
|
|
|
|
}
|
|
|
|
|
2022-03-16 12:13:45 +00:00
|
|
|
func Test_startOrStopCouldBeCalledMultipleTimesConcurrently(t *testing.T) {
|
|
|
|
monitor := New(1*time.Minute, nil, context.Background())
|
|
|
|
|
|
|
|
go monitor.Start()
|
|
|
|
monitor.Start()
|
|
|
|
|
|
|
|
go monitor.Stop()
|
|
|
|
monitor.Stop()
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
func Test_canStopStartedMonitor(t *testing.T) {
|
|
|
|
monitor := New(1*time.Minute, nil, context.Background())
|
|
|
|
monitor.Start()
|
|
|
|
assert.NotNil(t, monitor.cancellationFunc, "cancellation function is missing in started monitor")
|
|
|
|
|
|
|
|
monitor.Stop()
|
|
|
|
assert.Nil(t, monitor.cancellationFunc, "cancellation function should absent in stopped monitor")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_start_shouldFatalAfterTimeout_ifNotInitialized(t *testing.T) {
|
|
|
|
timeout := 10 * time.Millisecond
|
|
|
|
|
|
|
|
datastore := i.NewDatastore(i.WithUsers([]portainer.User{}))
|
|
|
|
|
2022-03-15 12:52:41 +00:00
|
|
|
ch := make(chan struct{})
|
2021-04-06 10:08:43 +00:00
|
|
|
var fataled bool
|
|
|
|
origLogFatalf := logFatalf
|
2022-03-15 12:52:41 +00:00
|
|
|
|
|
|
|
logFatalf = func(s string, v ...interface{}) {
|
|
|
|
fataled = true
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
defer func() {
|
|
|
|
logFatalf = origLogFatalf
|
|
|
|
}()
|
|
|
|
|
|
|
|
monitor := New(timeout, datastore, context.Background())
|
|
|
|
monitor.Start()
|
|
|
|
<-time.After(2 * timeout)
|
2022-03-15 12:52:41 +00:00
|
|
|
<-ch
|
2021-04-06 10:08:43 +00:00
|
|
|
|
|
|
|
assert.True(t, fataled, "monitor should been timeout and fatal")
|
|
|
|
}
|