diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 3750df1cb365..a6e9efa18284 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -594,42 +594,42 @@ { "ImportPath": "github.com/openshift/source-to-image/pkg/build", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/git", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/tar", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/api", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/scripts", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/errors", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/docker", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/openshift/source-to-image/pkg/util", "Comment": "v0.2", - "Rev": "ad5adc054311686baf316cd8bf91c4d42ae1bd4e" + "Rev": "c0c154efcba27ea5693c428bfe28560c220b4850" }, { "ImportPath": "github.com/pkg/profile", diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/scripts.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/scripts.go index e573c54de26b..f53329cc9c06 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/scripts.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/scripts.go @@ -9,6 +9,11 @@ const ( SaveArtifacts = "save-artifacts" // Usage is the name of the script responsible for printing the builder image's short info. Usage = "usage" + + // Environment contains list of key value pairs that will be set during the + // STI build. Users can use this file to provide extra configuration + // depending on the builder image used. + Environment = "environment" ) const ( diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/types.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/types.go index b8b7319a3f9b..505c5c40ce91 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/types.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/api/types.go @@ -21,8 +21,8 @@ type Request struct { // Tag is a result image tag name. Tag string - // Clean describes whether to perform full build even if the build is eligible for incremental build. - Clean bool + // Incremental describes whether to try to perform incremental build. + Incremental bool // RemovePreviousImage describes if previous image should be removed after successful build. // This applies only to incremental builds. @@ -43,9 +43,6 @@ type Request struct { // ForcePull describes if the builder should pull the images from registry prior to building. ForcePull bool - // Incremental describes incremental status of current build - Incremental bool - // WorkingDir describes temporary directory used for downloading sources, scripts and tar operations. WorkingDir string diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/onbuild/onbuild.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/onbuild/onbuild.go index 7cff63c343f1..ea081ece94b1 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/onbuild/onbuild.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/onbuild/onbuild.go @@ -13,6 +13,7 @@ import ( "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/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" ) @@ -124,6 +125,12 @@ func (b *OnBuild) CreateDockerfile(request *api.Request) error { if err != nil { return err } + env, err := scripts.GetEnvironment(request) + if err != nil { + glog.V(1).Infof("Environment: %v", err) + } else { + buffer.WriteString(scripts.ConvertEnvironmentToDocker(env)) + } // If there is an assemble script present, run it as part of the build process // as the last thing. if b.hasAssembleScript(request) { diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti.go index 09f07d278afe..9bce147d4d13 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti.go @@ -51,6 +51,7 @@ type STI struct { optionalScripts []string externalScripts map[string]bool installedScripts map[string]bool + incremental bool // Interfaces preparer build.Preparer @@ -112,14 +113,14 @@ func (b *STI) Build(request *api.Request) (*api.Result, error) { return nil, err } - if b.request.Incremental = b.artifacts.Exists(request); b.request.Incremental { + if b.incremental = b.artifacts.Exists(request); b.incremental { glog.V(1).Infof("Existing image for tag %s detected for incremental build", request.Tag) } else { glog.V(1).Infof("Clean build will be performed") } glog.V(2).Infof("Performing source build from %s", request.Source) - if request.Incremental { + if b.incremental { if err := b.artifacts.Save(request); err != nil { glog.Warningf("Error saving previous build artifacts: %v", err) glog.Warning("Clean build will be performed!") @@ -202,16 +203,23 @@ func (b *STI) PostExecute(containerID string, location string) error { previousImageID string ) - if b.request.Incremental && b.request.RemovePreviousImage { + if b.incremental && b.request.RemovePreviousImage { if previousImageID, err = b.docker.GetImageID(b.request.Tag); err != nil { glog.Errorf("Error retrieving previous image's metadata: %v", err) } } + env, err := scripts.GetEnvironment(b.request) + if err != nil { + glog.V(1).Infof("No .sti/environment provided (%v)", err) + } + + buildEnv := append(scripts.ConvertEnvironment(env), b.generateConfigEnv()...) + cmd := []string{} opts := docker.CommitContainerOptions{ Command: append(cmd, filepath.Join(location, api.Run)), - Env: b.generateConfigEnv(), + Env: buildEnv, ContainerID: containerID, Repository: b.request.Tag, } @@ -225,7 +233,7 @@ func (b *STI) PostExecute(containerID string, location string) error { b.result.ImageID = imageID glog.V(1).Infof("Tagged %s as %s", imageID, b.request.Tag) - if b.request.Incremental && b.request.RemovePreviousImage && previousImageID != "" { + if b.incremental && b.request.RemovePreviousImage && previousImageID != "" { glog.V(1).Infof("Removing previously-tagged image %s", previousImageID) if err = b.docker.RemoveImage(previousImageID); err != nil { glog.Errorf("Unable to remove previous image: %v", err) @@ -244,7 +252,7 @@ func (b *STI) PostExecute(containerID string, location string) error { // It checks if the previous image exists in the system and if so, then it // verifies that the save-artifacts script is present. func (b *STI) Exists(request *api.Request) bool { - if request.Clean { + if !request.Incremental { return false } @@ -295,6 +303,13 @@ func (b *STI) Save(request *api.Request) (err error) { func (b *STI) Execute(command string, request *api.Request) error { glog.V(2).Infof("Using image name %s", request.BaseImage) + env, err := scripts.GetEnvironment(request) + if err != nil { + glog.V(1).Infof("No .sti/environment provided (%v)", err) + } + + buildEnv := append(scripts.ConvertEnvironment(env), b.generateConfigEnv()...) + uploadDir := filepath.Join(request.WorkingDir, "upload") tarFileName, err := b.tar.CreateTarFile(request.WorkingDir, uploadDir) if err != nil { @@ -328,7 +343,7 @@ func (b *STI) Execute(command string, request *api.Request) error { ScriptsURL: request.ScriptsURL, Location: request.Location, Command: command, - Env: b.generateConfigEnv(), + Env: buildEnv, PostExec: b.postExecutor, } if !request.LayeredBuild { diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti_test.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti_test.go index 079dc1b54fd4..1694412dc970 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti_test.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/build/strategies/sti/sti_test.go @@ -81,7 +81,7 @@ func (f *FakeSTI) Prepare(*api.Request) error { func (f *FakeSTI) Exists(*api.Request) bool { f.ExistsCalled = true - return false + return true } func (f *FakeSTI) Request() *api.Request { @@ -132,7 +132,6 @@ func (f *FakeDockerBuild) Build(*api.Request) (*api.Result, error) { func TestBuild(t *testing.T) { incrementalTest := []bool{false, true} for _, incremental := range incrementalTest { - fh := &FakeSTI{ BuildRequest: &api.Request{Incremental: incremental}, BuildResult: &api.Result{}, @@ -261,6 +260,7 @@ func TestPostExecute(t *testing.T) { bh.request.Incremental = incremental if previousImageID != "" { bh.request.RemovePreviousImage = true + bh.incremental = incremental bh.docker.(*test.FakeDocker).GetImageIDResult = previousImageID } err := bh.PostExecute("test-container-id", "cmd1") @@ -291,8 +291,8 @@ func TestPostExecute(t *testing.T) { func TestExists(t *testing.T) { type incrementalTest struct { - // clean flag was passed - clean bool + // incremental flag was passed + incremental bool // previous image existence previousImage bool // script installed @@ -302,26 +302,26 @@ func TestExists(t *testing.T) { } tests := []incrementalTest{ - // 0-1: no clean, no image, no matter what with scripts - {false, false, false, false}, - {false, false, true, false}, - - // 2: no clean, previous image, no scripts - {false, true, false, false}, - // 3: no clean, previous image, scripts installed - {false, true, true, true}, - - // 4-7: clean build - should always return false no matter what other flags are + // 0-1: incremental, no image, no matter what with scripts {true, false, false, false}, {true, false, true, false}, + + // 2: incremental, previous image, no scripts {true, true, false, false}, - {true, true, true, false}, + // 3: incremental, previous image, scripts installed + {true, true, true, true}, + + // 4-7: no incremental build - should always return false no matter what other flags are + {false, false, false, false}, + {false, false, true, false}, + {false, true, false, false}, + {false, true, true, false}, } for i, ti := range tests { bh := testBuildHandler() bh.request.WorkingDir = "/working-dir" - bh.request.Clean = ti.clean + bh.request.Incremental = ti.incremental bh.installedScripts = map[string]bool{api.SaveArtifacts: ti.scriptInstalled} bh.docker.(*test.FakeDocker).PullResult = ti.previousImage @@ -330,7 +330,7 @@ func TestExists(t *testing.T) { t.Errorf("(%d) Unexpected incremental result: %v. Expected: %v", i, incremental, ti.expected) } - if !ti.clean && ti.previousImage && ti.scriptInstalled { + if ti.incremental && ti.previousImage && ti.scriptInstalled { if len(bh.fs.(*test.FakeFileSystem).ExistsFile) == 0 { continue } diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/config/config.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/config/config.go index bacbddc1c6ab..9e018325e35c 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/config/config.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/config/config.go @@ -45,7 +45,7 @@ func Save(req *api.Request, cmd *cobra.Command) { return } -// Restore loads the arguments from disk and prefill the Request +// Restore loads the arguments from disk and prefills the Request func Restore(req *api.Request, cmd *cobra.Command) { data, err := ioutil.ReadFile(DefaultConfigPath) if err != nil { diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/create.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/create.go index da734dadda4c..2c1440acc2d2 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/create.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/create.go @@ -14,8 +14,7 @@ type Bootstrap struct { ImageName string } -// NewCreate returns a new bootstrap for given image name and destination -// directory +// New returns a new bootstrap for given image name and destination directory func New(name, dst string) *Bootstrap { return &Bootstrap{ImageName: name, DestinationDir: dst} } diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/docker.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/docker.go index 45bde70d5d01..62fb132eabe8 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/docker.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/docker.go @@ -2,44 +2,18 @@ package templates const Dockerfile = ` # {{.ImageName}} -FROM centos:centos7 +FROM openshift/base-centos7 -# TODO: Install required packages: -# -# RUN yum install -y --enablerepo=centosplus epel-release +ENV STI_NODEJS_VERSION 0.10 +# TODO: Install required packages here: +# RUN yum install -y ... ; yum clean all -y -# Add STI usage script -ADD ./.sti/bin/usage /usage +USER default -# You can add any other required files/configurations here -# ADD conf /app-root/conf +# TODO (optional): Copy the builder files into /opt/openshift +# COPY .// /opt/openshift/ -# TODO: Define the URL from where the default STI script will be fetched: -# -# ENV STI_SCRIPTS_URL https://raw.githubusercontent.com//master/.sti/bin - -# Default destination of scripts and sources, this is where assemble will look for them -ENV STI_LOCATION /tmp - -# TODO: Create the application runtime user account and app-root directory: -RUN mkdir -p /app-root/src && \ - groupadd -r appuser -f -g 433 && \ - useradd -u 431 -r -g appuser -d /app-root -s /sbin/nologin -c "Application user" appuser && \ - chown -R appuser:appuser /app-root - -# TODO: Specify the application root folder (if other than root folder), application user -# and working directory. -# -ENV APP_ROOT . -ENV HOME /app-root -ENV PATH $HOME/bin:$PATH - -WORKDIR /app-root/src -USER appuser - -# TODO: Specify the default port your application is running on -EXPOSE 8080 - -CMD ["/usage"] +# TODO: Set the default port for applications built using this image +# EXPOSE 3000 ` diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/scripts.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/scripts.go index 1fe2bbed3de6..9cd1457bad78 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/scripts.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/scripts.go @@ -10,38 +10,25 @@ const AssembleScript = ` # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md # -# The $HOME variable points to the application root -HOME=${HOME-"/app-root"} - -# The application sources are mounted during the STI build into /tmp/src -APP_SRC_DIR="/tmp/src" - -# The directory where the sources should be installed -APP_RUNTIME_DIR="${HOME}/src" - -# Display assemble script usage -# if [ "$1" = "-h" ]; then # If the '{{.ImageName}}' assemble script is executed with '-h' flag, # print the usage. - exit 0 + exec /usr/local/sti/usage fi # Restore artifacts from the previous build (if exists). # if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then - echo "Restoring build artifacts" - mv /tmp/artifacts/* $HOME/. + echo "---> Restoring build artifacts" + mv /tmp/artifacts/* ./ fi echo "---> Installing application source" -mkdir -p ${APP_RUNTIME_DIR} -cp -Rf ${APP_SRC_DIR}/* ${APP_RUNTIME_DIR}/ +cp -Rf /tmp/src/* ./ -pushd "$APP_RUNTIME_DIR/${APP_ROOT}" >/dev/null echo "---> Building application from source" -# TODO: Add build steps for your application -popd >/dev/null +# TODO: Add build steps for your application, for example npm install or +# bundle install, etc... ` const RunScript = ` @@ -53,9 +40,7 @@ const RunScript = ` # For more informations see the documentation: # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md # -APP_ROOT_DIR="${HOME}/src/${APP_ROOT:-.}" -cd $APP_ROOT_DIR exec ` @@ -85,7 +70,5 @@ const SaveArtifactsScript = ` # For more informations see the documentation: # https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md # -pushd ${HOME} >/dev/null # tar cf - -popd >/dev/null ` diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/test.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/test.go index 7df84aa8a402..106ac8968197 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/test.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/create/templates/test.go @@ -155,5 +155,4 @@ build: test: docker build -t $(IMAGE_NAME)-candidate . IMAGE_NAME=$(IMAGE_NAME)-candidate test/run - ` diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/scripts/environment.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/scripts/environment.go new file mode 100644 index 000000000000..8fe9725f15b9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/scripts/environment.go @@ -0,0 +1,73 @@ +package scripts + +import ( + "bufio" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/golang/glog" + "github.com/openshift/source-to-image/pkg/api" +) + +// Environment represents a single environment variable definition +type Environment struct { + Name string + Value string +} + +// GetEnvironment gets the .sti/environment file located in the sources and +// parse it into []environment +func GetEnvironment(request *api.Request) ([]Environment, error) { + envPath := filepath.Join(request.WorkingDir, api.Source, ".sti", api.Environment) + if _, err := os.Stat(envPath); os.IsNotExist(err) { + return nil, errors.New("no evironment file found in application sources") + } + + f, err := os.Open(envPath) + if err != nil { + return nil, errors.New("unable to read .sti/environment file") + } + defer f.Close() + + result := []Environment{} + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + s := scanner.Text() + // Allow for comments in environment file + if strings.HasPrefix(s, "#") { + continue + } + parts := strings.SplitN(s, "=", 2) + if len(parts) != 2 { + continue + } + e := Environment{ + Name: strings.TrimSpace(parts[0]), + Value: strings.TrimSpace(parts[1]), + } + glog.V(1).Infof("Setting '%s' to '%s'", e.Name, e.Value) + result = append(result, e) + } + + return result, scanner.Err() +} + +// ConvertEnvironment converts the []Environment to "key=val" strings +func ConvertEnvironment(env []Environment) (result []string) { + for _, e := range env { + result = append(result, fmt.Sprintf("%s=%s", e.Name, e.Value)) + } + return +} + +// ConvertEnvironmentToDocker converts the []Environment into Dockerfile format +func ConvertEnvironmentToDocker(env []Environment) (result string) { + for _, e := range env { + result += fmt.Sprintf("ENV %s %s\n", e.Name, e.Value) + } + return +} diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/scripts/environment_test.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/scripts/environment_test.go new file mode 100644 index 000000000000..cf35741fc618 --- /dev/null +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/scripts/environment_test.go @@ -0,0 +1,16 @@ +package scripts + +import "testing" + +func TestConvertEnvironment(t *testing.T) { + env := []Environment{ + {"FOO", "BAR"}, + } + result := ConvertEnvironment(env) + if len(result) != 1 { + t.Errorf("Expected 1 item, got %d", len(result)) + } + if result[0] != "FOO=BAR" { + t.Errorf("Expected FOO=BAR, got %v", result) + } +} diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/docker.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/docker.go index 881955ac8738..f44970bd8752 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/docker.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/docker.go @@ -36,6 +36,8 @@ type FakeDocker struct { RemoveImageError error BuildImageOpts docker.BuildImageOptions BuildImageError error + PullResult bool + PullError error mutex sync.Mutex } @@ -105,7 +107,11 @@ func (f *FakeDocker) RemoveImage(name string) error { // PullImage pulls a fake docker image func (f *FakeDocker) PullImage(imageName string) (*dockerclient.Image, error) { - return nil, nil + if f.PullResult { + return &dockerclient.Image{}, nil + } + + return nil, f.PullError } // CheckAndPull pulls a fake docker image diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/download.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/download.go index 4b16c884221c..c9ef1fda84f7 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/download.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/download.go @@ -7,19 +7,19 @@ import ( // FakeDownloader provides a fake downloader interface type FakeDownloader struct { - URL []url.URL - File []string - Err map[string]error - mutex sync.Mutex + URL []url.URL + Target []string + Err map[string]error + mutex sync.Mutex } -// DownloadFile downloads a fake file from the URL -func (f *FakeDownloader) DownloadFile(url *url.URL, targetFile string) (bool, error) { +// Download downloads a fake file from the URL +func (f *FakeDownloader) Download(url *url.URL, target string) error { f.mutex.Lock() defer f.mutex.Unlock() f.URL = append(f.URL, *url) - f.File = append(f.File, targetFile) + f.Target = append(f.Target, target) - return true, f.Err[url.String()] + return f.Err[url.String()] } diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/fs.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/fs.go index ff185abf78b1..547c688d036b 100644 --- a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/fs.go +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/fs.go @@ -2,11 +2,28 @@ package test import ( "bytes" + "errors" "io" "os" + "strings" "sync" + "time" ) +// FakeFile represents a fake file and implements the os.FileInfo interface +type FakeFile struct { + FileName string + Dir bool + FMode os.FileMode +} + +func (f *FakeFile) Name() string { return f.FileName } +func (f *FakeFile) Size() int64 { return 0 } +func (f *FakeFile) Mode() os.FileMode { return f.FMode } +func (f *FakeFile) ModTime() time.Time { return time.Now() } +func (f *FakeFile) IsDir() bool { return f.Dir } +func (f *FakeFile) Sys() interface{} { return nil } + // FakeFileSystem provides a fake filesystem structure for testing type FakeFileSystem struct { ChmodFile []string @@ -47,6 +64,8 @@ type FakeFileSystem struct { WriteFileError error WriteFileContent string + Files []os.FileInfo + mutex sync.Mutex } @@ -63,6 +82,21 @@ func (f *FakeReadCloser) Close() error { return f.CloseError } +// ReadDir reads the files in specified directory +func (f *FakeFileSystem) ReadDir(p string) ([]os.FileInfo, error) { + return f.Files, nil +} + +// Stat provides stats about a single file +func (f *FakeFileSystem) Stat(p string) (os.FileInfo, error) { + for _, f := range f.Files { + if strings.HasSuffix(p, "/"+f.Name()) { + return f, nil + } + } + return nil, &os.PathError{Path: p, Err: errors.New("file does not exists")} +} + // Chmod manipulates permissions on the fake filesystem func (f *FakeFileSystem) Chmod(file string, mode os.FileMode) error { f.mutex.Lock() @@ -128,7 +162,7 @@ func (f *FakeFileSystem) Open(file string) (io.ReadCloser, error) { return f.OpenFileResult, f.OpenError } -// Write writes a file +// WriteFile writes a file func (f *FakeFileSystem) WriteFile(file string, data []byte) error { f.WriteFileName = file f.WriteFileContent = string(data) diff --git a/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/install.go b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/install.go new file mode 100644 index 000000000000..1d6c2dc3a7e1 --- /dev/null +++ b/Godeps/_workspace/src/github.com/openshift/source-to-image/pkg/test/install.go @@ -0,0 +1,27 @@ +package test + +import ( + "github.com/openshift/source-to-image/pkg/api" +) + +// FakeInstaller provides a fake installer +type FakeInstaller struct { + Scripts [][]string + DstDir []string + Error error +} + +func (f *FakeInstaller) run(scripts []string, dstDir string) []api.InstallResult { + result := []api.InstallResult{} + f.Scripts = append(f.Scripts, scripts) + f.DstDir = append(f.DstDir, dstDir) + return result +} + +func (f *FakeInstaller) InstallRequired(scripts []string, dstDir string) ([]api.InstallResult, error) { + return f.run(scripts, dstDir), f.Error +} + +func (f *FakeInstaller) InstallOptional(scripts []string, dstDir string) []api.InstallResult { + return f.run(scripts, dstDir) +} diff --git a/pkg/build/builder/sti.go b/pkg/build/builder/sti.go index 06ff90c1a53d..528efb1bff45 100644 --- a/pkg/build/builder/sti.go +++ b/pkg/build/builder/sti.go @@ -40,8 +40,9 @@ func (s *STIBuilder) Build() error { Tag: tag, ScriptsURL: s.build.Parameters.Strategy.STIStrategy.Scripts, Environment: getBuildEnvVars(s.build), - Clean: !s.build.Parameters.Strategy.STIStrategy.Incremental, + Incremental: s.build.Parameters.Strategy.STIStrategy.Incremental, } + if s.build.Parameters.Revision != nil && s.build.Parameters.Revision.Git != nil && s.build.Parameters.Revision.Git.Commit != "" { request.Ref = s.build.Parameters.Revision.Git.Commit