github.imxd.top/operator-framework/operator-sdk@v0.8.2/pkg/helm/engine/ownerref_test.go (about)

     1  // Copyright 2018 The Operator-SDK 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 engine
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/helm/pkg/chartutil"
    23  	"k8s.io/helm/pkg/proto/hapi/chart"
    24  )
    25  
    26  type mockEngine struct {
    27  	out map[string]string
    28  }
    29  
    30  func (e *mockEngine) Render(chrt *chart.Chart, v chartutil.Values) (map[string]string, error) {
    31  	return e.out, nil
    32  }
    33  
    34  func TestOwnerRefEngine(t *testing.T) {
    35  	ownerRefs := []metav1.OwnerReference{
    36  		{
    37  			APIVersion: "v1",
    38  			Kind:       "Test",
    39  			Name:       "test",
    40  			UID:        "123",
    41  		},
    42  	}
    43  
    44  	baseOut := `apiVersion: stable.nicolerenee.io/v1
    45  kind: Character
    46  metadata:
    47    name: nemo
    48  spec:
    49    Name: Nemo
    50  `
    51  
    52  	expectedOut := `---
    53  apiVersion: stable.nicolerenee.io/v1
    54  kind: Character
    55  metadata:
    56    name: nemo
    57    ownerReferences:
    58    - apiVersion: v1
    59      kind: Test
    60      name: test
    61      uid: "123"
    62  spec:
    63    Name: Nemo
    64  `
    65  	expected := map[string]string{"template.yaml": expectedOut, "template2.yaml": expectedOut}
    66  
    67  	baseEngineOutput := map[string]string{
    68  		"template.yaml":  baseOut,
    69  		"template2.yaml": baseOut,
    70  		"empty.yaml":     "",
    71  		"comment.yaml":   "# This is empty",
    72  	}
    73  
    74  	engine := NewOwnerRefEngine(&mockEngine{out: baseEngineOutput}, ownerRefs)
    75  	out, err := engine.Render(&chart.Chart{}, map[string]interface{}{})
    76  	require.NoError(t, err)
    77  	require.EqualValues(t, expected, out)
    78  }
    79  
    80  func TestOwnerRefEngine_MultiDocumentYaml(t *testing.T) {
    81  	ownerRefs := []metav1.OwnerReference{
    82  		{
    83  			APIVersion: "v1",
    84  			Kind:       "Test",
    85  			Name:       "test",
    86  			UID:        "123",
    87  		},
    88  	}
    89  
    90  	baseOut := `kind: ConfigMap
    91  apiVersion: v1
    92  metadata:
    93    name: eighth
    94    data:
    95      name: value
    96  ---
    97  apiVersion: v1
    98  kind: Pod
    99  metadata:
   100    name: example-test
   101  `
   102  
   103  	expectedOut := `---
   104  apiVersion: v1
   105  kind: ConfigMap
   106  metadata:
   107    data:
   108      name: value
   109    name: eighth
   110    ownerReferences:
   111    - apiVersion: v1
   112      kind: Test
   113      name: test
   114      uid: "123"
   115  ---
   116  apiVersion: v1
   117  kind: Pod
   118  metadata:
   119    name: example-test
   120    ownerReferences:
   121    - apiVersion: v1
   122      kind: Test
   123      name: test
   124      uid: "123"
   125  `
   126  
   127  	expected := map[string]string{"template.yaml": expectedOut}
   128  
   129  	baseEngineOutput := map[string]string{
   130  		"template.yaml": baseOut,
   131  	}
   132  
   133  	engine := NewOwnerRefEngine(&mockEngine{out: baseEngineOutput}, ownerRefs)
   134  	out, err := engine.Render(&chart.Chart{}, map[string]interface{}{})
   135  
   136  	require.NoError(t, err)
   137  	require.Equal(t, expected, out)
   138  }