get.porter.sh/porter@v1.3.0/pkg/porter/logs_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/portercontext"
     9  	"get.porter.sh/porter/pkg/storage"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestLogsShowOptions_Validate(t *testing.T) {
    15  
    16  	t.Run("installation specified", func(t *testing.T) {
    17  		c := portercontext.NewTestContext(t)
    18  		opts := LogsShowOptions{}
    19  		opts.Name = "mybun"
    20  
    21  		err := opts.Validate(c.Context)
    22  		require.NoError(t, err)
    23  	})
    24  
    25  	t.Run("installation defaulted", func(t *testing.T) {
    26  		c := portercontext.NewTestContext(t)
    27  		c.AddTestFile("testdata/porter.yaml", "porter.yaml")
    28  
    29  		opts := LogsShowOptions{}
    30  
    31  		err := opts.Validate(c.Context)
    32  		require.NoError(t, err)
    33  		assert.NotEmpty(t, opts.File) // it should pick up that there is one present, the name is defaulted when the action is run just like install
    34  	})
    35  
    36  	t.Run("run specified", func(t *testing.T) {
    37  		c := portercontext.NewTestContext(t)
    38  		opts := LogsShowOptions{}
    39  		opts.RunID = "abc123"
    40  
    41  		err := opts.Validate(c.Context)
    42  		require.NoError(t, err)
    43  	})
    44  
    45  	t.Run("both specified", func(t *testing.T) {
    46  		c := portercontext.NewTestContext(t)
    47  		opts := LogsShowOptions{}
    48  		opts.Name = "mybun"
    49  		opts.RunID = "abc123"
    50  
    51  		err := opts.Validate(c.Context)
    52  		require.Error(t, err)
    53  		assert.Contains(t, err.Error(), "either --installation or --run should be specified, not both")
    54  	})
    55  
    56  	t.Run("neither specified", func(t *testing.T) {
    57  		c := portercontext.NewTestContext(t)
    58  		opts := LogsShowOptions{}
    59  
    60  		err := opts.Validate(c.Context)
    61  		require.Error(t, err)
    62  		assert.Contains(t, err.Error(), "either --installation or --run is required")
    63  	})
    64  }
    65  
    66  func TestPorter_ShowInstallationLogs(t *testing.T) {
    67  	bun := cnab.ExtendedBundle{}
    68  	t.Run("no logs found", func(t *testing.T) {
    69  		p := NewTestPorter(t)
    70  		defer p.Close()
    71  
    72  		i := p.TestInstallations.CreateInstallation(storage.NewInstallation("", "test"))
    73  		c := p.TestInstallations.CreateRun(i.NewRun(cnab.ActionInstall, bun))
    74  		p.TestInstallations.CreateResult(c.NewResult(cnab.StatusSucceeded))
    75  
    76  		var opts LogsShowOptions
    77  		opts.Name = "test"
    78  		err := p.ShowInstallationLogs(context.Background(), &opts)
    79  		require.Error(t, err, "ShowInstallationLogs should have failed")
    80  		assert.Contains(t, err.Error(), "no logs found")
    81  	})
    82  
    83  	t.Run("has logs", func(t *testing.T) {
    84  		const testLogs = "some mighty fine logs"
    85  
    86  		p := NewTestPorter(t)
    87  		defer p.Close()
    88  
    89  		i := p.TestInstallations.CreateInstallation(storage.NewInstallation("", "test"))
    90  		c := p.TestInstallations.CreateRun(i.NewRun(cnab.ActionInstall, bun))
    91  		r := p.TestInstallations.CreateResult(c.NewResult(cnab.StatusSucceeded), func(r *storage.Result) {
    92  			require.NoError(t, r.OutputMetadata.SetGeneratedByBundle(cnab.OutputInvocationImageLogs, false))
    93  		})
    94  
    95  		p.TestInstallations.CreateOutput(r.NewOutput(cnab.OutputInvocationImageLogs, []byte(testLogs)))
    96  
    97  		var opts LogsShowOptions
    98  		opts.Name = "test"
    99  		err := p.ShowInstallationLogs(context.Background(), &opts)
   100  		require.NoError(t, err, "ShowInstallationLogs failed")
   101  
   102  		assert.Contains(t, p.TestConfig.TestContext.GetOutput(), testLogs)
   103  	})
   104  }