diff --git a/pkg/build/cleanup.go b/pkg/build/cleanup.go index 407d474b1..9e75efc11 100644 --- a/pkg/build/cleanup.go +++ b/pkg/build/cleanup.go @@ -3,7 +3,7 @@ package build import ( "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/docker" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) @@ -13,12 +13,12 @@ var glog = utilglog.StderrLog // temporary directories created by STI build and it also cleans the temporary // Docker images produced by LayeredBuild type DefaultCleaner struct { - fs util.FileSystem + fs fs.FileSystem docker docker.Docker } // NewDefaultCleaner creates a new instance of the default Cleaner implementation -func NewDefaultCleaner(fs util.FileSystem, docker docker.Docker) Cleaner { +func NewDefaultCleaner(fs fs.FileSystem, docker docker.Docker) Cleaner { return &DefaultCleaner{ fs: fs, docker: docker, diff --git a/pkg/build/strategies/layered/layered.go b/pkg/build/strategies/layered/layered.go index 85f3beed7..9a82d419c 100644 --- a/pkg/build/strategies/layered/layered.go +++ b/pkg/build/strategies/layered/layered.go @@ -17,7 +17,7 @@ import ( "github.com/openshift/source-to-image/pkg/docker" s2ierr "github.com/openshift/source-to-image/pkg/errors" "github.com/openshift/source-to-image/pkg/tar" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" utilstatus "github.com/openshift/source-to-image/pkg/util/status" ) @@ -34,14 +34,14 @@ const defaultDestination = "/tmp" type Layered struct { config *api.Config docker docker.Docker - fs util.FileSystem + fs fs.FileSystem tar tar.Tar scripts build.ScriptsHandler hasOnBuild bool } // New creates a Layered builder. -func New(client docker.Client, config *api.Config, fs util.FileSystem, scripts build.ScriptsHandler, overrides build.Overrides) (*Layered, error) { +func New(client docker.Client, config *api.Config, fs fs.FileSystem, scripts build.ScriptsHandler, overrides build.Overrides) (*Layered, error) { excludePattern, err := regexp.Compile(config.ExcludeRegExp) if err != nil { return nil, err diff --git a/pkg/build/strategies/layered/layered_test.go b/pkg/build/strategies/layered/layered_test.go index 86ac6bd6b..2645c2900 100644 --- a/pkg/build/strategies/layered/layered_test.go +++ b/pkg/build/strategies/layered/layered_test.go @@ -14,6 +14,7 @@ import ( "github.com/openshift/source-to-image/pkg/build" "github.com/openshift/source-to-image/pkg/docker" "github.com/openshift/source-to-image/pkg/test" + testfs "github.com/openshift/source-to-image/pkg/test/fs" ) type FakeExecutor struct{} @@ -26,7 +27,7 @@ func newFakeLayered() *Layered { return &Layered{ docker: &docker.FakeDocker{}, config: &api.Config{}, - fs: &test.FakeFileSystem{}, + fs: &testfs.FakeFileSystem{}, tar: &test.FakeTar{}, scripts: &FakeExecutor{}, } @@ -36,7 +37,7 @@ func newFakeLayeredWithScripts(workDir string) *Layered { return &Layered{ docker: &docker.FakeDocker{}, config: &api.Config{WorkingDir: workDir}, - fs: &test.FakeFileSystem{}, + fs: &testfs.FakeFileSystem{}, tar: &test.FakeTar{}, scripts: &FakeExecutor{}, } @@ -143,7 +144,7 @@ func TestBuildNoScriptsProvided(t *testing.T) { func TestBuildErrorWriteDockerfile(t *testing.T) { l := newFakeLayered() l.config.BuilderImage = "test/image" - l.fs.(*test.FakeFileSystem).WriteFileError = errors.New("WriteDockerfileError") + l.fs.(*testfs.FakeFileSystem).WriteFileError = errors.New("WriteDockerfileError") _, err := l.Build(l.config) if err == nil || err.Error() != "WriteDockerfileError" { t.Errorf("An error was expected for WriteDockerfile, but got different: %v", err) diff --git a/pkg/build/strategies/onbuild/entrypoint.go b/pkg/build/strategies/onbuild/entrypoint.go index b42621cc6..6eb277c9c 100644 --- a/pkg/build/strategies/onbuild/entrypoint.go +++ b/pkg/build/strategies/onbuild/entrypoint.go @@ -5,7 +5,7 @@ import ( "path/filepath" "regexp" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) @@ -20,7 +20,7 @@ var validEntrypoints = []*regexp.Regexp{ // GuessEntrypoint tries to guess the valid entrypoint from the source code // repository. The valid entrypoints are defined above (run,start,exec,execute) -func GuessEntrypoint(fs util.FileSystem, sourceDir string) (string, error) { +func GuessEntrypoint(fs fs.FileSystem, sourceDir string) (string, error) { files, err := fs.ReadDir(sourceDir) if err != nil { return "", err @@ -40,7 +40,7 @@ func GuessEntrypoint(fs util.FileSystem, sourceDir string) (string, error) { // isValidEntrypoint checks if the given file exists and if it is a regular // file. Valid ENTRYPOINT must be an executable file, so the executable bit must // be set. -func isValidEntrypoint(fs util.FileSystem, path string) bool { +func isValidEntrypoint(fs fs.FileSystem, path string) bool { stat, err := fs.Stat(path) if err != nil { return false diff --git a/pkg/build/strategies/onbuild/entrypoint_test.go b/pkg/build/strategies/onbuild/entrypoint_test.go index 953b1e216..8f11b6da2 100644 --- a/pkg/build/strategies/onbuild/entrypoint_test.go +++ b/pkg/build/strategies/onbuild/entrypoint_test.go @@ -5,46 +5,46 @@ import ( "strings" "testing" - "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + testfs "github.com/openshift/source-to-image/pkg/test/fs" + "github.com/openshift/source-to-image/pkg/util/fs" ) func TestGuessEntrypoint(t *testing.T) { testMatrix := map[string][]os.FileInfo{ "run": { - &util.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "run", FileIsDir: false, FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileIsDir: false, FileMode: 0777}, }, "start.sh": { - &util.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "start.sh", FileIsDir: false, FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "start.sh", FileIsDir: false, FileMode: 0777}, }, "execute": { - &util.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "execute", FileIsDir: false, FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "execute", FileIsDir: false, FileMode: 0777}, }, "ERR:run_not_executable": { - &util.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "run", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileIsDir: false, FileMode: 0600}, }, "ERR:run_is_dir": { - &util.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "run", FileIsDir: true, FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileIsDir: true, FileMode: 0777}, }, "ERR:none": { - &util.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "config.ru", FileIsDir: false, FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileIsDir: false, FileMode: 0600}, }, } for expectedEntrypoint, files := range testMatrix { - f := &test.FakeFileSystem{Files: files} + f := &testfs.FakeFileSystem{Files: files} result, err := GuessEntrypoint(f, "/test") if strings.HasPrefix(expectedEntrypoint, "ERR:") { diff --git a/pkg/build/strategies/onbuild/onbuild.go b/pkg/build/strategies/onbuild/onbuild.go index 75123f11b..0ae1fc7f1 100644 --- a/pkg/build/strategies/onbuild/onbuild.go +++ b/pkg/build/strategies/onbuild/onbuild.go @@ -16,7 +16,7 @@ import ( "github.com/openshift/source-to-image/pkg/scm/git" "github.com/openshift/source-to-image/pkg/scripts" "github.com/openshift/source-to-image/pkg/tar" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilstatus "github.com/openshift/source-to-image/pkg/util/status" ) @@ -25,7 +25,7 @@ import ( type OnBuild struct { docker docker.Docker git git.Git - fs util.FileSystem + fs fs.FileSystem tar tar.Tar source build.SourceHandler garbage build.Cleaner @@ -38,7 +38,7 @@ type onBuildSourceHandler struct { } // New returns a new instance of OnBuild builder -func New(client docker.Client, config *api.Config, fs util.FileSystem, overrides build.Overrides) (*OnBuild, error) { +func New(client docker.Client, config *api.Config, fs fs.FileSystem, overrides build.Overrides) (*OnBuild, error) { dockerHandler := docker.New(client, config.PullAuthentication) builder := &OnBuild{ docker: dockerHandler, diff --git a/pkg/build/strategies/onbuild/onbuild_test.go b/pkg/build/strategies/onbuild/onbuild_test.go index 38b8b3c2a..22c936d20 100644 --- a/pkg/build/strategies/onbuild/onbuild_test.go +++ b/pkg/build/strategies/onbuild/onbuild_test.go @@ -12,7 +12,8 @@ import ( "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/docker" "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + testfs "github.com/openshift/source-to-image/pkg/test/fs" + "github.com/openshift/source-to-image/pkg/util/fs" ) type fakeSourceHandler struct{} @@ -37,14 +38,14 @@ func newFakeOnBuild() *OnBuild { return &OnBuild{ docker: &docker.FakeDocker{}, git: &test.FakeGit{}, - fs: &test.FakeFileSystem{}, + fs: &testfs.FakeFileSystem{}, tar: &test.FakeTar{}, source: &fakeSourceHandler{}, garbage: &fakeCleaner{}, } } -func checkDockerfile(fs *test.FakeFileSystem, t *testing.T) { +func checkDockerfile(fs *testfs.FakeFileSystem, t *testing.T) { if fs.WriteFileError != nil { t.Errorf("%v", fs.WriteFileError) } @@ -71,11 +72,11 @@ func TestCreateDockerfile(t *testing.T) { }, } b := newFakeOnBuild() - fakeFs := &test.FakeFileSystem{ + fakeFs := &testfs.FakeFileSystem{ Files: []os.FileInfo{ - &util.FileInfo{FileName: "config.ru", FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileMode: 0600}, - &util.FileInfo{FileName: "run", FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileMode: 0777}, }, } b.fs = fakeFs @@ -91,12 +92,12 @@ func TestCreateDockerfileWithAssemble(t *testing.T) { BuilderImage: "fake:onbuild", } b := newFakeOnBuild() - fakeFs := &test.FakeFileSystem{ + fakeFs := &testfs.FakeFileSystem{ Files: []os.FileInfo{ - &util.FileInfo{FileName: "config.ru", FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileMode: 0600}, - &util.FileInfo{FileName: "run", FileMode: 0777}, - &util.FileInfo{FileName: "assemble", FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileMode: 0777}, + &fs.FileInfo{FileName: "assemble", FileMode: 0777}, }, } b.fs = fakeFs @@ -116,11 +117,11 @@ func TestBuild(t *testing.T) { Tag: "fakeapp", } b := newFakeOnBuild() - fakeFs := &test.FakeFileSystem{ + fakeFs := &testfs.FakeFileSystem{ Files: []os.FileInfo{ - &util.FileInfo{FileName: "config.ru", FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileMode: 0600}, - &util.FileInfo{FileName: "run", FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileMode: 0777}, }, } b.fs = fakeFs @@ -142,11 +143,11 @@ func TestBuildOnBuildBlocked(t *testing.T) { BlockOnBuild: true, } b := newFakeOnBuild() - fakeFs := &test.FakeFileSystem{ + fakeFs := &testfs.FakeFileSystem{ Files: []os.FileInfo{ - &util.FileInfo{FileName: "config.ru", FileMode: 0600}, - &util.FileInfo{FileName: "app.rb", FileMode: 0600}, - &util.FileInfo{FileName: "run", FileMode: 0777}, + &fs.FileInfo{FileName: "config.ru", FileMode: 0600}, + &fs.FileInfo{FileName: "app.rb", FileMode: 0600}, + &fs.FileInfo{FileName: "run", FileMode: 0777}, }, } b.fs = fakeFs diff --git a/pkg/build/strategies/sti/postexecutorstep.go b/pkg/build/strategies/sti/postexecutorstep.go index 95821367e..b06cd27f3 100644 --- a/pkg/build/strategies/sti/postexecutorstep.go +++ b/pkg/build/strategies/sti/postexecutorstep.go @@ -17,6 +17,7 @@ import ( s2ierr "github.com/openshift/source-to-image/pkg/errors" s2itar "github.com/openshift/source-to-image/pkg/tar" "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilstatus "github.com/openshift/source-to-image/pkg/util/status" ) @@ -107,7 +108,7 @@ type commitImageStep struct { image string builder *STI docker dockerpkg.Docker - fs util.FileSystem + fs fs.FileSystem tar s2itar.Tar } @@ -168,7 +169,7 @@ func (step *commitImageStep) execute(ctx *postExecutorStepContext) error { type downloadFilesFromBuilderImageStep struct { builder *STI docker dockerpkg.Docker - fs util.FileSystem + fs fs.FileSystem tar s2itar.Tar } @@ -234,7 +235,7 @@ func (step *downloadFilesFromBuilderImageStep) downloadAndExtractFile(artifactPa type startRuntimeImageAndUploadFilesStep struct { builder *STI docker dockerpkg.Docker - fs util.FileSystem + fs fs.FileSystem } func (step *startRuntimeImageAndUploadFilesStep) execute(ctx *postExecutorStepContext) error { diff --git a/pkg/build/strategies/sti/sti.go b/pkg/build/strategies/sti/sti.go index 2d427b5b8..958df0880 100644 --- a/pkg/build/strategies/sti/sti.go +++ b/pkg/build/strategies/sti/sti.go @@ -23,6 +23,7 @@ import ( "github.com/openshift/source-to-image/pkg/scripts" "github.com/openshift/source-to-image/pkg/tar" "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" utilstatus "github.com/openshift/source-to-image/pkg/util/status" ) @@ -50,7 +51,7 @@ type STI struct { installer scripts.Installer runtimeInstaller scripts.Installer git git.Git - fs util.FileSystem + fs fs.FileSystem tar tar.Tar docker dockerpkg.Docker incrementalDocker dockerpkg.Docker @@ -87,7 +88,7 @@ type STI struct { // If the layeredBuilder parameter is specified, then the builder provided will // be used for the case that the base Docker image does not have 'tar' or 'bash' // installed. -func New(client dockerpkg.Client, config *api.Config, fs util.FileSystem, overrides build.Overrides) (*STI, error) { +func New(client dockerpkg.Client, config *api.Config, fs fs.FileSystem, overrides build.Overrides) (*STI, error) { excludePattern, err := regexp.Compile(config.ExcludeRegExp) if err != nil { return nil, err diff --git a/pkg/build/strategies/sti/sti_test.go b/pkg/build/strategies/sti/sti_test.go index db0707a72..18ee3f8f0 100644 --- a/pkg/build/strategies/sti/sti_test.go +++ b/pkg/build/strategies/sti/sti_test.go @@ -19,7 +19,8 @@ import ( "github.com/openshift/source-to-image/pkg/scm/file" "github.com/openshift/source-to-image/pkg/scm/git" "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + testfs "github.com/openshift/source-to-image/pkg/test/fs" + "github.com/openshift/source-to-image/pkg/util/fs" ) type FakeSTI struct { @@ -55,7 +56,7 @@ func newFakeBaseSTI() *STI { docker: &docker.FakeDocker{}, installer: &test.FakeInstaller{}, git: &test.FakeGit{}, - fs: &test.FakeFileSystem{}, + fs: &testfs.FakeFileSystem{}, tar: &test.FakeTar{}, } } @@ -68,7 +69,7 @@ func newFakeSTI(f *FakeSTI) *STI { runtimeDocker: &docker.FakeDocker{}, installer: &test.FakeInstaller{}, git: &test.FakeGit{}, - fs: &test.FakeFileSystem{}, + fs: &testfs.FakeFileSystem{}, tar: &test.FakeTar{}, preparer: f, ignorer: &ignore.DockerIgnorer{}, @@ -152,7 +153,7 @@ func TestDefaultSource(t *testing.T) { if err != nil { t.Fatal(err) } - sti, err := New(client, config, util.NewFileSystem(), build.Overrides{}) + sti, err := New(client, config, fs.NewFileSystem(), build.Overrides{}) if err != nil { t.Fatal(err) } @@ -173,7 +174,7 @@ func TestEmptySource(t *testing.T) { if err != nil { t.Fatal(err) } - sti, err := New(client, config, util.NewFileSystem(), build.Overrides{}) + sti, err := New(client, config, fs.NewFileSystem(), build.Overrides{}) if err != nil { t.Fatal(err) } @@ -195,7 +196,7 @@ func TestOverrides(t *testing.T) { &api.Config{ DockerConfig: &api.DockerConfig{Endpoint: "unix:///var/run/docker.sock"}, }, - util.NewFileSystem(), + fs.NewFileSystem(), build.Overrides{ Downloader: fd, }, @@ -320,7 +321,7 @@ func testBuildHandler() *STI { incrementalDocker: &docker.FakeDocker{}, installer: &test.FakeInstaller{}, git: &test.FakeGit{}, - fs: &test.FakeFileSystem{ExistsResult: map[string]bool{filepath.FromSlash("a-repo-source"): true}}, + fs: &testfs.FakeFileSystem{ExistsResult: map[string]bool{filepath.FromSlash("a-repo-source"): true}}, tar: &test.FakeTar{}, config: &api.Config{}, result: &api.Result{}, @@ -444,10 +445,10 @@ func TestExists(t *testing.T) { i, incremental, ti.expected) } if ti.incremental && ti.previousImage && ti.scriptInstalled { - if len(bh.fs.(*test.FakeFileSystem).ExistsFile) == 0 { + if len(bh.fs.(*testfs.FakeFileSystem).ExistsFile) == 0 { continue } - scriptChecked := bh.fs.(*test.FakeFileSystem).ExistsFile[0] + scriptChecked := bh.fs.(*testfs.FakeFileSystem).ExistsFile[0] expectedScript := "/working-dir/upload/scripts/save-artifacts" if scriptChecked != expectedScript { t.Errorf("(%d) Unexpected script checked. Actual: %s. Expected: %s", @@ -461,7 +462,7 @@ func TestSaveArtifacts(t *testing.T) { bh := testBuildHandler() bh.config.WorkingDir = "/working-dir" bh.config.Tag = "image/tag" - fs := bh.fs.(*test.FakeFileSystem) + fs := bh.fs.(*testfs.FakeFileSystem) fd := bh.docker.(*docker.FakeDocker) th := bh.tar.(*test.FakeTar) err := bh.Save(bh.config) @@ -487,7 +488,7 @@ func TestSaveArtifactsCustomTag(t *testing.T) { bh.config.WorkingDir = "/working-dir" bh.config.IncrementalFromTag = "custom/tag" bh.config.Tag = "image/tag" - fs := bh.fs.(*test.FakeFileSystem) + fs := bh.fs.(*testfs.FakeFileSystem) fd := bh.docker.(*docker.FakeDocker) th := bh.tar.(*test.FakeTar) err := bh.Save(bh.config) @@ -616,19 +617,19 @@ func TestFetchSource(t *testing.T) { func TestPrepareOK(t *testing.T) { rh := newFakeSTI(&FakeSTI{}) rh.SetScripts([]string{api.Assemble, api.Run}, []string{api.SaveArtifacts}) - rh.fs.(*test.FakeFileSystem).WorkingDirResult = "/working-dir" + rh.fs.(*testfs.FakeFileSystem).WorkingDirResult = "/working-dir" err := rh.Prepare(rh.config) if err != nil { t.Errorf("An error occurred setting up the config handler: %v", err) } - if !rh.fs.(*test.FakeFileSystem).WorkingDirCalled { + if !rh.fs.(*testfs.FakeFileSystem).WorkingDirCalled { t.Errorf("Working directory was not created.") } var expected []string for _, dir := range workingDirs { expected = append(expected, filepath.FromSlash("/working-dir/"+dir)) } - mkdirs := rh.fs.(*test.FakeFileSystem).MkdirAllDir + mkdirs := rh.fs.(*testfs.FakeFileSystem).MkdirAllDir if !reflect.DeepEqual(mkdirs, expected) { t.Errorf("Unexpected set of MkdirAll calls: %#v", mkdirs) } @@ -643,7 +644,7 @@ func TestPrepareOK(t *testing.T) { func TestPrepareErrorCreatingWorkingDir(t *testing.T) { rh := newFakeSTI(&FakeSTI{}) - rh.fs.(*test.FakeFileSystem).WorkingDirError = errors.New("WorkingDirError") + rh.fs.(*testfs.FakeFileSystem).WorkingDirError = errors.New("WorkingDirError") err := rh.Prepare(rh.config) if err == nil || err.Error() != "WorkingDirError" { t.Errorf("An error was expected for WorkingDir, but got different: %v", err) @@ -652,7 +653,7 @@ func TestPrepareErrorCreatingWorkingDir(t *testing.T) { func TestPrepareErrorMkdirAll(t *testing.T) { rh := newFakeSTI(&FakeSTI{}) - rh.fs.(*test.FakeFileSystem).MkdirAllError = errors.New("MkdirAllError") + rh.fs.(*testfs.FakeFileSystem).MkdirAllError = errors.New("MkdirAllError") err := rh.Prepare(rh.config) if err == nil || err.Error() != "MkdirAllError" { t.Errorf("An error was expected for MkdirAll, but got different: %v", err) @@ -843,7 +844,7 @@ func TestExecuteOK(t *testing.T) { if filepath.ToSlash(th.CreateTarDir) != "/working-dir/upload" { t.Errorf("Unexpected tar directory: %s", th.CreateTarDir) } - fh, ok := rh.fs.(*test.FakeFileSystem) + fh, ok := rh.fs.(*testfs.FakeFileSystem) if !ok { t.Fatalf("Unable to convert %v to FakeFilesystem", rh.fs) } @@ -911,10 +912,10 @@ func TestCleanup(t *testing.T) { preserve := []bool{false, true} for _, p := range preserve { rh.config.PreserveWorkingDir = p - rh.fs = &test.FakeFileSystem{} + rh.fs = &testfs.FakeFileSystem{} rh.garbage = build.NewDefaultCleaner(rh.fs, rh.docker) rh.garbage.Cleanup(rh.config) - removedDir := rh.fs.(*test.FakeFileSystem).RemoveDirName + removedDir := rh.fs.(*testfs.FakeFileSystem).RemoveDirName if p && removedDir != "" { t.Errorf("Expected working directory to be preserved, but it was removed.") } else if !p && removedDir == "" { diff --git a/pkg/build/strategies/sti/usage.go b/pkg/build/strategies/sti/usage.go index b23230bf7..f71b941a0 100644 --- a/pkg/build/strategies/sti/usage.go +++ b/pkg/build/strategies/sti/usage.go @@ -4,7 +4,7 @@ import ( "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/build" "github.com/openshift/source-to-image/pkg/docker" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) // UsageHandler handles a config to display usage @@ -23,7 +23,7 @@ type Usage struct { // NewUsage creates a new instance of the default Usage implementation func NewUsage(client docker.Client, config *api.Config) (*Usage, error) { - b, err := New(client, config, util.NewFileSystem(), build.Overrides{}) + b, err := New(client, config, fs.NewFileSystem(), build.Overrides{}) if err != nil { return nil, err } diff --git a/pkg/build/strategies/strategies.go b/pkg/build/strategies/strategies.go index da7570fe6..7a1849244 100644 --- a/pkg/build/strategies/strategies.go +++ b/pkg/build/strategies/strategies.go @@ -8,7 +8,7 @@ import ( "github.com/openshift/source-to-image/pkg/build/strategies/onbuild" "github.com/openshift/source-to-image/pkg/build/strategies/sti" "github.com/openshift/source-to-image/pkg/docker" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilstatus "github.com/openshift/source-to-image/pkg/util/status" ) @@ -24,7 +24,7 @@ func Strategy(client docker.Client, config *api.Config, overrides build.Override var builder build.Builder var buildInfo api.BuildInfo - fs := util.NewFileSystem() + fs := fs.NewFileSystem() startTime := time.Now() image, err := docker.GetBuilderImage(client, config) diff --git a/pkg/docker/docker.go b/pkg/docker/docker.go index c16a0f7eb..029758aa4 100644 --- a/pkg/docker/docker.go +++ b/pkg/docker/docker.go @@ -31,6 +31,7 @@ import ( s2ierr "github.com/openshift/source-to-image/pkg/errors" s2itar "github.com/openshift/source-to-image/pkg/tar" "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" "github.com/openshift/source-to-image/pkg/util/interrupt" ) @@ -128,8 +129,8 @@ type Docker interface { GetImageUser(name string) (string, error) GetImageEntrypoint(name string) ([]string, error) GetLabels(name string) (map[string]string, error) - UploadToContainer(fs util.FileSystem, srcPath, destPath, container string) error - UploadToContainerWithTarWriter(fs util.FileSystem, srcPath, destPath, container string, makeTarWriter func(io.Writer) s2itar.Writer) error + UploadToContainer(fs fs.FileSystem, srcPath, destPath, container string) error + UploadToContainerWithTarWriter(fs fs.FileSystem, srcPath, destPath, container string, makeTarWriter func(io.Writer) s2itar.Writer) error DownloadFromContainer(containerPath string, w io.Writer, container string) error Version() (dockertypes.Version, error) CheckReachable() error @@ -371,7 +372,7 @@ func (d *stiDocker) GetImageEntrypoint(name string) ([]string, error) { } // UploadToContainer uploads artifacts to the container. -func (d *stiDocker) UploadToContainer(fs util.FileSystem, src, dest, container string) error { +func (d *stiDocker) UploadToContainer(fs fs.FileSystem, src, dest, container string) error { makeWorldWritable := func(writer io.Writer) s2itar.Writer { return s2itar.ChmodAdapter{Writer: tar.NewWriter(writer), NewFileMode: 0666, NewExecFileMode: 0666, NewDirMode: 0777} } @@ -384,7 +385,7 @@ func (d *stiDocker) UploadToContainer(fs util.FileSystem, src, dest, container s // the destination (which has to be directory as well). // If the source is a single file, then the file copied into destination (which // has to be full path to a file inside the container). -func (d *stiDocker) UploadToContainerWithTarWriter(fs util.FileSystem, src, dest, container string, makeTarWriter func(io.Writer) s2itar.Writer) error { +func (d *stiDocker) UploadToContainerWithTarWriter(fs fs.FileSystem, src, dest, container string, makeTarWriter func(io.Writer) s2itar.Writer) error { path := filepath.Dir(dest) r, w := io.Pipe() go func() { diff --git a/pkg/docker/docker_test.go b/pkg/docker/docker_test.go index 48ad12d14..1a0e6b6d1 100644 --- a/pkg/docker/docker_test.go +++ b/pkg/docker/docker_test.go @@ -13,7 +13,7 @@ import ( "github.com/openshift/source-to-image/pkg/api" dockertest "github.com/openshift/source-to-image/pkg/docker/test" - "github.com/openshift/source-to-image/pkg/test" + testfs "github.com/openshift/source-to-image/pkg/test/fs" dockertypes "github.com/docker/engine-api/types" dockercontainer "github.com/docker/engine-api/types/container" @@ -148,7 +148,7 @@ func TestCopyToContainer(t *testing.T) { } dh := getDocker(fakeDocker) - err = dh.UploadToContainer(&test.FakeFileSystem{}, fileName, fileName, tst.containerID) + err = dh.UploadToContainer(&testfs.FakeFileSystem{}, fileName, fileName, tst.containerID) // the error we are inducing will prevent call into engine-api if len(tst.src) > 0 { if err != nil { diff --git a/pkg/docker/fake_docker.go b/pkg/docker/fake_docker.go index da7d2da3b..a52b67399 100644 --- a/pkg/docker/fake_docker.go +++ b/pkg/docker/fake_docker.go @@ -8,7 +8,7 @@ import ( dockertypes "github.com/docker/engine-api/types" "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/tar" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) // FakeDocker provides a fake docker interface @@ -134,12 +134,12 @@ func (f *FakeDocker) RunContainer(opts RunContainerOptions) error { } // UploadToContainer uploads artifacts to the container. -func (f *FakeDocker) UploadToContainer(fs util.FileSystem, srcPath, destPath, container string) error { +func (f *FakeDocker) UploadToContainer(fs fs.FileSystem, srcPath, destPath, container string) error { return nil } // UploadToContainerWithTarWriter uploads artifacts to the container. -func (f *FakeDocker) UploadToContainerWithTarWriter(fs util.FileSystem, srcPath, destPath, container string, makeTarWriter func(io.Writer) tar.Writer) error { +func (f *FakeDocker) UploadToContainerWithTarWriter(fs fs.FileSystem, srcPath, destPath, container string, makeTarWriter func(io.Writer) tar.Writer) error { return errors.New("not implemented") } diff --git a/pkg/ignore/ignore_test.go b/pkg/ignore/ignore_test.go index ca346b8bf..290dcc800 100644 --- a/pkg/ignore/ignore_test.go +++ b/pkg/ignore/ignore_test.go @@ -7,12 +7,12 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/api" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) func baseTest(t *testing.T, patterns []string, filesToDel []string, filesToKeep []string) { // create working dir - workingDir, werr := util.NewFileSystem().CreateWorkingDirectory() + workingDir, werr := fs.NewFileSystem().CreateWorkingDirectory() if werr != nil { t.Errorf("problem allocating working dir: %v", werr) } else { diff --git a/pkg/scm/file/download.go b/pkg/scm/file/download.go index e494ba365..2cfb69c6a 100644 --- a/pkg/scm/file/download.go +++ b/pkg/scm/file/download.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/openshift/source-to-image/pkg/api" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) @@ -14,7 +14,7 @@ var glog = utilglog.StderrLog // File represents a simplest possible Downloader implementation where the // sources are just copied from local directory. type File struct { - util.FileSystem + fs.FileSystem } // Download copies sources from a local directory into the working directory diff --git a/pkg/scm/file/download_test.go b/pkg/scm/file/download_test.go index 4bd585570..0c06eedf7 100644 --- a/pkg/scm/file/download_test.go +++ b/pkg/scm/file/download_test.go @@ -5,11 +5,11 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/api" - "github.com/openshift/source-to-image/pkg/test" + testfs "github.com/openshift/source-to-image/pkg/test/fs" ) func TestDownload(t *testing.T) { - fs := &test.FakeFileSystem{} + fs := &testfs.FakeFileSystem{} f := &File{fs} config := &api.Config{ @@ -28,7 +28,7 @@ func TestDownload(t *testing.T) { } func TestDownloadWithContext(t *testing.T) { - fs := &test.FakeFileSystem{} + fs := &testfs.FakeFileSystem{} f := &File{fs} config := &api.Config{ diff --git a/pkg/scm/git/clone.go b/pkg/scm/git/clone.go index 1528b9a4f..4dd7a054f 100644 --- a/pkg/scm/git/clone.go +++ b/pkg/scm/git/clone.go @@ -8,13 +8,14 @@ import ( "strings" "github.com/openshift/source-to-image/pkg/api" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/cygpath" + "github.com/openshift/source-to-image/pkg/util/fs" ) // Clone knows how to clone a Git repository. type Clone struct { Git - util.FileSystem + fs.FileSystem } // Download downloads the application source code from the Git repository @@ -40,9 +41,9 @@ func (c *Clone) Download(config *api.Config) (*api.SourceInfo, error) { if strings.HasPrefix(config.Source, "file://") { s := strings.TrimPrefix(config.Source, "file://") - if util.UsingCygwinGit { + if cygpath.UsingCygwinGit { var err error - s, err = util.ToSlashCygwin(s) + s, err = cygpath.ToSlashCygwin(s) if err != nil { glog.V(0).Infof("error: Cygwin path conversion failed: %v", err) return nil, err diff --git a/pkg/scm/git/clone_test.go b/pkg/scm/git/clone_test.go index 350b4ce8f..4de212489 100644 --- a/pkg/scm/git/clone_test.go +++ b/pkg/scm/git/clone_test.go @@ -6,13 +6,14 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/api" - "github.com/openshift/source-to-image/pkg/test" + testcmd "github.com/openshift/source-to-image/pkg/test/cmd" + testfs "github.com/openshift/source-to-image/pkg/test/fs" ) func TestCloneWithContext(t *testing.T) { - fs := &test.FakeFileSystem{} + fs := &testfs.FakeFileSystem{} gh := New(fs).(*stiGit) - cr := &test.FakeCmdRunner{} + cr := &testcmd.FakeCmdRunner{} gh.CommandRunner = cr c := &Clone{gh, fs} diff --git a/pkg/scm/git/git.go b/pkg/scm/git/git.go index 30c9dd477..0a213fda7 100644 --- a/pkg/scm/git/git.go +++ b/pkg/scm/git/git.go @@ -17,7 +17,9 @@ import ( "github.com/openshift/source-to-image/pkg/api" s2ierr "github.com/openshift/source-to-image/pkg/errors" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/cmd" + "github.com/openshift/source-to-image/pkg/util/cygpath" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) @@ -37,16 +39,16 @@ type Git interface { } // New returns a new instance of the default implementation of the Git interface -func New(fs util.FileSystem) Git { +func New(fs fs.FileSystem) Git { return &stiGit{ FileSystem: fs, - CommandRunner: util.NewCommandRunner(), + CommandRunner: cmd.NewCommandRunner(), } } type stiGit struct { - util.FileSystem - util.CommandRunner + fs.FileSystem + cmd.CommandRunner } // URLMods encapsulates potential changes to similarly named fields in the URL struct defined in golang @@ -229,7 +231,7 @@ func makePathAbsolute(source string) string { // expect git clone spec syntax; it also provides details if the file:// // proto was explicitly specified, if we should use OS copy vs. the git // binary, and if a frag/ref has a bad format -func ParseFile(fs util.FileSystem, source string) (*FileProtoDetails, *URLMods, error) { +func ParseFile(fs fs.FileSystem, source string) (*FileProtoDetails, *URLMods, error) { // Checking to see if the user included a "file://" in the call protoSpecified := false if strings.HasPrefix(source, "file://") && len(source) > 7 { @@ -378,7 +380,7 @@ func ParseSSH(source string) (*URLMods, error) { // it the gitdir value, which is supposed to indicate the location of the // corresponding .git /directory/. Note: the gitdir value should point directly // to the corresponding .git directory even in the case of nested submodules. -func followGitSubmodule(fs util.FileSystem, gitPath string) (string, error) { +func followGitSubmodule(fs fs.FileSystem, gitPath string) (string, error) { f, err := os.Open(gitPath) if err != nil { return "", err @@ -412,7 +414,7 @@ func followGitSubmodule(fs util.FileSystem, gitPath string) (string, error) { // isValidGitRepository checks to see if there is a git repository in the // directory and if the repository is valid -- i.e. it has remotes or commits -func isValidGitRepository(fs util.FileSystem, dir string) (bool, error) { +func isValidGitRepository(fs fs.FileSystem, dir string) (bool, error) { gitPath := filepath.Join(strings.TrimPrefix(dir, "file://"), ".git") fi, err := fs.Stat(gitPath) @@ -476,9 +478,9 @@ func hasGitBinary() bool { // Clone clones a git repository to a specific target directory func (h *stiGit) Clone(source, target string, c api.CloneConfig) error { - if util.UsingCygwinGit { + if cygpath.UsingCygwinGit { var err error - target, err = util.ToSlashCygwin(target) + target, err = cygpath.ToSlashCygwin(target) if err != nil { return err } @@ -497,7 +499,7 @@ func (h *stiGit) Clone(source, target string, c api.CloneConfig) error { cloneArgs := append([]string{"clone"}, cloneConfigToArgs(c)...) cloneArgs = append(cloneArgs, []string{source, target}...) errReader, errWriter, _ := os.Pipe() - opts := util.CommandOpts{Stderr: errWriter} + opts := cmd.CommandOpts{Stderr: errWriter} err := h.RunWithOptions(opts, "git", cloneArgs...) errWriter.Close() if err != nil { @@ -513,7 +515,7 @@ func (h *stiGit) Clone(source, target string, c api.CloneConfig) error { // Checkout checks out a specific branch reference of a given git repository func (h *stiGit) Checkout(repo, ref string) error { - opts := util.CommandOpts{ + opts := cmd.CommandOpts{ Stdout: os.Stdout, Stderr: os.Stderr, Dir: repo, @@ -526,7 +528,7 @@ func (h *stiGit) Checkout(repo, ref string) error { // SubmoduleInit initializes/clones submodules func (h *stiGit) SubmoduleInit(repo string) error { - opts := util.CommandOpts{ + opts := cmd.CommandOpts{ Stdout: os.Stdout, Stderr: os.Stderr, Dir: repo, @@ -545,7 +547,7 @@ func (h *stiGit) SubmoduleUpdate(repo string, init, recursive bool) error { updateArgs = append(updateArgs, "--recursive") } - opts := util.CommandOpts{ + opts := cmd.CommandOpts{ Stdout: os.Stdout, Stderr: os.Stderr, Dir: repo, @@ -562,7 +564,7 @@ func (h *stiGit) LsTree(repo, ref string, recursive bool) ([]os.FileInfo, error) args = append(args, "-r") } - opts := util.CommandOpts{ + opts := cmd.CommandOpts{ Dir: repo, } @@ -586,7 +588,7 @@ func (h *stiGit) LsTree(repo, ref string, recursive bool) ([]os.FileInfo, error) submodules = append(submodules, filepath.Join(repo, path)) continue } - rv = append(rv, &util.FileInfo{FileMode: os.FileMode(mode), FileName: path}) + rv = append(rv, &fs.FileInfo{FileMode: os.FileMode(mode), FileName: path}) } err = scanner.Err() if err != nil { diff --git a/pkg/scm/git/git_munge_test.go b/pkg/scm/git/git_munge_test.go index 1d719d950..c35975c54 100644 --- a/pkg/scm/git/git_munge_test.go +++ b/pkg/scm/git/git_munge_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) // NB: MungeNoProtocolURL is only called by OpenShift, running on a Linux @@ -103,7 +103,7 @@ func TestMungeNoProtocolURL(t *testing.T) { }, } - gh := New(util.NewFileSystem()) + gh := New(fs.NewFileSystem()) for scenario, test := range tests { uri, err := url.Parse(scenario) diff --git a/pkg/scm/git/git_test.go b/pkg/scm/git/git_test.go index 95c9eca8d..b1d6013e1 100644 --- a/pkg/scm/git/git_test.go +++ b/pkg/scm/git/git_test.go @@ -10,11 +10,13 @@ import ( "github.com/openshift/source-to-image/pkg/api" s2ierr "github.com/openshift/source-to-image/pkg/errors" "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + testcmd "github.com/openshift/source-to-image/pkg/test/cmd" + testfs "github.com/openshift/source-to-image/pkg/test/fs" + "github.com/openshift/source-to-image/pkg/util/fs" ) func TestIsValidGitRepository(t *testing.T) { - fs := util.NewFileSystem() + fs := fs.NewFileSystem() d := test.CreateLocalGitDirectory(t) defer os.RemoveAll(d) @@ -103,7 +105,7 @@ func TestValidCloneSpec(t *testing.T) { "http://github.com/user/repo#%%%%", } - gh := New(util.NewFileSystem()) + gh := New(fs.NewFileSystem()) for _, scenario := range valid { result, _ := gh.ValidCloneSpec(scenario) @@ -138,7 +140,7 @@ func TestValidCloneSpecRemoteOnly(t *testing.T) { "/home/user/code/repo.git", } - gh := New(util.NewFileSystem()) + gh := New(fs.NewFileSystem()) for _, scenario := range valid { result := gh.ValidCloneSpecRemoteOnly(scenario) @@ -154,9 +156,9 @@ func TestValidCloneSpecRemoteOnly(t *testing.T) { } } -func getGit() (*stiGit, *test.FakeCmdRunner) { - gh := New(&test.FakeFileSystem{}).(*stiGit) - cr := &test.FakeCmdRunner{} +func getGit() (*stiGit, *testcmd.FakeCmdRunner) { + gh := New(&testfs.FakeFileSystem{}).(*stiGit) + cr := &testcmd.FakeCmdRunner{} gh.CommandRunner = cr return gh, cr diff --git a/pkg/scm/scm.go b/pkg/scm/scm.go index e5b54a59c..fe91b92a0 100644 --- a/pkg/scm/scm.go +++ b/pkg/scm/scm.go @@ -3,21 +3,20 @@ package scm import ( "fmt" - s2ierr "github.com/openshift/source-to-image/pkg/errors" - utilglog "github.com/openshift/source-to-image/pkg/util/glog" - "github.com/openshift/source-to-image/pkg/build" + s2ierr "github.com/openshift/source-to-image/pkg/errors" "github.com/openshift/source-to-image/pkg/scm/empty" "github.com/openshift/source-to-image/pkg/scm/file" "github.com/openshift/source-to-image/pkg/scm/git" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" + utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) var glog = utilglog.StderrLog // DownloaderForSource determines what SCM plugin should be used for downloading // the sources from the repository. -func DownloaderForSource(fs util.FileSystem, s string, forceCopy bool) (build.Downloader, string, error) { +func DownloaderForSource(fs fs.FileSystem, s string, forceCopy bool) (build.Downloader, string, error) { glog.V(4).Infof("DownloadForSource %s", s) if len(s) == 0 { diff --git a/pkg/scm/scm_test.go b/pkg/scm/scm_test.go index 6cbd3c1d5..07a47572e 100644 --- a/pkg/scm/scm_test.go +++ b/pkg/scm/scm_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) func TestDownloaderForSource(t *testing.T) { @@ -39,7 +39,7 @@ func TestDownloaderForSource(t *testing.T) { } for s, expected := range tc { - r, filePathUpdate, err := DownloaderForSource(util.NewFileSystem(), s, false) + r, filePathUpdate, err := DownloaderForSource(fs.NewFileSystem(), s, false) if err != nil { if expected != "error" { t.Errorf("Unexpected error %q for %q, expected %q", err, s, expected) @@ -64,7 +64,7 @@ func TestDownloaderForSourceOnRelativeGit(t *testing.T) { gitLocalDir := test.CreateLocalGitDirectory(t) defer os.RemoveAll(gitLocalDir) os.Chdir(gitLocalDir) - r, s, err := DownloaderForSource(util.NewFileSystem(), ".", false) + r, s, err := DownloaderForSource(fs.NewFileSystem(), ".", false) if err != nil { t.Errorf("Unexpected error %q for %q, expected %q", err, ".", "git.Clone") } @@ -80,7 +80,7 @@ func TestDownloaderForceCopy(t *testing.T) { gitLocalDir := test.CreateLocalGitDirectory(t) defer os.RemoveAll(gitLocalDir) os.Chdir(gitLocalDir) - r, s, err := DownloaderForSource(util.NewFileSystem(), ".", true) + r, s, err := DownloaderForSource(fs.NewFileSystem(), ".", true) if err != nil { t.Errorf("Unexpected error %q for %q, expected %q", err, ".", "*file.File") } diff --git a/pkg/scripts/install.go b/pkg/scripts/install.go index c5c959f41..adfda7e12 100755 --- a/pkg/scripts/install.go +++ b/pkg/scripts/install.go @@ -9,7 +9,7 @@ import ( "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/docker" s2ierr "github.com/openshift/source-to-image/pkg/errors" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) // Installer interface is responsible for installing scripts needed to run the @@ -32,7 +32,7 @@ type URLScriptHandler struct { URL string DestinationDir string download Downloader - fs util.FileSystem + fs fs.FileSystem name string } @@ -104,7 +104,7 @@ func (s *URLScriptHandler) Install(r *api.InstallResult) error { // source code directory. type SourceScriptHandler struct { DestinationDir string - fs util.FileSystem + fs fs.FileSystem } // Get verifies if the script is present in the source directory and get the @@ -170,7 +170,7 @@ type DefaultScriptSourceManager struct { docker docker.Docker dockerAuth api.AuthConfig sources []ScriptHandler - fs util.FileSystem + fs fs.FileSystem } // Add registers a new script source handler. @@ -182,7 +182,7 @@ func (m *DefaultScriptSourceManager) Add(s ScriptHandler) { } // NewInstaller returns a new instance of the default Installer implementation -func NewInstaller(image string, scriptsURL string, proxyConfig *api.ProxyConfig, docker docker.Docker, auth api.AuthConfig, fs util.FileSystem) Installer { +func NewInstaller(image string, scriptsURL string, proxyConfig *api.ProxyConfig, docker docker.Docker, auth api.AuthConfig, fs fs.FileSystem) Installer { m := DefaultScriptSourceManager{ Image: image, ScriptsURL: scriptsURL, diff --git a/pkg/scripts/install_test.go b/pkg/scripts/install_test.go index 6e1f91a84..83988f915 100644 --- a/pkg/scripts/install_test.go +++ b/pkg/scripts/install_test.go @@ -10,13 +10,14 @@ import ( "github.com/openshift/source-to-image/pkg/api" dockerpkg "github.com/openshift/source-to-image/pkg/docker" "github.com/openshift/source-to-image/pkg/test" - "github.com/openshift/source-to-image/pkg/util" + testfs "github.com/openshift/source-to-image/pkg/test/fs" + "github.com/openshift/source-to-image/pkg/util/fs" ) type fakeScriptManagerConfig struct { download Downloader docker dockerpkg.Docker - fs util.FileSystem + fs fs.FileSystem url string } @@ -24,7 +25,7 @@ func newFakeConfig() *fakeScriptManagerConfig { return &fakeScriptManagerConfig{ docker: &dockerpkg.FakeDocker{}, download: &test.FakeDownloader{}, - fs: &test.FakeFileSystem{}, + fs: &testfs.FakeFileSystem{}, url: "http://the.scripts.url/s2i/bin", } } @@ -151,7 +152,7 @@ func TestInstallRequiredFromSource(t *testing.T) { // There is no other script source than the source code config.url = "" deprecatedSourceScripts := strings.Replace(api.SourceScripts, ".s2i", ".sti", -1) - config.fs.(*test.FakeFileSystem).ExistsResult = map[string]bool{ + config.fs.(*testfs.FakeFileSystem).ExistsResult = map[string]bool{ filepath.Join("/workdir", api.SourceScripts, api.Assemble): true, filepath.Join("/workdir", deprecatedSourceScripts, api.Run): true, } @@ -179,7 +180,7 @@ func TestInstallRequiredFromSource(t *testing.T) { t.Errorf("expected %q has result URL %s, got %#v", s, filepath.FromSlash(sourcesRootAbbrev+"/.s2i/bin/"+s), result) } chmodCalled := false - fs := config.fs.(*test.FakeFileSystem) + fs := config.fs.(*testfs.FakeFileSystem) for _, f := range fs.ChmodFile { if filepath.ToSlash(f) == "/workdir/upload/scripts/"+s { chmodCalled = true @@ -203,7 +204,7 @@ func TestInstallRequiredOrder(t *testing.T) { config.url + "/" + api.Assemble: fmt.Errorf("not available"), config.url + "/" + api.SaveArtifacts: fmt.Errorf("not available"), } - config.fs.(*test.FakeFileSystem).ExistsResult = map[string]bool{ + config.fs.(*testfs.FakeFileSystem).ExistsResult = map[string]bool{ filepath.Join("/workdir", api.SourceScripts, api.Assemble): true, filepath.Join("/workdir", api.SourceScripts, api.Run): false, filepath.Join("/workdir", api.SourceScripts, api.SaveArtifacts): false, @@ -265,7 +266,7 @@ func TestInstallRequiredFromInvalidURL(t *testing.T) { func TestNewInstaller(t *testing.T) { docker := &dockerpkg.FakeDocker{DefaultURLResult: "image://docker"} - inst := NewInstaller("test-image", "http://foo.bar", nil, docker, api.AuthConfig{}, &test.FakeFileSystem{}) + inst := NewInstaller("test-image", "http://foo.bar", nil, docker, api.AuthConfig{}, &testfs.FakeFileSystem{}) sources := inst.(*DefaultScriptSourceManager).sources firstHandler, ok := sources[0].(*URLScriptHandler) if !ok { diff --git a/pkg/tar/tar.go b/pkg/tar/tar.go index 5732182c7..4c2bf4517 100644 --- a/pkg/tar/tar.go +++ b/pkg/tar/tar.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/openshift/source-to-image/pkg/util/fs" utilglog "github.com/openshift/source-to-image/pkg/util/glog" s2ierr "github.com/openshift/source-to-image/pkg/errors" @@ -132,7 +133,7 @@ func (a RenameAdapter) WriteHeader(hdr *tar.Header) error { } // New creates a new Tar -func New(fs util.FileSystem) Tar { +func New(fs fs.FileSystem) Tar { return &stiTar{ FileSystem: fs, exclude: DefaultExclusionPattern, @@ -142,7 +143,7 @@ func New(fs util.FileSystem) Tar { // stiTar is an implementation of the Tar interface type stiTar struct { - util.FileSystem + fs.FileSystem timeout time.Duration exclude *regexp.Regexp includeDirInPath bool diff --git a/pkg/tar/tar_test.go b/pkg/tar/tar_test.go index 09314830f..ab85e4e43 100644 --- a/pkg/tar/tar_test.go +++ b/pkg/tar/tar_test.go @@ -13,7 +13,7 @@ import ( "time" s2ierr "github.com/openshift/source-to-image/pkg/errors" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" ) type dirDesc struct { @@ -189,7 +189,7 @@ func TestCreateTarStreamIncludeParentDir(t *testing.T) { if err = createTestFiles(tempDir, testDirs, testFiles, []linkDesc{}); err != nil { t.Fatalf("Cannot create test files: %v", err) } - th := New(util.NewFileSystem()) + th := New(fs.NewFileSystem()) tarFile, err := ioutil.TempFile("", "testtarout") if err != nil { t.Fatalf("Unable to create temporary file %v", err) @@ -210,7 +210,7 @@ func TestCreateTarStreamIncludeParentDir(t *testing.T) { } func TestCreateTar(t *testing.T) { - th := New(util.NewFileSystem()) + th := New(fs.NewFileSystem()) tempDir, err := ioutil.TempDir("", "testtar") defer os.RemoveAll(tempDir) if err != nil { @@ -249,7 +249,7 @@ func TestCreateTar(t *testing.T) { } func TestCreateTarIncludeDotGit(t *testing.T) { - th := New(util.NewFileSystem()) + th := New(fs.NewFileSystem()) th.SetExclusionPattern(regexp.MustCompile("test3.txt")) tempDir, err := ioutil.TempDir("", "testtar") defer os.RemoveAll(tempDir) @@ -289,7 +289,7 @@ func TestCreateTarIncludeDotGit(t *testing.T) { } func TestCreateTarEmptyRegexp(t *testing.T) { - th := New(util.NewFileSystem()) + th := New(fs.NewFileSystem()) th.SetExclusionPattern(regexp.MustCompile("")) tempDir, err := ioutil.TempDir("", "testtar") defer os.RemoveAll(tempDir) @@ -461,7 +461,7 @@ func TestExtractTarStream(t *testing.T) { t.Fatalf("Cannot create temp directory: %v", err) } defer os.RemoveAll(destDir) - th := New(util.NewFileSystem()) + th := New(fs.NewFileSystem()) go func() { writer.CloseWithError(createTestTar(testFiles, writer)) @@ -477,7 +477,7 @@ func TestExtractTarStreamTimeout(t *testing.T) { t.Fatalf("Cannot create temp directory: %v", err) } defer os.RemoveAll(destDir) - th := New(util.NewFileSystem()) + th := New(fs.NewFileSystem()) th.(*stiTar).timeout = 10 * time.Millisecond time.AfterFunc(20*time.Millisecond, func() { writer.Close() }) err = th.ExtractTarStream(destDir, reader) diff --git a/pkg/test/cmd.go b/pkg/test/cmd/cmd.go similarity index 63% rename from pkg/test/cmd.go rename to pkg/test/cmd/cmd.go index be84bc595..f8bca7c45 100644 --- a/pkg/test/cmd.go +++ b/pkg/test/cmd/cmd.go @@ -1,23 +1,23 @@ -package test +package cmd import ( "bytes" "io" "io/ioutil" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/cmd" ) // FakeCmdRunner provider the fake command runner type FakeCmdRunner struct { Name string Args []string - Opts util.CommandOpts + Opts cmd.CommandOpts Err error } // RunWithOptions runs the command runner with extra options -func (f *FakeCmdRunner) RunWithOptions(opts util.CommandOpts, name string, args ...string) error { +func (f *FakeCmdRunner) RunWithOptions(opts cmd.CommandOpts, name string, args ...string) error { f.Name = name f.Args = args f.Opts = opts @@ -26,12 +26,12 @@ func (f *FakeCmdRunner) RunWithOptions(opts util.CommandOpts, name string, args // Run runs the fake command runner func (f *FakeCmdRunner) Run(name string, args ...string) error { - return f.RunWithOptions(util.CommandOpts{}, name, args...) + return f.RunWithOptions(cmd.CommandOpts{}, name, args...) } // StartWithStdoutPipe executes a command returning a ReadCloser connected to // the command's stdout. -func (f *FakeCmdRunner) StartWithStdoutPipe(opts util.CommandOpts, name string, arg ...string) (io.ReadCloser, error) { +func (f *FakeCmdRunner) StartWithStdoutPipe(opts cmd.CommandOpts, name string, arg ...string) (io.ReadCloser, error) { return ioutil.NopCloser(&bytes.Buffer{}), f.Err } diff --git a/pkg/test/fs.go b/pkg/test/fs/fs.go similarity index 99% rename from pkg/test/fs.go rename to pkg/test/fs/fs.go index 67e627982..f3102bdcc 100644 --- a/pkg/test/fs.go +++ b/pkg/test/fs/fs.go @@ -1,4 +1,4 @@ -package test +package fs import ( "bytes" diff --git a/pkg/test/git.go b/pkg/test/git.go index ef5133355..2bde6b8fc 100644 --- a/pkg/test/git.go +++ b/pkg/test/git.go @@ -8,7 +8,8 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/api" - "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/cmd" + "github.com/openshift/source-to-image/pkg/util/cygpath" ) // FakeGit provides a fake Git @@ -97,18 +98,18 @@ func (f *FakeGit) GetInfo(repo string) *api.SourceInfo { // CreateLocalGitDirectory creates a git directory with a commit func CreateLocalGitDirectory(t *testing.T) string { - cr := util.NewCommandRunner() + cr := cmd.NewCommandRunner() dir := CreateEmptyLocalGitDirectory(t) f, err := os.Create(filepath.Join(dir, "testfile")) if err != nil { t.Fatal(err) } f.Close() - err = cr.RunWithOptions(util.CommandOpts{Dir: dir}, "git", "add", ".") + err = cr.RunWithOptions(cmd.CommandOpts{Dir: dir}, "git", "add", ".") if err != nil { t.Fatal(err) } - err = cr.RunWithOptions(util.CommandOpts{Dir: dir, EnvAppend: []string{"GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@test", "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@test"}}, "git", "commit", "-m", "testcommit") + err = cr.RunWithOptions(cmd.CommandOpts{Dir: dir, EnvAppend: []string{"GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@test", "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@test"}}, "git", "commit", "-m", "testcommit") if err != nil { t.Fatal(err) } @@ -118,13 +119,13 @@ func CreateLocalGitDirectory(t *testing.T) string { // CreateEmptyLocalGitDirectory creates a git directory with no checkin yet func CreateEmptyLocalGitDirectory(t *testing.T) string { - cr := util.NewCommandRunner() + cr := cmd.NewCommandRunner() dir, err := ioutil.TempDir(os.TempDir(), "gitdir-s2i-test") if err != nil { t.Fatal(err) } - err = cr.RunWithOptions(util.CommandOpts{Dir: dir}, "git", "init") + err = cr.RunWithOptions(cmd.CommandOpts{Dir: dir}, "git", "init") if err != nil { t.Fatal(err) } @@ -134,21 +135,21 @@ func CreateEmptyLocalGitDirectory(t *testing.T) string { // CreateLocalGitDirectoryWithSubmodule creates a git directory with a submodule func CreateLocalGitDirectoryWithSubmodule(t *testing.T) string { - cr := util.NewCommandRunner() + cr := cmd.NewCommandRunner() submodule := CreateLocalGitDirectory(t) defer os.RemoveAll(submodule) - if util.UsingCygwinGit { + if cygpath.UsingCygwinGit { var err error - submodule, err = util.ToSlashCygwin(submodule) + submodule, err = cygpath.ToSlashCygwin(submodule) if err != nil { t.Fatal(err) } } dir := CreateEmptyLocalGitDirectory(t) - err := cr.RunWithOptions(util.CommandOpts{Dir: dir}, "git", "submodule", "add", submodule, "submodule") + err := cr.RunWithOptions(cmd.CommandOpts{Dir: dir}, "git", "submodule", "add", submodule, "submodule") if err != nil { t.Fatal(err) } diff --git a/pkg/util/cmd.go b/pkg/util/cmd/cmd.go similarity index 99% rename from pkg/util/cmd.go rename to pkg/util/cmd/cmd.go index 53d471be7..ddd74084f 100644 --- a/pkg/util/cmd.go +++ b/pkg/util/cmd/cmd.go @@ -1,4 +1,4 @@ -package util +package cmd import ( "io" diff --git a/pkg/util/cygpath.go b/pkg/util/cygpath/cygpath.go similarity index 98% rename from pkg/util/cygpath.go rename to pkg/util/cygpath/cygpath.go index f65b4524d..83e16ff63 100644 --- a/pkg/util/cygpath.go +++ b/pkg/util/cygpath/cygpath.go @@ -1,4 +1,4 @@ -package util +package cygpath import ( "os/exec" diff --git a/pkg/util/fs.go b/pkg/util/fs/fs.go similarity index 98% rename from pkg/util/fs.go rename to pkg/util/fs/fs.go index 300720d22..9b6f11656 100644 --- a/pkg/util/fs.go +++ b/pkg/util/fs/fs.go @@ -1,4 +1,4 @@ -package util +package fs import ( "fmt" @@ -12,6 +12,7 @@ import ( "sync" "time" + "github.com/openshift/source-to-image/pkg/util/cmd" utilglog "github.com/openshift/source-to-image/pkg/util/glog" s2ierr "github.com/openshift/source-to-image/pkg/errors" @@ -43,13 +44,13 @@ type FileSystem interface { // implementation func NewFileSystem() FileSystem { return &fs{ - runner: NewCommandRunner(), + runner: cmd.NewCommandRunner(), fileModes: make(map[string]os.FileMode), } } type fs struct { - runner CommandRunner + runner cmd.CommandRunner // on Windows, fileModes is used to track the UNIX file mode of every file we // work with; m is used to synchronize access to fileModes. diff --git a/pkg/util/injection.go b/pkg/util/injection.go index 1ef2710ad..423f0d44e 100644 --- a/pkg/util/injection.go +++ b/pkg/util/injection.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/openshift/source-to-image/pkg/api" + "github.com/openshift/source-to-image/pkg/util/fs" ) // FixInjectionsWithRelativePath fixes the injections that does not specify the @@ -38,7 +39,7 @@ func FixInjectionsWithRelativePath(workdir string, injections api.VolumeList) ap // ExpandInjectedFiles returns a flat list of all files that are injected into a // container. All files from nested directories are returned in the list. -func ExpandInjectedFiles(fs FileSystem, injections api.VolumeList) ([]string, error) { +func ExpandInjectedFiles(fs fs.FileSystem, injections api.VolumeList) ([]string, error) { result := []string{} for _, s := range injections { if _, err := os.Stat(s.Source); err != nil { diff --git a/pkg/util/injection_test.go b/pkg/util/injection_test.go index 2df771073..e1784bcc6 100644 --- a/pkg/util/injection_test.go +++ b/pkg/util/injection_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/openshift/source-to-image/pkg/api" + "github.com/openshift/source-to-image/pkg/util/fs" ) func TestCreateInjectedFilesRemovalScript(t *testing.T) { @@ -47,7 +48,7 @@ func TestExpandInjectedFiles(t *testing.T) { list := api.VolumeList{{Source: tmp, Destination: "/foo"}} f1, _ := ioutil.TempFile(tmp, "foo") f2, _ := ioutil.TempFile(tmpNested, "bar") - files, err := ExpandInjectedFiles(NewFileSystem(), list) + files, err := ExpandInjectedFiles(fs.NewFileSystem(), list) if err != nil { t.Errorf("Unexpected error: %v", err) } diff --git a/pkg/util/util.go b/pkg/util/util.go index c1a2678dd..bd3dd64c2 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -2,8 +2,12 @@ package util import ( "github.com/docker/engine-api/types/container" + + utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) +var glog = utilglog.StderrLog + // SafeForLoggingContainerConfig returns a copy of the container.Config object // with sensitive information (proxy environment variables containing credentials) // redacted. diff --git a/test/integration/integration_test.go b/test/integration/integration_test.go index 500542eaa..1a43be25c 100644 --- a/test/integration/integration_test.go +++ b/test/integration/integration_test.go @@ -26,6 +26,7 @@ import ( dockerpkg "github.com/openshift/source-to-image/pkg/docker" "github.com/openshift/source-to-image/pkg/tar" "github.com/openshift/source-to-image/pkg/util" + "github.com/openshift/source-to-image/pkg/util/fs" "golang.org/x/net/context" ) @@ -429,7 +430,7 @@ func (i *integrationTest) exerciseInjectionBuild(tag, imageName string, injectio i.fileExists(containerID, "/sti-fake/relative-secret-delivered") // Make sure the injected file does not exists in resulting image - files, err := util.ExpandInjectedFiles(util.NewFileSystem(), injectionList) + files, err := util.ExpandInjectedFiles(fs.NewFileSystem(), injectionList) if err != nil { t.Errorf("Unexpected error: %v", err) }