...

Source file src/k8s.io/cli-runtime/pkg/resource/kustomizevisitor_test.go

Documentation: k8s.io/cli-runtime/pkg/resource

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     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 resource
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/util/dump"
    23  	"sigs.k8s.io/kustomize/api/konfig"
    24  	"sigs.k8s.io/kustomize/kyaml/filesys"
    25  )
    26  
    27  const (
    28  	kustomizationContent1 = `
    29  apiVersion: kustomize.config.k8s.io/v1beta1
    30  kind: Kustomization
    31  namePrefix: foo-
    32  nameSuffix: -bar
    33  namespace: ns1
    34  commonLabels:
    35    app: nginx
    36  commonAnnotations:
    37    note: This is a test annotation
    38  resources:
    39    - deployment.yaml
    40    - namespace.yaml
    41  configMapGenerator:
    42  - name: literalConfigMap
    43    literals:
    44    - DB_USERNAME=admin
    45    - DB_PASSWORD=somepw
    46  secretGenerator:
    47  - name: secret
    48    literals:
    49      - DB_USERNAME=admin
    50      - DB_PASSWORD=somepw
    51    type: Opaque
    52  patchesJson6902:
    53  - target:
    54      group: apps
    55      version: v1
    56      kind: Deployment
    57      name: dply1
    58    path: jsonpatch.json
    59  `
    60  	deploymentContent = `
    61  apiVersion: apps/v1
    62  metadata:
    63    name: dply1
    64  kind: Deployment
    65  `
    66  	namespaceContent = `
    67  apiVersion: v1
    68  kind: Namespace
    69  metadata:
    70    name: ns1
    71  `
    72  	jsonpatchContent = `[
    73      {"op": "add", "path": "/spec/replica", "value": "3"}
    74  ]`
    75  
    76  	expectedContent = `apiVersion: v1
    77  kind: Namespace
    78  metadata:
    79    annotations:
    80      note: This is a test annotation
    81    labels:
    82      app: nginx
    83    name: ns1
    84  ---
    85  apiVersion: v1
    86  data:
    87    DB_PASSWORD: somepw
    88    DB_USERNAME: admin
    89  kind: ConfigMap
    90  metadata:
    91    annotations:
    92      note: This is a test annotation
    93    labels:
    94      app: nginx
    95    name: foo-literalConfigMap-bar-g5f6t456f5
    96    namespace: ns1
    97  ---
    98  apiVersion: v1
    99  data:
   100    DB_PASSWORD: c29tZXB3
   101    DB_USERNAME: YWRtaW4=
   102  kind: Secret
   103  metadata:
   104    annotations:
   105      note: This is a test annotation
   106    labels:
   107      app: nginx
   108    name: foo-secret-bar-82c2g5f8f6
   109    namespace: ns1
   110  type: Opaque
   111  ---
   112  apiVersion: apps/v1
   113  kind: Deployment
   114  metadata:
   115    annotations:
   116      note: This is a test annotation
   117    labels:
   118      app: nginx
   119    name: foo-dply1-bar
   120    namespace: ns1
   121  spec:
   122    replica: "3"
   123    selector:
   124      matchLabels:
   125        app: nginx
   126    template:
   127      metadata:
   128        annotations:
   129          note: This is a test annotation
   130        labels:
   131          app: nginx
   132  `
   133  )
   134  
   135  func TestKustomizeVisitor(t *testing.T) {
   136  	fSys := filesys.MakeFsInMemory()
   137  	fSys.WriteFile(
   138  		konfig.DefaultKustomizationFileName(),
   139  		[]byte(kustomizationContent1))
   140  	fSys.WriteFile("deployment.yaml", []byte(deploymentContent))
   141  	fSys.WriteFile("namespace.yaml", []byte(namespaceContent))
   142  	fSys.WriteFile("jsonpatch.json", []byte(jsonpatchContent))
   143  	b := newDefaultBuilder()
   144  	kv := KustomizeVisitor{
   145  		mapper:  b.mapper,
   146  		dirPath: ".",
   147  		schema:  b.schema,
   148  		fSys:    fSys,
   149  	}
   150  	tv := &testVisitor{}
   151  	if err := kv.Visit(tv.Handle); err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	if len(tv.Infos) != 4 {
   155  		t.Fatal(dump.Pretty(tv.Infos))
   156  	}
   157  	if string(kv.yml) != expectedContent {
   158  		t.Fatalf("expected:\n%s\nbut got:\n%s", expectedContent, string(kv.yml))
   159  	}
   160  }
   161  

View as plain text