get.porter.sh/porter@v1.3.0/pkg/pkgmgmt/client/helpers.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path"
     7  	"sync"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg/pkgmgmt"
    11  	"get.porter.sh/porter/pkg/portercontext"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  var _ pkgmgmt.PackageManager = &TestPackageManager{}
    16  
    17  // TestPackageManager helps us test mixins/plugins in our unit tests without
    18  // actually hitting any real executables on the file system.
    19  type TestPackageManager struct {
    20  	PkgType           string
    21  	Packages          []pkgmgmt.PackageMetadata
    22  	RunAssertions     []func(pkgContext *portercontext.Context, name string, commandOpts pkgmgmt.CommandOptions) error
    23  	InstallAssertions []func(installOpts pkgmgmt.InstallOptions) error
    24  
    25  	// called keeps track of which mixins/plugins were called
    26  	called sync.Map
    27  	lock   sync.Mutex
    28  }
    29  
    30  // GetCalled tracks how many times each package was called
    31  func (p *TestPackageManager) GetCalled(mixin string) int {
    32  	calls, _ := p.called.LoadOrStore(mixin, 0)
    33  	return calls.(int)
    34  }
    35  
    36  func (p *TestPackageManager) recordCalled(name string) {
    37  	p.lock.Lock()
    38  	defer p.lock.Unlock()
    39  
    40  	hits := p.GetCalled(name)
    41  	hits = hits + 1
    42  	p.called.Store(name, hits)
    43  }
    44  
    45  func (p *TestPackageManager) List() ([]string, error) {
    46  	names := make([]string, 0, len(p.Packages))
    47  	for _, pkg := range p.Packages {
    48  		names = append(names, pkg.GetName())
    49  	}
    50  	return names, nil
    51  }
    52  
    53  func (p *TestPackageManager) GetPackageDir(name string) (string, error) {
    54  	return path.Join("/home/myuser/.porter", p.PkgType, name), nil
    55  }
    56  
    57  func (p *TestPackageManager) GetMetadata(ctx context.Context, name string) (pkgmgmt.PackageMetadata, error) {
    58  	for _, pkg := range p.Packages {
    59  		if pkg.GetName() == name {
    60  			p.recordCalled(name)
    61  			return pkg, nil
    62  		}
    63  	}
    64  	return nil, fmt.Errorf("%s %s not installed", p.PkgType, name)
    65  }
    66  
    67  func (p *TestPackageManager) Install(ctx context.Context, opts pkgmgmt.InstallOptions) error {
    68  	for _, assert := range p.InstallAssertions {
    69  		err := assert(opts)
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func (p *TestPackageManager) Uninstall(ctx context.Context, opts pkgmgmt.UninstallOptions) error {
    78  	// do nothing
    79  	return nil
    80  }
    81  
    82  func (p *TestPackageManager) Run(ctx context.Context, pkgContext *portercontext.Context, name string, commandOpts pkgmgmt.CommandOptions) error {
    83  	for _, assert := range p.RunAssertions {
    84  		p.recordCalled(name)
    85  		err := assert(pkgContext, name, commandOpts)
    86  		if err != nil {
    87  			return err
    88  		}
    89  	}
    90  	return nil
    91  }
    92  
    93  type TestRunner struct {
    94  	*Runner
    95  	TestContext *portercontext.TestContext
    96  }
    97  
    98  // NewTestRunner initializes a test runner, with the output buffered, and an in-memory file system.
    99  func NewTestRunner(t *testing.T, name string, pkgType string, runtime bool) *TestRunner {
   100  	c := portercontext.NewTestContext(t)
   101  	pkgDir := fmt.Sprintf("/home/myuser/.porter/%s/%s", pkgType, name)
   102  	r := &TestRunner{
   103  		Runner:      NewRunner(name, pkgDir, runtime),
   104  		TestContext: c,
   105  	}
   106  	r.Context = c.Context
   107  
   108  	// Setup Porter home
   109  	_, err := c.FileSystem.Create("/home/myuser/.porter/porter")
   110  	require.NoError(t, err)
   111  	_, err = c.FileSystem.Create("/home/myuser/.porter/runtimes/porter-runtime")
   112  	require.NoError(t, err)
   113  	_, err = c.FileSystem.Create(path.Join(pkgDir, name))
   114  	require.NoError(t, err)
   115  	_, err = c.FileSystem.Create(path.Join(pkgDir, "runtimes", name+"-runtime"))
   116  	require.NoError(t, err)
   117  
   118  	return r
   119  }