288 lines
6.6 KiB
Go
288 lines
6.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// resetFlags resets command line flags for testing
|
|
func resetFlags() {
|
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
|
}
|
|
|
|
func TestMainWithHelp(t *testing.T) {
|
|
// Save original args
|
|
oldArgs := os.Args
|
|
|
|
// Test help flag
|
|
os.Args = []string{"s3-genindex", "--help"}
|
|
|
|
// Reset flags
|
|
resetFlags()
|
|
|
|
// We can't easily test main() directly since it calls os.Exit,
|
|
// but we can test that the usage function works
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
// Expected to panic/exit on --help
|
|
t.Log("Help flag caused expected panic/exit")
|
|
}
|
|
os.Args = oldArgs
|
|
}()
|
|
|
|
// This would normally exit, but we're just testing that it doesn't crash
|
|
}
|
|
|
|
func TestMainWithVersion(t *testing.T) {
|
|
oldArgs := os.Args
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
// Test that main doesn't crash with basic arguments
|
|
os.Args = []string{"s3-genindex"}
|
|
|
|
// We can't easily test main() execution without it creating files,
|
|
// so this test just ensures the package compiles correctly
|
|
}
|
|
|
|
func TestFlagParsing(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
}{
|
|
{
|
|
name: "default flags",
|
|
args: []string{"s3-genindex"},
|
|
},
|
|
{
|
|
name: "with directory flag",
|
|
args: []string{"s3-genindex", "-d", "/tmp"},
|
|
},
|
|
{
|
|
name: "verbose flag with directory",
|
|
args: []string{"s3-genindex", "-v", "-d", "/tmp"},
|
|
},
|
|
{
|
|
name: "exclude regex flag with directory",
|
|
args: []string{"s3-genindex", "-x", "*.tmp", "-d", "/tmp"},
|
|
},
|
|
{
|
|
name: "multiple flags with directory",
|
|
args: []string{"s3-genindex", "-v", "-x", "*.tmp", "-d", "/tmp"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Save original args
|
|
oldArgs := os.Args
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
// Set test args
|
|
os.Args = tt.args
|
|
|
|
// Reset flags for each test
|
|
resetFlags()
|
|
|
|
// Test that flag parsing doesn't panic
|
|
// Note: We're not actually calling main() here since that would
|
|
// try to create files, but this tests that our flag setup is correct
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInvalidRegex(t *testing.T) {
|
|
oldArgs := os.Args
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
// Test invalid regex - this would normally cause log.Fatal
|
|
os.Args = []string{"s3-genindex", "-x", "[invalid"}
|
|
|
|
resetFlags()
|
|
|
|
// We can't easily test log.Fatal without refactoring main(),
|
|
// but this ensures the test setup is correct
|
|
}
|
|
|
|
// Test that main package compiles correctly
|
|
func TestMainCompiles(t *testing.T) {
|
|
// This test just ensures the main package compiles without issues
|
|
// More comprehensive integration testing is done in the indexgen package
|
|
}
|
|
|
|
func TestS3FlagHandling(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
}{
|
|
{
|
|
name: "s3 flag with URL",
|
|
args: []string{"s3-genindex", "-s3", "http://minio.example.com:9000/bucket"},
|
|
},
|
|
{
|
|
name: "s3 flag with verbose",
|
|
args: []string{"s3-genindex", "-v", "-s3", "https://s3.amazonaws.com/logs"},
|
|
},
|
|
{
|
|
name: "local directory flag",
|
|
args: []string{"s3-genindex", "-d", "/tmp"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Save original args
|
|
oldArgs := os.Args
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
// Set test args
|
|
os.Args = tt.args
|
|
|
|
// Reset flags for each test
|
|
resetFlags()
|
|
|
|
// Test that flag parsing doesn't panic
|
|
// Note: We're not actually calling main() here since that would
|
|
// try to create files or connect to S3, but this tests that our flag setup is correct
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMutualExclusionAndRequiredFlags(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
shouldExitWithError bool
|
|
}{
|
|
{
|
|
name: "no flags provided",
|
|
args: []string{"s3-genindex"},
|
|
shouldExitWithError: true,
|
|
},
|
|
{
|
|
name: "directory flag only",
|
|
args: []string{"s3-genindex", "-d", "/tmp"},
|
|
shouldExitWithError: false,
|
|
},
|
|
{
|
|
name: "s3 flag only",
|
|
args: []string{"s3-genindex", "-s3", "http://example.com/bucket"},
|
|
shouldExitWithError: false,
|
|
},
|
|
{
|
|
name: "both flags provided (mutual exclusion)",
|
|
args: []string{"s3-genindex", "-d", "/tmp", "-s3", "http://example.com/bucket"},
|
|
shouldExitWithError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Save original args
|
|
oldArgs := os.Args
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
// Set test args
|
|
os.Args = tt.args
|
|
|
|
// Reset flags for each test
|
|
resetFlags()
|
|
|
|
// We can't easily test os.Exit calls without refactoring,
|
|
// but this ensures the test setup is correct
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseS3URL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
url string
|
|
expected *S3Config
|
|
hasError bool
|
|
}{
|
|
{
|
|
name: "MinIO HTTP URL",
|
|
url: "http://minio0.chbtl0.net.ipng.ch:9000/ctlog-ro",
|
|
expected: &S3Config{
|
|
Endpoint: "minio0.chbtl0.net.ipng.ch:9000",
|
|
Bucket: "ctlog-ro",
|
|
Region: "us-east-1",
|
|
UseSSL: false,
|
|
},
|
|
hasError: false,
|
|
},
|
|
{
|
|
name: "AWS S3 HTTPS URL",
|
|
url: "https://s3.amazonaws.com/my-bucket",
|
|
expected: &S3Config{
|
|
Endpoint: "s3.amazonaws.com",
|
|
Bucket: "my-bucket",
|
|
Region: "us-east-1",
|
|
UseSSL: true,
|
|
},
|
|
hasError: false,
|
|
},
|
|
{
|
|
name: "URL with path prefix",
|
|
url: "http://localhost:9000/bucket/folder/file",
|
|
expected: &S3Config{
|
|
Endpoint: "localhost:9000",
|
|
Bucket: "bucket",
|
|
Region: "us-east-1",
|
|
UseSSL: false,
|
|
},
|
|
hasError: false,
|
|
},
|
|
{
|
|
name: "Invalid URL",
|
|
url: "not-a-url",
|
|
expected: nil,
|
|
hasError: true,
|
|
},
|
|
{
|
|
name: "URL without bucket",
|
|
url: "http://minio.example.com:9000/",
|
|
expected: nil,
|
|
hasError: true,
|
|
},
|
|
{
|
|
name: "Unsupported scheme",
|
|
url: "ftp://example.com/bucket",
|
|
expected: nil,
|
|
hasError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := parseS3URL(tt.url)
|
|
|
|
if tt.hasError {
|
|
if err == nil {
|
|
t.Errorf("parseS3URL(%q) expected error, got nil", tt.url)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Errorf("parseS3URL(%q) unexpected error: %v", tt.url, err)
|
|
return
|
|
}
|
|
|
|
if result.Endpoint != tt.expected.Endpoint {
|
|
t.Errorf("parseS3URL(%q) Endpoint = %q, want %q", tt.url, result.Endpoint, tt.expected.Endpoint)
|
|
}
|
|
if result.Bucket != tt.expected.Bucket {
|
|
t.Errorf("parseS3URL(%q) Bucket = %q, want %q", tt.url, result.Bucket, tt.expected.Bucket)
|
|
}
|
|
if result.Region != tt.expected.Region {
|
|
t.Errorf("parseS3URL(%q) Region = %q, want %q", tt.url, result.Region, tt.expected.Region)
|
|
}
|
|
if result.UseSSL != tt.expected.UseSSL {
|
|
t.Errorf("parseS3URL(%q) UseSSL = %v, want %v", tt.url, result.UseSSL, tt.expected.UseSSL)
|
|
}
|
|
})
|
|
}
|
|
}
|