...

Source file src/sigs.k8s.io/kustomize/api/krusty/baseandoverlaymedium_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  func writeMediumBase(th kusttest_test.Harness) {
    13  	th.WriteK("base", `
    14  namePrefix: baseprefix-
    15  commonLabels:
    16    foo: bar
    17  commonAnnotations:
    18    baseAnno: This is a base annotation
    19  resources:
    20  - deployment/deployment.yaml
    21  - service/service.yaml
    22  `)
    23  	th.WriteF("base/service/service.yaml", `
    24  apiVersion: v1
    25  kind: Service
    26  metadata:
    27    name: mungebot-service
    28    labels:
    29      app: mungebot
    30  spec:
    31    ports:
    32      - port: 7002
    33    selector:
    34      app: mungebot
    35  `)
    36  	th.WriteF("base/deployment/deployment.yaml", `
    37  apiVersion: apps/v1
    38  kind: Deployment
    39  metadata:
    40    name: mungebot
    41    labels:
    42      app: mungebot
    43  spec:
    44    replicas: 1
    45    template:
    46      metadata:
    47        labels:
    48          app: mungebot
    49      spec:
    50        containers:
    51        - name: nginx
    52          image: nginx
    53          env:
    54          - name: foo
    55            value: bar
    56          ports:
    57          - containerPort: 80
    58  `)
    59  }
    60  
    61  func TestMediumBase(t *testing.T) {
    62  	th := kusttest_test.MakeHarness(t)
    63  	writeMediumBase(th)
    64  	m := th.Run("base", th.MakeDefaultOptions())
    65  	th.AssertActualEqualsExpected(m, `
    66  apiVersion: apps/v1
    67  kind: Deployment
    68  metadata:
    69    annotations:
    70      baseAnno: This is a base annotation
    71    labels:
    72      app: mungebot
    73      foo: bar
    74    name: baseprefix-mungebot
    75  spec:
    76    replicas: 1
    77    selector:
    78      matchLabels:
    79        foo: bar
    80    template:
    81      metadata:
    82        annotations:
    83          baseAnno: This is a base annotation
    84        labels:
    85          app: mungebot
    86          foo: bar
    87      spec:
    88        containers:
    89        - env:
    90          - name: foo
    91            value: bar
    92          image: nginx
    93          name: nginx
    94          ports:
    95          - containerPort: 80
    96  ---
    97  apiVersion: v1
    98  kind: Service
    99  metadata:
   100    annotations:
   101      baseAnno: This is a base annotation
   102    labels:
   103      app: mungebot
   104      foo: bar
   105    name: baseprefix-mungebot-service
   106  spec:
   107    ports:
   108    - port: 7002
   109    selector:
   110      app: mungebot
   111      foo: bar
   112  `)
   113  }
   114  
   115  func TestMediumOverlay(t *testing.T) {
   116  	th := kusttest_test.MakeHarness(t)
   117  	writeMediumBase(th)
   118  	th.WriteK("overlay", `
   119  namePrefix: test-infra-
   120  commonLabels:
   121    app: mungebot
   122    org: kubernetes
   123    repo: test-infra
   124  commonAnnotations:
   125    note: This is a test annotation
   126  resources:
   127  - ../base
   128  patchesStrategicMerge:
   129  - deployment/deployment.yaml
   130  configMapGenerator:
   131  - name: app-env
   132    envs:
   133    - configmap/db.env
   134    - configmap/units.ini
   135    - configmap/food.ini
   136  - name: app-config
   137    files:
   138    - nonsense=configmap/dummy.txt
   139  images:
   140  - name: nginx
   141    newTag: 1.8.0`)
   142  
   143  	th.WriteF("overlay/configmap/db.env", `
   144  DB_USERNAME=admin
   145  DB_PASSWORD=somepw
   146  `)
   147  	th.WriteF("overlay/configmap/units.ini", `
   148  LENGTH=kilometer
   149  ENERGY=electronvolt
   150  `)
   151  	th.WriteF("overlay/configmap/food.ini", `
   152  FRUIT=banana
   153  LEGUME=chickpea
   154  `)
   155  	th.WriteF("overlay/configmap/dummy.txt",
   156  		`Lorem ipsum dolor sit amet, consectetur
   157  adipiscing elit, sed do eiusmod tempor
   158  incididunt ut labore et dolore magna aliqua. 
   159  `)
   160  	th.WriteF("overlay/deployment/deployment.yaml", `
   161  apiVersion: apps/v1
   162  kind: Deployment
   163  metadata:
   164    name: mungebot
   165  spec:
   166    replicas: 2
   167    template:
   168      spec:
   169        containers:
   170        - name: nginx
   171          image: nginx:1.7.9
   172          env:
   173          - name: FOO
   174            valueFrom:
   175              configMapKeyRef:
   176                name: app-env
   177                key: somekey
   178        - name: busybox
   179          image: busybox
   180          envFrom:
   181          - configMapRef:
   182              name: someConfigMap
   183          - configMapRef:
   184              name: app-env
   185          volumeMounts:
   186          - mountPath: /tmp/env
   187            name: app-env
   188        volumes:
   189        - configMap:
   190            name: app-env
   191          name: app-env
   192  `)
   193  	m := th.Run("overlay", th.MakeDefaultOptions())
   194  	th.AssertActualEqualsExpected(m, `
   195  apiVersion: apps/v1
   196  kind: Deployment
   197  metadata:
   198    annotations:
   199      baseAnno: This is a base annotation
   200      note: This is a test annotation
   201    labels:
   202      app: mungebot
   203      foo: bar
   204      org: kubernetes
   205      repo: test-infra
   206    name: test-infra-baseprefix-mungebot
   207  spec:
   208    replicas: 2
   209    selector:
   210      matchLabels:
   211        app: mungebot
   212        foo: bar
   213        org: kubernetes
   214        repo: test-infra
   215    template:
   216      metadata:
   217        annotations:
   218          baseAnno: This is a base annotation
   219          note: This is a test annotation
   220        labels:
   221          app: mungebot
   222          foo: bar
   223          org: kubernetes
   224          repo: test-infra
   225      spec:
   226        containers:
   227        - env:
   228          - name: FOO
   229            valueFrom:
   230              configMapKeyRef:
   231                key: somekey
   232                name: test-infra-app-env-8h5mh7f7ch
   233          - name: foo
   234            value: bar
   235          image: nginx:1.8.0
   236          name: nginx
   237          ports:
   238          - containerPort: 80
   239        - envFrom:
   240          - configMapRef:
   241              name: someConfigMap
   242          - configMapRef:
   243              name: test-infra-app-env-8h5mh7f7ch
   244          image: busybox
   245          name: busybox
   246          volumeMounts:
   247          - mountPath: /tmp/env
   248            name: app-env
   249        volumes:
   250        - configMap:
   251            name: test-infra-app-env-8h5mh7f7ch
   252          name: app-env
   253  ---
   254  apiVersion: v1
   255  kind: Service
   256  metadata:
   257    annotations:
   258      baseAnno: This is a base annotation
   259      note: This is a test annotation
   260    labels:
   261      app: mungebot
   262      foo: bar
   263      org: kubernetes
   264      repo: test-infra
   265    name: test-infra-baseprefix-mungebot-service
   266  spec:
   267    ports:
   268    - port: 7002
   269    selector:
   270      app: mungebot
   271      foo: bar
   272      org: kubernetes
   273      repo: test-infra
   274  ---
   275  apiVersion: v1
   276  data:
   277    DB_PASSWORD: somepw
   278    DB_USERNAME: admin
   279    ENERGY: electronvolt
   280    FRUIT: banana
   281    LEGUME: chickpea
   282    LENGTH: kilometer
   283  kind: ConfigMap
   284  metadata:
   285    annotations:
   286      note: This is a test annotation
   287    labels:
   288      app: mungebot
   289      org: kubernetes
   290      repo: test-infra
   291    name: test-infra-app-env-8h5mh7f7ch
   292  ---
   293  apiVersion: v1
   294  data:
   295    nonsense: "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod
   296      tempor\nincididunt ut labore et dolore magna aliqua. \n"
   297  kind: ConfigMap
   298  metadata:
   299    annotations:
   300      note: This is a test annotation
   301    labels:
   302      app: mungebot
   303      org: kubernetes
   304      repo: test-infra
   305    name: test-infra-app-config-49d6f5h7b5
   306  `)
   307  }
   308  

View as plain text