...

Source file src/edge-infra.dev/pkg/sds/patching/test/setup.go

Documentation: edge-infra.dev/pkg/sds/patching/test

     1  package test
     2  
     3  import (
     4  	"archive/tar"
     5  	"compress/gzip"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"gotest.tools/v3/fs"
    14  
    15  	"edge-infra.dev/pkg/sds/patching/common"
    16  )
    17  
    18  func NewConfig(t *testing.T) common.Config {
    19  	dir := fs.NewDir(t, "testing")
    20  	MountPath := dir.Path()
    21  
    22  	c := common.Config{
    23  		MountPath:         MountPath,
    24  		ArtefactsPath:     MountPath + "/versions",
    25  		ArtefactsTempPath: MountPath + "/versions/.tmp",
    26  		LiveBootPath:      MountPath + "/live",
    27  		ScriptsPath:       MountPath + "/patching",
    28  		ScriptsTempPath:   MountPath + "/patching/.tmp",
    29  		EnvFilePath:       MountPath + "/patching/patching.env",
    30  		LegacyScriptsPath: MountPath + "/upgrade",
    31  
    32  		BootFiles:   MountPath + "/boot/",
    33  		ScriptFiles: MountPath + "/patching/",
    34  
    35  		PatchsetMount: MountPath + "/mnt/patchset",
    36  		RebootPath:    MountPath + "/mnt/run/reboot-required",
    37  	}
    38  	return c
    39  }
    40  
    41  func CreateFileTree(cfg common.Config) error {
    42  	items := []string{
    43  		cfg.MountPath,
    44  		cfg.ArtefactsPath,
    45  		cfg.ArtefactsTempPath,
    46  		cfg.LiveBootPath,
    47  		cfg.ScriptsPath,
    48  		cfg.ScriptsTempPath,
    49  		cfg.LegacyScriptsPath,
    50  		cfg.BootFiles,
    51  		cfg.ScriptFiles,
    52  		cfg.MountPath + "/mnt",
    53  		cfg.MountPath + "/mnt/run",
    54  		cfg.MountPath + "/boot",
    55  	}
    56  
    57  	for _, item := range items {
    58  		if err := os.MkdirAll(item, 0744); err != nil {
    59  			return err
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  func CreateTempFile(dir string, count int) ([]os.File, error) {
    66  	tempFiles := make([]os.File, count)
    67  
    68  	for i := 0; i < count; i++ {
    69  		tempFile, err := os.CreateTemp(dir, "")
    70  		if err != nil {
    71  			return nil, fmt.Errorf("error creating tempfile: %s", err)
    72  		}
    73  
    74  		tempFiles[i] = *tempFile
    75  		tempFile.Close()
    76  	}
    77  	return tempFiles, nil
    78  }
    79  
    80  func SetupTarArchive(filePath string, tempFiles []os.File, cfg common.Config) (*os.File, error) {
    81  	writeArchive, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0744)
    82  	if err != nil {
    83  		return nil, fmt.Errorf("error opening archive file: %s", err)
    84  	}
    85  
    86  	gw := gzip.NewWriter(writeArchive)
    87  	defer gw.Close()
    88  	tw := tar.NewWriter(gw)
    89  	defer tw.Close()
    90  
    91  	for _, f := range tempFiles {
    92  		file, err := os.Open(f.Name())
    93  		if err != nil {
    94  			return nil, fmt.Errorf("error opening tempfile: %s", err)
    95  		}
    96  
    97  		tempFileInfo, err := file.Stat()
    98  		if err != nil {
    99  			return nil, fmt.Errorf("%s", err)
   100  		}
   101  
   102  		header, err := tar.FileInfoHeader(tempFileInfo, tempFileInfo.Name())
   103  		if err != nil {
   104  			return nil, fmt.Errorf("error creating tempfile header: %s", err)
   105  		}
   106  
   107  		header.Name = strings.TrimPrefix(strings.Replace(file.Name(), cfg.MountPath, "", -1), string(filepath.Separator))
   108  
   109  		err = tw.WriteHeader(header)
   110  		if err != nil {
   111  			return nil, fmt.Errorf("error writing header to archive: %s", err)
   112  		}
   113  
   114  		_, err = io.Copy(tw, file)
   115  		if err != nil {
   116  			return nil, fmt.Errorf("error copying tempfile to archive: %s", err)
   117  		}
   118  
   119  		if err := file.Close(); err != nil {
   120  			return nil, fmt.Errorf("error closing tempfile: %s", err)
   121  		}
   122  	}
   123  
   124  	readArchive, err := os.OpenFile(filePath, os.O_RDONLY, 0744)
   125  	if err != nil {
   126  		return nil, fmt.Errorf("error opening archive file: %s", err)
   127  	}
   128  	return readArchive, nil
   129  }
   130  

View as plain text