get.porter.sh/porter@v1.3.0/pkg/storage/installation_provider_helpers.go (about)

     1  package storage
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"get.porter.sh/porter/pkg/config"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var (
    14  	_ InstallationProvider = TestInstallationProvider{}
    15  
    16  	// A fixed now timestamp that we can use for comparisons in tests
    17  	now = time.Date(2020, time.April, 18, 1, 2, 3, 4, time.UTC)
    18  
    19  	installationID = "01FZVC5AVP8Z7A78CSCP1EJ604"
    20  )
    21  
    22  type TestInstallationProvider struct {
    23  	InstallationStore
    24  	TestStore
    25  	t         *testing.T
    26  	idCounter uint
    27  }
    28  
    29  func NewTestInstallationProvider(t *testing.T) *TestInstallationProvider {
    30  	tc := config.NewTestConfig(t)
    31  	testStore := NewTestStore(tc)
    32  	return NewTestInstallationProviderFor(t, testStore)
    33  }
    34  
    35  func NewTestInstallationProviderFor(t *testing.T, testStore TestStore) *TestInstallationProvider {
    36  	return &TestInstallationProvider{
    37  		t:                 t,
    38  		TestStore:         testStore,
    39  		InstallationStore: NewInstallationStore(testStore),
    40  	}
    41  }
    42  
    43  func (p *TestInstallationProvider) Close() error {
    44  	return p.TestStore.Close()
    45  }
    46  
    47  // CreateInstallation creates a new test installation and saves it.
    48  func (p *TestInstallationProvider) CreateInstallation(i Installation, transformations ...func(i *Installation)) Installation {
    49  	for _, transform := range transformations {
    50  		transform(&i)
    51  	}
    52  
    53  	err := p.InsertInstallation(context.Background(), i)
    54  	require.NoError(p.t, err, "InsertInstallation failed")
    55  	return i
    56  }
    57  
    58  func (p *TestInstallationProvider) SetMutableInstallationValues(i *Installation) {
    59  	i.ID = installationID
    60  	i.Status.Created = now
    61  	i.Status.Modified = now
    62  }
    63  
    64  // CreateRun creates a new test run and saves it.
    65  func (p *TestInstallationProvider) CreateRun(r Run, transformations ...func(r *Run)) Run {
    66  	for _, transform := range transformations {
    67  		transform(&r)
    68  	}
    69  
    70  	err := p.InsertRun(context.Background(), r)
    71  	require.NoError(p.t, err, "InsertRun failed")
    72  	return r
    73  }
    74  
    75  func (p *TestInstallationProvider) SetMutableRunValues(r *Run) {
    76  	p.idCounter += 1
    77  	r.ID = fmt.Sprintf("%d", p.idCounter)
    78  	r.Revision = r.ID
    79  	r.Created = now
    80  }
    81  
    82  // CreateResult creates a new test result and saves it.
    83  func (p *TestInstallationProvider) CreateResult(r Result, transformations ...func(r *Result)) Result {
    84  	for _, transform := range transformations {
    85  		transform(&r)
    86  	}
    87  
    88  	err := p.InsertResult(context.Background(), r)
    89  	require.NoError(p.t, err, "InsertResult failed")
    90  	return r
    91  }
    92  
    93  func (p *TestInstallationProvider) SetMutableResultValues(r *Result) {
    94  	p.idCounter += 1
    95  	r.ID = fmt.Sprintf("%d", p.idCounter)
    96  	r.Created = now
    97  }
    98  
    99  // CreateOutput creates a new test output and saves it.
   100  func (p *TestInstallationProvider) CreateOutput(o Output, transformations ...func(o *Output)) Output {
   101  	for _, transform := range transformations {
   102  		transform(&o)
   103  	}
   104  
   105  	err := p.InsertOutput(context.Background(), o)
   106  	require.NoError(p.t, err, "InsertOutput failed")
   107  	return o
   108  }