This commit is contained in:
Kristoffer Dalby
2025-07-15 14:51:23 +00:00
parent 024ed59ea9
commit 8253d588c6
31 changed files with 300 additions and 364 deletions

View File

@@ -40,7 +40,7 @@ func TestServeCommand(t *testing.T) {
},
)
assertNoErr(t, err)
// Help text should contain expected information
assert.Contains(t, result, "serve", "help should mention serve command")
assert.Contains(t, result, "Launches the headscale server", "help should contain command description")
@@ -83,7 +83,7 @@ func TestServeCommandValidation(t *testing.T) {
// We'll test that it accepts extra args without crashing immediately
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
// Use a goroutine to test that the command doesn't immediately fail
done := make(chan error, 1)
go func() {
@@ -97,7 +97,7 @@ func TestServeCommandValidation(t *testing.T) {
)
done <- err
}()
select {
case err := <-done:
// If it returns an error quickly, it should be about args validation
@@ -132,28 +132,28 @@ func TestServeCommandHealthCheck(t *testing.T) {
t.Run("test_serve_health_endpoint", func(t *testing.T) {
// Test that the serve command starts a server that responds to health checks
// This is effectively testing that the server is running and accessible
// Get the server endpoint
endpoint := headscale.GetEndpoint()
assert.NotEmpty(t, endpoint, "headscale endpoint should not be empty")
// Make a simple HTTP request to verify the server is running
healthURL := fmt.Sprintf("%s/health", endpoint)
// Use a timeout to avoid hanging
client := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get(healthURL)
if err != nil {
// If we can't connect, check if it's because server isn't ready
assert.Contains(t, err.Error(), "connection",
assert.Contains(t, err.Error(), "connection",
"health check failure should be connection-related if server not ready")
} else {
defer resp.Body.Close()
// If we can connect, verify we get a reasonable response
assert.True(t, resp.StatusCode >= 200 && resp.StatusCode < 500,
assert.True(t, resp.StatusCode >= 200 && resp.StatusCode < 500,
"health endpoint should return reasonable status code")
}
})
@@ -162,24 +162,24 @@ func TestServeCommandHealthCheck(t *testing.T) {
// Test that the serve command starts a server with API endpoints
endpoint := headscale.GetEndpoint()
assert.NotEmpty(t, endpoint, "headscale endpoint should not be empty")
// Try to access a known API endpoint (version info)
// This tests that the gRPC gateway is running
versionURL := fmt.Sprintf("%s/api/v1/version", endpoint)
client := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := client.Get(versionURL)
if err != nil {
// Connection errors are acceptable if server isn't fully ready
assert.Contains(t, err.Error(), "connection",
assert.Contains(t, err.Error(), "connection",
"API endpoint failure should be connection-related if server not ready")
} else {
defer resp.Body.Close()
// If we can connect, check that we get some response
assert.True(t, resp.StatusCode >= 200 && resp.StatusCode < 500,
assert.True(t, resp.StatusCode >= 200 && resp.StatusCode < 500,
"API endpoint should return reasonable status code")
}
})
@@ -205,7 +205,7 @@ func TestServeCommandServerBehavior(t *testing.T) {
t.Run("test_serve_accepts_connections", func(t *testing.T) {
// Test that the server accepts connections from clients
// This is a basic integration test to ensure serve works
// Create a user for testing
user := spec.Users[0]
_, err := headscale.Execute(
@@ -217,7 +217,7 @@ func TestServeCommandServerBehavior(t *testing.T) {
},
)
assertNoErr(t, err)
// Create a pre-auth key
result, err := headscale.Execute(
[]string{
@@ -229,7 +229,7 @@ func TestServeCommandServerBehavior(t *testing.T) {
},
)
assertNoErr(t, err)
// Verify the preauth key creation worked
assert.NotEmpty(t, result, "preauth key creation should produce output")
assert.Contains(t, result, "key", "preauth key output should contain key field")
@@ -238,7 +238,7 @@ func TestServeCommandServerBehavior(t *testing.T) {
t.Run("test_serve_handles_node_operations", func(t *testing.T) {
// Test that the server can handle basic node operations
_ = spec.Users[0] // Test user for context
// List nodes (should work even if empty)
result, err := headscale.Execute(
[]string{
@@ -249,10 +249,10 @@ func TestServeCommandServerBehavior(t *testing.T) {
},
)
assertNoErr(t, err)
// Should return valid JSON array (even if empty)
trimmed := strings.TrimSpace(result)
assert.True(t, strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]"),
assert.True(t, strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]"),
"nodes list should return JSON array")
})
@@ -267,12 +267,12 @@ func TestServeCommandServerBehavior(t *testing.T) {
},
)
assertNoErr(t, err)
// Should return valid JSON array
trimmed := strings.TrimSpace(result)
assert.True(t, strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]"),
assert.True(t, strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]"),
"users list should return JSON array")
// Should contain our test user
assert.Contains(t, result, spec.Users[0], "users list should contain test user")
})
@@ -299,7 +299,7 @@ func TestServeCommandEdgeCases(t *testing.T) {
// Test that the server can handle multiple rapid commands
// This tests the server's ability to handle concurrent requests
user := spec.Users[0]
// Create user first
_, err := headscale.Execute(
[]string{
@@ -310,7 +310,7 @@ func TestServeCommandEdgeCases(t *testing.T) {
},
)
assertNoErr(t, err)
// Execute multiple commands rapidly
for i := 0; i < 3; i++ {
result, err := headscale.Execute(
@@ -334,7 +334,7 @@ func TestServeCommandEdgeCases(t *testing.T) {
},
)
assertNoErr(t, err)
// Basic help should work
result, err := headscale.Execute(
[]string{
@@ -357,7 +357,7 @@ func TestServeCommandEdgeCases(t *testing.T) {
)
// Should fail gracefully for non-existent commands
assert.Error(t, err, "should fail gracefully for non-existent commands")
// Should not cause server to crash (we can still execute other commands)
result, err := headscale.Execute(
[]string{
@@ -369,4 +369,4 @@ func TestServeCommandEdgeCases(t *testing.T) {
assertNoErr(t, err)
assert.NotEmpty(t, result, "server should still work after malformed request")
})
}
}