github.com/wtsi-hgi/go-softpack-builder@v1.8.1/build/install_test.go (about)

     1  /*******************************************************************************
     2   * Copyright (c) 2023 Genome Research Ltd.
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining
     5   * a copy of this software and associated documentation files (the
     6   * "Software"), to deal in the Software without restriction, including
     7   * without limitation the rights to use, copy, modify, merge, publish,
     8   * distribute, sublicense, and/or sell copies of the Software, and to
     9   * permit persons to whom the Software is furnished to do so, subject to
    10   * the following conditions:
    11   *
    12   * The above copyright notice and this permission notice shall be included
    13   * in all copies or substantial portions of the Software.
    14   *
    15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    16   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    17   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    18   * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    19   * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    20   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    21   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22   ******************************************************************************/
    23  
    24  package build
    25  
    26  import (
    27  	"errors"
    28  	"io"
    29  	"os"
    30  	"path/filepath"
    31  	"strings"
    32  	"testing"
    33  
    34  	. "github.com/smartystreets/goconvey/convey"
    35  	"github.com/wtsi-hgi/go-softpack-builder/core"
    36  )
    37  
    38  func TestInstall(t *testing.T) {
    39  	Convey("Given a Spack Definition, image file Reader, and a module file reader, install the files", t, func() {
    40  		tmpScriptsDir := t.TempDir()
    41  		tmpModulesDir := t.TempDir()
    42  
    43  		def := getExampleDefinition()
    44  		moduleFile := "some module file"
    45  		imageFile := "some image file"
    46  
    47  		// exes would normally be determined during the build process, and
    48  		// wrapperScript would come from config
    49  		exes := []string{"a", "b"}
    50  		wrapperScript := "/path/to/wrapper.script"
    51  
    52  		err := installModule(tmpScriptsDir, tmpModulesDir, def,
    53  			strings.NewReader(moduleFile), strings.NewReader(imageFile), exes, wrapperScript)
    54  		So(err, ShouldBeNil)
    55  
    56  		createdModuleFile := readFile(t, filepath.Join(tmpModulesDir, def.EnvironmentPath,
    57  			def.EnvironmentName, def.EnvironmentVersion))
    58  		scriptsDir := filepath.Join(tmpScriptsDir, def.EnvironmentPath, def.EnvironmentName,
    59  			def.EnvironmentVersion+ScriptsDirSuffix)
    60  		createdImageFile := readFile(t, filepath.Join(scriptsDir, core.ImageBasename))
    61  
    62  		So(createdModuleFile, ShouldEqual, moduleFile)
    63  		So(createdImageFile, ShouldEqual, imageFile)
    64  
    65  		for _, exe := range exes {
    66  			dest, err := os.Readlink(filepath.Join(scriptsDir, exe))
    67  			So(err, ShouldBeNil)
    68  			So(dest, ShouldEqual, wrapperScript)
    69  		}
    70  	})
    71  
    72  	Convey("makeDirectory works with relative paths", t, func() {
    73  		tmpDir := t.TempDir()
    74  		err := os.Chdir(tmpDir)
    75  		So(err, ShouldBeNil)
    76  
    77  		baseDir := filepath.Join(tmpDir, "base")
    78  		err = os.MkdirAll(baseDir, perms)
    79  		So(err, ShouldBeNil)
    80  
    81  		leafDir := filepath.Join("base", "sub1", "sub2")
    82  
    83  		err = makeDirectory(leafDir, baseDir)
    84  		So(err, ShouldBeNil)
    85  
    86  		absLeafDir, err := filepath.Abs(leafDir)
    87  		So(err, ShouldBeNil)
    88  
    89  		_, err = os.Stat(absLeafDir)
    90  		So(err, ShouldBeNil)
    91  
    92  		Convey("unless baseDir is not a parent of a leafDir", func() {
    93  			leafDir = filepath.Join("sub1", "sub2")
    94  			err = makeDirectory(leafDir, baseDir)
    95  			So(err, ShouldNotBeNil)
    96  			So(errors.Is(err, ErrMakeDirectory), ShouldBeTrue)
    97  		})
    98  	})
    99  }
   100  
   101  func readFile(t *testing.T, path string) string {
   102  	t.Helper()
   103  
   104  	f, err := os.Open(path)
   105  	So(err, ShouldBeNil)
   106  
   107  	buf, err := io.ReadAll(f)
   108  	So(err, ShouldBeNil)
   109  
   110  	return string(buf)
   111  }