@@ -205,57 +205,110 @@ func constructArtifactInformation(t *testing.T, roleUrn string, path string, sha
205205 }
206206}
207207
208+ func clearEnvVars () {
209+ _ = os .Unsetenv (cloudProfilingJobName )
210+ _ = os .Unsetenv (cloudProfilingJobID )
211+ }
212+
208213func TestConfigureGoogleCloudProfilerEnvVars (t * testing.T ) {
209214 tests := []struct {
210- name string
211- inputMetadata map [string ]string
212- expectedName string
213- expectedID string
214- expectedError string
215+ name string
216+ options string
217+ metadata map [string ]string
218+ expectedName string
219+ expectedID string
220+ expectingError bool
215221 }{
216222 {
217- "nil metadata" ,
218- nil ,
219- "" ,
220- "" ,
221- "enable_google_cloud_profiler is set to true, but no metadata is received from provision server, profiling will not be enabled" ,
223+ name : "Profiler name from options" ,
224+ options : `{
225+ "beam:option:go_options:v1": {
226+ "options": {
227+ "dataflow_service_options": "enable_google_cloud_profiler=custom_profiler"
228+ }
229+ }
230+ }` ,
231+ metadata : map [string ]string {
232+ "job_id" : "job-123" ,
233+ },
234+ expectedName : "custom_profiler" ,
235+ expectedID : "job-123" ,
236+ expectingError : false ,
222237 },
223238 {
224- "missing name" ,
225- map [string ]string {"job_id" : "12345" },
226- "" ,
227- "" ,
228- "required job_name missing from metadata, profiling will not be enabled without it" ,
239+ name : "Fallback to job_name" ,
240+ options : `{
241+ "beam:option:go_options:v1": {
242+ "options": {
243+ "dataflow_service_options": "enable_google_cloud_profiler"
244+ }
245+ }
246+ }` ,
247+ metadata : map [string ]string {
248+ "job_name" : "fallback_profiler" ,
249+ "job_id" : "job-456" ,
250+ },
251+ expectedName : "fallback_profiler" ,
252+ expectedID : "job-456" ,
253+ expectingError : false ,
229254 },
230255 {
231- "missing id" ,
232- map [string ]string {"job_name" : "my_job" },
233- "" ,
234- "" ,
235- "required job_id missing from metadata, profiling will not be enabled without it" ,
256+ name : "Missing job_id" ,
257+ options : `{
258+ "beam:option:go_options:v1": {
259+ "options": {
260+ "dataflow_service_options": "enable_google_cloud_profiler=custom_profiler"
261+ }
262+ }
263+ }` ,
264+ metadata : map [string ]string {
265+ "job_name" : "custom_profiler" ,
266+ },
267+ expectingError : true ,
236268 },
237269 {
238- "correct" ,
239- map [string ]string {"job_name" : "my_job" , "job_id" : "42" },
240- "my_job" ,
241- "42" ,
242- "" ,
243- },
270+ name : "Missing profiler name and job_name" ,
271+ options : `{
272+ "beam:option:go_options:v1": {
273+ "options": {
274+ "dataflow_service_options": "enable_google_cloud_profiler"
275+ }
276+ }
277+ }` ,
278+ metadata : map [string ]string {
279+ "job_id" : "job-789" ,
280+ },
281+ expectingError : true ,
282+ },
244283 }
245- for _ , test := range tests {
246- t .Run (test .name , func (t * testing.T ) {
247- t .Cleanup (os .Clearenv )
248- err := configureGoogleCloudProfilerEnvVars (context .Background (), & tools.Logger {}, test .inputMetadata )
249- if err != nil {
250- if got , want := err .Error (), test .expectedError ; got != want {
251- t .Errorf ("got error %v, want error %v" , got , want )
284+
285+ for _ , tt := range tests {
286+ t .Run (tt .name , func (t * testing.T ) {
287+ clearEnvVars ()
288+ ctx := context .Background ()
289+
290+ err := configureGoogleCloudProfilerEnvVars (ctx , & tools.Logger {}, tt .metadata , tt .options )
291+
292+ if tt .expectingError {
293+ if err == nil {
294+ t .Errorf ("Expected error but got nil" )
295+ }
296+ return
297+ } else {
298+ if err != nil {
299+ t .Errorf ("Did not expect error but got: %v" , err )
300+ return
252301 }
253302 }
254- if got , want := os .Getenv (cloudProfilingJobName ), test .expectedName ; got != want {
255- t .Errorf ("got job name %v, want %v" , got , want )
303+
304+ gotName := os .Getenv (cloudProfilingJobName )
305+ gotID := os .Getenv (cloudProfilingJobID )
306+
307+ if gotName != tt .expectedName {
308+ t .Errorf ("Expected profiler name '%s', got '%s'" , tt .expectedName , gotName )
256309 }
257- if got , want := os . Getenv ( cloudProfilingJobID ), test . expectedID ; got != want {
258- t .Errorf ("got job id %v, want %v " , got , want )
310+ if gotID != tt . expectedID {
311+ t .Errorf ("Expected job ID '%s', got '%s' " , tt . expectedID , gotID )
259312 }
260313 })
261314 }
0 commit comments