get.porter.sh/porter@v1.3.0/pkg/cnab/provider/helpers.go (about) 1 package cnabprovider 2 3 import ( 4 "context" 5 "testing" 6 7 "get.porter.sh/porter/pkg/cnab" 8 "get.porter.sh/porter/pkg/config" 9 "get.porter.sh/porter/pkg/portercontext" 10 "get.porter.sh/porter/pkg/secrets" 11 "get.porter.sh/porter/pkg/storage" 12 "get.porter.sh/porter/pkg/test" 13 "github.com/hashicorp/go-multierror" 14 "github.com/stretchr/testify/require" 15 ) 16 17 const debugDriver = "debug" 18 19 var _ CNABProvider = &TestRuntime{} 20 21 type TestRuntime struct { 22 *Runtime 23 TestStorage storage.TestStore 24 TestInstallations *storage.TestInstallationProvider 25 TestCredentials *storage.TestCredentialSetProvider 26 TestParameters *storage.TestParameterSetProvider 27 TestConfig *config.TestConfig 28 } 29 30 func NewTestRuntime(t *testing.T) *TestRuntime { 31 tc := config.NewTestConfig(t) 32 testStorage := storage.NewTestStore(tc) 33 testSecrets := secrets.NewTestSecretsProvider() 34 testInstallations := storage.NewTestInstallationProviderFor(tc.TestContext.T, testStorage) 35 testCredentials := storage.NewTestCredentialProviderFor(tc.TestContext.T, testStorage, testSecrets) 36 testParameters := storage.NewTestParameterProviderFor(tc.TestContext.T, testStorage, testSecrets) 37 38 return NewTestRuntimeFor(tc, testInstallations, testCredentials, testParameters, testSecrets) 39 } 40 41 func NewTestRuntimeFor(tc *config.TestConfig, testInstallations *storage.TestInstallationProvider, testCredentials *storage.TestCredentialSetProvider, testParameters *storage.TestParameterSetProvider, testSecrets secrets.Store) *TestRuntime { 42 return &TestRuntime{ 43 Runtime: NewRuntime(tc.Config, testInstallations, testCredentials, testParameters, testSecrets, storage.NewSanitizer(testParameters, testSecrets)), 44 TestStorage: storage.TestStore{}, 45 TestInstallations: testInstallations, 46 TestCredentials: testCredentials, 47 TestParameters: testParameters, 48 TestConfig: tc, 49 } 50 } 51 52 func (t *TestRuntime) Close() error { 53 var bigErr *multierror.Error 54 55 bigErr = multierror.Append(bigErr, t.TestInstallations.Close()) 56 bigErr = multierror.Append(bigErr, t.TestCredentials.Close()) 57 bigErr = multierror.Append(bigErr, t.TestParameters.Close()) 58 59 return bigErr.ErrorOrNil() 60 } 61 62 func (t *TestRuntime) LoadBundle(bundleFile string) (cnab.ExtendedBundle, error) { 63 return t.Runtime.LoadBundle(bundleFile) 64 } 65 66 func (t *TestRuntime) LoadTestBundle(bundleFile string) cnab.ExtendedBundle { 67 bun, err := cnab.LoadBundle(portercontext.New(), bundleFile) 68 require.NoError(t.TestConfig.TestContext.T, err) 69 return bun 70 } 71 72 func (t *TestRuntime) Execute(ctx context.Context, args ActionArguments) error { 73 if args.Driver == "" { 74 args.Driver = debugDriver 75 } 76 return t.Runtime.Execute(ctx, args) 77 } 78 79 func (t *TestRuntime) MockGetDockerGroupId() { 80 // mock retrieving the docker group id on linux 81 // This is only called on linux, and we just need to have it return something 82 // so that the test doesn't fail 83 t.Setenv(test.ExpectedCommandOutputEnv, "docker:x:103") 84 }