Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ jobs:
go build -v -o bin/hdbbackint .

- name: Run tests
env:
APIKEY: ${{ secrets.APIKEY }}
run: go test -v -race -coverprofile=coverage.out ./tests/...
36 changes: 36 additions & 0 deletions tests/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tests

import (
"testing"
)

// Test functions

func TestCheckConfig(t *testing.T) {
configTests := setupConfigTests(t)
executablePath := getExecutablePath()

for _, cfgTest := range configTests {
t.Run(cfgTest.name, func(t *testing.T) {
output, err := runCommand(t, executablePath, "-p", cfgTest.cfgPath, "-check")

if cfgTest.shouldSucceed {
if err != nil {
t.Fatal("hdbbackint -check should succeed but failed")
} else {
t.Log("Config check passed")
}
} else {
if err == nil {
t.Fatal("hdbbackint -check should fail but succeeded")
} else {
if errMsgOk(string(output), cfgTest.msgToCheck) {
t.Log("Config check passed")
} else {
t.Fatalf("Wrong error message(s): \nOutput: %s", output)
}
}
}
})
}
}
128 changes: 0 additions & 128 deletions tests/hdbbackint_test.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,9 @@
package tests

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

const (
binaryPath = "../bin/hdbbackint"
validConfigPath = "testdata/valid_config.cfg"
invalidConfigPath = "testdata/invalid_config.cfg"
apiKeyPlaceholder = "{{APIKEYFILE}}"
dummyAPIKeyContent = "dummy"
)

// Helper functions

// setupTempDir creates a temporary directory for test files
func setupTempDir(t *testing.T) string {
t.Helper()
return t.TempDir()
}

// createAPIKeyFile creates a dummy API key file in the specified directory
func createAPIKeyFile(t *testing.T, tmpDir string) string {
t.Helper()
apiKeyFile := filepath.Join(tmpDir, "apikey")
if err := os.WriteFile(apiKeyFile, []byte(dummyAPIKeyContent), 0644); err != nil {
t.Fatalf("failed to create API key file: %v", err)
}
return apiKeyFile
}

// prepareConfigFile reads a config template, replaces placeholders, and writes it to a temp location
func prepareConfigFile(t *testing.T, tmpDir, templatePath string) string {
t.Helper()

apiKeyFile := createAPIKeyFile(t, tmpDir)

cfgBytes, err := os.ReadFile(templatePath)
if err != nil {
t.Fatalf("failed to read config template %s: %v", templatePath, err)
}

cfgContent := strings.ReplaceAll(string(cfgBytes), apiKeyPlaceholder, apiKeyFile)

cfgPath := filepath.Join(tmpDir, "hdbbackint.cfg")
if err := os.WriteFile(cfgPath, []byte(cfgContent), 0644); err != nil {
t.Fatalf("failed to write config file: %v", err)
}

return cfgPath
}

// getExecutablePath returns the path to the hdbbackint binary
func getExecutablePath() string {
return binaryPath
}

// runCommand executes a command and returns output and error
func runCommand(t *testing.T, name string, args ...string) ([]byte, error) {
cmd := exec.Command(name, args...)
return cmd.CombinedOutput()
}

// Test functions

func TestVersion(t *testing.T) {
Expand All @@ -80,69 +18,3 @@ func TestVersion(t *testing.T) {

t.Logf("Version output: %s", string(output))
}

func TestCheckConfigSuccess(t *testing.T) {
tmpDir := setupTempDir(t)
cfgPath := prepareConfigFile(t, tmpDir, validConfigPath)

output, err := runCommand(t, getExecutablePath(), "-p", cfgPath, "-check")
if err != nil {
t.Fatalf("hdbbackint -check should succeed but failed: %v\nOutput: %s", err, output)
}

t.Logf("Config check passed: %s", string(output))
}

func TestCheckConfigFail(t *testing.T) {
tmpDir := setupTempDir(t)
cfgPath := prepareConfigFile(t, tmpDir, invalidConfigPath)

output, err := runCommand(t, getExecutablePath(), "-p", cfgPath, "-check")
if err == nil {
t.Fatalf("hdbbackint -check should fail but succeeded\nOutput: %s", output)
}

t.Logf("Config check failed as expected: %s", string(output))
}

// Table-driven test for config validation
func TestConfigValidation(t *testing.T) {
tests := []struct {
name string
configPath string
shouldSucceed bool
description string
}{
{
name: "ValidConfig",
configPath: validConfigPath,
shouldSucceed: true,
description: "Valid configuration with all required fields",
},
{
name: "InvalidConfig",
configPath: invalidConfigPath,
shouldSucceed: false,
description: "Invalid configuration missing required region field",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := setupTempDir(t)
cfgPath := prepareConfigFile(t, tmpDir, tt.configPath)

output, err := runCommand(t, getExecutablePath(), "-p", cfgPath, "-check")

if tt.shouldSucceed && err != nil {
t.Errorf("%s: expected success but got error: %v\nOutput: %s",
tt.description, err, output)
}

if !tt.shouldSucceed && err == nil {
t.Errorf("%s: expected failure but got success\nOutput: %s",
tt.description, output)
}
})
}
}
183 changes: 183 additions & 0 deletions tests/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package tests

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

const (
binaryPath = "../bin/hdbbackint"
templateDir = "testdata/configs"
apiKeyPlaceholder = "{{APIKEYFILE}}"
)

