Skip to content

Commit

Permalink
Merge pull request #6 from Dewberry/main
Browse files Browse the repository at this point in the history
Update development against new main
  • Loading branch information
mxkpp authored Feb 8, 2024
2 parents db4316c + e252550 commit a10a215
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 22 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/papigoplug-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# papigoplug-test.yaml

name: papigoplug-test

on:
push:
branches: [main, development, tests]
pull_request:
workflow_dispatch:

jobs:
papigoplug-test-ubuntu-latest:
runs-on: ubuntu-latest

steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.21.3
id: go

- name: Install Packages
run: go get -v -t -d ./...

- name: Run Tests
run: go test -v ./papigoplug_test/papigoplug_test.go
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ func main() {
// Now it's safe to call other papigoplug functions that might use Log.
}
```

See [example.go](example.go) and [papigoplug_test/papigoplug_test.go](papigoplug_test/papigoplug_test.go) for usage details.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.3

require (
github.com/aws/aws-sdk-go v1.46.4
github.com/google/go-cmp v0.6.0
github.com/sirupsen/logrus v1.9.3
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/aws/aws-sdk-go v1.46.4/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Ph
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
36 changes: 14 additions & 22 deletions papigoplug/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,9 @@ func AWSGetSession() (sess *session.Session, err error) {

// S3Download creates a local file a blob from s3 and saves it to a local file path, using multipart concurrency.
// The file is first downloaded to a temporary location on the disk, and then is renamed/moved to the final destination.
func S3Download(sess *session.Session, s3url string, fileName string) (err error) {
Log.Infof("Downloading %q -> %q", s3url, fileName)

bucket, key, err := S3SplitBucketKey(s3url)
if err != nil {
return
}
func S3Download(sess *session.Session, bucket, key, fileName string) (err error) {
s3url := fmt.Sprintf("s3://%s/%s", bucket, key)
Log.Debugf("Downloading %s -> %s", s3url, fileName)

// Download to a temporary file
tmpFile, err := os.CreateTemp("", "")
Expand All @@ -65,43 +61,39 @@ func S3Download(sess *session.Session, s3url string, fileName string) (err error
Key: aws.String(key),
})
if err != nil {
Log.Errorf("Failed to download %q: %s", s3url, err)
Log.Errorf("Failed to download %s: %s", s3url, err)
return
}
err = tmpFile.Sync()
if err != nil {
Log.Errorf("Failed to download %q: %s", s3url, err)
Log.Errorf("Failed to download %s: %s", s3url, err)
return
}

// Rename the temporary file to the final name
err = os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
if err != nil {
Log.Errorf("Failed to make directory tree %q: %s", filepath.Dir(fileName), err)
Log.Errorf("Failed to make directory tree %s: %s", filepath.Dir(fileName), err)
return
}
err = os.Rename(tmpFile.Name(), fileName)
if err != nil {
Log.Errorf("After downloading %q, failed to move temp file to %q: %s", s3url, fileName, err)
Log.Errorf("After downloading %s, failed to move temp file to %s: %s", s3url, fileName, err)
return
}

Log.Infof("Successfully downloaded %q", s3url)
Log.Infof("Successfully downloaded %s", s3url)
return
}

// S3Upload creates a blob on s3 by streaming the bytes from a local file path, using multipart concurrency.
func S3Upload(sess *session.Session, s3url string, fileName string) (err error) {
Log.Infof("Uploading %q -> %q", fileName, s3url)

bucket, key, err := S3SplitBucketKey(s3url)
if err != nil {
return
}
func S3Upload(sess *session.Session, bucket, key, fileName string) (err error) {
s3url := fmt.Sprintf("s3://%s/%s", bucket, key)
Log.Infof("Uploading %s -> %s", fileName, s3url)

file, err := os.Open(fileName)
if err != nil {
Log.Errorf("Failed to open %q: %s", fileName, err)
Log.Errorf("Failed to open %s: %s", fileName, err)
return
}
uploader := s3manager.NewUploader(sess, func(u *s3manager.Uploader) {
Expand All @@ -113,11 +105,11 @@ func S3Upload(sess *session.Session, s3url string, fileName string) (err error)
Body: file,
})
if err != nil {
Log.Errorf("Failed to upload %q -> %q: %s", fileName, s3url, err)
Log.Errorf("Failed to upload %s -> %s: %s", fileName, s3url, err)
return
}

Log.Infof("Successfully uploaded %q -> %q", fileName, s3url)
Log.Infof("Successfully uploaded %s -> %s", fileName, s3url)
return
}

Expand Down
106 changes: 106 additions & 0 deletions papigoplug_test/papigoplug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package papigoplug_test

import (
"errors"
"fmt"
"os"
"testing"

plug "github.com/Dewberry/papigoplug/papigoplug"
"github.com/google/go-cmp/cmp"
)

func TestMain(m *testing.M) {
setup()
code := m.Run()
shutdown()
os.Exit(code)
}

func TestLog(t *testing.T) {
plug.Log.Info("Hello, World! From, papigoplug.")
}

func TestGoodParamsWithoutOptional(t *testing.T) {
pseudoOSArgs := []string{`test`, `{"last": "Torvalds", "first": "Linus"}`}
paramsExpect := map[string]interface{}{"last": "Torvalds", "first": "Linus"}
t.Logf("parsing pseudoOSArgs=%s", pseudoOSArgs)
params, err := plug.ParseInput(pseudoOSArgs, allowedParams)
if err != nil {
plug.Log.Error(err)
t.Fatal(err)
}
t.Logf("pseudoOSArgs parsed into params=%s", params)
if !cmp.Equal(params, paramsExpect) {
err = fmt.Errorf("params != paramsExpect: %s != %s", params, paramsExpect)
t.Fatal(err)
}
}

func TestGoodParamsWithOptional(t *testing.T) {
pseudoOSArgs := []string{`test`, `{"last": "Torvalds", "first": "Linus", "middle": "Benedict"}`}
paramsExpect := map[string]interface{}{"last": "Torvalds", "first": "Linus", "middle": "Benedict"}
t.Logf("parsing pseudoOSArgs=%s", pseudoOSArgs)
params, err := plug.ParseInput(pseudoOSArgs, allowedParams)
if err != nil {
t.Fatal(err)
}
t.Logf("pseudoOSArgs parsed into params=%s", params)
if !cmp.Equal(params, paramsExpect) {
err = fmt.Errorf("params != paramsExpect: %s != %s", params, paramsExpect)
t.Fatal(err)
}
}

func TestPrintResults(t *testing.T) {
testResults := map[string]interface{}{"foo": "bar"}
var err error = plug.PrintResults(testResults)
if err != nil {
t.Fatal(err)
}
}

func TestBadParamsMissingRequiredKey(t *testing.T) {
pseudoOSArgs := []string{`test`, `{"last": "Torvalds"}`}
t.Logf("parsing pseudoOSArgs=%s", pseudoOSArgs)
params, err := plug.ParseInput(pseudoOSArgs, allowedParams)
if err == nil {
err = errors.Join(err, fmt.Errorf("pseudoOSArgs should have failed to parse, but err was nil. They parsed to params=%s", params))
t.Fatal(err)
}
}

func TestBadParamsUnexpectedKey(t *testing.T) {
pseudoOSArgs := []string{`test`, `{"last": "Torvalds", "first": "Linux", "typo": true}`}
t.Logf("parsing pseudoOSArgs=%s", pseudoOSArgs)
params, err := plug.ParseInput(pseudoOSArgs, allowedParams)
if err == nil {
err = errors.Join(err, fmt.Errorf("pseudoOSArgs should have failed to parse, but err was nil. They parsed to params=%s", params))
t.Fatal(err)
}
}

// TODO: func TestS3(t *testing.T)


// e.g. these are both allowed:
// {"last": "Torvalds", "first": "Linus"}
// {"last": "Torvalds", "first": "Linus", "middle": "Benedict"}
var allowedParams = plug.PluginParams{
Required: []string{"last", "first"},
Optional: []string{"middle"},
}

func initTestLog (level string) {
plug.InitLog(level)
}

// setup is called by TestMain at the beginning of every Test
func setup() {
initTestLog("info")
}

// shutdown is called by TestMain at the end of every Test
func shutdown() {

}

0 comments on commit a10a215

Please sign in to comment.