...

Source file src/sigs.k8s.io/kustomize/api/krusty/configmaps_test.go

Documentation: sigs.k8s.io/kustomize/api/krusty

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package krusty_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
    10  )
    11  
    12  // Numbers and booleans are quoted
    13  func TestGeneratorIntVsStringNoMerge(t *testing.T) {
    14  	th := kusttest_test.MakeHarness(t)
    15  	th.WriteK(".", `
    16  resources:
    17  - service.yaml
    18  configMapGenerator:
    19  - name: bob
    20    literals:
    21    - fruit=Indian Gooseberry
    22    - year=2020
    23    - crisis=true
    24  `)
    25  	th.WriteF("service.yaml", `
    26  apiVersion: v1
    27  kind: Service
    28  metadata:
    29    name: demo
    30  spec:
    31    clusterIP: None
    32  `)
    33  	m := th.Run(".", th.MakeDefaultOptions())
    34  	th.AssertActualEqualsExpected(
    35  		m, `
    36  apiVersion: v1
    37  kind: Service
    38  metadata:
    39    name: demo
    40  spec:
    41    clusterIP: None
    42  ---
    43  apiVersion: v1
    44  data:
    45    crisis: "true"
    46    fruit: Indian Gooseberry
    47    year: "2020"
    48  kind: ConfigMap
    49  metadata:
    50    name: bob-79t79mt227
    51  `)
    52  }
    53  
    54  func TestGeneratorIntVsStringWithMerge(t *testing.T) {
    55  	th := kusttest_test.MakeHarness(t)
    56  	th.WriteK("base", `
    57  configMapGenerator:
    58  - name: bob
    59    literals:
    60    - fruit=Indian Gooseberry
    61    - year=2020
    62    - crisis=true
    63  `)
    64  	th.WriteK("overlay", `
    65  resources:
    66  - ../base
    67  configMapGenerator:
    68  - name: bob
    69    behavior: merge
    70    literals:
    71    - month=12
    72  `)
    73  	m := th.Run("overlay", th.MakeDefaultOptions())
    74  	th.AssertActualEqualsExpected(m, `apiVersion: v1
    75  data:
    76    crisis: "true"
    77    fruit: Indian Gooseberry
    78    month: "12"
    79    year: "2020"
    80  kind: ConfigMap
    81  metadata:
    82    name: bob-bk46gm59c6
    83  `)
    84  }
    85  
    86  func TestGeneratorFromProperties(t *testing.T) {
    87  	th := kusttest_test.MakeHarness(t)
    88  	th.WriteK("base", `
    89  configMapGenerator:
    90    - name: test-configmap
    91      behavior: create
    92      envs:
    93      - properties
    94  `)
    95  	th.WriteF("base/properties", `
    96  VAR1=100
    97  `)
    98  	th.WriteK("overlay", `
    99  resources:
   100  - ../base
   101  configMapGenerator:
   102  - name: test-configmap
   103    behavior: "merge"
   104    envs:
   105    - properties
   106  `)
   107  	th.WriteF("overlay/properties", `
   108  VAR2=200
   109  `)
   110  	m := th.Run("overlay", th.MakeDefaultOptions())
   111  	th.AssertActualEqualsExpected(m, `apiVersion: v1
   112  data:
   113    VAR1: "100"
   114    VAR2: "200"
   115  kind: ConfigMap
   116  metadata:
   117    name: test-configmap-hdghb5ddkg
   118  `)
   119  }
   120  
   121  // Generate a Secret and a ConfigMap from the same data
   122  // to compare the result.
   123  func TestGeneratorBasics(t *testing.T) {
   124  	th := kusttest_test.MakeHarness(t)
   125  	th.WriteK(".", `
   126  namePrefix: blah-
   127  configMapGenerator:
   128  - name: bob
   129    literals:
   130    - fruit=apple
   131    - vegetable=broccoli
   132    envs:
   133    - foo.env
   134    env: bar.env
   135    files:
   136    - passphrase=phrase.dat
   137    - forces.txt
   138  - name: json
   139    literals:
   140    - 'v2=[{"path": "var/druid/segment-cache"}]'
   141    - >-
   142      druid_segmentCache_locations=[{"path":
   143      "var/druid/segment-cache",
   144      "maxSize": 32000000000,
   145      "freeSpacePercent": 1.0}]
   146  secretGenerator:
   147  - name: bob
   148    literals:
   149    - fruit=apple
   150    - vegetable=broccoli
   151    envs:
   152    - foo.env
   153    files:
   154    - passphrase=phrase.dat
   155    - forces.txt
   156    env: bar.env
   157  `)
   158  	th.WriteF("foo.env", `
   159  MOUNTAIN=everest
   160  OCEAN=pacific
   161  `)
   162  	th.WriteF("bar.env", `
   163  BIRD=falcon
   164  `)
   165  	th.WriteF("phrase.dat", `
   166  Life is short.
   167  But the years are long.
   168  Not while the evil days come not.
   169  `)
   170  	th.WriteF("forces.txt", `
   171  gravitational
   172  electromagnetic
   173  strong nuclear
   174  weak nuclear
   175  `)
   176  	opts := th.MakeDefaultOptions()
   177  	m := th.Run(".", opts)
   178  	th.AssertActualEqualsExpected(
   179  		m, `
   180  apiVersion: v1
   181  data:
   182    BIRD: falcon
   183    MOUNTAIN: everest
   184    OCEAN: pacific
   185    forces.txt: |2
   186  
   187      gravitational
   188      electromagnetic
   189      strong nuclear
   190      weak nuclear
   191    fruit: apple
   192    passphrase: |2
   193  
   194      Life is short.
   195      But the years are long.
   196      Not while the evil days come not.
   197    vegetable: broccoli
   198  kind: ConfigMap
   199  metadata:
   200    name: blah-bob-g9df72cd5b
   201  ---
   202  apiVersion: v1
   203  data:
   204    druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize": 32000000000,
   205      "freeSpacePercent": 1.0}]'
   206    v2: '[{"path": "var/druid/segment-cache"}]'
   207  kind: ConfigMap
   208  metadata:
   209    name: blah-json-m8529t979f
   210  ---
   211  apiVersion: v1
   212  data:
   213    BIRD: ZmFsY29u
   214    MOUNTAIN: ZXZlcmVzdA==
   215    OCEAN: cGFjaWZpYw==
   216    forces.txt: |
   217      CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbn
   218      VjbGVhcgo=
   219    fruit: YXBwbGU=
   220    passphrase: |
   221      CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aG
   222      UgZXZpbCBkYXlzIGNvbWUgbm90Lgo=
   223    vegetable: YnJvY2NvbGk=
   224  kind: Secret
   225  metadata:
   226    name: blah-bob-58g62h555c
   227  type: Opaque
   228  `)
   229  }
   230  
   231  // TODO: This should be an error instead. However, we can't strict unmarshal until we have a yaml
   232  // lib that support case-insensitive keys and anchors.
   233  // See https://github.com/kubernetes-sigs/kustomize/issues/5061
   234  func TestGeneratorRepeatsInKustomization(t *testing.T) {
   235  	th := kusttest_test.MakeHarness(t)
   236  	th.WriteK(".", `
   237  namePrefix: blah-
   238  configMapGenerator:
   239  - name: bob
   240    behavior: create
   241    literals:
   242    - bean=pinto
   243    - star=wolf-rayet
   244    literals:
   245    - fruit=apple
   246    - vegetable=broccoli
   247    files:
   248    - forces.txt
   249    files:
   250    - nobles=nobility.txt
   251  `)
   252  	th.WriteF("forces.txt", `
   253  gravitational
   254  electromagnetic
   255  strong nuclear
   256  weak nuclear
   257  `)
   258  	th.WriteF("nobility.txt", `
   259  helium
   260  neon
   261  argon
   262  krypton
   263  xenon
   264  radon
   265  `)
   266  	m := th.Run(".", th.MakeDefaultOptions())
   267  	th.AssertActualEqualsExpected(m, `
   268  apiVersion: v1
   269  data:
   270    fruit: apple
   271    nobles: |2
   272  
   273      helium
   274      neon
   275      argon
   276      krypton
   277      xenon
   278      radon
   279    vegetable: broccoli
   280  kind: ConfigMap
   281  metadata:
   282    name: blah-bob-db529cg5bk
   283  `)
   284  }
   285  
   286  func TestIssue3393(t *testing.T) {
   287  	th := kusttest_test.MakeHarness(t)
   288  	th.WriteK(".", `
   289  resources:
   290  - cm.yaml
   291  configMapGenerator:
   292    - name: project
   293      behavior: merge
   294      literals:
   295      - ANOTHER_ENV_VARIABLE="bar"
   296  `)
   297  	th.WriteF("cm.yaml", `
   298  apiVersion: v1
   299  kind: ConfigMap
   300  metadata:
   301    name: project
   302  data:
   303    A_FIRST_ENV_VARIABLE: "foo"
   304  `)
   305  	m := th.Run(".", th.MakeDefaultOptions())
   306  	th.AssertActualEqualsExpected(m, `
   307  apiVersion: v1
   308  data:
   309    A_FIRST_ENV_VARIABLE: foo
   310    ANOTHER_ENV_VARIABLE: bar
   311  kind: ConfigMap
   312  metadata:
   313    name: project
   314  `)
   315  }
   316  
   317  func TestGeneratorSimpleOverlay(t *testing.T) {
   318  	th := kusttest_test.MakeHarness(t)
   319  	th.WriteK("base", `
   320  namePrefix: p-
   321  configMapGenerator:
   322  - name: cm
   323    behavior: create
   324    literals:
   325    - fruit=apple
   326  `)
   327  	th.WriteK("overlay", `
   328  resources:
   329  - ../base
   330  configMapGenerator:
   331  - name: cm
   332    behavior: merge
   333    literals:
   334    - veggie=broccoli
   335  `)
   336  	m := th.Run("overlay", th.MakeDefaultOptions())
   337  	th.AssertActualEqualsExpected(m, `
   338  apiVersion: v1
   339  data:
   340    fruit: apple
   341    veggie: broccoli
   342  kind: ConfigMap
   343  metadata:
   344    name: p-cm-877mt5hc89
   345  `)
   346  }
   347  
   348  var binaryHello = []byte{
   349  	0xff, // non-utf8
   350  	0x68, // h
   351  	0x65, // e
   352  	0x6c, // l
   353  	0x6c, // l
   354  	0x6f, // o
   355  }
   356  
   357  func manyHellos(count int) (result []byte) {
   358  	for i := 0; i < count; i++ {
   359  		result = append(result, binaryHello...)
   360  	}
   361  	return
   362  }
   363  
   364  func TestGeneratorOverlaysBinaryData(t *testing.T) {
   365  	th := kusttest_test.MakeHarness(t)
   366  	th.WriteF("base/data.bin", string(manyHellos(30)))
   367  	th.WriteK("base", `
   368  namePrefix: p1-
   369  configMapGenerator:
   370  - name: com1
   371    behavior: create
   372    files:
   373    - data.bin
   374  `)
   375  	th.WriteK("overlay", `
   376  resources:
   377  - ../base
   378  configMapGenerator:
   379  - name: com1
   380    behavior: merge
   381  `)
   382  	m := th.Run("overlay", th.MakeDefaultOptions())
   383  	th.AssertActualEqualsExpected(m, `
   384  apiVersion: v1
   385  binaryData:
   386    data.bin: |
   387      /2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbG
   388      xv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hl
   389      bGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2
   390      hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
   391  kind: ConfigMap
   392  metadata:
   393    name: p1-com1-96gmmt6gt5
   394  `)
   395  }
   396  
   397  func TestGeneratorOverlays(t *testing.T) {
   398  	th := kusttest_test.MakeHarness(t)
   399  	th.WriteK("base1", `
   400  namePrefix: p1-
   401  configMapGenerator:
   402  - name: com1
   403    behavior: create
   404    literals:
   405    - from=base
   406  `)
   407  	th.WriteK("base2", `
   408  namePrefix: p2-
   409  configMapGenerator:
   410  - name: com2
   411    behavior: create
   412    literals:
   413    - from=base
   414  `)
   415  	th.WriteK("overlay/o1", `
   416  resources:
   417  - ../../base1
   418  configMapGenerator:
   419  - name: com1
   420    behavior: merge
   421    literals:
   422    - from=overlay
   423  `)
   424  	th.WriteK("overlay/o2", `
   425  resources:
   426  - ../../base2
   427  configMapGenerator:
   428  - name: com2
   429    behavior: merge
   430    literals:
   431    - from=overlay
   432  `)
   433  	th.WriteK("overlay", `
   434  resources:
   435  - o1
   436  - o2
   437  configMapGenerator:
   438  - name: com1
   439    behavior: merge
   440    literals:
   441    - foo=bar
   442    - baz=qux
   443  `)
   444  	m := th.Run("overlay", th.MakeDefaultOptions())
   445  	th.AssertActualEqualsExpected(m, `
   446  apiVersion: v1
   447  data:
   448    baz: qux
   449    foo: bar
   450    from: overlay
   451  kind: ConfigMap
   452  metadata:
   453    name: p1-com1-8tc62428t2
   454  ---
   455  apiVersion: v1
   456  data:
   457    from: overlay
   458  kind: ConfigMap
   459  metadata:
   460    name: p2-com2-87mcggf7d7
   461  `)
   462  }
   463  
   464  func TestConfigMapGeneratorMergeNamePrefix(t *testing.T) {
   465  	th := kusttest_test.MakeHarness(t)
   466  	th.WriteK("base", `
   467  configMapGenerator:
   468  - name: cm
   469    behavior: create
   470    literals:
   471    - foo=bar
   472  `)
   473  	th.WriteK("o1", `
   474  resources:
   475  - ../base
   476  namePrefix: o1-
   477  `)
   478  	th.WriteK("o2", `
   479  resources:
   480  - ../base
   481  nameSuffix: -o2
   482  `)
   483  	th.WriteK(".", `
   484  resources:
   485  - o1
   486  - o2
   487  configMapGenerator:
   488  - name: o1-cm
   489    behavior: merge
   490    literals:
   491    - big=bang
   492  - name: cm-o2
   493    behavior: merge
   494    literals:
   495    - big=crunch
   496  `)
   497  	m := th.Run(".", th.MakeDefaultOptions())
   498  	th.AssertActualEqualsExpected(m, `
   499  apiVersion: v1
   500  data:
   501    big: bang
   502    foo: bar
   503  kind: ConfigMap
   504  metadata:
   505    name: o1-cm-ft9mmdc8c6
   506  ---
   507  apiVersion: v1
   508  data:
   509    big: crunch
   510    foo: bar
   511  kind: ConfigMap
   512  metadata:
   513    name: cm-o2-5k95kd76ft
   514  `)
   515  }
   516  
   517  func TestConfigMapGeneratorLiteralNewline(t *testing.T) {
   518  	th := kusttest_test.MakeHarness(t)
   519  	th.WriteK(".", `
   520  generators:
   521  - configmaps.yaml
   522  `)
   523  	th.WriteF("configmaps.yaml", `
   524  apiVersion: builtin
   525  kind: ConfigMapGenerator
   526  metadata:
   527    name: testing
   528  literals:
   529    - |
   530      initial.txt=greetings
   531      everyone
   532    - |
   533      final.txt=different
   534      behavior
   535  ---
   536  `)
   537  	m := th.Run(".", th.MakeDefaultOptions())
   538  	th.AssertActualEqualsExpected(
   539  		m, `
   540  apiVersion: v1
   541  data:
   542    final.txt: |
   543      different
   544      behavior
   545    initial.txt: |
   546      greetings
   547      everyone
   548  kind: ConfigMap
   549  metadata:
   550    name: testing-tt4769fb52
   551  `)
   552  }
   553  
   554  // regression test for https://github.com/kubernetes-sigs/kustomize/issues/4233
   555  func TestDataEndsWithQuotes(t *testing.T) {
   556  	th := kusttest_test.MakeHarness(t)
   557  	th.WriteK(".", `
   558  configMapGenerator:
   559    - name: test
   560      literals:
   561        - TEST=this is a 'test'
   562  `)
   563  
   564  	m := th.Run(".", th.MakeDefaultOptions())
   565  	th.AssertActualEqualsExpected(
   566  		m, `apiVersion: v1
   567  data:
   568    TEST: this is a 'test'
   569  kind: ConfigMap
   570  metadata:
   571    name: test-k9cc55dfm5
   572  `)
   573  }
   574  
   575  func TestDataIsSingleQuote(t *testing.T) {
   576  	th := kusttest_test.MakeHarness(t)
   577  	th.WriteK(".", `
   578  configMapGenerator:
   579    - name: test
   580      literals:
   581        - TEST='
   582  `)
   583  
   584  	m := th.Run(".", th.MakeDefaultOptions())
   585  	th.AssertActualEqualsExpected(
   586  		m, `apiVersion: v1
   587  data:
   588    TEST: ''''
   589  kind: ConfigMap
   590  metadata:
   591    name: test-m8t7bmb6g2
   592  `)
   593  }
   594  
   595  // Regression test for https://github.com/kubernetes-sigs/kustomize/issues/5047
   596  func TestPrefixSuffix(t *testing.T) {
   597  	th := kusttest_test.MakeHarness(t)
   598  	th.WriteF("kustomization.yaml", `
   599  resources:
   600  - a
   601  - b
   602  `)
   603  
   604  	th.WriteF("a/kustomization.yaml", `
   605  resources:
   606  - ../common
   607  
   608  namePrefix: a
   609  `)
   610  
   611  	th.WriteF("b/kustomization.yaml", `
   612  resources:
   613  - ../common
   614  
   615  namePrefix: b
   616  `)
   617  
   618  	th.WriteF("common/kustomization.yaml", `
   619  resources:
   620  - service
   621  
   622  configMapGenerator:
   623  - name: "-example-configmap"
   624  `)
   625  
   626  	th.WriteF("common/service/deployment.yaml", `
   627  kind: Deployment
   628  apiVersion: apps/v1
   629  
   630  metadata:
   631    name: "-"
   632  
   633  spec:
   634    template:
   635      spec:
   636        containers:
   637        - name: app
   638          envFrom:
   639          - configMapRef:
   640              name: "-example-configmap"
   641  `)
   642  
   643  	th.WriteF("common/service/kustomization.yaml", `
   644  resources:
   645  - deployment.yaml
   646  
   647  nameSuffix: api
   648  `)
   649  
   650  	m := th.Run(".", th.MakeDefaultOptions())
   651  	th.AssertActualEqualsExpected(m, `
   652  apiVersion: apps/v1
   653  kind: Deployment
   654  metadata:
   655    name: a-api
   656  spec:
   657    template:
   658      spec:
   659        containers:
   660        - envFrom:
   661          - configMapRef:
   662              name: a-example-configmap-6ct58987ht
   663          name: app
   664  ---
   665  apiVersion: v1
   666  kind: ConfigMap
   667  metadata:
   668    name: a-example-configmap-6ct58987ht
   669  ---
   670  apiVersion: apps/v1
   671  kind: Deployment
   672  metadata:
   673    name: b-api
   674  spec:
   675    template:
   676      spec:
   677        containers:
   678        - envFrom:
   679          - configMapRef:
   680              name: b-example-configmap-6ct58987ht
   681          name: app
   682  ---
   683  apiVersion: v1
   684  kind: ConfigMap
   685  metadata:
   686    name: b-example-configmap-6ct58987ht
   687  `)
   688  }
   689  
   690  // Regression test for https://github.com/kubernetes-sigs/kustomize/issues/5047
   691  func TestPrefixSuffix2(t *testing.T) {
   692  	th := kusttest_test.MakeHarness(t)
   693  	th.WriteF("kustomization.yaml", `
   694  resources:
   695  - a
   696  - b
   697  `)
   698  
   699  	th.WriteF("a/kustomization.yaml", `
   700  resources:
   701  - ../common
   702  
   703  namePrefix: a
   704  `)
   705  
   706  	th.WriteF("b/kustomization.yaml", `
   707  resources:
   708  - ../common
   709  
   710  namePrefix: b
   711  `)
   712  
   713  	th.WriteF("common/deployment.yaml", `
   714  apiVersion: apps/v1
   715  kind: Deployment
   716  metadata:
   717    name: "-example"
   718  spec:
   719    template:
   720      spec:
   721        containers:
   722        - name: app
   723          envFrom:
   724          - configMapRef:
   725              name: "-example-configmap"
   726  `)
   727  
   728  	th.WriteF("common/kustomization.yaml", `
   729  resources:
   730  - deployment.yaml
   731  
   732  configMapGenerator:
   733  - name: "-example-configmap"
   734  `)
   735  
   736  	m := th.Run(".", th.MakeDefaultOptions())
   737  	th.AssertActualEqualsExpected(m, `
   738  apiVersion: apps/v1
   739  kind: Deployment
   740  metadata:
   741    name: a-example
   742  spec:
   743    template:
   744      spec:
   745        containers:
   746        - envFrom:
   747          - configMapRef:
   748              name: a-example-configmap-6ct58987ht
   749          name: app
   750  ---
   751  apiVersion: v1
   752  kind: ConfigMap
   753  metadata:
   754    name: a-example-configmap-6ct58987ht
   755  ---
   756  apiVersion: apps/v1
   757  kind: Deployment
   758  metadata:
   759    name: b-example
   760  spec:
   761    template:
   762      spec:
   763        containers:
   764        - envFrom:
   765          - configMapRef:
   766              name: b-example-configmap-6ct58987ht
   767          name: app
   768  ---
   769  apiVersion: v1
   770  kind: ConfigMap
   771  metadata:
   772    name: b-example-configmap-6ct58987ht
   773  `)
   774  }
   775  

View as plain text