@@ -346,3 +346,53 @@ func TestOnWorkflowTemplate(t *testing.T) {
346346 tmpl := newCtx .tmplBase .GetTemplateByName ("whalesay" )
347347 assert .NotNil (t , tmpl )
348348}
349+
350+ // TestGetTemplateFromRefWithPodMetadataAndMissingTemplate tests the bug where
351+ // GetTemplateFromRef causes a nil pointer dereference when:
352+ // 1. A WorkflowTemplate has podMetadata defined
353+ // 2. A templateRef references a template name that doesn't exist in that WorkflowTemplate
354+ func TestGetTemplateFromRefWithPodMetadataAndMissingTemplate (t * testing.T ) {
355+ wfClientset := fakewfclientset .NewSimpleClientset ()
356+
357+ // Create a WorkflowTemplate with podMetadata but without the template "nonexistent"
358+ workflowTemplateWithPodMetadata := `
359+ apiVersion: argoproj.io/v1alpha1
360+ kind: WorkflowTemplate
361+ metadata:
362+ name: template-with-podmetadata
363+ spec:
364+ podMetadata:
365+ labels:
366+ example-label: example-value
367+ annotations:
368+ example-annotation: example-value
369+ templates:
370+ - name: existing-template
371+ container:
372+ image: alpine:latest
373+ command: [echo, hello]
374+ `
375+
376+ err := createWorkflowTemplate (wfClientset , workflowTemplateWithPodMetadata )
377+ require .NoError (t , err )
378+
379+ // Create a base workflow template to use as context
380+ baseWftmpl := unmarshalWftmpl (baseWorkflowTemplateYaml )
381+ tplCtx := NewContextFromClientSet (
382+ wfClientset .ArgoprojV1alpha1 ().WorkflowTemplates (metav1 .NamespaceDefault ),
383+ wfClientset .ArgoprojV1alpha1 ().ClusterWorkflowTemplates (),
384+ baseWftmpl ,
385+ nil ,
386+ )
387+
388+ // Try to get a template that doesn't exist from a WorkflowTemplate that HAS podMetadata
389+ tmplRef := wfv1.TemplateRef {
390+ Name : "template-with-podmetadata" ,
391+ Template : "nonexistent-template" ,
392+ }
393+
394+ _ , err = tplCtx .GetTemplateFromRef (& tmplRef )
395+
396+ require .Error (t , err )
397+ require .Contains (t , err .Error (), "template nonexistent-template not found in workflow template template-with-podmetadata" )
398+ }
0 commit comments