github.com/splunk/dan1-qbec@v0.7.3/internal/vm/helm_test.go (about)

     1  package vm
     2  
     3  import (
     4  	"encoding/json"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestHelmOptions(t *testing.T) {
    13  	a := assert.New(t)
    14  	var h helmOptions
    15  	a.Nil(h.toArgs())
    16  	h = helmOptions{
    17  		Execute:     []string{"a.yaml", "b.yaml"},
    18  		KubeVersion: "1.10",
    19  		Name:        "foo",
    20  		Namespace:   "foobar",
    21  		ThisFile:    "/path/to/my.jsonnet",
    22  		Verbose:     true,
    23  	}
    24  	a.EqualValues([]string{
    25  		"--execute", "a.yaml",
    26  		"--execute", "b.yaml",
    27  		"--kube-version", "1.10",
    28  		"--name", "foo",
    29  		"--namespace", "foobar",
    30  	}, h.toArgs())
    31  }
    32  
    33  type cmOrSecret struct {
    34  	APIVersion string `json:"apiVersion"`
    35  	Kind       string `json:"kind"`
    36  	Metadata   struct {
    37  		Namespace string `json:"namespace"`
    38  		Name      string `json:"name"`
    39  	}
    40  	Data map[string]string `json:"data"`
    41  }
    42  
    43  func TestHelmSimpleExpand(t *testing.T) {
    44  	a := assert.New(t)
    45  	jvm := New(Config{})
    46  	file := "./consumer.jsonnet"
    47  	inputCode := `
    48  local expandHelmTemplate = std.native('expandHelmTemplate');
    49  
    50  expandHelmTemplate(
    51      './testdata/charts/foobar',
    52      {
    53          foo: 'barbar',
    54      },
    55      {
    56          namespace: 'my-ns',
    57          name: 'my-name',
    58          thisFile: std.thisFile,
    59  		verbose: true,
    60      }
    61  )
    62  `
    63  	code, err := jvm.EvaluateSnippet(file, inputCode)
    64  	require.Nil(t, err)
    65  
    66  	var output []cmOrSecret
    67  	err = json.Unmarshal([]byte(code), &output)
    68  	require.Nil(t, err)
    69  
    70  	require.Equal(t, 2, len(output))
    71  
    72  	sort.Slice(output, func(i, j int) bool {
    73  		return output[i].Kind < output[j].Kind
    74  	})
    75  
    76  	ob := output[0]
    77  	a.Equal("ConfigMap", ob.Kind)
    78  	a.Equal("my-ns", ob.Metadata.Namespace)
    79  	a.Equal("my-name", ob.Metadata.Name)
    80  	a.Equal("barbar", ob.Data["foo"])
    81  	a.Equal("baz", ob.Data["bar"])
    82  
    83  	ob = output[1]
    84  	a.Equal("Secret", ob.Kind)
    85  	a.Equal("my-ns", ob.Metadata.Namespace)
    86  	a.Equal("my-name", ob.Metadata.Name)
    87  	a.Equal("Y2hhbmdlbWUK", ob.Data["secret"])
    88  }
    89  
    90  func TestHelmBadRelative(t *testing.T) {
    91  	a := assert.New(t)
    92  	jvm := New(Config{})
    93  	file := "./consumer.jsonnet"
    94  	inputCode := `
    95  local expandHelmTemplate = std.native('expandHelmTemplate');
    96  
    97  expandHelmTemplate(
    98      './testdata/charts/foobar',
    99      {
   100          foo: 'barbar',
   101      },
   102      {
   103          namespace: 'my-ns',
   104          name: 'my-name',
   105  		verbose: true,
   106      }
   107  )
   108  `
   109  	_, err := jvm.EvaluateSnippet(file, inputCode)
   110  	require.NotNil(t, err)
   111  	a.Contains(err.Error(), "exit status 1")
   112  }