...

Source file src/sigs.k8s.io/kustomize/kyaml/kio/filters/grep_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/kio/filters

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package filters_test
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"sigs.k8s.io/kustomize/kyaml/kio"
    12  	. "sigs.k8s.io/kustomize/kyaml/kio/filters"
    13  	"sigs.k8s.io/kustomize/kyaml/yaml"
    14  )
    15  
    16  func TestGrepFilter_Filter(t *testing.T) {
    17  	in := `kind: Deployment
    18  metadata:
    19    labels:
    20      app: nginx2
    21    name: foo
    22    annotations:
    23      app: nginx2
    24  spec:
    25    replicas: 1
    26  ---
    27  kind: Deployment
    28  metadata:
    29    labels:
    30      app: nginx
    31    annotations:
    32      app: nginx
    33    name: bar
    34  spec:
    35    replicas: 3
    36  ---
    37  kind: Service
    38  metadata:
    39    name: foo
    40    annotations:
    41      app: nginx
    42  spec:
    43    selector:
    44      app: nginx
    45  `
    46  	out := &bytes.Buffer{}
    47  	err := kio.Pipeline{
    48  		Inputs:  []kio.Reader{&kio.ByteReader{Reader: bytes.NewBufferString(in)}},
    49  		Filters: []kio.Filter{GrepFilter{Path: []string{"metadata", "name"}, Value: "foo"}},
    50  		Outputs: []kio.Writer{kio.ByteWriter{Writer: out}},
    51  	}.Execute()
    52  	if !assert.NoError(t, err) {
    53  		t.FailNow()
    54  	}
    55  
    56  	if !assert.Equal(t, `kind: Deployment
    57  metadata:
    58    labels:
    59      app: nginx2
    60    name: foo
    61    annotations:
    62      app: nginx2
    63  spec:
    64    replicas: 1
    65  ---
    66  kind: Service
    67  metadata:
    68    name: foo
    69    annotations:
    70      app: nginx
    71  spec:
    72    selector:
    73      app: nginx
    74  `, out.String()) {
    75  		t.FailNow()
    76  	}
    77  
    78  	out = &bytes.Buffer{}
    79  	err = kio.Pipeline{
    80  		Inputs:  []kio.Reader{&kio.ByteReader{Reader: bytes.NewBufferString(in)}},
    81  		Filters: []kio.Filter{GrepFilter{Path: []string{"kind"}, Value: "Deployment"}},
    82  		Outputs: []kio.Writer{kio.ByteWriter{Writer: out}},
    83  	}.Execute()
    84  	if !assert.NoError(t, err) {
    85  		t.FailNow()
    86  	}
    87  	if !assert.Equal(t, `kind: Deployment
    88  metadata:
    89    labels:
    90      app: nginx2
    91    name: foo
    92    annotations:
    93      app: nginx2
    94  spec:
    95    replicas: 1
    96  ---
    97  kind: Deployment
    98  metadata:
    99    labels:
   100      app: nginx
   101    annotations:
   102      app: nginx
   103    name: bar
   104  spec:
   105    replicas: 3
   106  `, out.String()) {
   107  		t.FailNow()
   108  	}
   109  
   110  	out = &bytes.Buffer{}
   111  	err = kio.Pipeline{
   112  		Inputs:  []kio.Reader{&kio.ByteReader{Reader: bytes.NewBufferString(in)}},
   113  		Filters: []kio.Filter{GrepFilter{Path: []string{"spec", "replicas"}, Value: "3"}},
   114  		Outputs: []kio.Writer{kio.ByteWriter{Writer: out}},
   115  	}.Execute()
   116  	if !assert.NoError(t, err) {
   117  		t.FailNow()
   118  	}
   119  	if !assert.Equal(t, `kind: Deployment
   120  metadata:
   121    labels:
   122      app: nginx
   123    annotations:
   124      app: nginx
   125    name: bar
   126  spec:
   127    replicas: 3
   128  `, out.String()) {
   129  		t.FailNow()
   130  	}
   131  
   132  	out = &bytes.Buffer{}
   133  	err = kio.Pipeline{
   134  		Inputs:  []kio.Reader{&kio.ByteReader{Reader: bytes.NewBufferString(in)}},
   135  		Filters: []kio.Filter{GrepFilter{Path: []string{"spec", "not-present"}, Value: "3"}},
   136  		Outputs: []kio.Writer{kio.ByteWriter{Writer: out}},
   137  	}.Execute()
   138  	if !assert.NoError(t, err) {
   139  		t.FailNow()
   140  	}
   141  	if !assert.Equal(t, ``, out.String()) {
   142  		t.FailNow()
   143  	}
   144  }
   145  
   146  func TestGrepFilter_init(t *testing.T) {
   147  	assert.Equal(t, GrepFilter{}, Filters["GrepFilter"]())
   148  }
   149  
   150  func TestGrepFilter_error(t *testing.T) {
   151  	v, err := GrepFilter{Path: []string{"metadata", "name"},
   152  		Value: "foo"}.Filter([]*yaml.RNode{{}})
   153  	if !assert.NoError(t, err) {
   154  		t.FailNow()
   155  	}
   156  	assert.Nil(t, v)
   157  }
   158  

View as plain text