...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/webhook/config/watcher/util_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/webhook/config/watcher

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package watcher
    17  
    18  import (
    19  	"html/template"
    20  	"testing"
    21  )
    22  
    23  func TestRenderTemplateWithArgs(t *testing.T) {
    24  	tmpl := template.Must(template.New("common-template").Parse(`initContainers:
    25  - name: inject-scripts
    26    image: pingcap/chaos-scripts:latest
    27    imagePullpolicy: Always
    28    command: ["sh", "-c", "/scripts/init.sh -d {{.DataPath}} -f {{.MountPath}}/fuse-data"]
    29  containers:
    30  - name: chaosfs
    31    image: pingcap/chaos-fs:latest
    32    imagePullpolicy: Always
    33    ports:
    34    - containerPort: 65534
    35    securityContext:
    36      privileged: true
    37    command:
    38      - /usr/local/bin/chaosfs
    39      - -addr=:65534
    40      - -pidfile=/tmp/fuse/pid
    41      - -original={{.MountPath}}/fuse-data
    42      - -mountpoint={{.DataPath}}
    43    volumeMounts:
    44    - name: {{.VolumeName}}
    45      mountPath: {{.MountPath}}
    46      mountPropagation: Bidirectional
    47  volumeMounts:
    48  - name: {{.VolumeName}}
    49    mountPath: {{.MountPath}}
    50    mountPropagation: HostToContainer
    51  - name: scripts
    52    mountPath: /tmp/scripts
    53  - name: fuse
    54    mountPath: /tmp/fuse
    55  volumes:
    56  - name: scripts
    57    emptyDir: {}
    58  - name: fuse
    59    emptyDir: {}
    60  postStart:
    61    {{.ContainerName}}:
    62      command:
    63        - /tmp/scripts/wait-fuse.sh`))
    64  
    65  	args := map[string]string{
    66  		"DataPath":      "/var/lib/pd/data",
    67  		"VolumeName":    "pd",
    68  		"MountPath":     "/var/lib/pd",
    69  		"ContainerName": "pd",
    70  	}
    71  	out, err := renderTemplateWithArgs(tmpl, args)
    72  	if err != nil {
    73  		t.Error("failed to render template", err)
    74  	}
    75  	expected := `initContainers:
    76  - name: inject-scripts
    77    image: pingcap/chaos-scripts:latest
    78    imagePullpolicy: Always
    79    command: ["sh", "-c", "/scripts/init.sh -d /var/lib/pd/data -f /var/lib/pd/fuse-data"]
    80  containers:
    81  - name: chaosfs
    82    image: pingcap/chaos-fs:latest
    83    imagePullpolicy: Always
    84    ports:
    85    - containerPort: 65534
    86    securityContext:
    87      privileged: true
    88    command:
    89      - /usr/local/bin/chaosfs
    90      - -addr=:65534
    91      - -pidfile=/tmp/fuse/pid
    92      - -original=/var/lib/pd/fuse-data
    93      - -mountpoint=/var/lib/pd/data
    94    volumeMounts:
    95    - name: pd
    96      mountPath: /var/lib/pd
    97      mountPropagation: Bidirectional
    98  volumeMounts:
    99  - name: pd
   100    mountPath: /var/lib/pd
   101    mountPropagation: HostToContainer
   102  - name: scripts
   103    mountPath: /tmp/scripts
   104  - name: fuse
   105    mountPath: /tmp/fuse
   106  volumes:
   107  - name: scripts
   108    emptyDir: {}
   109  - name: fuse
   110    emptyDir: {}
   111  postStart:
   112    pd:
   113      command:
   114        - /tmp/scripts/wait-fuse.sh`
   115  	if string(out) != expected {
   116  		t.Error("expected to get", expected, "but got", string(out))
   117  	}
   118  }
   119