Skip to content

Commit

Permalink
Rename push.PushCurrentDirFiles().
Browse files Browse the repository at this point in the history
- Make API more generic.
- Add tests.
  • Loading branch information
Stephano Zanzin committed Sep 15, 2017
1 parent a172ee8 commit 68a9f1e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 5 deletions.
8 changes: 7 additions & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"

"github.com/signavio/pimba/pkg/push"

Expand All @@ -19,7 +20,12 @@ var pushCmd = &cobra.Command{
token := viper.GetString("token")

fmt.Printf("Pushing files to the Pimba server %v...\n", serverURL)
pushResp, err := push.PushCurrentDirFiles(serverURL, bucketName, token)
currentDir, err := os.Getwd()
if err != nil {
os.Stderr.WriteString("Error: Couldn't find current directory.")
os.Exit(1)
}
pushResp, err := push.PushFiles(currentDir, serverURL, bucketName, token)
if err != nil {
fmt.Println("Error:", err)
return
Expand Down
144 changes: 144 additions & 0 deletions pkg/all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package pkgtest

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"

"github.com/signavio/pimba/pkg/api"
"github.com/signavio/pimba/pkg/push"
)

const (
TestStorage = "/tmp/pimba-storage-testing"
TestSecret = "123testing"
TestPort = 9398
TestServerURL = "localhost:9398"
TestBucket = "footest"
TestFilesPath = "/tmp/pimba-test-files"
)

func init() {
go api.Serve(TestPort, TestStorage, TestSecret)
}

func TestPushURL(t *testing.T) {
if err := createTestStorage(); err != nil {
clean()
t.Errorf("Error: %v", err)
}
if err := generateTestFiles(); err != nil {
clean()
t.Errorf("Error: %v", err)
}

want := "localhost:9398/footest"
got, err := push.PushFiles(TestFilesPath, TestServerURL, TestBucket, "")
if err != nil {
clean()
t.Errorf("Error: %v", err)
}

if got[0] != want {
clean()
t.Errorf("Expected '%v' got '%v'", want, got[0])
}

clean()
}

func TestPushToken(t *testing.T) {
if err := createTestStorage(); err != nil {
clean()
t.Errorf("Error: %v", err)
}
if err := generateTestFiles(); err != nil {
clean()
t.Errorf("Error: %v", err)
}

resp, err := push.PushFiles(TestFilesPath, TestServerURL, TestBucket, "")
if err != nil {
clean()
t.Errorf("Error: %v", err)
}

want := resp[1]
got, err := push.PushFiles(TestFilesPath, TestServerURL, TestBucket, want)
if err != nil {
clean()
t.Errorf("Error: %v", err)
}

if got[1] != want {
clean()
t.Errorf("Expected '%v' got '%v'", want, got[1])
}

clean()
}

func TestPushAccess(t *testing.T) {
if err := createTestStorage(); err != nil {
clean()
t.Errorf("Error: %v", err)
}
if err := generateTestFiles(); err != nil {
clean()
t.Errorf("Error: %v", err)
}

_, err := push.PushFiles(TestFilesPath, TestServerURL, TestBucket, "")
if err != nil {
clean()
t.Errorf("Error: %v", err)
}

fileURL := "http://localhost:9398/footest/foo-1"
resp, err := http.Get(fileURL)
if err != nil {
clean()
t.Errorf("Error: %v", err)
}
defer resp.Body.Close()

want := "FOOO"
got, err := ioutil.ReadAll(resp.Body)
if err != nil {
clean()
t.Errorf("Error: %v", err)
}
if string(got) != want {
clean()
t.Errorf("Expected '%v' got '%v'", want, got)
}

clean()
}

func createTestStorage() error {
return os.MkdirAll(TestStorage, 0755)
}

func generateTestFiles() error {
os.MkdirAll(TestFilesPath, 0755)

for n := 1; n <= 5; n++ {
f, err := os.Create(fmt.Sprintf("%v/foo-%v", TestFilesPath, n))
if err != nil {
return err
}
defer f.Close()
f.WriteString("FOOO")
f.Sync()
}

return nil
}

func clean() {
os.RemoveAll(TestStorage)
os.RemoveAll(TestFilesPath)
}
6 changes: 2 additions & 4 deletions pkg/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"

"vvoid.pw/archivebuffer"
Expand All @@ -30,9 +29,8 @@ type PushResponse struct {
Error string `json:"error"`
}

func PushCurrentDirFiles(pimbaServerURL, bucketName, token string) ([]string, error) {
currentDir, _ := os.Getwd()
tarball, err := archivebuffer.NewTarballBuffer(currentDir, false)
func PushFiles(sourcePath, pimbaServerURL, bucketName, token string) ([]string, error) {
tarball, err := archivebuffer.NewTarballBuffer(sourcePath, false)
if err != nil {
return nil, errors.New(fmt.Sprintf(errorCreatingTarball, err))
}
Expand Down

0 comments on commit 68a9f1e

Please sign in to comment.