github.com/wtsi-hgi/go-softpack-builder@v1.8.1/internal/s3mock/mock.go (about)

     1  /*******************************************************************************
     2   * Copyright (c) 2024 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 s3mock
    25  
    26  import (
    27  	"io"
    28  	"path/filepath"
    29  	"strings"
    30  
    31  	"github.com/wtsi-hgi/go-softpack-builder/core"
    32  	"github.com/wtsi-hgi/go-softpack-builder/internal"
    33  )
    34  
    35  const ErrS3Mock = internal.Error("Mock S3 error")
    36  
    37  // MockS3 can be used to test a build.Builder by implementing the build.S3
    38  // interface.
    39  type MockS3 struct {
    40  	Data        string
    41  	Def         string
    42  	SoftpackYML string
    43  	Readme      string
    44  	Fail        bool
    45  	Exes        string
    46  }
    47  
    48  // UploadData implements the build.S3 interface.
    49  func (m *MockS3) UploadData(data io.Reader, dest string) error {
    50  	if m.Fail {
    51  		return ErrS3Mock
    52  	}
    53  
    54  	buff, err := io.ReadAll(data)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	switch filepath.Ext(dest) {
    60  	case ".def":
    61  		m.Data = string(buff)
    62  		m.Def = dest
    63  	case ".yml":
    64  		m.SoftpackYML = string(buff)
    65  	case ".md":
    66  		m.Readme = string(buff)
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  // OpenFile implements the build.S3 interface.
    73  func (m *MockS3) OpenFile(source string) (io.ReadCloser, error) {
    74  	if filepath.Base(source) == core.ExesBasename {
    75  		return io.NopCloser(strings.NewReader(m.Exes)), nil
    76  	}
    77  
    78  	if filepath.Base(source) == core.BuilderOut {
    79  		return io.NopCloser(strings.NewReader("output")), nil
    80  	}
    81  
    82  	if filepath.Base(source) == core.SpackLockFile {
    83  		return io.NopCloser(strings.NewReader(`{"_meta":{"file-type":"spack-lockfile","lockfile-version":5,"specfile-version":4},"spack":{"version":"0.21.0.dev0","type":"git","commit":"dac3b453879439fd733b03d0106cc6fe070f71f6"},"roots":[{"hash":"oibd5a4hphfkgshqiav4fdkvw4hsq4ek","spec":"xxhash arch=None-None-x86_64_v3"}, {"hash":"1ibd5a4hphfkgshqiav4fdkvw4hsq4e1","spec":"py-anndata arch=None-None-x86_64_v3"}, {"hash":"2ibd5a4hphfkgshqiav4fdkvw4hsq4e2","spec":"r-seurat arch=None-None-x86_64_v3"}],"concrete_specs":{"oibd5a4hphfkgshqiav4fdkvw4hsq4ek":{"name":"xxhash","version":"0.8.1","arch":{"platform":"linux","platform_os":"ubuntu22.04","target":"x86_64_v3"},"compiler":{"name":"gcc","version":"11.4.0"},"namespace":"builtin","parameters":{"build_system":"makefile","cflags":[],"cppflags":[],"cxxflags":[],"fflags":[],"ldflags":[],"ldlibs":[]},"package_hash":"wuj5b2kjnmrzhtjszqovcvgc3q46m6hoehmiccimi5fs7nmsw22a====","hash":"oibd5a4hphfkgshqiav4fdkvw4hsq4ek"},"2ibd5a4hphfkgshqiav4fdkvw4hsq4e2":{"name":"r-seurat","version":"4","arch":{"platform":"linux","platform_os":"ubuntu22.04","target":"x86_64_v3"},"compiler":{"name":"gcc","version":"11.4.0"},"namespace":"builtin","parameters":{"build_system":"makefile","cflags":[],"cppflags":[],"cxxflags":[],"fflags":[],"ldflags":[],"ldlibs":[]},"package_hash":"2uj5b2kjnmrzhtjszqovcvgc3q46m6hoehmiccimi5fs7nmsw222====","hash":"2ibd5a4hphfkgshqiav4fdkvw4hsq4e2"}, "1ibd5a4hphfkgshqiav4fdkvw4hsq4e1":{"name":"py-anndata","version":"3.14","arch":{"platform":"linux","platform_os":"ubuntu22.04","target":"x86_64_v3"},"compiler":{"name":"gcc","version":"11.4.0"},"namespace":"builtin","parameters":{"build_system":"makefile","cflags":[],"cppflags":[],"cxxflags":[],"fflags":[],"ldflags":[],"ldlibs":[]},"package_hash":"2uj5b2kjnmrzhtjszqovcvgc3q46m6hoehmiccimi5fs7nmsw222====","hash":"1ibd5a4hphfkgshqiav4fdkvw4hsq4e1"}}}`)), nil //nolint:lll
    84  	}
    85  
    86  	if filepath.Base(source) == core.ImageBasename {
    87  		return io.NopCloser(strings.NewReader("image")), nil
    88  	}
    89  
    90  	return nil, io.ErrUnexpectedEOF
    91  }