@@ -25,6 +25,9 @@ import (
25
25
"text/template"
26
26
27
27
"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
28
+ "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
29
+ "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/util"
30
+ "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/yaml"
28
31
"github.com/GoogleContainerTools/skaffold/v2/testutil"
29
32
)
30
33
@@ -83,27 +86,81 @@ func TestDiagnose(t *testing.T) {
83
86
configContents , err := os .ReadFile (filepath .Join (test .dir , "skaffold.yaml" ))
84
87
t .CheckNoError (err )
85
88
templ , err := os .ReadFile (filepath .Join (test .dir , "diagnose.tmpl" ))
89
+ t .CheckNoError (err )
86
90
tmpDir .Write ("skaffold.yaml" , string (configContents ))
87
91
args := []string {"--yaml-only" , "--output" , tmpDir .Path (test .outputFile ), "-f" , tmpDir .Path ("skaffold.yaml" )}
88
92
args = append (args , test .args ... )
89
93
skaffold .Diagnose (args ... ).
90
94
InDir (test .dir ).RunOrFail (t .T )
91
- t .CheckNoError (err )
92
95
outTemplate := template .Must (template .New ("tmpl" ).Parse (string (templ )))
93
96
cwd , err := filepath .Abs (test .dir )
94
97
t .CheckNoError (err )
95
98
expected := & bytes.Buffer {}
96
99
outTemplate .Execute (expected , map [string ]string {"Root" : cwd })
97
100
98
101
outputPath := tmpDir .Path (test .outputFile )
99
- t .CheckNoError (err )
100
102
out , err := os .ReadFile (outputPath )
101
103
t .CheckNoError (err )
102
104
t .CheckDeepEqual (expected .String (), string (out ), testutil .YamlObj (t .T ))
103
105
})
104
106
}
105
107
}
106
108
109
+ // During the schema upgrade(v2beta28->v2beta29), Skaffold injects setTemplate fields into the configuration if a legacy Helm deployer is configured.
110
+ // These injected fields contain templates, and we want to ensure that when expanding them with Go templates, the original field values remain unchanged
111
+ // when environment variables are not set. This is important because users who use the skaffold diagnose command on the old schema
112
+ // with Helm might not be aware of the existence of these templated fields, leading to templating failures.
113
+ func TestDiagnoseTemplatingNotAllEnvsSet (t * testing.T ) {
114
+ tests := []struct {
115
+ description string
116
+ dir string
117
+ outputFile string
118
+ args []string
119
+ envs map [string ]string
120
+ }{
121
+ {
122
+ description : "apply replacements to templates in skaffold.yaml" ,
123
+ dir : "testdata/diagnose/not-all-envs-set" ,
124
+ outputFile : "abc.txt" ,
125
+ args : []string {"--enable-templating" },
126
+ envs : map [string ]string {"AAA" : "aaa" },
127
+ },
128
+ }
129
+
130
+ for _ , test := range tests {
131
+ MarkIntegrationTest (t , CanRunWithoutGcp )
132
+ testutil .Run (t , test .description , func (t * testutil.T ) {
133
+ if test .envs != nil {
134
+ for k , v := range test .envs {
135
+ t .Setenv (k , v )
136
+ }
137
+ }
138
+ tmpDir := testutil .NewTempDir (t .T )
139
+ configContents , err := os .ReadFile (filepath .Join (test .dir , "skaffold.yaml" ))
140
+ t .CheckNoError (err )
141
+ tmpDir .Write ("skaffold.yaml" , string (configContents ))
142
+ outputPath := tmpDir .Path (test .outputFile )
143
+ args := []string {"--yaml-only" , "--output" , outputPath , "-f" , tmpDir .Path ("skaffold.yaml" )}
144
+ args = append (args , test .args ... )
145
+ skaffold .Diagnose (args ... ).
146
+ InDir (test .dir ).RunOrFail (t .T )
147
+ out , err := os .ReadFile (outputPath )
148
+ t .CheckNoError (err )
149
+ var conf latest.SkaffoldConfig
150
+ yaml .Unmarshal (out , & conf )
151
+ // templates unchanged
152
+ t .CheckDeepEqual (conf .Deploy .LegacyHelmDeploy .Releases [0 ].SetValueTemplates , util.FlatMap {"image.repository" : "{{.IMAGE_REPO_test_image}}" ,
153
+ "image.tag" : "{{.IMAGE_TAG_test_image}}@{{.IMAGE_DIGEST_test_image}}" ,
154
+ })
155
+ cwd , err := filepath .Abs (test .dir )
156
+ t .CheckNoError (err )
157
+
158
+ // templates successfully expanded.
159
+ t .CheckDeepEqual (conf .Deploy .LegacyHelmDeploy .Releases [0 ].ValuesFiles [0 ], cwd + "/aaa/test-values.yaml" )
160
+ })
161
+ }
162
+ }
163
+
107
164
func folders (root string ) ([]string , error ) {
108
165
var folders []string
109
166
0 commit comments