github.com/verrazzano/verrazzano@v1.7.0/tools/charts-manager/vcm/pkg/fs/fs_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 fs
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	vzos "github.com/verrazzano/verrazzano/pkg/os"
    13  	"github.com/verrazzano/verrazzano/tools/charts-manager/vcm/pkg/helm"
    14  	vcmtesthelpers "github.com/verrazzano/verrazzano/tools/charts-manager/vcm/tests/pkg/helpers"
    15  )
    16  
    17  const (
    18  	testData      = "testdata"
    19  	testChart     = "testChart"
    20  	testVersion   = "x.y.z"
    21  	testChartsDir = "testdata/charts"
    22  )
    23  
    24  // TestHelmChartFileSystem_RearrangeChartDirectory_CopyFailsThrowsError tests that function RearrangeChartDirectory fails when copying the chart fails
    25  // GIVEN a call to RearrangeChartDirectory
    26  //
    27  //	WHEN copying the chart fails
    28  //	THEN the rearrange operation returns error.
    29  func TestHelmChartFileSystem_RearrangeChartDirectory_CopyFailsThrowsError(t *testing.T) {
    30  	runner = vzos.GenericTestRunner{
    31  		Err: fmt.Errorf(vcmtesthelpers.DummyError),
    32  	}
    33  	files := HelmChartFileSystem{}
    34  	err := files.RearrangeChartDirectory(testChartsDir, testChart, testVersion)
    35  	assert.ErrorContains(t, err, vcmtesthelpers.DummyError)
    36  	runner = vzos.DefaultRunner{}
    37  	os.RemoveAll(testData)
    38  }
    39  
    40  // TestHelmChartFileSystem_RearrangeChartDirectory_NoError tests that function RearrangeChartDirectory passes
    41  // GIVEN a call to RearrangeChartDirectory
    42  //
    43  //	WHEN none of the operation fails
    44  //	THEN the rearrange operation succeeds.
    45  func TestHelmChartFileSystem_RearrangeChartDirectory_NoError(t *testing.T) {
    46  	runner = vzos.GenericTestRunner{}
    47  	files := HelmChartFileSystem{}
    48  	err := files.RearrangeChartDirectory(testChartsDir, testChart, "a.b.c")
    49  	assert.NoError(t, err)
    50  	runner = vzos.DefaultRunner{}
    51  	os.RemoveAll(testData)
    52  }
    53  
    54  // TestHelmChartFileSystem_SaveUpstreamChart_NoError tests that function SaveUpstreamChart passes
    55  // GIVEN a call to SaveUpstreamChart
    56  //
    57  //	WHEN none of the operation fails
    58  //	THEN the save upstream operation succeeds.
    59  func TestHelmChartFileSystem_SaveUpstreamChart_NoError(t *testing.T) {
    60  	runner = vzos.GenericTestRunner{}
    61  	files := HelmChartFileSystem{}
    62  	err := files.SaveUpstreamChart(testChartsDir, testChart, testVersion, testVersion)
    63  	assert.NoError(t, err)
    64  	runner = vzos.DefaultRunner{}
    65  	os.RemoveAll(testData)
    66  }
    67  
    68  // TestHelmChartFileSystem_SaveUpstreamChart_NoError tests that function SaveUpstreamChart fails when copying fails
    69  // GIVEN a call to SaveUpstreamChart
    70  //
    71  //	WHEN copying chart to upstream fails
    72  //	THEN the save upstream operation fails.
    73  func TestHelmChartFileSystem_SaveUpstreamChart_Error(t *testing.T) {
    74  	runner = vzos.GenericTestRunner{
    75  		Err: fmt.Errorf(vcmtesthelpers.DummyError),
    76  	}
    77  	files := HelmChartFileSystem{}
    78  	err := files.SaveUpstreamChart(testChartsDir, testChart, testVersion, testVersion)
    79  	assert.ErrorContains(t, err, vcmtesthelpers.DummyError)
    80  	runner = vzos.DefaultRunner{}
    81  	os.RemoveAll(testData)
    82  }
    83  
    84  // TestHelmChartFileSystem_SaveChartProvenance_NoError tests that function SaveChartProvenance succeeds for valid chart provenance data
    85  // GIVEN a call to SaveChartProvenance
    86  //
    87  //	WHEN chart provenance is valid
    88  //	THEN the save chart provenance passes.
    89  func TestHelmChartFileSystem_SaveChartProvenance_NoError(t *testing.T) {
    90  	files := HelmChartFileSystem{}
    91  	err := files.SaveChartProvenance(testChartsDir, &helm.ChartProvenance{}, testChart, testVersion)
    92  	assert.NoError(t, err)
    93  	os.RemoveAll(testData)
    94  }