github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/fnruntime/utils_test.go (about)

     1  // Copyright 2021 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 fnruntime
    16  
    17  import (
    18  	"testing"
    19  
    20  	kptfile "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
    21  	"github.com/stretchr/testify/assert"
    22  	"sigs.k8s.io/kustomize/kyaml/yaml"
    23  )
    24  
    25  func TestIsMatch(t *testing.T) {
    26  	tests := []struct {
    27  		name     string
    28  		input    string
    29  		selector kptfile.Selector
    30  		expected bool
    31  	}{
    32  		{
    33  			name: "kind match",
    34  			input: `apiVersion: apps/v1
    35  kind: Deployment
    36  metadata:
    37    name: nginx-deployment
    38    annotations:
    39      internal.config.k8s.io/kpt-resource-id: "0"
    40  spec:
    41    replicas: 3`,
    42  			selector: kptfile.Selector{
    43  				Kind: "Deployment",
    44  			},
    45  			expected: true,
    46  		},
    47  		{
    48  			name: "name match",
    49  			input: `apiVersion: apps/v1
    50  kind: Deployment
    51  metadata:
    52    name: nginx-deployment
    53    annotations:
    54      internal.config.k8s.io/kpt-resource-id: "0"
    55  spec:
    56    replicas: 3`,
    57  			selector: kptfile.Selector{
    58  				Name: "nginx-deployment",
    59  			},
    60  			expected: true,
    61  		},
    62  		{
    63  			name: "namespace match",
    64  			input: `apiVersion: apps/v1
    65  kind: Deployment
    66  metadata:
    67    name: nginx-deployment
    68    namespace: staging
    69    annotations:
    70      internal.config.k8s.io/kpt-resource-id: "0"
    71  spec:
    72    replicas: 3`,
    73  			selector: kptfile.Selector{
    74  				Namespace: "staging",
    75  			},
    76  			expected: true,
    77  		},
    78  		{
    79  			name: "apiVersion match",
    80  			input: `apiVersion: apps/v1
    81  kind: Deployment
    82  metadata:
    83    name: nginx-deployment
    84    annotations:
    85      internal.config.k8s.io/kpt-resource-id: "0"
    86  spec:
    87    replicas: 3`,
    88  			selector: kptfile.Selector{
    89  				APIVersion: "apps/v1",
    90  			},
    91  			expected: true,
    92  		},
    93  		{
    94  			name: "GVKNN match",
    95  			input: `apiVersion: apps/v1
    96  kind: Deployment
    97  metadata:
    98    name: nginx-deployment
    99    namespace: staging
   100    annotations:
   101      internal.config.k8s.io/kpt-resource-id: "0"
   102  spec:
   103    replicas: 3`,
   104  			selector: kptfile.Selector{
   105  				Name:       "nginx-deployment",
   106  				Namespace:  "staging",
   107  				Kind:       "Deployment",
   108  				APIVersion: "apps/v1",
   109  			},
   110  			expected: true,
   111  		},
   112  		{
   113  			name: "namespace not matched but rest did",
   114  			input: `apiVersion: apps/v1
   115  kind: Deployment
   116  metadata:
   117    name: nginx-deployment
   118    namespace: staging
   119    annotations:
   120      internal.config.k8s.io/kpt-resource-id: "0"
   121  spec:
   122    replicas: 3`,
   123  			selector: kptfile.Selector{
   124  				Name:       "nginx-deployment",
   125  				Namespace:  "prod",
   126  				Kind:       "Deployment",
   127  				APIVersion: "apps/v1",
   128  			},
   129  			expected: false,
   130  		},
   131  	}
   132  
   133  	for i := range tests {
   134  		tc := tests[i]
   135  		t.Run(tc.name, func(t *testing.T) {
   136  			node, err := yaml.Parse(tc.input)
   137  			assert.NoError(t, err)
   138  			actual := isMatch(node, tc.selector)
   139  			assert.Equal(t, tc.expected, actual)
   140  		})
   141  	}
   142  }
   143  
   144  func TestNewConfigMap(t *testing.T) {
   145  	data := map[string]string{
   146  		"normal string": "abc",
   147  		"integer":       "8081",
   148  		"float":         "1.23",
   149  		"bool":          "true",
   150  	}
   151  	m, err := NewConfigMap(data)
   152  	assert.NoError(t, err)
   153  	mapAsString := m.MustString()
   154  	assert.Contains(t, mapAsString, `bool: "true"`)
   155  	assert.Contains(t, mapAsString, `normal string: abc`)
   156  	assert.Contains(t, mapAsString, `integer: "8081"`)
   157  	assert.Contains(t, mapAsString, `float: "1.23"`)
   158  }