refactoring process tests + adding lock on run function

This commit is contained in:
Julien Neuhart
2018-04-11 14:25:04 +02:00
parent 2e03070618
commit 76f120d6f6
2 changed files with 117 additions and 99 deletions

View File

@@ -22,100 +22,111 @@ func makeFile(workingDir string, fileName string) *gfile.File {
return f
}
func loadCommandConfigs(configurationFilePath string) {
path, _ := filepath.Abs(configurationFilePath)
c, _ := config.NewAppConfig(path)
Load(c.CommandsConfig)
}
func TestLoad(t *testing.T) {
path, _ := filepath.Abs("../../../_tests/configurations/gotenberg.yml")
c, _ := config.NewAppConfig(path)
Load(c.CommandsConfig)
if c.CommandsConfig != commandsConfig {
t.Error("Commands configuration should have loaded correctly!")
if c.CommandsConfig != forest.commandsConfig {
t.Error("Commands configuration should have been loaded correctly")
}
}
func TestRun(t *testing.T) {
var cmd string
// case 1: uses a simple command.
cmd = "echo Hello world"
if err := forest.run(cmd, 30); err != nil {
t.Errorf("Command '%s' should have worked", cmd)
}
// case 2: uses a simple command but with an unsuitable timeout.
cmd = "sleep 5"
if err := forest.run(cmd, 0); err == nil {
t.Errorf("Command '%s' should not have worked", cmd)
}
// case 3: uses a broken command.
cmd = "helloworld"
if err := forest.run(cmd, 30); err == nil {
t.Errorf("Command '%s' should not have worked", cmd)
}
}
func TestUnconv(t *testing.T) {
path, _ := filepath.Abs("../../../_tests/configurations/gotenberg.yml")
c, _ := config.NewAppConfig(path)
Load(c.CommandsConfig)
var file *gfile.File
workingDir := "test"
os.Mkdir(workingDir, 0666)
loadCommandConfigs("../../../_tests/configurations/gotenberg.yml")
// case 1: uses an HTML file type.
if _, err := Unconv(workingDir, makeFile(workingDir, "file.html")); err != nil {
t.Error("HTML conversion to PDF should have worked!")
file = makeFile(workingDir, "file.html")
if _, err := Unconv(workingDir, file); err != nil {
t.Errorf("Converting '%s' to PDF should have worked", file.Path)
}
// case 2: uses an Office file type.
if _, err := Unconv(workingDir, makeFile(workingDir, "file.docx")); err != nil {
t.Error("Office conversion to PDF should have worked!")
file = makeFile(workingDir, "file.docx")
if _, err := Unconv(workingDir, file); err != nil {
t.Errorf("Converting '%s' to PDF should have worked", file.Path)
}
// case 3: uses a PDF file type.
if _, err := Unconv(workingDir, makeFile(workingDir, "file.pdf")); err == nil {
t.Error("PDF conversion to PDF should not have worked!")
file = makeFile(workingDir, "file.pdf")
if _, err := Unconv(workingDir, file); err == nil {
t.Errorf("Converting '%s' to PDF should not have worked", file.Path)
}
loadCommandConfigs("../../../_tests/configurations/timeout-gotenberg.yml")
// case 4: uses a command with an unsuitable timeout.
path, _ = filepath.Abs("../../../_tests/configurations/timeout-gotenberg.yml")
c, _ = config.NewAppConfig(path)
Load(c.CommandsConfig)
file = makeFile(workingDir, "file.docx")
if _, err := Unconv(workingDir, makeFile(workingDir, "file.docx")); err == nil {
t.Error("Office conversion to PDF should have reached timeout!")
t.Errorf("Converting '%s' to PDF should have reached timeout", file.Path)
}
os.RemoveAll(workingDir)
}
func TestMerge(t *testing.T) {
path, _ := filepath.Abs("../../../_tests/configurations/gotenberg.yml")
c, _ := config.NewAppConfig(path)
Load(c.CommandsConfig)
workingDir := "test"
os.Mkdir(workingDir, 0666)
loadCommandConfigs("../../../_tests/configurations/gotenberg.yml")
var filesPaths []string
path, _ = filepath.Abs("../../../_tests/file.pdf")
path, _ := filepath.Abs("../../../_tests/file.pdf")
filesPaths = append(filesPaths, path)
filesPaths = append(filesPaths, path)
// case 1: simple merge.
if _, err := Merge(workingDir, filesPaths); err != nil {
t.Error("Merge should have worked!")
t.Error("Merge should have worked")
}
loadCommandConfigs("../../../_tests/configurations/timeout-gotenberg.yml")
// case 2: uses a command with an unsuitable timeout.
path, _ = filepath.Abs("../../../_tests/configurations/timeout-gotenberg.yml")
c, _ = config.NewAppConfig(path)
Load(c.CommandsConfig)
if _, err := Merge(workingDir, filesPaths); err == nil {
t.Error("Merge should have reached timeout!")
t.Error("Merge should have reached timeout")
}
os.RemoveAll(workingDir)
}
func TestRun(t *testing.T) {
// case 1: uses a simple command.
if err := run("echo Hello world", 30); err != nil {
t.Error("Command should have worked!")
}
// case 2: uses a simple command but with an unsuitable timeout.
if err := run("sleep 5", 0); err == nil {
t.Error("Command should not have worked!")
}
// case 3: uses a broken command.
if err := run("helloworld", 30); err == nil {
t.Error("Command should not have worked!")
}
}
func TestImpossibleConversionError(t *testing.T) {
err := &impossibleConversionError{}
if err.Error() != impossibleConversionErrorMessage {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), impossibleConversionErrorMessage)
t.Errorf("Error returned a wrong message: got '%s' want '%s'", err.Error(), impossibleConversionErrorMessage)
}
}
@@ -124,10 +135,8 @@ func TestCommandTimeoutError(t *testing.T) {
command: "echo hello",
timeout: 30,
}
expected := fmt.Sprintf("The command '%s' has reached the %d second(s) timeout", err.command, err.timeout)
if err.Error() != expected {
t.Errorf("Error returned a wrong message: got %s want %s", err.Error(), expected)
t.Errorf("Error returned a wrong message: got '%s' want '%s'", err.Error(), expected)
}
}