istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/helm/fs_renderer_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package helm
    16  
    17  import (
    18  	"errors"
    19  	"os"
    20  	"testing"
    21  
    22  	"helm.sh/helm/v3/pkg/chart"
    23  )
    24  
    25  func TestRenderManifest(t *testing.T) {
    26  	tests := []struct {
    27  		desc                  string
    28  		inValues              string
    29  		inChart               chart.Chart
    30  		startRender           bool
    31  		inPath                string
    32  		objFileTemplateReader Renderer
    33  		wantResult            string
    34  		wantErr               error
    35  	}{
    36  		{
    37  			desc:                  "not-started",
    38  			inValues:              "",
    39  			startRender:           false,
    40  			inChart:               chart.Chart{},
    41  			objFileTemplateReader: Renderer{},
    42  			wantResult:            "",
    43  			wantErr:               errors.New("fileTemplateRenderer for not started in renderChart"),
    44  		},
    45  		{
    46  			desc: "started-random-template",
    47  			inValues: `
    48  description: test
    49  `,
    50  			inPath:      "testdata/render/Chart.yaml",
    51  			startRender: true,
    52  			objFileTemplateReader: Renderer{
    53  				namespace:     "name-space",
    54  				componentName: "foo-component",
    55  				dir:           "testdata/render",
    56  				files:         os.DirFS("."),
    57  			},
    58  			wantResult: `apiVersion: v1
    59  description: test
    60  name: addon
    61  version: 1.1.0
    62  appVersion: 1.1.0
    63  tillerVersion: ">=2.7.2"
    64  keywords:
    65    - istio-addon
    66  
    67  ---
    68  `,
    69  			wantErr: nil,
    70  		},
    71  		{
    72  			desc:        "bad-file-path",
    73  			inValues:    "",
    74  			inPath:      "foo/bar/Chart.yaml",
    75  			startRender: true,
    76  			objFileTemplateReader: Renderer{
    77  				namespace:     "name-space",
    78  				componentName: "foo-component",
    79  				dir:           "foo/bar",
    80  				files:         os.DirFS("."),
    81  			},
    82  			wantResult: "",
    83  			wantErr:    errors.New(`component "foo-component" does not exist`),
    84  		},
    85  	}
    86  	for _, tt := range tests {
    87  		t.Run(tt.desc, func(t *testing.T) {
    88  			if tt.startRender {
    89  				err := tt.objFileTemplateReader.Run()
    90  				if err != nil && tt.wantErr != nil {
    91  					if err.Error() != tt.wantErr.Error() {
    92  						t.Errorf("%s: expected err: %q got %q", tt.desc, tt.wantErr.Error(), err.Error())
    93  					}
    94  				}
    95  			}
    96  			if res, err := tt.objFileTemplateReader.RenderManifest(tt.inValues); res != tt.wantResult ||
    97  				((tt.wantErr != nil && err == nil) || (tt.wantErr == nil && err != nil)) {
    98  				t.Errorf("%s: \nexpected vals: \n%v\n\nexpected err:%v\ngot vals:\n%v\n\n got err %v",
    99  					tt.desc, tt.wantResult, tt.wantErr, res, err)
   100  			}
   101  		})
   102  	}
   103  }