diff --git a/internal/pkg/config/config_test.go b/internal/pkg/config/config_test.go new file mode 100644 index 00000000..83dc4478 --- /dev/null +++ b/internal/pkg/config/config_test.go @@ -0,0 +1,108 @@ +package config + +import ( + "os" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/thecodingmachine/gotenberg/internal/pkg/standarderror" + "github.com/thecodingmachine/gotenberg/test" +) + +func TestDefaultWaitTimeout(t *testing.T) { + // should be OK. + os.Setenv(defaultWaitTimeoutEnvVar, "1.5") + config, err := FromEnv() + assert.Nil(t, err) + assert.Equal(t, 1.5, config.DefaultWaitTimeout()) + // should failed. + os.Setenv(defaultWaitTimeoutEnvVar, "foo") + _, err = FromEnv() + assert.NotNil(t, err) + standardized := test.RequireStandardError(t, err) + assert.Equal(t, standarderror.Invalid, standarderror.Code(standardized)) + os.Unsetenv(defaultWaitTimeoutEnvVar) +} + +func TestDefaultListenPort(t *testing.T) { + // should be OK. + os.Setenv(defaultListenPortEnvVar, "4000") + config, err := FromEnv() + assert.Nil(t, err) + assert.Equal(t, "4000", config.DefaultListenPort()) + // should failed. + os.Setenv(defaultListenPortEnvVar, "foo") + _, err = FromEnv() + assert.NotNil(t, err) + standardized := test.RequireStandardError(t, err) + assert.Equal(t, standarderror.Invalid, standarderror.Code(standardized)) + os.Setenv(defaultListenPortEnvVar, "100000000") + _, err = FromEnv() + assert.NotNil(t, err) + standardized = test.RequireStandardError(t, err) + assert.Equal(t, standarderror.Invalid, standarderror.Code(standardized)) + os.Unsetenv(defaultListenPortEnvVar) +} + +func TestEnableChromeEndpoints(t *testing.T) { + // should be OK. + os.Setenv(disableGoogleChromeEnvVar, "1") + config, err := FromEnv() + assert.Nil(t, err) + assert.Equal(t, false, config.EnableChromeEndpoints()) + os.Setenv(disableGoogleChromeEnvVar, "0") + config, err = FromEnv() + assert.Nil(t, err) + assert.Equal(t, true, config.EnableChromeEndpoints()) + // should failed. + os.Setenv(disableGoogleChromeEnvVar, "true") + _, err = FromEnv() + assert.NotNil(t, err) + standardized := test.RequireStandardError(t, err) + assert.Equal(t, standarderror.Invalid, standarderror.Code(standardized)) + os.Unsetenv(disableGoogleChromeEnvVar) +} + +func TestEnableUnoconvEndpoints(t *testing.T) { + // should be OK. + os.Setenv(disableUnoconvEnvVar, "1") + config, err := FromEnv() + assert.Nil(t, err) + assert.Equal(t, false, config.EnableUnoconvEndpoints()) + os.Setenv(disableUnoconvEnvVar, "0") + config, err = FromEnv() + assert.Nil(t, err) + assert.Equal(t, true, config.EnableUnoconvEndpoints()) + // should failed. + os.Setenv(disableUnoconvEnvVar, "true") + _, err = FromEnv() + assert.NotNil(t, err) + standardized := test.RequireStandardError(t, err) + assert.Equal(t, standarderror.Invalid, standarderror.Code(standardized)) + os.Unsetenv(disableUnoconvEnvVar) +} + +func TestLogLevel(t *testing.T) { + // should be OK. + os.Setenv(logLevelEnvVar, "DEBUG") + config, err := FromEnv() + assert.Nil(t, err) + assert.Equal(t, logrus.DebugLevel, config.LogLevel()) + os.Setenv(logLevelEnvVar, "INFO") + config, err = FromEnv() + assert.Nil(t, err) + assert.Equal(t, logrus.InfoLevel, config.LogLevel()) + os.Setenv(logLevelEnvVar, "ERROR") + config, err = FromEnv() + assert.Nil(t, err) + assert.Equal(t, logrus.ErrorLevel, config.LogLevel()) + // should failed. + os.Setenv(logLevelEnvVar, "foo") + config, err = FromEnv() + assert.Equal(t, logrus.InfoLevel, config.LogLevel()) + assert.NotNil(t, err) + standardized := test.RequireStandardError(t, err) + assert.Equal(t, standarderror.Invalid, standarderror.Code(standardized)) + os.Unsetenv(logLevelEnvVar) +} diff --git a/internal/pkg/pm2/chrome_test.go b/internal/pkg/pm2/chrome_test.go index edb8b5af..2fea8458 100644 --- a/internal/pkg/pm2/chrome_test.go +++ b/internal/pkg/pm2/chrome_test.go @@ -4,16 +4,17 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/thecodingmachine/gotenberg/test" ) func TestChromeStart(t *testing.T) { - p := NewChrome(false) + p := NewChrome(test.CreateTestLogger()) err := p.Start() require.Nil(t, err) } func TestChromeShutdown(t *testing.T) { - p := NewChrome(false) + p := NewChrome(test.CreateTestLogger()) err := p.Shutdown() require.Nil(t, err) } diff --git a/internal/pkg/pm2/unoconv_test.go b/internal/pkg/pm2/unoconv_test.go index 6995109e..4c5fb8bf 100644 --- a/internal/pkg/pm2/unoconv_test.go +++ b/internal/pkg/pm2/unoconv_test.go @@ -4,16 +4,17 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/thecodingmachine/gotenberg/test" ) func TestUnoconvStart(t *testing.T) { - p := NewUnoconv(false) + p := NewUnoconv(test.CreateTestLogger()) err := p.Start() require.Nil(t, err) } func TestUnoconvShutdown(t *testing.T) { - p := NewUnoconv(false) + p := NewUnoconv(test.CreateTestLogger()) err := p.Shutdown() require.Nil(t, err) } diff --git a/test/testfunc.go b/test/testfunc.go index 5fb43bb9..428d209a 100644 --- a/test/testfunc.go +++ b/test/testfunc.go @@ -14,8 +14,10 @@ import ( "runtime" "testing" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/thecodingmachine/gotenberg/internal/pkg/logger" "github.com/thecodingmachine/gotenberg/internal/pkg/standarderror" "golang.org/x/sync/errgroup" ) @@ -49,6 +51,12 @@ func RequireStandardError(t *testing.T, err error) *standarderror.Error { return standardized } +// CreateTestLogger create a default logger +// for our tests. +func CreateTestLogger() *logger.Logger { + return logger.New(logrus.DebugLevel, "tests") +} + // HTMLTestMultipartForm returns the body // for a multipate/form-data request with all // files under "html" folder.