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

     1  package porter
     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/mixin"
    10  	"get.porter.sh/porter/pkg/pkgmgmt"
    11  	"get.porter.sh/porter/pkg/portercontext"
    12  	"get.porter.sh/porter/pkg/yaml"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestPorter_Run(t *testing.T) {
    18  	p := NewTestPorter(t)
    19  	defer p.Close()
    20  
    21  	// Mock the mixin test runner and verify that we are calling runtime mixins, e.g. exec-runtime and not exec
    22  	mp := p.Mixins.(*mixin.TestMixinProvider)
    23  	mp.RunAssertions = append(mp.RunAssertions, func(mixinCxt *portercontext.Context, mixinName string, commandOpts pkgmgmt.CommandOptions) error {
    24  		assert.Equal(t, "exec", mixinName, "expected to call the exec mixin")
    25  		assert.True(t, commandOpts.Runtime, "the mixin command should be executed in runtime mode")
    26  		assert.Equal(t, "install", commandOpts.Command, "should have executed the mixin's install command")
    27  		return nil
    28  	})
    29  	p.TestConfig.TestContext.AddTestFile("testdata/bundle.json", "/cnab/bundle.json")
    30  	p.TestConfig.TestContext.AddTestFile("testdata/porter.yaml", "porter.yaml")
    31  
    32  	// Change the schemaVersion to validate that the runtime ignores this field and runs regardless
    33  	e := yaml.NewEditor(p.FileSystem)
    34  	require.NoError(t, e.ReadFile("porter.yaml"))
    35  	require.NoError(t, e.SetValue("schemaVersion", ""))
    36  	require.NoError(t, e.WriteFile("porter.yaml"))
    37  
    38  	_, err := p.FileSystem.Create("/home/nonroot/.kube/config")
    39  	require.NoError(t, err)
    40  
    41  	opts := NewRunOptions(p.Config)
    42  	opts.Action = cnab.ActionInstall
    43  	opts.File = "porter.yaml"
    44  
    45  	err = opts.Validate()
    46  	require.NoError(t, err, "could not validate run options")
    47  
    48  	err = p.Run(context.Background(), opts)
    49  	assert.NoError(t, err, "run failed")
    50  }
    51  
    52  func TestPorter_defaultDebugToOff(t *testing.T) {
    53  	p := New() // Don't use the test porter, it has debug on by default
    54  	defer p.Close()
    55  
    56  	opts := NewRunOptions(p.Config)
    57  
    58  	err := opts.defaultDebug()
    59  	require.NoError(t, err)
    60  	assert.False(t, opts.DebugMode)
    61  }
    62  
    63  func TestPorter_defaultDebugUsesEnvVar(t *testing.T) {
    64  	p := New() // Don't use the test porter, it has debug on by default
    65  	defer p.Close()
    66  
    67  	p.Setenv(config.EnvDEBUG, "true")
    68  
    69  	opts := NewRunOptions(p.Config)
    70  
    71  	err := opts.defaultDebug()
    72  	require.NoError(t, err)
    73  
    74  	assert.True(t, opts.DebugMode)
    75  }