github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/render/executor_test.go (about)

     1  // Copyright 2022 Google LLC
     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 render
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/GoogleContainerTools/kpt/internal/fnruntime"
    22  	"github.com/stretchr/testify/assert"
    23  	"sigs.k8s.io/kustomize/kyaml/kio"
    24  )
    25  
    26  func TestPathRelToRoot(t *testing.T) {
    27  	tests := []struct {
    28  		name         string
    29  		rootPath     string
    30  		subPkgPath   string
    31  		resourcePath string
    32  		expected     string
    33  		errString    string
    34  	}{
    35  		{
    36  			name:         "root package with non absolute path",
    37  			rootPath:     "tmp",
    38  			subPkgPath:   "/tmp/a",
    39  			resourcePath: "c.yaml",
    40  			expected:     "",
    41  			errString:    fmt.Sprintf("root package path %q must be absolute", "tmp"),
    42  		},
    43  		{
    44  			name:         "subpackage with non absolute path",
    45  			rootPath:     "/tmp",
    46  			subPkgPath:   "tmp/a",
    47  			resourcePath: "c.yaml",
    48  			expected:     "",
    49  			errString:    fmt.Sprintf("subpackage path %q must be absolute", "tmp/a"),
    50  		},
    51  		{
    52  			name:         "resource in a subpackage",
    53  			rootPath:     "/tmp",
    54  			subPkgPath:   "/tmp/a",
    55  			resourcePath: "c.yaml",
    56  			expected:     "a/c.yaml",
    57  		},
    58  		{
    59  			name:         "resource exists in a deeply nested subpackage",
    60  			rootPath:     "/tmp",
    61  			subPkgPath:   "/tmp/a/b/c",
    62  			resourcePath: "c.yaml",
    63  			expected:     "a/b/c/c.yaml",
    64  		},
    65  		{
    66  			name:         "resource exists in a sub dir with same name as sub package",
    67  			rootPath:     "/tmp",
    68  			subPkgPath:   "/tmp/a",
    69  			resourcePath: "a/c.yaml",
    70  			expected:     "a/a/c.yaml",
    71  		},
    72  		{
    73  			name:         "subpackage is not a descendant of root package",
    74  			rootPath:     "/tmp",
    75  			subPkgPath:   "/a",
    76  			resourcePath: "c.yaml",
    77  			expected:     "",
    78  			errString:    fmt.Sprintf("subpackage %q is not a descendant of %q", "/a", "/tmp"),
    79  		},
    80  	}
    81  
    82  	for _, test := range tests {
    83  		tc := test
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			newPath, err := pathRelToRoot(tc.rootPath,
    86  				tc.subPkgPath, tc.resourcePath)
    87  			assert.Equal(t, newPath, tc.expected)
    88  			if tc.errString != "" {
    89  				assert.Contains(t, err.Error(), tc.errString)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestMergeWithInput(t *testing.T) {
    96  	tests := []struct {
    97  		name          string
    98  		input         string
    99  		selectedInput string
   100  		output        string
   101  		expected      string
   102  	}{
   103  		{
   104  			name: "simple input",
   105  			input: `apiVersion: apps/v1
   106  kind: Deployment
   107  metadata:
   108    name: nginx-deployment
   109    annotations:
   110      internal.config.k8s.io/kpt-resource-id: "0"
   111  spec:
   112    replicas: 3`,
   113  			selectedInput: `apiVersion: apps/v1
   114  kind: Deployment
   115  metadata:
   116    name: nginx-deployment
   117    annotations:
   118      internal.config.k8s.io/kpt-resource-id: "0"
   119  spec:
   120    replicas: 3`,
   121  			output: `apiVersion: apps/v1
   122  kind: Deployment
   123  metadata:
   124    name: nginx-deployment
   125    namespace: staging
   126    annotations:
   127      internal.config.k8s.io/kpt-resource-id: "0"
   128  spec:
   129    replicas: 3`,
   130  			expected: `apiVersion: apps/v1
   131  kind: Deployment
   132  metadata:
   133    name: nginx-deployment
   134    namespace: staging
   135    annotations:
   136      internal.config.k8s.io/kpt-resource-id: "0"
   137  spec:
   138    replicas: 3
   139  `,
   140  		},
   141  		{
   142  			name: "complex example with generation, transformation and deletion of resource",
   143  			input: `apiVersion: apps/v1
   144  kind: Deployment
   145  metadata:
   146    name: nginx-deployment-0
   147    annotations:
   148      internal.config.k8s.io/kpt-resource-id: "0"
   149  ---
   150  apiVersion: apps/v1
   151  kind: Deployment
   152  metadata:
   153    name: nginx-deployment-1
   154    annotations:
   155      internal.config.k8s.io/kpt-resource-id: "1"
   156  ---
   157  apiVersion: apps/v1
   158  kind: Deployment
   159  metadata:
   160    name: nginx-deployment-2
   161    annotations:
   162      internal.config.k8s.io/kpt-resource-id: "2"
   163  `,
   164  			selectedInput: `apiVersion: apps/v1
   165  kind: Deployment
   166  metadata:
   167    name: nginx-deployment-0
   168    annotations:
   169      internal.config.k8s.io/kpt-resource-id: "0"
   170  ---
   171  apiVersion: apps/v1
   172  kind: Deployment
   173  metadata:
   174    name: nginx-deployment-1
   175    annotations:
   176      internal.config.k8s.io/kpt-resource-id: "1"
   177  `,
   178  			output: `apiVersion: apps/v1
   179  kind: Deployment
   180  metadata:
   181    name: nginx-deployment-0
   182    namespace: staging # transformed
   183    annotations:
   184      internal.config.k8s.io/kpt-resource-id: "0"
   185  ---
   186  apiVersion: apps/v1 # generated resource
   187  kind: Deployment
   188  metadata:
   189    name: nginx-deployment-3
   190  `,
   191  			expected: `apiVersion: apps/v1
   192  kind: Deployment
   193  metadata:
   194    name: nginx-deployment-0
   195    namespace: staging # transformed
   196    annotations:
   197      internal.config.k8s.io/kpt-resource-id: "0"
   198  ---
   199  apiVersion: apps/v1
   200  kind: Deployment
   201  metadata:
   202    name: nginx-deployment-2
   203    annotations:
   204      internal.config.k8s.io/kpt-resource-id: "2"
   205  ---
   206  apiVersion: apps/v1 # generated resource
   207  kind: Deployment
   208  metadata:
   209    name: nginx-deployment-3
   210  `,
   211  		},
   212  	}
   213  
   214  	for i := range tests {
   215  		tc := tests[i]
   216  		t.Run(tc.name, func(t *testing.T) {
   217  			output, err := kio.ParseAll(tc.output)
   218  			assert.NoError(t, err)
   219  			selectedInput, err := kio.ParseAll(tc.selectedInput)
   220  			assert.NoError(t, err)
   221  			input, err := kio.ParseAll(tc.input)
   222  			assert.NoError(t, err)
   223  			result := fnruntime.MergeWithInput(output, selectedInput, input)
   224  			actual, err := kio.StringAll(result)
   225  			assert.NoError(t, err)
   226  			assert.Equal(t, tc.expected, actual)
   227  		})
   228  	}
   229  }