Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with relative paths when loading data streams #742

Merged
merged 3 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. [#742](https://github.com/elastic/package-registry/pull/742)

### Added

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion packages/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion packages/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, fmt.Errorf("failed to obtain path under package root path (%s): %w", fs.path, err)
}
matches[i] = match
}
return
}
Expand Down
7 changes: 5 additions & 2 deletions packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, fmt.Errorf("failed to get data stream path inside package (%s): %w", dataStreamBasePath, err)
}
paths[i] = relPath
}

return paths, nil
Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions packages/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down