github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/helm/test/option_test.go (about) 1 package test 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/aquasecurity/defsec/pkg/scanners/options" 14 "github.com/aquasecurity/trivy-iac/pkg/scanners/helm/parser" 15 ) 16 17 func Test_helm_parser_with_options_with_values_file(t *testing.T) { 18 19 tests := []struct { 20 testName string 21 chartName string 22 valuesFile string 23 }{ 24 { 25 testName: "Parsing directory 'testchart'", 26 chartName: "testchart", 27 valuesFile: "values/values.yaml", 28 }, 29 } 30 31 for _, test := range tests { 32 t.Run(test.testName, func(t *testing.T) { 33 chartName := test.chartName 34 35 t.Logf("Running test: %s", test.testName) 36 37 var opts []options.ParserOption 38 39 if test.valuesFile != "" { 40 opts = append(opts, parser.OptionWithValuesFile(test.valuesFile)) 41 } 42 43 helmParser := parser.New(chartName, opts...) 44 err := helmParser.ParseFS(context.TODO(), os.DirFS(filepath.Join("testdata", chartName)), ".") 45 require.NoError(t, err) 46 manifests, err := helmParser.RenderedChartFiles() 47 require.NoError(t, err) 48 49 assert.Len(t, manifests, 3) 50 51 for _, manifest := range manifests { 52 expectedPath := filepath.Join("testdata", "expected", "options", chartName, manifest.TemplateFilePath) 53 54 expectedContent, err := os.ReadFile(expectedPath) 55 require.NoError(t, err) 56 57 cleanExpected := strings.ReplaceAll(string(expectedContent), "\r\n", "\n") 58 cleanActual := strings.ReplaceAll(manifest.ManifestContent, "\r\n", "\n") 59 60 assert.Equal(t, cleanExpected, cleanActual) 61 } 62 }) 63 } 64 } 65 66 func Test_helm_parser_with_options_with_set_value(t *testing.T) { 67 68 tests := []struct { 69 testName string 70 chartName string 71 valuesFile string 72 values string 73 }{ 74 { 75 testName: "Parsing directory 'testchart'", 76 chartName: "testchart", 77 values: "securityContext.runAsUser=0", 78 }, 79 } 80 81 for _, test := range tests { 82 t.Run(test.testName, func(t *testing.T) { 83 chartName := test.chartName 84 85 t.Logf("Running test: %s", test.testName) 86 87 var opts []options.ParserOption 88 89 if test.valuesFile != "" { 90 opts = append(opts, parser.OptionWithValuesFile(test.valuesFile)) 91 } 92 93 if test.values != "" { 94 opts = append(opts, parser.OptionWithValues(test.values)) 95 } 96 97 helmParser := parser.New(chartName, opts...) 98 err := helmParser.ParseFS(context.TODO(), os.DirFS(filepath.Join("testdata", chartName)), ".") 99 require.NoError(t, err) 100 manifests, err := helmParser.RenderedChartFiles() 101 require.NoError(t, err) 102 103 assert.Len(t, manifests, 3) 104 105 for _, manifest := range manifests { 106 expectedPath := filepath.Join("testdata", "expected", "options", chartName, manifest.TemplateFilePath) 107 108 expectedContent, err := os.ReadFile(expectedPath) 109 require.NoError(t, err) 110 111 cleanExpected := strings.ReplaceAll(string(expectedContent), "\r\n", "\n") 112 cleanActual := strings.ReplaceAll(manifest.ManifestContent, "\r\n", "\n") 113 114 assert.Equal(t, cleanExpected, cleanActual) 115 } 116 }) 117 } 118 } 119 120 func Test_helm_parser_with_options_with_api_versions(t *testing.T) { 121 122 tests := []struct { 123 testName string 124 chartName string 125 apiVersions []string 126 }{ 127 { 128 testName: "Parsing directory 'with-api-version'", 129 chartName: "with-api-version", 130 apiVersions: []string{"policy/v1/PodDisruptionBudget"}, 131 }, 132 } 133 134 for _, test := range tests { 135 t.Run(test.testName, func(t *testing.T) { 136 chartName := test.chartName 137 138 t.Logf("Running test: %s", test.testName) 139 140 var opts []options.ParserOption 141 142 if len(test.apiVersions) > 0 { 143 opts = append(opts, parser.OptionWithAPIVersions(test.apiVersions...)) 144 } 145 146 helmParser := parser.New(chartName, opts...) 147 err := helmParser.ParseFS(context.TODO(), os.DirFS(filepath.Join("testdata", chartName)), ".") 148 require.NoError(t, err) 149 manifests, err := helmParser.RenderedChartFiles() 150 require.NoError(t, err) 151 152 assert.Len(t, manifests, 1) 153 154 for _, manifest := range manifests { 155 expectedPath := filepath.Join("testdata", "expected", "options", chartName, manifest.TemplateFilePath) 156 157 expectedContent, err := os.ReadFile(expectedPath) 158 require.NoError(t, err) 159 160 cleanExpected := strings.TrimSpace(strings.ReplaceAll(string(expectedContent), "\r\n", "\n")) 161 cleanActual := strings.TrimSpace(strings.ReplaceAll(manifest.ManifestContent, "\r\n", "\n")) 162 163 assert.Equal(t, cleanExpected, cleanActual) 164 } 165 }) 166 } 167 }