Skip to content

Commit

Permalink
assets: move asset store impl to own package
Browse files Browse the repository at this point in the history
Tests are being created that use the asset store implementation to
validate restoring assets from the state file and disk. The tests are
much easier when they have access to the private fields of the asset
store. Rather than exposing more public fields and methods on the store,
the tests will be placed in the same package as the store. Unfortunately,
this creates cyclic imports between the base asset package and the
various packages containing the targeted assets. To rectify this,
the concrete asset store implementation has been moved to a new
pkg/asset/store package.

Changes for https://jira.coreos.com/browse/CORS-940
  • Loading branch information
staebler committed Feb 1, 2019
1 parent a7fddff commit c6f6449
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 441 deletions.
3 changes: 2 additions & 1 deletion cmd/openshift-install/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/kubeconfig"
"github.com/openshift/installer/pkg/asset/manifests"
assetstore "github.com/openshift/installer/pkg/asset/store"
"github.com/openshift/installer/pkg/asset/templates"
"github.com/openshift/installer/pkg/asset/tls"
destroybootstrap "github.com/openshift/installer/pkg/destroy/bootstrap"
Expand Down Expand Up @@ -154,7 +155,7 @@ func newCreateCmd() *cobra.Command {

func runTargetCmd(targets ...asset.WritableAsset) func(cmd *cobra.Command, args []string) {
runner := func(directory string) error {
assetStore, err := asset.NewStore(directory)
assetStore, err := assetstore.NewStore(directory)
if err != nil {
return errors.Wrapf(err, "failed to create asset store")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/openshift-install/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/installer/pkg/asset"
assetstore "github.com/openshift/installer/pkg/asset/store"
"github.com/openshift/installer/pkg/destroy"
"github.com/openshift/installer/pkg/destroy/bootstrap"
_ "github.com/openshift/installer/pkg/destroy/libvirt"
Expand Down Expand Up @@ -52,7 +52,7 @@ func runDestroyCmd(directory string) error {
return errors.Wrap(err, "Failed to destroy cluster")
}

store, err := asset.NewStore(directory)
store, err := assetstore.NewStore(directory)
if err != nil {
return errors.Wrapf(err, "failed to create asset store")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func PersistToFile(asset WritableAsset, directory string) error {
return nil
}

// deleteAssetFromDisk removes all the files for asset from disk.
// DeleteAssetFromDisk removes all the files for asset from disk.
// this is function is not safe for calling concurrently on the same directory.
func deleteAssetFromDisk(asset WritableAsset, directory string) error {
func DeleteAssetFromDisk(asset WritableAsset, directory string) error {
logrus.Debugf("Purging asset %q from disk", asset.Name())
for _, f := range asset.Files() {
path := filepath.Join(directory, f.Filename)
Expand Down
48 changes: 0 additions & 48 deletions pkg/asset/filefetcher.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package asset

import (
"io/ioutil"
"path/filepath"
"sort"
)

//go:generate mockgen -source=./filefetcher.go -destination=./mock/filefetcher_generated.go -package=mock

// FileFetcher fetches the asset files from disk.
Expand All @@ -15,45 +9,3 @@ type FileFetcher interface {
// FetchByPattern returns the files whose name match the given glob.
FetchByPattern(pattern string) ([]*File, error)
}

type fileFetcher struct {
directory string
}

// FetchByName returns the file with the given name.
func (f *fileFetcher) FetchByName(name string) (*File, error) {
data, err := ioutil.ReadFile(filepath.Join(f.directory, name))
if err != nil {
return nil, err
}
return &File{Filename: name, Data: data}, nil
}

// FetchByPattern returns the files whose name match the given regexp.
func (f *fileFetcher) FetchByPattern(pattern string) (files []*File, err error) {
matches, err := filepath.Glob(filepath.Join(f.directory, pattern))
if err != nil {
return nil, err
}

files = make([]*File, 0, len(matches))
for _, path := range matches {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

filename, err := filepath.Rel(f.directory, path)
if err != nil {
return nil, err
}

files = append(files, &File{
Filename: filename,
Data: data,
})
}

sort.Slice(files, func(i, j int) bool { return files[i].Filename < files[j].Filename })
return files, nil
}
Loading

0 comments on commit c6f6449

Please sign in to comment.