From efa9e39a70bd939222fd5673f8e00ffbdba23e76 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 7 Oct 2021 17:24:06 +0200 Subject: [PATCH 1/3] Fix issue with relative paths when loading data streams --- CHANGELOG.md | 2 ++ packages/datastream.go | 2 +- packages/fs.go | 6 +++++- packages/package.go | 7 +++++-- packages/package_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d022f4c9c..1ab4c2e98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bugfixes +* Fix issue with relative paths when loading data streams. []() + ### Added ### Deprecated diff --git a/packages/datastream.go b/packages/datastream.go index 01e5dd20a..7f0296bf3 100644 --- a/packages/datastream.go +++ b/packages/datastream.go @@ -122,7 +122,7 @@ func NewDataStream(basePath string, p *Package) (*DataStream, error) { // Check if manifest exists _, err = fs.Stat(manifestPath) if err != nil && os.IsNotExist(err) { - return nil, errors.Wrapf(err, "manifest does not exist for package: %s", p.BasePath) + return nil, errors.Wrapf(err, "manifest does not exist for data stream: %s", p.BasePath) } dataStreamPath := filepath.Base(basePath) diff --git a/packages/fs.go b/packages/fs.go index def8c9234..f89c6ff64 100644 --- a/packages/fs.go +++ b/packages/fs.go @@ -49,7 +49,11 @@ func (fs *ExtractedPackageFileSystem) Glob(pattern string) (matches []string, er return } for i := range matches { - matches[i] = matches[i][len(fs.path+string(filepath.Separator)):] + match, err := filepath.Rel(fs.path, matches[i]) + if err != nil { + return nil, err + } + matches[i] = match } return } diff --git a/packages/package.go b/packages/package.go index 66269742e..d49d53d47 100644 --- a/packages/package.go +++ b/packages/package.go @@ -511,7 +511,11 @@ func (p *Package) GetDataStreamPaths() ([]string, error) { } for i, _ := range paths { - paths[i] = paths[i][len(dataStreamBasePath)+1:] + relPath, err := filepath.Rel(dataStreamBasePath, paths[i]) + if err != nil { + return nil, err + } + paths[i] = relPath } return paths, nil @@ -525,7 +529,6 @@ func (p *Package) LoadDataSets() error { dataStreamsBasePath := "data_stream" for _, dataStreamPath := range dataStreamPaths { - dataStreamBasePath := filepath.Join(dataStreamsBasePath, dataStreamPath) d, err := NewDataStream(dataStreamBasePath, p) diff --git a/packages/package_test.go b/packages/package_test.go index 6e8e8a869..c65c64c1e 100644 --- a/packages/package_test.go +++ b/packages/package_test.go @@ -6,10 +6,12 @@ package packages import ( "log" + "path/filepath" "testing" "github.com/Masterminds/semver/v3" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var ( @@ -209,6 +211,45 @@ func TestHasKibanaVersion(t *testing.T) { } } +func TestNewPackageFromPath(t *testing.T) { + packagePath := "../testdata/package/reference/1.0.0" + absPath, err := filepath.Abs(packagePath) + require.NoError(t, err) + + cases := []struct { + title string + path string + }{ + { + title: "relative path", + path: packagePath, + }, + { + title: "relative path with slash", + path: packagePath + "/", + }, + { + title: "absolute path", + path: absPath, + }, + { + title: "absolute path with slash", + path: absPath + "/", + }, + } + + fsBuilder := func(p *Package) (PackageFileSystem, error) { + return NewExtractedPackageFileSystem(p) + } + + for _, c := range cases { + t.Run(c.title, func(t *testing.T) { + _, err := NewPackage(c.path, fsBuilder) + assert.NoError(t, err) + }) + } +} + func BenchmarkNewPackage(b *testing.B) { fsBuilder := func(p *Package) (PackageFileSystem, error) { return NewExtractedPackageFileSystem(p) From bea43355c5cf00c75f234557885fdb80f586e993 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 7 Oct 2021 17:27:29 +0200 Subject: [PATCH 2/3] Fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ab4c2e98..004b63efd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bugfixes -* Fix issue with relative paths when loading data streams. []() +* Fix issue with relative paths when loading data streams. [#742](https://github.com/elastic/package-registry/pull/742) ### Added From d3341a16916e53154e91e8ef43abee1b762d1fcc Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Mon, 11 Oct 2021 13:29:09 +0200 Subject: [PATCH 3/3] Provide more info on errors --- packages/fs.go | 2 +- packages/package.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fs.go b/packages/fs.go index f89c6ff64..0b4e1f49e 100644 --- a/packages/fs.go +++ b/packages/fs.go @@ -51,7 +51,7 @@ func (fs *ExtractedPackageFileSystem) Glob(pattern string) (matches []string, er for i := range matches { match, err := filepath.Rel(fs.path, matches[i]) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to obtain path under package root path (%s): %w", fs.path, err) } matches[i] = match } diff --git a/packages/package.go b/packages/package.go index d49d53d47..6eac65624 100644 --- a/packages/package.go +++ b/packages/package.go @@ -513,7 +513,7 @@ func (p *Package) GetDataStreamPaths() ([]string, error) { for i, _ := range paths { relPath, err := filepath.Rel(dataStreamBasePath, paths[i]) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get data stream path inside package (%s): %w", dataStreamBasePath, err) } paths[i] = relPath }