@@ -20,6 +20,7 @@ import (
2020 "github.com/rs/zerolog"
2121
2222 "hauler.dev/go/hauler/v2/internal/flags"
23+ v1 "hauler.dev/go/hauler/v2/pkg/apis/hauler.cattle.io/v1"
2324 "hauler.dev/go/hauler/v2/pkg/consts"
2425)
2526
@@ -49,6 +50,95 @@ func newSyncOpts(storeDir string) *flags.SyncOpts {
4950 }
5051}
5152
53+ // --------------------------------------------------------------------------
54+ // resolveChartCreds tests
55+ // --------------------------------------------------------------------------
56+
57+ func TestResolveChartCreds_BothEmpty (t * testing.T ) {
58+ ch := v1.Chart {Name : "mychart" , RepoURL : "https://charts.example.com" }
59+ u , p , err := resolveChartCreds (ch )
60+ if err != nil {
61+ t .Fatalf ("unexpected error: %v" , err )
62+ }
63+ if u != "" || p != "" {
64+ t .Errorf ("expected empty creds, got username=%q password=%q" , u , p )
65+ }
66+ }
67+
68+ func TestResolveChartCreds_BothSetAndEnvPopulated (t * testing.T ) {
69+ t .Setenv ("CHART_TEST_USER" , "alice" )
70+ t .Setenv ("CHART_TEST_PASS" , "s3cr3t" )
71+
72+ ch := v1.Chart {
73+ Name : "mychart" ,
74+ RepoURL : "https://charts.example.com" ,
75+ UsernameEnv : "CHART_TEST_USER" ,
76+ PasswordEnv : "CHART_TEST_PASS" ,
77+ }
78+ u , p , err := resolveChartCreds (ch )
79+ if err != nil {
80+ t .Fatalf ("unexpected error: %v" , err )
81+ }
82+ if u != "alice" {
83+ t .Errorf ("username: got %q, want %q" , u , "alice" )
84+ }
85+ if p != "s3cr3t" {
86+ t .Errorf ("password: got %q, want %q" , p , "s3cr3t" )
87+ }
88+ }
89+
90+ func TestResolveChartCreds_OnlyUsernameEnvSet_ReturnsError (t * testing.T ) {
91+ ch := v1.Chart {
92+ Name : "mychart" ,
93+ RepoURL : "https://charts.example.com" ,
94+ UsernameEnv : "CHART_TEST_USER_ONLY" ,
95+ // PasswordEnv intentionally omitted
96+ }
97+ _ , _ , err := resolveChartCreds (ch )
98+ if err == nil {
99+ t .Fatal ("expected error when only usernameEnv is set, got nil" )
100+ }
101+ if ! strings .Contains (err .Error (), "usernameEnv and passwordEnv must both be set" ) {
102+ t .Errorf ("unexpected error message: %v" , err )
103+ }
104+ }
105+
106+ func TestResolveChartCreds_OnlyPasswordEnvSet_ReturnsError (t * testing.T ) {
107+ ch := v1.Chart {
108+ Name : "mychart" ,
109+ RepoURL : "https://charts.example.com" ,
110+ // UsernameEnv intentionally omitted
111+ PasswordEnv : "CHART_TEST_PASS_ONLY" ,
112+ }
113+ _ , _ , err := resolveChartCreds (ch )
114+ if err == nil {
115+ t .Fatal ("expected error when only passwordEnv is set, got nil" )
116+ }
117+ if ! strings .Contains (err .Error (), "usernameEnv and passwordEnv must both be set" ) {
118+ t .Errorf ("unexpected error message: %v" , err )
119+ }
120+ }
121+
122+ func TestResolveChartCreds_EnvVarUnset_ReturnsError (t * testing.T ) {
123+ // Ensure the env vars are definitely absent.
124+ t .Setenv ("CHART_UNSET_USER" , "" )
125+ t .Setenv ("CHART_UNSET_PASS" , "" )
126+
127+ ch := v1.Chart {
128+ Name : "mychart" ,
129+ RepoURL : "https://charts.example.com" ,
130+ UsernameEnv : "CHART_UNSET_USER" ,
131+ PasswordEnv : "CHART_UNSET_PASS" ,
132+ }
133+ _ , _ , err := resolveChartCreds (ch )
134+ if err == nil {
135+ t .Fatal ("expected error when env vars are empty, got nil" )
136+ }
137+ if ! strings .Contains (err .Error (), "must both be set and non-empty" ) {
138+ t .Errorf ("unexpected error message: %v" , err )
139+ }
140+ }
141+
52142// --------------------------------------------------------------------------
53143// processContent tests
54144// --------------------------------------------------------------------------
0 commit comments