...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/kfns_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/yaml

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package yaml
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestSetMeta(t *testing.T) {
    13  	rn := MustParse(`apiVersion: v1
    14  kind: ConfigMap
    15  data:
    16    altGreeting: "Good Morning!"
    17  `)
    18  	_, err := rn.Pipe(SetK8sName("foo"), SetK8sNamespace("bar"))
    19  	if err != nil {
    20  		t.Fatalf("unexpected error %v", err)
    21  	}
    22  	output := rn.MustString()
    23  
    24  	expected := `apiVersion: v1
    25  kind: ConfigMap
    26  data:
    27    altGreeting: "Good Morning!"
    28  metadata:
    29    name: foo
    30    namespace: bar
    31  `
    32  	if !assert.Equal(t, expected, output) {
    33  		t.FailNow()
    34  	}
    35  }
    36  
    37  func TestSetLabel1(t *testing.T) {
    38  	rn := MustParse(`apiVersion: v1
    39  kind: ConfigMap
    40  metadata:
    41    name: the-map
    42  data:
    43    altGreeting: "Good Morning!"
    44    enableRisky: "false"
    45  `)
    46  	_, err := rn.Pipe(SetLabel("foo", "bar"))
    47  	if err != nil {
    48  		t.Fatalf("unexpected error %v", err)
    49  	}
    50  	output := rn.MustString()
    51  
    52  	expected := `apiVersion: v1
    53  kind: ConfigMap
    54  metadata:
    55    name: the-map
    56    labels:
    57      foo: 'bar'
    58  data:
    59    altGreeting: "Good Morning!"
    60    enableRisky: "false"
    61  `
    62  	if output != expected {
    63  		t.Fatalf("expected \n%s\nbut got \n%s\n", expected, output)
    64  	}
    65  }
    66  
    67  func TestSetLabel2(t *testing.T) {
    68  	rn := MustParse(`apiVersion: v1
    69  kind: ConfigMap
    70  data:
    71    altGreeting: "Good Morning!"
    72  `)
    73  	_, err := rn.Pipe(SetLabel("foo", "bar"))
    74  	if err != nil {
    75  		t.Fatalf("unexpected error %v", err)
    76  	}
    77  	expected := `apiVersion: v1
    78  kind: ConfigMap
    79  data:
    80    altGreeting: "Good Morning!"
    81  metadata:
    82    labels:
    83      foo: 'bar'
    84  `
    85  	if output := rn.MustString(); output != expected {
    86  		t.Fatalf("expected \n%s\nbut got \n%s\n", expected, output)
    87  	}
    88  }
    89  
    90  func TestAnnotation(t *testing.T) {
    91  	rn := MustParse(`apiVersion: v1
    92  kind: ConfigMap
    93  metadata:
    94    name: the-map
    95  data:
    96    altGreeting: "Good Morning!"
    97    enableRisky: "false"
    98  `)
    99  	_, err := rn.Pipe(SetAnnotation("foo", "bar"))
   100  	if err != nil {
   101  		t.Fatalf("unexpected error %v", err)
   102  	}
   103  
   104  	expected := `apiVersion: v1
   105  kind: ConfigMap
   106  metadata:
   107    name: the-map
   108    annotations:
   109      foo: 'bar'
   110  data:
   111    altGreeting: "Good Morning!"
   112    enableRisky: "false"
   113  `
   114  	if output := rn.MustString(); output != expected {
   115  		t.Fatalf("expected \n%s\nbut got \n%s\n", expected, output)
   116  	}
   117  }
   118  

View as plain text