github.com/koderover/helm@v2.17.0+incompatible/pkg/renderutil/render_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package renderutil
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"k8s.io/helm/pkg/chartutil"
    25  	"k8s.io/helm/pkg/proto/hapi/chart"
    26  )
    27  
    28  const cmTemplate = `kind: ConfigMap
    29  apiVersion: v1
    30  metadata:
    31    name: example
    32  data:
    33    Chart:
    34  {{.Chart | toYaml | indent 4}}
    35    Release:
    36  {{.Release | toYaml | indent 4}}
    37    Values:
    38  {{.Values | toYaml | indent 4}}
    39  `
    40  
    41  func TestRender(t *testing.T) {
    42  
    43  	testChart := &chart.Chart{
    44  		Metadata: &chart.Metadata{Name: "hello"},
    45  		Templates: []*chart.Template{
    46  			{Name: "templates/cm.yaml", Data: []byte(cmTemplate)},
    47  		},
    48  		Values: &chart.Config{Raw: "meow: defaultmeow"},
    49  	}
    50  
    51  	newConfig := &chart.Config{Raw: "meow: newmeow"}
    52  	defaultConfig := &chart.Config{Raw: "{}"}
    53  
    54  	tests := map[string]struct {
    55  		chart  *chart.Chart
    56  		config *chart.Config
    57  		opts   Options
    58  		want   map[string]string
    59  	}{
    60  		"BasicWithValues": {
    61  			chart:  testChart,
    62  			config: newConfig,
    63  			opts:   Options{},
    64  			want: map[string]string{
    65  				"hello/templates/cm.yaml": `kind: ConfigMap
    66  apiVersion: v1
    67  metadata:
    68    name: example
    69  data:
    70    Chart:
    71      name: hello
    72      
    73    Release:
    74      IsInstall: false
    75      IsUpgrade: false
    76      Name: ""
    77      Namespace: ""
    78      Revision: 0
    79      Service: Tiller
    80      Time: null
    81      
    82    Values:
    83      meow: newmeow
    84      
    85  `,
    86  			},
    87  		},
    88  		"BasicNoValues": {
    89  			chart:  testChart,
    90  			config: defaultConfig,
    91  			opts:   Options{},
    92  			want: map[string]string{
    93  				"hello/templates/cm.yaml": `kind: ConfigMap
    94  apiVersion: v1
    95  metadata:
    96    name: example
    97  data:
    98    Chart:
    99      name: hello
   100      
   101    Release:
   102      IsInstall: false
   103      IsUpgrade: false
   104      Name: ""
   105      Namespace: ""
   106      Revision: 0
   107      Service: Tiller
   108      Time: null
   109      
   110    Values:
   111      meow: defaultmeow
   112      
   113  `,
   114  			},
   115  		},
   116  		"SetSomeReleaseValues": {
   117  			chart:  testChart,
   118  			config: defaultConfig,
   119  			opts:   Options{ReleaseOptions: chartutil.ReleaseOptions{Name: "meow"}},
   120  			want: map[string]string{
   121  				"hello/templates/cm.yaml": `kind: ConfigMap
   122  apiVersion: v1
   123  metadata:
   124    name: example
   125  data:
   126    Chart:
   127      name: hello
   128      
   129    Release:
   130      IsInstall: false
   131      IsUpgrade: false
   132      Name: meow
   133      Namespace: ""
   134      Revision: 0
   135      Service: Tiller
   136      Time: null
   137      
   138    Values:
   139      meow: defaultmeow
   140      
   141  `,
   142  			},
   143  		},
   144  	}
   145  
   146  	for testName, tt := range tests {
   147  		t.Run(testName, func(t *testing.T) {
   148  			got, err := Render(tt.chart, tt.config, tt.opts)
   149  			require.NoError(t, err)
   150  			require.Equal(t, tt.want, got)
   151  		})
   152  	}
   153  }