diff options
Diffstat (limited to 'client/go')
-rw-r--r-- | client/go/cmd/cert.go | 2 | ||||
-rw-r--r-- | client/go/cmd/deploy.go | 6 | ||||
-rw-r--r-- | client/go/vespa/deploy.go | 31 | ||||
-rw-r--r-- | client/go/vespa/deploy_test.go | 76 |
4 files changed, 68 insertions, 47 deletions
diff --git a/client/go/cmd/cert.go b/client/go/cmd/cert.go index d53f47be1ae..269694e8630 100644 --- a/client/go/cmd/cert.go +++ b/client/go/cmd/cert.go @@ -29,7 +29,7 @@ var certCmd = &cobra.Command{ Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { app := getApplication() - pkg, err := vespa.ApplicationPackageFrom(applicationSource(args)) + pkg, err := vespa.FindApplicationPackage(applicationSource(args), false) if err != nil { fatalErr(err) return diff --git a/client/go/cmd/deploy.go b/client/go/cmd/deploy.go index 866759b18c5..705fd73aab5 100644 --- a/client/go/cmd/deploy.go +++ b/client/go/cmd/deploy.go @@ -41,7 +41,7 @@ If application directory is not specified, it defaults to working directory.`, Args: cobra.MaximumNArgs(1), DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { - pkg, err := vespa.ApplicationPackageFrom(applicationSource(args)) + pkg, err := vespa.FindApplicationPackage(applicationSource(args), true) if err != nil { fatalErr(nil, err.Error()) return @@ -92,7 +92,7 @@ var prepareCmd = &cobra.Command{ Args: cobra.MaximumNArgs(1), DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { - pkg, err := vespa.ApplicationPackageFrom(applicationSource(args)) + pkg, err := vespa.FindApplicationPackage(applicationSource(args), true) if err != nil { fatalErr(err, "Could not find application package") return @@ -125,7 +125,7 @@ var activateCmd = &cobra.Command{ Args: cobra.MaximumNArgs(1), DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { - pkg, err := vespa.ApplicationPackageFrom(applicationSource(args)) + pkg, err := vespa.FindApplicationPackage(applicationSource(args), true) if err != nil { fatalErr(err, "Could not find application package") return diff --git a/client/go/vespa/deploy.go b/client/go/vespa/deploy.go index 22ab5380c23..19cce36e81c 100644 --- a/client/go/vespa/deploy.go +++ b/client/go/vespa/deploy.go @@ -107,26 +107,27 @@ func (ap *ApplicationPackage) zipReader() (io.ReadCloser, error) { return r, nil } -// Find an application package zip or directory below an application path -func ApplicationPackageFrom(application string) (ApplicationPackage, error) { - if isZip(application) { - return ApplicationPackage{Path: application}, nil - } - if util.PathExists(filepath.Join(application, "pom.xml")) { - zip := filepath.Join(application, "target", "application.zip") - if !util.PathExists(zip) { - return ApplicationPackage{}, errors.New("pom.xml exists but no target/application.zip. Run mvn package first") - } else { +// FindApplicationPackage finds the path to an application package from the zip file or directory zipOrDir. +func FindApplicationPackage(zipOrDir string, requirePackaging bool) (ApplicationPackage, error) { + if isZip(zipOrDir) { + return ApplicationPackage{Path: zipOrDir}, nil + } + if util.PathExists(filepath.Join(zipOrDir, "pom.xml")) { + zip := filepath.Join(zipOrDir, "target", "application.zip") + if util.PathExists(zip) { return ApplicationPackage{Path: zip}, nil } + if requirePackaging { + return ApplicationPackage{}, errors.New("pom.xml exists but no target/application.zip. Run mvn package first") + } } - if util.PathExists(filepath.Join(application, "src", "main", "application")) { - return ApplicationPackage{Path: filepath.Join(application, "src", "main", "application")}, nil + if util.PathExists(filepath.Join(zipOrDir, "src", "main", "application")) { + return ApplicationPackage{Path: filepath.Join(zipOrDir, "src", "main", "application")}, nil } - if util.PathExists(filepath.Join(application, "services.xml")) { - return ApplicationPackage{Path: application}, nil + if util.PathExists(filepath.Join(zipOrDir, "services.xml")) { + return ApplicationPackage{Path: zipOrDir}, nil } - return ApplicationPackage{}, errors.New("Could not find an application package source in '" + application + "'") + return ApplicationPackage{}, errors.New("Could not find an application package source in '" + zipOrDir + "'") } func ApplicationFromString(s string) (ApplicationID, error) { diff --git a/client/go/vespa/deploy_test.go b/client/go/vespa/deploy_test.go index f99d42f537e..32b31eebf7c 100644 --- a/client/go/vespa/deploy_test.go +++ b/client/go/vespa/deploy_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -25,38 +26,57 @@ func TestZoneFromString(t *testing.T) { assert.NotNil(t, err) } -func TestApplicationPackageFrom(t *testing.T) { +func TestFindApplicationPackage(t *testing.T) { dir := t.TempDir() - var tests = []struct { - in string - out string - fail bool - }{ - {filepath.Join(dir, "foo"), "", true}, - {filepath.Join(dir, "services.xml"), dir, false}, - {filepath.Join(dir, "src", "main", "application", "services.xml"), filepath.Join(dir, "src", "main", "application"), false}, - } + assertFindApplicationPackage(t, dir, pkgFixture{ + expectedPath: dir, + existingFile: filepath.Join(dir, "services.xml"), + }) + assertFindApplicationPackage(t, dir, pkgFixture{ + expectedPath: filepath.Join(dir, "src", "main", "application"), + existingFile: filepath.Join(dir, "src", "main", "application") + string(os.PathSeparator), + }) + assertFindApplicationPackage(t, dir, pkgFixture{ + expectedPath: filepath.Join(dir, "src", "main", "application"), + existingFile: filepath.Join(dir, "pom.xml"), + }) + assertFindApplicationPackage(t, dir, pkgFixture{ + existingFile: filepath.Join(dir, "pom.xml"), + requirePackaging: true, + fail: true, + }) + assertFindApplicationPackage(t, dir, pkgFixture{ + expectedPath: filepath.Join(dir, "target", "application.zip"), + existingFiles: []string{filepath.Join(dir, "pom.xml"), filepath.Join(dir, "target", "application.zip")}, + requirePackaging: true, + }) +} - zipFile := filepath.Join(dir, "application.zip") - writeFile(t, zipFile) - pkg, err := ApplicationPackageFrom(zipFile) - assert.Nil(t, err) - assert.Equal(t, zipFile, pkg.Path) - - for i, tt := range tests { - writeFile(t, tt.in) - pkg, err := ApplicationPackageFrom(dir) - if tt.fail { - assert.NotNil(t, err) - } else if pkg.Path != tt.out { - t.Errorf("#%d: FindApplicationPackage(%q) = (%q, %s), want (%q, nil)", i, dir, pkg.Path, err, tt.out) - } +type pkgFixture struct { + expectedPath string + existingFile string + existingFiles []string + requirePackaging bool + fail bool +} + +func assertFindApplicationPackage(t *testing.T, zipOrDir string, fixture pkgFixture) { + if fixture.existingFile != "" { + writeFile(t, fixture.existingFile) + } + for _, f := range fixture.existingFiles { + writeFile(t, f) } + pkg, err := FindApplicationPackage(zipOrDir, fixture.requirePackaging) + assert.Equal(t, err != nil, fixture.fail, "Expected error for "+zipOrDir) + assert.Equal(t, fixture.expectedPath, pkg.Path) } -func writeFile(t *testing.T, filename string) { - err := os.MkdirAll(filepath.Dir(filename), 0755) - assert.Nil(t, err) - err = ioutil.WriteFile(filename, []byte{0}, 0644) +func writeFile(t *testing.T, name string) { + err := os.MkdirAll(filepath.Dir(name), 0755) assert.Nil(t, err) + if !strings.HasSuffix(name, string(os.PathSeparator)) { + err = ioutil.WriteFile(name, []byte{0}, 0644) + assert.Nil(t, err) + } } |