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