Debian Codegraph @

cmd/config/internal/commands/count_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package commands_test
5
6 import (
7 "bytes"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "sigs.k8s.io/kustomize/cmd/config/internal/commands"
16 "sigs.k8s.io/kustomize/kyaml/copyutil"
17 "sigs.k8s.io/kustomize/kyaml/filesys"
18 "sigs.k8s.io/kustomize/kyaml/openapi"
19 )
20
21 func TestCountCommand_files(t *testing.T) {
22 d := t.TempDir()
23
24 err := os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
25 kind: Deployment
26 metadata:
27 labels:
28 app: nginx2
29 name: foo
30 annotations:
31 app: nginx2
32 spec:
33 replicas: 1
34 ---
35 kind: Service
36 metadata:
37 name: foo
38 annotations:
39 app: nginx
40 spec:
41 selector:
42 app: nginx
43 `), 0600)
44 if !assert.NoError(t, err) {
45 return
46 }
47 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment
48 metadata:
49 labels:
50 app: nginx
51 name: bar
52 annotations:
53 app: nginx
54 spec:
55 replicas: 3
56 `), 0600)
57 if !assert.NoError(t, err) {
58 return
59 }
60
61 // fmt the files
62 b := &bytes.Buffer{}
63 r := commands.GetCountRunner("")
64 r.Command.SetArgs([]string{d})
65 r.Command.SetOut(b)
66 if !assert.NoError(t, r.Command.Execute()) {
67 return
68 }
69
70 if !assert.Contains(t, b.String(), "Deployment: 2\nService: 1\n") {
71 return
72 }
73 }
74
75 func TestCountSubPackages(t *testing.T) {
76 var tests = []struct {
77 name string
78 dataset string
79 packagePath string
80 args []string
81 expected string
82 }{
83 {
84 name: "count-recurse-subpackages",
85 dataset: "dataset-without-setters",
86 expected: `${baseDir}/
87
88 ${baseDir}/mysql/
89 Deployment: 1
90
91 ${baseDir}/mysql/storage/
92 Deployment: 1
93 `,
94 },
95 {
96 name: "count-top-level-pkg-no-recurse-subpackages",
97 dataset: "dataset-without-setters",
98 args: []string{"-R=false"},
99 packagePath: "mysql",
100 expected: `${baseDir}/mysql/
101 Deployment: 1
102 `,
103 },
104 {
105 name: "count-nested-pkg-no-recurse-subpackages",
106 dataset: "dataset-without-setters",
107 packagePath: "mysql/storage",
108 args: []string{"-R=false"},
109 expected: `${baseDir}/mysql/storage/
110 Deployment: 1
111 `,
112 },
113 }
114 for i := range tests {
115 test := tests[i]
116 t.Run(test.name, func(t *testing.T) {
117 // reset the openAPI afterward
118 openapi.ResetOpenAPI()
119 defer openapi.ResetOpenAPI()
120 sourceDir := filepath.Join("test", "testdata", test.dataset)
121 baseDir := t.TempDir()
122 require.NoError(t, copyutil.CopyDir(filesys.MakeFsOnDisk(), sourceDir, baseDir))
123 runner := commands.GetCountRunner("")
124 actual := &bytes.Buffer{}
125 runner.Command.SetOut(actual)
126 runner.Command.SetArgs(append([]string{filepath.Join(baseDir, test.packagePath)}, test.args...))
127 err := runner.Command.Execute()
128 if !assert.NoError(t, err) {
129 t.FailNow()
130 }
131
132 // normalize path format for windows
133 actualNormalized := strings.ReplaceAll(
134 strings.ReplaceAll(actual.String(), "\\", "/"),
135 "//", "/")
136
137 expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir)
138 expectedNormalized := strings.ReplaceAll(expected, "\\", "/")
139 if !assert.Equal(t, expectedNormalized, actualNormalized) {
140 t.FailNow()
141 }
142 })
143 }
144 }