1+ //go:build unit
2+
3+ package flags
4+
5+ import (
6+ "testing"
7+
8+ "github.com/stretchr/testify/assert"
9+ "github.com/stretchr/testify/suite"
10+ )
11+
12+ type TestSuiteFlagsValidation struct {
13+ suite.Suite
14+
15+ originalPort int
16+ originalConfig string
17+ }
18+
19+ func (suite * TestSuiteFlagsValidation ) BeforeTest (suiteName , testName string ) {
20+ // Save original values
21+ suite .originalPort = Port
22+ suite .originalConfig = Config
23+ }
24+
25+ func (suite * TestSuiteFlagsValidation ) AfterTest (suiteName , testName string ) {
26+ // Restore original values
27+ Port = suite .originalPort
28+ Config = suite .originalConfig
29+ }
30+
31+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_ValidPort () {
32+ assert := assert .New (suite .T ())
33+
34+ Port = 8080
35+ Config = "webhooked.yaml"
36+
37+ err := ValidateFlags ()
38+
39+ assert .NoError (err )
40+ }
41+
42+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_ValidPortRange () {
43+ assert := assert .New (suite .T ())
44+
45+ testCases := []int {1 , 80 , 443 , 8080 , 65535 }
46+
47+ for _ , port := range testCases {
48+ Port = port
49+ Config = "webhooked.yaml"
50+
51+ err := ValidateFlags ()
52+
53+ assert .NoError (err , "Port %d should be valid" , port )
54+ }
55+ }
56+
57+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_InvalidPortTooLow () {
58+ assert := assert .New (suite .T ())
59+
60+ Port = 0
61+ Config = "webhooked.yaml"
62+
63+ err := ValidateFlags ()
64+
65+ assert .Error (err )
66+ assert .Contains (err .Error (), "invalid port number: 0" )
67+ assert .Contains (err .Error (), "must be between 1 and 65535" )
68+ }
69+
70+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_InvalidPortNegative () {
71+ assert := assert .New (suite .T ())
72+
73+ Port = - 1
74+ Config = "webhooked.yaml"
75+
76+ err := ValidateFlags ()
77+
78+ assert .Error (err )
79+ assert .Contains (err .Error (), "invalid port number: -1" )
80+ assert .Contains (err .Error (), "must be between 1 and 65535" )
81+ }
82+
83+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_InvalidPortTooHigh () {
84+ assert := assert .New (suite .T ())
85+
86+ Port = 65536
87+ Config = "webhooked.yaml"
88+
89+ err := ValidateFlags ()
90+
91+ assert .Error (err )
92+ assert .Contains (err .Error (), "invalid port number: 65536" )
93+ assert .Contains (err .Error (), "must be between 1 and 65535" )
94+ }
95+
96+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_InvalidPortVeryHigh () {
97+ assert := assert .New (suite .T ())
98+
99+ Port = 99999
100+ Config = "webhooked.yaml"
101+
102+ err := ValidateFlags ()
103+
104+ assert .Error (err )
105+ assert .Contains (err .Error (), "invalid port number: 99999" )
106+ assert .Contains (err .Error (), "must be between 1 and 65535" )
107+ }
108+
109+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_EmptyConfig () {
110+ assert := assert .New (suite .T ())
111+
112+ Port = 8080
113+ Config = ""
114+
115+ err := ValidateFlags ()
116+
117+ assert .Error (err )
118+ assert .Contains (err .Error (), "config file path is required" )
119+ }
120+
121+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_ValidConfig () {
122+ assert := assert .New (suite .T ())
123+
124+ validConfigs := []string {
125+ "webhooked.yaml" ,
126+ "/path/to/config.yaml" ,
127+ "./relative/path/config.yml" ,
128+ "config.json" ,
129+ "/absolute/path/with spaces/config.yaml" ,
130+ }
131+
132+ for _ , config := range validConfigs {
133+ Port = 8080
134+ Config = config
135+
136+ err := ValidateFlags ()
137+
138+ assert .NoError (err , "Config '%s' should be valid" , config )
139+ }
140+ }
141+
142+ func (suite * TestSuiteFlagsValidation ) TestValidateFlags_BothInvalid () {
143+ assert := assert .New (suite .T ())
144+
145+ Port = 0
146+ Config = ""
147+
148+ err := ValidateFlags ()
149+
150+ // Should return the port error first
151+ assert .Error (err )
152+ assert .Contains (err .Error (), "invalid port number" )
153+ }
154+
155+ func (suite * TestSuiteFlagsValidation ) TestDefaultValues () {
156+ assert := assert .New (suite .T ())
157+
158+ // Test that default values are set correctly
159+ // Note: These are set during package initialization
160+ assert .Equal ("webhooked.yaml" , Config )
161+ assert .Equal (8080 , Port )
162+ assert .False (Help )
163+ assert .False (Init )
164+ assert .False (Validate )
165+ assert .False (Version )
166+ assert .False (Debug )
167+ }
168+
169+ func (suite * TestSuiteFlagsValidation ) TestUsageConstant () {
170+ assert := assert .New (suite .T ())
171+
172+ // Test that usage constant contains expected content
173+ assert .Contains (usage , "Usage: webhooked [options]" )
174+ assert .Contains (usage , "--help" )
175+ assert .Contains (usage , "--version" )
176+ assert .Contains (usage , "--config" )
177+ assert .Contains (usage , "--init" )
178+ assert .Contains (usage , "--port" )
179+ assert .Contains (usage , "--validate" )
180+ }
181+
182+ func (suite * TestSuiteFlagsValidation ) TestFlagVariablesExist () {
183+ assert := assert .New (suite .T ())
184+
185+ // Test that all flag variables are accessible
186+ assert .IsType ("" , Config )
187+ assert .IsType (0 , Port )
188+ assert .IsType (false , Help )
189+ assert .IsType (false , Init )
190+ assert .IsType (false , Validate )
191+ assert .IsType (false , Version )
192+ assert .IsType (false , Debug )
193+ }
194+
195+ func (suite * TestSuiteFlagsValidation ) TestPortBoundaryValues () {
196+ assert := assert .New (suite .T ())
197+
198+ // Test exact boundary values
199+ testCases := []struct {
200+ port int
201+ valid bool
202+ message string
203+ }{
204+ {0 , false , "Port 0 should be invalid" },
205+ {1 , true , "Port 1 should be valid (minimum)" },
206+ {65535 , true , "Port 65535 should be valid (maximum)" },
207+ {65536 , false , "Port 65536 should be invalid" },
208+ }
209+
210+ for _ , tc := range testCases {
211+ Port = tc .port
212+ Config = "webhooked.yaml"
213+
214+ err := ValidateFlags ()
215+
216+ if tc .valid {
217+ assert .NoError (err , tc .message )
218+ } else {
219+ assert .Error (err , tc .message )
220+ }
221+ }
222+ }
223+
224+ func TestRunFlagsValidationSuite (t * testing.T ) {
225+ suite .Run (t , new (TestSuiteFlagsValidation ))
226+ }
227+
228+ // Benchmarks
229+
230+ func BenchmarkValidateFlags_Valid (b * testing.B ) {
231+ originalPort := Port
232+ originalConfig := Config
233+ defer func () {
234+ Port = originalPort
235+ Config = originalConfig
236+ }()
237+
238+ Port = 8080
239+ Config = "webhooked.yaml"
240+
241+ b .ResetTimer ()
242+ for i := 0 ; i < b .N ; i ++ {
243+ ValidateFlags () // nolint:errcheck
244+ }
245+ }
246+
247+ func BenchmarkValidateFlags_InvalidPort (b * testing.B ) {
248+ originalPort := Port
249+ originalConfig := Config
250+ defer func () {
251+ Port = originalPort
252+ Config = originalConfig
253+ }()
254+
255+ Port = 0
256+ Config = "webhooked.yaml"
257+
258+ b .ResetTimer ()
259+ for i := 0 ; i < b .N ; i ++ {
260+ ValidateFlags () // nolint:errcheck
261+ }
262+ }
263+
264+ func BenchmarkValidateFlags_EmptyConfig (b * testing.B ) {
265+ originalPort := Port
266+ originalConfig := Config
267+ defer func () {
268+ Port = originalPort
269+ Config = originalConfig
270+ }()
271+
272+ Port = 8080
273+ Config = ""
274+
275+ b .ResetTimer ()
276+ for i := 0 ; i < b .N ; i ++ {
277+ ValidateFlags () // nolint:errcheck
278+ }
279+ }
0 commit comments