github.com/banzaicloud/operator-tools@v0.28.10/pkg/helm/render_test.go (about)

     1  // Copyright © 2020 Banzai Cloud
     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  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/ghodss/yaml"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  	v1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    28  )
    29  
    30  func TestRenderChartWithCrdsAndTemplates(t *testing.T) {
    31  	chart := http.Dir("testdata/crds-and-templates/logging-operator")
    32  
    33  	defaultValues, err := GetDefaultValues(chart)
    34  	require.NoError(t, err)
    35  
    36  	valuesMap := map[string]interface{}{}
    37  	err = yaml.Unmarshal(defaultValues, &valuesMap)
    38  	require.NoError(t, err)
    39  
    40  	// custom resources in templates must be disabled explicitly
    41  	valuesMap["createCustomResource"] = false
    42  
    43  	objects, err := Render(chart, valuesMap, ReleaseOptions{
    44  		Name:      "release-name",
    45  		Namespace: "release-namespace",
    46  	}, "logging-operator")
    47  	require.NoError(t, err)
    48  
    49  	assert.Len(t, objects, 1)
    50  
    51  	o, ok := objects[0].(*unstructured.Unstructured)
    52  	assert.True(t, ok, "object should be unstructured")
    53  
    54  	assert.Equal(t, "loggings.logging.banzaicloud.io", o.GetName())
    55  }
    56  
    57  
    58  func TestRenderChartWithCrdsOnly(t *testing.T) {
    59  	chart := http.Dir("testdata/crds-only/logging-operator")
    60  
    61  	defaultValues, err := GetDefaultValues(chart)
    62  	require.NoError(t, err)
    63  
    64  	valuesMap := map[string]interface{}{}
    65  	err = yaml.Unmarshal(defaultValues, &valuesMap)
    66  	require.NoError(t, err)
    67  
    68  	objects, err := Render(chart, valuesMap, ReleaseOptions{
    69  		Name:      "release-name",
    70  		Namespace: "release-namespace",
    71  	}, "logging-operator")
    72  	require.NoError(t, err)
    73  
    74  	assert.Len(t, objects, 1)
    75  
    76  	o, ok := objects[0].(*unstructured.Unstructured)
    77  	assert.True(t, ok, "object should be unstructured")
    78  
    79  	assert.Equal(t, "loggings.logging.banzaicloud.io", o.GetName())
    80  }
    81  
    82  func TestRenderWithScheme(t *testing.T) {
    83  	chart := http.Dir("testdata/templates/logging-operator")
    84  
    85  	defaultValues, err := GetDefaultValues(chart)
    86  	require.NoError(t, err)
    87  
    88  	valuesMap := map[string]interface{}{}
    89  	err = yaml.Unmarshal(defaultValues, &valuesMap)
    90  	require.NoError(t, err)
    91  
    92  	scheme := runtime.NewScheme()
    93  	_ = clientgoscheme.AddToScheme(scheme)
    94  
    95  	objects, err := Render(chart, valuesMap, ReleaseOptions{
    96  		Name:      "release-name",
    97  		Namespace: "release-namespace",
    98  		Scheme: scheme,
    99  	}, "logging-operator")
   100  	require.NoError(t, err)
   101  
   102  	assert.Len(t, objects, 1)
   103  
   104  	_, ok := objects[0].(*v1.ServiceAccount)
   105  	assert.True(t, ok, "object should be a ServiceAccount")
   106  }