github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/internal/job/testEnv_test.go (about)

     1  package job
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	"github.com/raphaelreyna/go-recon"
    13  	"github.com/raphaelreyna/go-recon/sources"
    14  )
    15  
    16  type mockDB struct {
    17  	data map[string]interface{}
    18  }
    19  
    20  func (mdb *mockDB) Store(ctx context.Context, uid string, i interface{}) error {
    21  	mdb.data[uid] = i
    22  	return nil
    23  }
    24  
    25  func (mdb *mockDB) Fetch(ctx context.Context, uid string) (interface{}, error) {
    26  	result, exists := mdb.data[uid]
    27  	if !exists {
    28  		return nil, errors.New("file not found")
    29  	}
    30  	return result, nil
    31  }
    32  
    33  func (mdb *mockDB) Ping(ctx context.Context) error {
    34  	return nil
    35  }
    36  
    37  func (mdb *mockDB) AddFileAs(name, destination string, perm os.FileMode) error {
    38  	file, err := os.OpenFile(destination, os.O_CREATE|os.O_WRONLY, perm)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	defer file.Close()
    43  
    44  	data, exists := mdb.data[name]
    45  	if !exists {
    46  		os.Remove(file.Name())
    47  		return fmt.Errorf("could not find file")
    48  	}
    49  
    50  	dataString := string(data.([]uint8))
    51  
    52  	_, err = file.Write([]byte(dataString))
    53  
    54  	return err
    55  }
    56  
    57  type TestEnv struct {
    58  	Root string
    59  
    60  	// Name of .tex file in the testing tex assets folder
    61  	TexFile    string
    62  	TexFileLoc int // 1 - registered and on disk; 2 - registered and in db and not on disk
    63  
    64  	// Name of .json file in the testing details assets folder
    65  	DtlsFile    string
    66  	DtlsFileLoc int // 0 - unregistered; 1 - registered and on disk; 2 - registered and in db and not on disk
    67  
    68  	// List of resource file names in the testing resources assets folder
    69  	Resources    []string
    70  	ResourcesLoc int // 0 - unregistered; 1 - registered and on disk; 2 - registered and in db and not on disk
    71  
    72  	rootDir string
    73  }
    74  
    75  func (te *TestEnv) SourceChain() (recon.SourceChain, error) {
    76  	var err error
    77  	te.rootDir, err = ioutil.TempDir(te.Root, "source_*")
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	db := &mockDB{data: map[string]interface{}{}}
    83  	sc := sources.NewDirSourceChain(sources.SoftLink, te.rootDir)
    84  
    85  	switch te.TexFileLoc {
    86  	case 0:
    87  		loc := filepath.Join(te.rootDir, te.TexFile)
    88  		file, err := os.OpenFile(loc, os.O_CREATE|os.O_WRONLY, 0600)
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  		defer file.Close()
    93  
    94  		originalLoc := filepath.Join(te.rootDir, "../../assets/templates/", te.TexFile)
    95  		originalFile, err := os.Open(originalLoc)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		defer originalFile.Close()
   100  
   101  		if _, err := io.Copy(file, originalFile); err != nil {
   102  			return nil, err
   103  		}
   104  	case 1:
   105  		originalLoc := filepath.Join(te.rootDir, "../../assets/templates/", te.TexFile)
   106  
   107  		data, err := ioutil.ReadFile(originalLoc)
   108  		if err != nil {
   109  			return nil, err
   110  		}
   111  
   112  		if err = db.Store(context.Background(), te.TexFile, data); err != nil {
   113  			return nil, err
   114  		}
   115  	}
   116  
   117  	switch te.DtlsFileLoc {
   118  	case 0:
   119  		loc := filepath.Join(te.rootDir, te.DtlsFile)
   120  		file, err := os.OpenFile(loc, os.O_CREATE|os.O_WRONLY, 0600)
   121  		if err != nil {
   122  			return nil, err
   123  		}
   124  		defer file.Close()
   125  
   126  		originalLoc := filepath.Join(te.rootDir, "../../assets/details/"+te.DtlsFile)
   127  		originalFile, err := os.Open(originalLoc)
   128  		if err != nil {
   129  			return nil, err
   130  		}
   131  		defer originalFile.Close()
   132  
   133  		if _, err := io.Copy(file, originalFile); err != nil {
   134  			return nil, err
   135  		}
   136  	case 1:
   137  		originalLoc := filepath.Join(te.rootDir, "../../assets/details/"+te.DtlsFile)
   138  
   139  		data, err := ioutil.ReadFile(originalLoc)
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  
   144  		if err = db.Store(context.Background(), te.DtlsFile, data); err != nil {
   145  			return nil, err
   146  		}
   147  	}
   148  
   149  	switch te.ResourcesLoc {
   150  	case 0:
   151  		for _, resource := range te.Resources {
   152  			loc := filepath.Join(te.rootDir, resource)
   153  			file, err := os.OpenFile(loc, os.O_CREATE|os.O_WRONLY, 0600)
   154  			if err != nil {
   155  				return nil, err
   156  			}
   157  			defer file.Close()
   158  
   159  			originalLoc := filepath.Join(te.rootDir, "../../assets/templates/", resource)
   160  			originalFile, err := os.Open(originalLoc)
   161  			if err != nil {
   162  				return nil, err
   163  			}
   164  			defer originalFile.Close()
   165  
   166  			if _, err := io.Copy(file, originalFile); err != nil {
   167  				return nil, err
   168  			}
   169  		}
   170  	case 1:
   171  		for _, resource := range te.Resources {
   172  			originalLoc := filepath.Join(te.rootDir, "../../assets/templates/", resource)
   173  
   174  			data, err := ioutil.ReadFile(originalLoc)
   175  			if err != nil {
   176  				return nil, err
   177  			}
   178  
   179  			if err = db.Store(context.Background(), resource, data); err != nil {
   180  				return nil, err
   181  			}
   182  		}
   183  	}
   184  
   185  	sc = append(sc, db)
   186  
   187  	return sc, nil
   188  }
   189  
   190  func (te *TestEnv) Clean() error {
   191  	if te.rootDir == "" {
   192  		return nil
   193  	}
   194  
   195  	return os.RemoveAll(te.rootDir)
   196  }