feat: add more clear error message and Environment info for template (#413)
* feat: add more clear error message and Environment info for template Signed-off-by: yxxhero <aiopsclub@163.com> * Update pkg/state/state.go Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com> Signed-off-by: yxxhero <aiopsclub@163.com> Signed-off-by: yxxhero <aiopsclub@163.com> Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
This commit is contained in:
		
							parent
							
								
									a8101e44c7
								
							
						
					
					
						commit
						e06abe4620
					
				|  | @ -3200,7 +3200,7 @@ func (st *HelmState) GenerateOutputFilePath(release *ReleaseSpec, outputFileTemp | ||||||
| 
 | 
 | ||||||
| 	t, err := template.New("output-file").Parse(outputFileTemplate) | 	t, err := template.New("output-file").Parse(outputFileTemplate) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", fmt.Errorf("parsing output-file template") | 		return "", fmt.Errorf("parsing output-file template %q: %w", outputFileTemplate, err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	buf := &bytes.Buffer{} | 	buf := &bytes.Buffer{} | ||||||
|  | @ -3213,8 +3213,9 @@ func (st *HelmState) GenerateOutputFilePath(release *ReleaseSpec, outputFileTemp | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	data := struct { | 	data := struct { | ||||||
| 		State   state | 		State       state | ||||||
| 		Release *ReleaseSpec | 		Release     *ReleaseSpec | ||||||
|  | 		Environment *environment.Environment | ||||||
| 	}{ | 	}{ | ||||||
| 		State: state{ | 		State: state{ | ||||||
| 			BaseName:    stateFileName, | 			BaseName:    stateFileName, | ||||||
|  | @ -3222,7 +3223,8 @@ func (st *HelmState) GenerateOutputFilePath(release *ReleaseSpec, outputFileTemp | ||||||
| 			AbsPath:     stateAbsPath, | 			AbsPath:     stateAbsPath, | ||||||
| 			AbsPathSHA1: sha1sum, | 			AbsPathSHA1: sha1sum, | ||||||
| 		}, | 		}, | ||||||
| 		Release: release, | 		Release:     release, | ||||||
|  | 		Environment: &st.Env, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if err := t.Execute(buf, data); err != nil { | 	if err := t.Execute(buf, data); err != nil { | ||||||
|  |  | ||||||
|  | @ -8,8 +8,10 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/Masterminds/semver/v3" | 	"github.com/Masterminds/semver/v3" | ||||||
|  | 	"github.com/stretchr/testify/require" | ||||||
| 	"github.com/variantdev/vals" | 	"github.com/variantdev/vals" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/helmfile/helmfile/pkg/environment" | ||||||
| 	"github.com/helmfile/helmfile/pkg/exectest" | 	"github.com/helmfile/helmfile/pkg/exectest" | ||||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/helmexec" | 	"github.com/helmfile/helmfile/pkg/helmexec" | ||||||
|  | @ -2542,3 +2544,54 @@ func Test_gatherUsernamePassword(t *testing.T) { | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestGenerateOutputFilePath(t *testing.T) { | ||||||
|  | 	tests := []struct { | ||||||
|  | 		envName            string | ||||||
|  | 		filePath           string | ||||||
|  | 		releaseName        string | ||||||
|  | 		outputFileTemplate string | ||||||
|  | 		wantErr            bool | ||||||
|  | 		expected           string | ||||||
|  | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			envName:            "dev", | ||||||
|  | 			releaseName:        "release1", | ||||||
|  | 			filePath:           "/path/to/helmfile.yaml", | ||||||
|  | 			outputFileTemplate: "helmfile-{{ .Environment.Name }}.yaml", | ||||||
|  | 			expected:           "helmfile-dev.yaml", | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			envName:            "error", | ||||||
|  | 			releaseName:        "release2", | ||||||
|  | 			filePath:           "helmfile.yaml", | ||||||
|  | 			outputFileTemplate: "helmfile-{{ .Environment.Name", | ||||||
|  | 			wantErr:            true, | ||||||
|  | 			expected:           "", | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	for _, tt := range tests { | ||||||
|  | 		t.Run(tt.envName, func(t *testing.T) { | ||||||
|  | 			st := &HelmState{ | ||||||
|  | 				FilePath: tt.envName, | ||||||
|  | 				ReleaseSetSpec: ReleaseSetSpec{ | ||||||
|  | 					Env: environment.Environment{ | ||||||
|  | 						Name: tt.envName, | ||||||
|  | 					}, | ||||||
|  | 				}, | ||||||
|  | 			} | ||||||
|  | 			ra := &ReleaseSpec{ | ||||||
|  | 				Name: tt.releaseName, | ||||||
|  | 			} | ||||||
|  | 			got, err := st.GenerateOutputFilePath(ra, tt.outputFileTemplate) | ||||||
|  | 
 | ||||||
|  | 			if tt.wantErr { | ||||||
|  | 				require.Errorf(t, err, "GenerateOutputFilePath() error = %v, want error", err) | ||||||
|  | 			} else { | ||||||
|  | 				require.NoError(t, err, "GenerateOutputFilePath() error = %v, want nil", err) | ||||||
|  | 			} | ||||||
|  | 			require.Equalf(t, got, tt.expected, "GenerateOutputFilePath() got = %v, want %v", got, tt.expected) | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue