1+ /*
2+ Copyright 2021 The Flux authors
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ package utils
18+
19+ import (
20+ "os"
21+ "path/filepath"
22+ "testing"
23+
24+ "github.com/stretchr/testify/assert"
25+ "github.com/stretchr/testify/require"
26+ )
27+
28+ func TestIsRecognizedKustomizationFile (t * testing.T ) {
29+ tests := []struct {
30+ name string
31+ path string
32+ expected bool
33+ }{
34+ {
35+ name : "kustomization.yaml" ,
36+ path : "/path/to/kustomization.yaml" ,
37+ expected : true ,
38+ },
39+ {
40+ name : "kustomization.yml" ,
41+ path : "/path/to/kustomization.yml" ,
42+ expected : true ,
43+ },
44+ {
45+ name : "Kustomization" ,
46+ path : "/path/to/Kustomization" ,
47+ expected : true ,
48+ },
49+ {
50+ name : "regular yaml file" ,
51+ path : "/path/to/deployment.yaml" ,
52+ expected : false ,
53+ },
54+ {
55+ name : "no extension" ,
56+ path : "/path/to/somefile" ,
57+ expected : false ,
58+ },
59+ }
60+
61+ for _ , tt := range tests {
62+ t .Run (tt .name , func (t * testing.T ) {
63+ result := isRecognizedKustomizationFile (tt .path )
64+ assert .Equal (t , tt .expected , result )
65+ })
66+ }
67+ }
68+
69+ func TestReadObjects (t * testing.T ) {
70+ tests := []struct {
71+ name string
72+ setupFile func (t * testing.T ) (root , manifestPath string )
73+ expectError bool
74+ }{
75+ {
76+ name : "valid yaml file" ,
77+ setupFile : func (t * testing.T ) (string , string ) {
78+ dir := t .TempDir ()
79+ manifestPath := filepath .Join (dir , "test.yaml" )
80+ content := `apiVersion: v1
81+ kind: ConfigMap
82+ metadata:
83+ name: test-cm
84+ namespace: default
85+ data:
86+ key: value`
87+ require .NoError (t , os .WriteFile (manifestPath , []byte (content ), 0644 ))
88+ return dir , manifestPath
89+ },
90+ expectError : false ,
91+ },
92+ {
93+ name : "directory instead of file" ,
94+ setupFile : func (t * testing.T ) (string , string ) {
95+ dir := t .TempDir ()
96+ subDir := filepath .Join (dir , "subdir" )
97+ require .NoError (t , os .Mkdir (subDir , 0755 ))
98+ return dir , subDir
99+ },
100+ expectError : true ,
101+ },
102+ {
103+ name : "non-existent file" ,
104+ setupFile : func (t * testing.T ) (string , string ) {
105+ dir := t .TempDir ()
106+ return dir , filepath .Join (dir , "missing.yaml" )
107+ },
108+ expectError : true ,
109+ },
110+ {
111+ name : "kustomization file" ,
112+ setupFile : func (t * testing.T ) (string , string ) {
113+ dir := t .TempDir ()
114+ kustomizationPath := filepath .Join (dir , "kustomization.yaml" )
115+ content := `apiVersion: kustomize.config.k8s.io/v1beta1
116+ kind: Kustomization
117+ resources:
118+ - deployment.yaml`
119+ require .NoError (t , os .WriteFile (kustomizationPath , []byte (content ), 0644 ))
120+
121+ deploymentPath := filepath .Join (dir , "deployment.yaml" )
122+ deploymentContent := `apiVersion: apps/v1
123+ kind: Deployment
124+ metadata:
125+ name: test-deployment`
126+ require .NoError (t , os .WriteFile (deploymentPath , []byte (deploymentContent ), 0644 ))
127+ return dir , kustomizationPath
128+ },
129+ expectError : false ,
130+ },
131+ }
132+
133+ for _ , tt := range tests {
134+ t .Run (tt .name , func (t * testing.T ) {
135+ root , manifestPath := tt .setupFile (t )
136+
137+ objects , err := readObjects (root , manifestPath )
138+
139+ if tt .expectError {
140+ require .Error (t , err )
141+ assert .Nil (t , objects )
142+ } else {
143+ require .NoError (t , err )
144+ assert .NotEmpty (t , objects )
145+ }
146+ })
147+ }
148+ }
0 commit comments