github.com/verrazzano/verrazzano@v1.7.0/tools/charts-manager/vcm/cmd/diff/diff_test.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package diff
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/verrazzano/verrazzano/tools/charts-manager/vcm/cmd/helpers"
    12  	"github.com/verrazzano/verrazzano/tools/charts-manager/vcm/pkg/constants"
    13  	"github.com/verrazzano/verrazzano/tools/charts-manager/vcm/tests/pkg/fakes"
    14  	vcmtesthelpers "github.com/verrazzano/verrazzano/tools/charts-manager/vcm/tests/pkg/helpers"
    15  )
    16  
    17  // TestNewCmdDiff tests that function NewCmdDiff creates diff cmd with correct flags
    18  // GIVEN a call to NewCmdDiff
    19  //
    20  //	WHEN correct arguments are passed
    21  //	THEN the diff cmd instance created contains all the required flags.
    22  func TestNewCmdDiff(t *testing.T) {
    23  	rc, cleanup, err := vcmtesthelpers.ContextSetup()
    24  	assert.NoError(t, err)
    25  	defer cleanup()
    26  	cmd := NewCmdDiff(rc, fakes.FakeHelmChartFileSystem{})
    27  	assert.NotNil(t, cmd, "command is nil")
    28  	assert.NotNil(t, cmd.PersistentFlags().Lookup(constants.FlagChartName), fmt.Sprintf(vcmtesthelpers.FlagNotFound, constants.FlagChartName))
    29  	assert.NotNil(t, cmd.PersistentFlags().Lookup(constants.FlagVersionName), fmt.Sprintf(vcmtesthelpers.FlagNotFound, constants.FlagVersionName))
    30  	assert.NotNil(t, cmd.PersistentFlags().Lookup(constants.FlagDirName), fmt.Sprintf(vcmtesthelpers.FlagNotFound, constants.FlagDirName))
    31  	assert.NotNil(t, cmd.PersistentFlags().Lookup(constants.FlagDiffSourceName), fmt.Sprintf(vcmtesthelpers.FlagNotFound, constants.FlagDiffSourceName))
    32  	assert.Equal(t, buildExample(), cmd.Example)
    33  }
    34  
    35  // TestExecDiffCmd tests the execution of diff command
    36  // GIVEN a call to NewCmdDiff and then executing the resulting diff command with specific parameters to generate
    37  // a diff of a chart directory against a source directory
    38  //
    39  //	WHEN invalid arguments are passed
    40  //	THEN the cmd execution results in an error.
    41  //
    42  //	WHEN the patch generation returns an error
    43  //	THEN the cmd execution results in an error.
    44  //
    45  //	WHEN the patch generation does not return an error or no patch is generated
    46  //	THEN the cmd execution does not result in an error.
    47  func TestExecDiffCmd(t *testing.T) {
    48  	rc, cleanup, err := vcmtesthelpers.ContextSetup()
    49  	assert.NoError(t, err)
    50  	defer cleanup()
    51  	type args struct {
    52  		chart     string
    53  		version   string
    54  		chartsDir string
    55  		sourceDir string
    56  	}
    57  	tests := []struct {
    58  		name      string
    59  		args      args
    60  		hfs       fakes.FakeHelmChartFileSystem
    61  		wantError error
    62  	}{
    63  		{
    64  			name:      "testChartArgumentNilThrowsError",
    65  			args:      args{chart: "", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: "/tmp/preveious"},
    66  			hfs:       fakes.FakeHelmChartFileSystem{},
    67  			wantError: fmt.Errorf(helpers.ErrFormatMustSpecifyFlag, constants.FlagChartName, constants.FlagChartName, constants.FlagChartShorthand),
    68  		},
    69  		{
    70  			name:      "testChartArgumentEmptyThrowsError",
    71  			args:      args{chart: "\n", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: "/tmp/preveious"},
    72  			hfs:       fakes.FakeHelmChartFileSystem{},
    73  			wantError: fmt.Errorf(helpers.ErrFormatNotEmpty, constants.FlagChartName),
    74  		},
    75  		{
    76  			name:      "testVersionArgumentNilThrowsError",
    77  			args:      args{chart: "chart", version: "", chartsDir: "/tmp/charts", sourceDir: "/tmp/preveious"},
    78  			hfs:       fakes.FakeHelmChartFileSystem{},
    79  			wantError: fmt.Errorf(helpers.ErrFormatMustSpecifyFlag, constants.FlagVersionName, constants.FlagVersionName, constants.FlagVersionShorthand),
    80  		},
    81  		{
    82  			name:      "testVersionArgumentEmptyThrowsError",
    83  			args:      args{chart: "chart", version: "\t", chartsDir: "/tmp/charts", sourceDir: "/tmp/preveious"},
    84  			hfs:       fakes.FakeHelmChartFileSystem{},
    85  			wantError: fmt.Errorf(helpers.ErrFormatNotEmpty, constants.FlagVersionName),
    86  		},
    87  		{
    88  			name:      "testDirArgumentNilThrowsError",
    89  			args:      args{chart: "chart", version: "0.0.1", chartsDir: "", sourceDir: "/tmp/preveious"},
    90  			hfs:       fakes.FakeHelmChartFileSystem{},
    91  			wantError: fmt.Errorf(helpers.ErrFormatMustSpecifyFlag, constants.FlagDirName, constants.FlagDirName, constants.FlagDirShorthand),
    92  		},
    93  		{
    94  			name:      "testDirArgumentEmptyThrowsError",
    95  			args:      args{chart: "chart", version: "0.0.1", chartsDir: "\n", sourceDir: "/tmp/preveious"},
    96  			hfs:       fakes.FakeHelmChartFileSystem{},
    97  			wantError: fmt.Errorf(helpers.ErrFormatNotEmpty, constants.FlagDirName),
    98  		},
    99  		{
   100  			name:      "testSourceArgumentNilThrowsError",
   101  			args:      args{chart: "chart", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: ""},
   102  			hfs:       fakes.FakeHelmChartFileSystem{},
   103  			wantError: fmt.Errorf(helpers.ErrFormatMustSpecifyFlag, constants.FlagDiffSourceName, constants.FlagDiffSourceName, constants.FlagDiffSourceShorthand),
   104  		},
   105  		{
   106  			name:      "testSourceArgumentEmptyThrowsError",
   107  			args:      args{chart: "chart", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: "\t\n"},
   108  			hfs:       fakes.FakeHelmChartFileSystem{},
   109  			wantError: fmt.Errorf(helpers.ErrFormatNotEmpty, constants.FlagDiffSourceName),
   110  		},
   111  		{
   112  			name: "testGeneratePatchThrowsError",
   113  			args: args{chart: "chart", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: "/tmp/previous"},
   114  			hfs: fakes.FakeHelmChartFileSystem{
   115  				FakeGeneratePatchWithSourceDir: func(chartsDir string, chart string, version string, sourceDir string) (string, error) {
   116  					return "", fmt.Errorf(vcmtesthelpers.DummyError)
   117  				},
   118  			},
   119  			wantError: fmt.Errorf(vcmtesthelpers.DummyError),
   120  		},
   121  		{
   122  			name: "testGeneratePatchRetursNoPatchFile",
   123  			args: args{chart: "chart", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: "/tmp/previous"},
   124  			hfs: fakes.FakeHelmChartFileSystem{
   125  				FakeGeneratePatchWithSourceDir: func(chartsDir string, chart string, version string, sourceDir string) (string, error) {
   126  					return "", nil
   127  				},
   128  			},
   129  			wantError: nil,
   130  		},
   131  		{
   132  			name: "testGeneratePatchRetursValidPatchFile",
   133  			args: args{chart: "chart", version: "0.0.1", chartsDir: "/tmp/charts", sourceDir: "/tmp/previous"},
   134  			hfs: fakes.FakeHelmChartFileSystem{
   135  				FakeGeneratePatchWithSourceDir: func(chartsDir string, chart string, version string, sourceDir string) (string, error) {
   136  					return "/tmp/patchfile", nil
   137  				},
   138  			},
   139  			wantError: nil,
   140  		},
   141  	}
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			cmd := NewCmdDiff(rc, tt.hfs)
   145  			cmd.PersistentFlags().Set(constants.FlagChartName, tt.args.chart)
   146  			cmd.PersistentFlags().Set(constants.FlagVersionName, tt.args.version)
   147  			cmd.PersistentFlags().Set(constants.FlagDirName, tt.args.chartsDir)
   148  			cmd.PersistentFlags().Set(constants.FlagDiffSourceName, tt.args.sourceDir)
   149  			err := cmd.Execute()
   150  			if err != nil && tt.wantError == nil {
   151  				t.Errorf("diff exec with args %v resulted in error %v", tt.args, err)
   152  			}
   153  
   154  			if err != nil && tt.wantError != nil && err.Error() != tt.wantError.Error() {
   155  				t.Errorf("diff exec with args %v resulted in error %v, expected %v", tt.args, err, tt.wantError)
   156  			}
   157  
   158  			if err == nil && tt.wantError != nil {
   159  				t.Errorf("diff exec with args %v resulted in no error, expected %v", tt.args, tt.wantError)
   160  			}
   161  		})
   162  	}
   163  }