Skip to content

Commit

Permalink
Merge pull request #580 from joereuss12/init_config_with_plugin_branch
Browse files Browse the repository at this point in the history
Stash plugin calls init-config & added unit test
  • Loading branch information
bbockelm authored Jan 5, 2024
2 parents 1eb1c96 + 6fd9403 commit 3d627c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func init() {
}

func stashPluginMain(args []string) {
config.InitConfig()
err := config.InitClient()
if err != nil {
log.Errorln(err)
Expand All @@ -83,7 +84,6 @@ func stashPluginMain(args []string) {
client.ObjectClientOptions.ProgressBars = false
client.ObjectClientOptions.Version = version
client.ObjectClientOptions.Plugin = true
config.SetLogging(log.PanicLevel)
methods := []string{"http"}
var infile, outfile, testCachePath string
var useOutFile bool = false
Expand Down Expand Up @@ -300,10 +300,17 @@ func stashPluginMain(args []string) {
if err = outputFile.Sync(); err != nil {
var perr *fs.PathError
var serr syscall.Errno
if errors.As(err, &perr) && errors.As(perr.Unwrap(), &serr) && serr == syscall.EINVAL {
// Error code 1 (serr) is ERROR_INVALID_FUNCTION, the expected Windows syscall error
// Error code EINVAL is returned on Linux
// Error code ENODEV is returned on Mac OS X
if errors.As(err, &perr) && errors.As(perr.Unwrap(), &serr) && (int(serr) == 1 || serr == syscall.EINVAL || serr == syscall.ENODEV) {
log.Debugf("Error when syncing: %s; can be ignored\n", perr)
} else {
log.Errorln("Failed to sync output file:", err)
if errors.As(err, &perr) && errors.As(perr.Unwrap(), &serr) {
log.Errorf("Failed to sync output file: %s (errno %d)", serr, int(serr))
} else {
log.Errorln("Failed to sync output file:", err)
}
os.Exit(1)
}
}
Expand Down
42 changes: 42 additions & 0 deletions cmd/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ package main

import (
"bufio"
"bytes"
"os"
"os/exec"
"strings"
"testing"

"github.com/pelicanplatform/pelican/config"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -50,3 +56,39 @@ func TestReadMultiTransfer(t *testing.T) {
assert.Equal(t, "url://server/some/directory//blah", transfers[0].url)
assert.Equal(t, "/path/to/local/copy/of/blah", transfers[0].localFile)
}

func TestStashPluginMain(t *testing.T) {
viper.Reset()
config.SetPreferredPrefix("STASH")

// Temp dir for downloads
tempDir := os.TempDir()
defer os.Remove(tempDir)

// Parts of test adapted from: https://stackoverflow.com/questions/26225513/how-to-test-os-exit-scenarios-in-go
if os.Getenv("RUN_STASHPLUGIN") == "1" {
// Download a test file
args := []string{"pelican://pelican.example.com/osgconnect/public/osg/testfile.txt", tempDir}
stashPluginMain(args)
os.Unsetenv("STASH_LOGGING_LEVEL")
os.Unsetenv("RUN_STASHPLUGIN")
return
}

// Create a process to run the command (since stashPluginMain calls os.Exit(0))
cmd := exec.Command(os.Args[0], "-test.run=TestStashPluginMain")
cmd.Env = append(os.Environ(), "RUN_STASHPLUGIN=1", "STASH_LOGGING_LEVEL=debug")

// Create buffers for stderr (the output we want for test)
var stderr bytes.Buffer
cmd.Stderr = &stderr

err := cmd.Run()
assert.Error(t, err, stderr.String())

// changing output for "\\" since in windows there are excess "\" printed in debug logs
output := strings.Replace(stderr.String(), "\\\\", "\\", -1)

expectedOutput := "Downloading: pelican://pelican.example.com/osgconnect/public/osg/testfile.txt to " + tempDir
assert.Contains(t, output, expectedOutput)
}

0 comments on commit 3d627c4

Please sign in to comment.