const (
cfgSuccessFile = "cfg_success.tpl"
cfgWrongSectionFile = "cfg_wrong_section_name.tpl"
cfgwrongRangeFile = "cfg_wrong_range.tpl"
cfgWrongObjectTagsFile = "cfg_wrong_object_tags.tpl"
cfgUnknownParmFile = "cfg_unknown_parm.tpl"
cfgParmInWrongSectionFile = "cfg_parm_in_wrong_section.tpl"
cfgObjLockWrongPeriodFile = "cfg_obj_lock_wrong_period.tpl"
cfgObjMissingPeriodFile = "cfg_obj_lock_missing_period.tpl"
cfgMissingMandParmFile = "cfg_missing_mand_parm.tpl"
cfgMixedErrorFile = "cfg_mixed_error.tpl"
)

type ConfigTest struct {
name string
cfgPath string
shouldSucceed bool
msgToCheck string
}

// Helper functions

// setupTempDir creates a temporary directory for test files
func setupTempDir(t *testing.T) string {
t.Helper()
return t.TempDir()
}

// createAPIKeyFile creates a dummy API key file in the specified directory
func createAPIKeyFile(t *testing.T, tmpDir string) string {
t.Helper()
apiKeyFile := filepath.Join(tmpDir, "apikey")
apikey := os.Getenv("APIKEY")
if apikey == "" {
t.Fatal("APIKEY not set.")
}
if err := os.WriteFile(apiKeyFile, []byte(apikey), 0644); err != nil {
t.Fatalf("failed to create API key file: %v", err)
}
return apiKeyFile
}

// prepareConfigFile reads a config template, replaces placeholders, and writes it to a temp location
func prepareConfigFile(t *testing.T, tmpDir, apiKeyFile string, templateFile string) string {
t.Helper()
templatePath := filepath.Join(templateDir, templateFile)
cfgFile := strings.ReplaceAll(templateFile, "tpl", "cfg")
cfgBytes, err := os.ReadFile(templatePath)
if err != nil {
t.Fatalf("failed to read config template %s: %v", templatePath, err)
}

cfgContent := strings.ReplaceAll(string(cfgBytes), apiKeyPlaceholder, apiKeyFile)

cfgPath := filepath.Join(tmpDir, cfgFile)
if err := os.WriteFile(cfgPath, []byte(cfgContent), 0644); err != nil {
t.Fatalf("failed to write config file: %v", err)
}

return cfgPath
}

// getExecutablePath returns the path to the hdbbackint binary
func getExecutablePath() string {
return binaryPath
}

func setupConfigTests(t *testing.T) []ConfigTest {
t.Helper()

tmpDir := setupTempDir(t)
apiKeyFile := createAPIKeyFile(t, tmpDir)

var configTests []ConfigTest

configTests = append(configTests, ConfigTest{
name: "Valid Configuration",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgSuccessFile),
shouldSucceed: true,
msgToCheck: "",
})

configTests = append(configTests, ConfigTest{
name: "Invalid section",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgWrongSectionFile),
shouldSucceed: false,
msgToCheck: "ERROR: You specified the section 'cloud'," +
" but it is not part of the hdbbackint configuration.",
})

configTests = append(configTests, ConfigTest{
name: "Invalid range",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgwrongRangeFile),
shouldSucceed: false,
msgToCheck: "ERROR: 'max_concurrency':" +
" the value '1234' you specified is invalid.",
})

configTests = append(configTests, ConfigTest{
name: "Invalid object tags",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgWrongObjectTagsFile),
shouldSucceed: false,
msgToCheck: "ERROR: 'object_tags': the value" +
" 'key1,val1,key2=val2' you specified is invalid.",
})

configTests = append(configTests, ConfigTest{
name: "Unknown parameter",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgUnknownParmFile),
shouldSucceed: false,
msgToCheck: "ERROR: You specified 'unknown_key'" +
" in section 'objects', but the key is unknown." +
" The value of 'unknown_key' will be ignored.",
})

configTests = append(configTests, ConfigTest{
name: "Parameter in wrong section",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgParmInWrongSectionFile),
shouldSucceed: false,
msgToCheck: "ERROR: You specified 'object_tags' in section" +
" 'cloud_storage', but key belongs to section objects." +
" The value of 'object_tags' will be ignored.",
})

configTests = append(configTests, ConfigTest{
name: "Wrong object lock retention period",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgObjLockWrongPeriodFile),
shouldSucceed: false,
msgToCheck: "The value you specified for 'object_lock_retention_period'" +
" does not have the correct format.",
})

configTests = append(configTests, ConfigTest{
name: "Missing object lock retention period",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgObjMissingPeriodFile),
shouldSucceed: false,
msgToCheck: "ERROR: You specified 'object_lock_retention_mode = cmp'," +
" but no 'object_lock_retention_period' is specified.",
})

configTests = append(configTests, ConfigTest{
name: "Missing mandatory parameter",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgMissingMandParmFile),
shouldSucceed: false,
msgToCheck: "ERROR: You did not specify a value for the mandatory parameter",
})

configTests = append(configTests, ConfigTest{
name: "More than one error",
cfgPath: prepareConfigFile(t, tmpDir, apiKeyFile, cfgMixedErrorFile),
shouldSucceed: false,
msgToCheck: "ERROR: 'object_tags': the value" +
" 'key1,val1,key2=val2' you specified is invalid.",
})

return configTests
}

// runCommand executes a command and returns output and error
func runCommand(t *testing.T, name string, args ...string) ([]byte, error) {
cmd := exec.Command(name, args...)
return cmd.CombinedOutput()
}

func errMsgOk(output string, message string) bool {
return strings.Contains(output, message)
}
14 changes: 14 additions & 0 deletions tests/testdata/configs/cfg_missing_mand_parm.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[cloud_storage]
auth_mode = apikey
auth_keypath = {{APIKEYFILE}}
bucket = bucket
region = us-east

[objects]
object_tags = key1=val1,key2=val2
object_lock_retention_mode = cmp
object_lock_retention_period = 0,1,2

[backint]
max_concurrency = 8
multipart_chunksize = 512MB
Loading
Loading