github.com/helmwave/helmwave@v0.36.4-0.20240509190856-b35563eba4c6/pkg/action/diff_local_internal_test.go (about)

     1  //go:build integration
     2  
     3  package action
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/databus23/helm-diff/v3/diff"
    13  	"github.com/helmwave/helmwave/pkg/template"
    14  	"github.com/helmwave/helmwave/tests"
    15  	log "github.com/sirupsen/logrus"
    16  	"github.com/stretchr/testify/suite"
    17  	"github.com/urfave/cli/v2"
    18  )
    19  
    20  var buf bytes.Buffer
    21  
    22  type DiffLocalTestSuite struct {
    23  	suite.Suite
    24  
    25  	ctx context.Context
    26  }
    27  
    28  //nolint:paralleltest // we capture output for global logger and uses helm repository.yaml flock
    29  func TestDiffLocalTestSuite(t *testing.T) {
    30  	// t.Parallel()
    31  	suite.Run(t, new(DiffLocalTestSuite))
    32  }
    33  
    34  func (ts *DiffLocalTestSuite) SetupTest() {
    35  	ts.ctx = tests.GetContext(ts.T())
    36  
    37  	log.StandardLogger().SetOutput(&buf)
    38  	ts.T().Cleanup(func() {
    39  		log.StandardLogger().SetOutput(os.Stderr)
    40  	})
    41  }
    42  
    43  func (ts *DiffLocalTestSuite) TestCmd() {
    44  	s := &DiffLocal{}
    45  	cmd := s.Cmd()
    46  
    47  	ts.Require().NotNil(cmd)
    48  	ts.Require().NotEmpty(cmd.Name)
    49  }
    50  
    51  func (ts *DiffLocalTestSuite) TestRun() {
    52  	s1 := &Build{
    53  		plandir: ts.T().TempDir(),
    54  		tags:    cli.StringSlice{},
    55  		yml: &Yml{
    56  			tpl:       filepath.Join(tests.Root, "02_helmwave.yml"),
    57  			file:      filepath.Join(tests.Root, "02_helmwave.yml"),
    58  			templater: template.TemplaterSprig,
    59  		},
    60  		diff:     &Diff{Options: &diff.Options{}},
    61  		diffMode: DiffModeLive,
    62  	}
    63  
    64  	s2 := &Build{
    65  		plandir: ts.T().TempDir(),
    66  		tags:    cli.StringSlice{},
    67  		yml: &Yml{
    68  			tpl:       filepath.Join(tests.Root, "03_helmwave.yml"),
    69  			file:      filepath.Join(tests.Root, "03_helmwave.yml"),
    70  			templater: template.TemplaterSprig,
    71  		},
    72  		diff:     &Diff{Options: &diff.Options{}},
    73  		diffMode: DiffModeLive,
    74  	}
    75  
    76  	d := DiffLocal{diff: s1.diff, plandir1: s1.plandir, plandir2: s2.plandir}
    77  
    78  	ts.Require().ErrorIs(d.Run(ts.ctx), os.ErrNotExist)
    79  	ts.Require().NoError(s1.Run(ts.ctx))
    80  	ts.Require().ErrorIs(d.Run(ts.ctx), os.ErrNotExist)
    81  	ts.Require().NoError(s2.Run(ts.ctx))
    82  
    83  	buf.Reset()
    84  	ts.Require().NoError(d.Run(ts.ctx))
    85  
    86  	output := buf.String()
    87  	buf.Reset()
    88  
    89  	ts.Require().Contains(output, "nginx, Deployment (apps) has been added")
    90  	ts.Require().Contains(output, "memcached-a-redis, Secret (v1) has been removed")
    91  }