...

Source file src/sigs.k8s.io/cli-utils/pkg/manifestreader/manifestloader_test.go

Documentation: sigs.k8s.io/cli-utils/pkg/manifestreader

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package manifestreader
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    14  )
    15  
    16  func TestMReader_Read(t *testing.T) {
    17  	testCases := map[string]struct {
    18  		manifests        map[string]string
    19  		namespace        string
    20  		enforceNamespace bool
    21  		validate         bool
    22  		path             string
    23  
    24  		infosCount int
    25  		namespaces []string
    26  	}{
    27  		"path mReader: namespace should be set if not already present": {
    28  			namespace:        "foo",
    29  			enforceNamespace: true,
    30  			path:             "${reader-test-dir}",
    31  			infosCount:       1,
    32  			namespaces:       []string{"foo"},
    33  		},
    34  		"stream mReader: namespace should be set if not already present": {
    35  			namespace:        "foo",
    36  			enforceNamespace: true,
    37  			path:             "-",
    38  			infosCount:       1,
    39  			namespaces:       []string{"foo"},
    40  		},
    41  	}
    42  
    43  	for tn, tc := range testCases {
    44  		t.Run(tn, func(t *testing.T) {
    45  			tf := cmdtesting.NewTestFactory().WithNamespace("test-ns")
    46  			defer tf.Cleanup()
    47  
    48  			mapper, err := tf.ToRESTMapper()
    49  			if !assert.NoError(t, err) {
    50  				t.FailNow()
    51  			}
    52  
    53  			dir, err := os.MkdirTemp("", "reader-test")
    54  			assert.NoError(t, err)
    55  			p := filepath.Join(dir, "dep.yaml")
    56  			err = os.WriteFile(p, []byte(depManifest), 0600)
    57  			assert.NoError(t, err)
    58  			stringReader := strings.NewReader(depManifest)
    59  
    60  			if tc.path == "${reader-test-dir}" {
    61  				tc.path = dir
    62  			}
    63  
    64  			objs, err := mReader(tc.path, stringReader, ReaderOptions{
    65  				Mapper:           mapper,
    66  				Namespace:        tc.namespace,
    67  				EnforceNamespace: tc.enforceNamespace,
    68  				Validate:         tc.validate,
    69  			}).Read()
    70  
    71  			assert.NoError(t, err)
    72  			assert.Equal(t, len(objs), tc.infosCount)
    73  
    74  			for i, obj := range objs {
    75  				assert.Equal(t, tc.namespaces[i], obj.GetNamespace())
    76  			}
    77  		})
    78  	}
    79  }
    80  

View as plain text