Skip to content

Commit

Permalink
Merge pull request #40 from bzimmer/integration-tests
Browse files Browse the repository at this point in the history
Integration tests
  • Loading branch information
bzimmer authored Oct 16, 2021
2 parents 754b414 + c15c7d9 commit 373f79e
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 101 deletions.
6 changes: 3 additions & 3 deletions cmd/ma/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func main() {
return err
}

grabber := &http.Client{}
grab := &http.Client{}
if c.Bool("debug") {
grabber.Transport = &httpwares.VerboseTransport{}
grab.Transport = &httpwares.VerboseTransport{}
}

httpclient, err := smugmug.NewHTTPClient(
Expand Down Expand Up @@ -156,7 +156,7 @@ func main() {
ma.RuntimeKey: &ma.Runtime{
Client: client,
Sink: sink,
Grab: grabber,
Grab: grab,
Metrics: metric,
Encoder: enc,
Fs: afero.NewOsFs(),
Expand Down
10 changes: 2 additions & 8 deletions cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,11 @@ func copy(fs afero.Fs, src, dst string) error {
if err := fs.MkdirAll(dirname, 0777); err != nil {
return err
}
log.Debug().Str("file", dst).Msg("writing")
out, err := fs.Create(dst)
if err != nil {
return err
}
defer out.Close()
log.Debug().Str("file", src).Msg("reading")
in, err := fs.Open(src)
if err != nil {
return err
Expand All @@ -163,12 +161,8 @@ func copy(fs afero.Fs, src, dst string) error {
if err = fs.Chtimes(dst, mtime, mtime); err != nil {
return err
}
info, err = fs.Stat(dst)
if err != nil {
return err
}
log.Debug().Str("src", src).Str("dst", dst).Time("src-mtime", mtime).Time("dst-mtime", info.ModTime()).Msg("preserve times")
return nil
_, err = fs.Stat(dst)
return err
}

type entangle struct {
Expand Down
1 change: 0 additions & 1 deletion cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func createTestFile(t *testing.T, fs afero.Fs) afero.File {
}

func TestCopy(t *testing.T) { //nolint
t.Parallel()
a := assert.New(t)
tests := []harness{
{
Expand Down
6 changes: 5 additions & 1 deletion export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"golang.org/x/sync/errgroup"
)

type Grab interface {
Do(req *http.Request) (*http.Response, error)
}

type request struct {
URI string
URL string
Expand All @@ -34,7 +38,7 @@ type response struct {
type exporter struct {
mg *smugmug.Client
fs afero.Fs
grab *http.Client
grab Grab
metrics *metrics.Metrics
force bool
concurrency int
Expand Down
56 changes: 40 additions & 16 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
package ma_test

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

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"

"github.com/bzimmer/httpwares"
"github.com/bzimmer/ma"
)

type grab struct {
url string
status int
}

func (g *grab) Do(req *http.Request) (*http.Response, error) {
if g.status > 0 {
res := &http.Response{
StatusCode: g.status,
ContentLength: 0,
Body: ioutil.NopCloser(bytes.NewBuffer(nil)),
Header: make(map[string][]string),
Request: req,
}
return res, nil
}
url := fmt.Sprintf("%s%s", g.url, req.URL.Path)
proxy, err := http.NewRequestWithContext(req.Context(), req.Method, url, req.Body)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(proxy)
}

func TestExport(t *testing.T) { //nolint
t.Parallel()
a := assert.New(t)

mux := http.NewServeMux()
Expand All @@ -26,6 +51,9 @@ func TestExport(t *testing.T) { //nolint
mux.HandleFunc("/album/TDZWbg!images", func(w http.ResponseWriter, r *http.Request) {
a.NoError(copyFile(w, "testdata/album_TDZWbg_images.json"))
})
mux.HandleFunc("/photos/", func(w http.ResponseWriter, r *http.Request) {
a.NoError(copyFile(w, "testdata/Nikon_D70.jpg"))
})

tests := []harness{
{
Expand All @@ -40,11 +68,7 @@ func TestExport(t *testing.T) { //nolint
"ma.export.download.ok": 1,
},
before: func(app *cli.App) {
runtime(app).Grab = &http.Client{Transport: &httpwares.TestDataTransport{
Status: http.StatusOK,
Filename: "Nikon_D70.jpg",
ContentType: "image/jpg",
}}
runtime(app).Grab = &grab{url: runtime(app).URL}
},
after: func(app *cli.App) {
stat, err := runtime(app).Fs.Stat("/foo/bar/hdxDH/VsQ7zr/Nikon_D70.jpg")
Expand All @@ -59,9 +83,10 @@ func TestExport(t *testing.T) { //nolint
"ma.export.download.failed.not_found": 1,
},
before: func(app *cli.App) {
runtime(app).Grab = &http.Client{Transport: &httpwares.TestDataTransport{
Status: http.StatusNotFound,
}}
runtime(app).Grab = &grab{
url: runtime(app).URL,
status: http.StatusNotFound,
}
},
after: func(app *cli.App) {
stat, err := runtime(app).Fs.Stat("/foo/bar/hdxDH/VsQ7zr/Nikon_D70.jpg")
Expand All @@ -78,9 +103,10 @@ func TestExport(t *testing.T) { //nolint
},
err: "download failed",
before: func(app *cli.App) {
runtime(app).Grab = &http.Client{Transport: &httpwares.TestDataTransport{
Status: http.StatusInternalServerError,
}}
runtime(app).Grab = &grab{
url: runtime(app).URL,
status: http.StatusInternalServerError,
}
},
after: func(app *cli.App) {
stat, err := runtime(app).Fs.Stat("/foo/bar/hdxDH/VsQ7zr/Nikon_D70.jpg")
Expand All @@ -96,9 +122,7 @@ func TestExport(t *testing.T) { //nolint
"ma.export.download.skipping.exists": 1,
},
before: func(app *cli.App) {
runtime(app).Grab = &http.Client{Transport: &httpwares.TestDataTransport{
Status: http.StatusOK,
}}
runtime(app).Grab = &grab{url: runtime(app).URL}
fp, err := runtime(app).Fs.Create("/foo/bar/hdxDH/VsQ7zr/Nikon_D70.jpg")
a.NotNil(fp)
a.NoError(err)
Expand Down
1 change: 0 additions & 1 deletion find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func TestFind(t *testing.T) {
t.Parallel()
a := assert.New(t)

mux := http.NewServeMux()
Expand Down
25 changes: 7 additions & 18 deletions ls_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,26 @@
package ma_test

import (
"bytes"
"encoding/json"
"testing"

"github.com/bzimmer/smugmug"
"github.com/stretchr/testify/assert"
)

func TestListIntegration(t *testing.T) {
tests := []struct {
name string
args []string
}{
a := assert.New(t)
for _, tt := range []harnessIntegration{
{
name: "ls",
args: []string{"-j", "ls", "node"},
after: func(res map[string]interface{}) {
a.Equal(smugmug.TypeFolder, res["Type"])
},
},
}

for _, tt := range tests {
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
a := assert.New(t)
ma, err := command(tt.args...)
a.NoError(err)
out, err := ma.Output()
a.NoError(err)
res := make(map[string]interface{})
dec := json.NewDecoder(bytes.NewBuffer(out))
a.NoError(dec.Decode(&res))
a.Equal(smugmug.TypeFolder, res["Type"])
harnessIntegrationFunc(t, tt)
})
}
}
38 changes: 29 additions & 9 deletions ma_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
package ma_test

import (
"bytes"
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

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

// root finds the root of the source tree by recursively ascending until 'go.mod' is located
Expand All @@ -34,22 +39,37 @@ func root() (string, error) {
return "", errors.New("unable to find go.mod")
}

func binary() (string, error) {
func command(t *testing.T, args ...string) *exec.Cmd {
dir, err := root()
if err != nil {
return "", err
t.Error(err)
}
cmd := filepath.Join(dir, "dist", "ma")
if _, err = os.Stat(cmd); err != nil {
return "", err
t.Error(err)
}
if err != nil {
t.Error(err)
}
return cmd, nil
return exec.Command(cmd, args...)
}

func command(args ...string) (*exec.Cmd, error) {
cmd, err := binary()
if err != nil {
return nil, err
type harnessIntegration struct {
name string
args []string
exit int
after func(map[string]interface{})
}

func harnessIntegrationFunc(t *testing.T, tt harnessIntegration) {
a := assert.New(t)
ma := command(t, tt.args...)
out, err := ma.Output()
a.NoError(err)
res := make(map[string]interface{})
dec := json.NewDecoder(bytes.NewBuffer(out))
a.NoError(dec.Decode(&res))
if tt.after != nil {
tt.after(res)
}
return exec.Command(cmd, args...), nil
}
Loading

0 comments on commit 373f79e

Please sign in to comment.