github.com/splunk/qbec@v0.15.2/vm/internal/natives/helm_test.go (about)

     1  /*
     2     Copyright 2021 Splunk Inc.
     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 natives
    18  
    19  import (
    20  	"encoding/json"
    21  	"sort"
    22  	"testing"
    23  
    24  	"github.com/google/go-jsonnet"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestHelmOptions(t *testing.T) {
    30  	a := assert.New(t)
    31  	var h helmOptions
    32  	a.Nil(h.toArgs())
    33  	h = helmOptions{
    34  		Execute:     []string{"a.yaml", "b.yaml"},
    35  		KubeVersion: "1.10",
    36  		Name:        "foo",
    37  		Namespace:   "foobar",
    38  		ThisFile:    "/path/to/my.jsonnet",
    39  		Verbose:     true,
    40  	}
    41  	a.EqualValues([]string{
    42  		"--execute", "a.yaml",
    43  		"--execute", "b.yaml",
    44  		"--kube-version", "1.10",
    45  		"--name", "foo",
    46  		"--namespace", "foobar",
    47  	}, h.toArgs())
    48  }
    49  
    50  type cmOrSecret struct {
    51  	APIVersion string `json:"apiVersion"`
    52  	Kind       string `json:"kind"`
    53  	Metadata   struct {
    54  		Namespace string `json:"namespace"`
    55  		Name      string `json:"name"`
    56  	}
    57  	Data map[string]string `json:"data"`
    58  }
    59  
    60  func TestHelmSimpleExpand(t *testing.T) {
    61  	a := assert.New(t)
    62  	jvm := jsonnet.MakeVM()
    63  	Register(jvm)
    64  	code, err := jvm.EvaluateFile("./testdata/consumer.jsonnet")
    65  	require.Nil(t, err)
    66  
    67  	var output []cmOrSecret
    68  	err = json.Unmarshal([]byte(code), &output)
    69  	require.Nil(t, err)
    70  
    71  	require.Equal(t, 2, len(output))
    72  
    73  	sort.Slice(output, func(i, j int) bool {
    74  		return output[i].Kind < output[j].Kind
    75  	})
    76  
    77  	ob := output[0]
    78  	a.Equal("ConfigMap", ob.Kind)
    79  	a.Equal("my-ns", ob.Metadata.Namespace)
    80  	a.Equal("my-name", ob.Metadata.Name)
    81  	a.Equal("barbar", ob.Data["foo"])
    82  	a.Equal("baz", ob.Data["bar"])
    83  
    84  	ob = output[1]
    85  	a.Equal("Secret", ob.Kind)
    86  	a.Equal("my-ns", ob.Metadata.Namespace)
    87  	a.Equal("my-name", ob.Metadata.Name)
    88  	a.Equal("Y2hhbmdlbWUK", ob.Data["secret"])
    89  }
    90  
    91  func TestHelmBadRelative(t *testing.T) {
    92  	a := assert.New(t)
    93  	jvm := jsonnet.MakeVM()
    94  	Register(jvm)
    95  	_, err := jvm.EvaluateFile("./testdata/bad-relative.jsonnet")
    96  	require.NotNil(t, err)
    97  	a.Contains(err.Error(), "exit status 1")
    98  }