Remove stubbed version and fix tests

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
Dimitris Karakasilis
2025-09-24 14:29:28 +03:00
parent 5fb15c81f6
commit fac5dfb32d
3 changed files with 60 additions and 76 deletions

View File

@@ -5,7 +5,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/kairos-io/kairos-sdk/kcrypt/bus"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@@ -26,53 +25,46 @@ var _ = Describe("CLI Interface", func() {
_ = os.Remove("/tmp/kcrypt-challenger-client.log") _ = os.Remove("/tmp/kcrypt-challenger-client.log")
}) })
Context("CLI help and version", func() { Context("CLI help", func() {
It("should show help when --help is used", func() { It("should show help when --help is used", func() {
exitCode := RunCLIMode([]string{"--help"}) err := ExecuteWithArgs([]string{"--help"})
Expect(exitCode).To(Equal(0)) Expect(err).To(BeNil())
// We can't easily test the output content without complex output capture, // We can't easily test the output content without complex output capture,
// but we can verify the function executes and returns the correct exit code // but we can verify the function executes without error
})
It("should show version when --version is used", func() {
exitCode := RunCLIMode([]string{"--version"})
Expect(exitCode).To(Equal(0))
// We can't easily test the output content without complex output capture,
// but we can verify the function executes and returns the correct exit code
}) })
}) })
Context("Input validation", func() { Context("Input validation", func() {
It("should require all partition parameters", func() { It("should require all partition parameters for get command", func() {
exitCode := RunCLIMode([]string{"--partition-name=/dev/sda2"}) err := ExecuteWithArgs([]string{"get"})
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Should exit with error code 1 when required parameters are missing // Should return an error when required parameters are missing
}) })
It("should validate that all required fields are provided", func() { It("should validate that all required fields are provided for get command", func() {
// Test missing UUID // Test with valid partition parameters
exitCode := RunCLIMode([]string{"--partition-name=/dev/sda2", "--partition-label=test"}) err := ExecuteWithArgs([]string{"get", "--partition-name=/dev/sda2"})
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred()) // Should fail at client connection but parsing should work
// Test missing label // Test with valid UUID
exitCode = RunCLIMode([]string{"--partition-name=/dev/sda2", "--partition-uuid=12345"}) err = ExecuteWithArgs([]string{"get", "--partition-uuid=12345"})
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred()) // Should fail at client connection but parsing should work
}) })
It("should handle invalid flags gracefully", func() { It("should handle invalid flags gracefully", func() {
exitCode := RunCLIMode([]string{"--invalid-flag"}) err := ExecuteWithArgs([]string{"--invalid-flag"})
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// FlagSet should handle the error and return exit code 1 // Should return an error for invalid flags
}) })
}) })
Context("Flow detection and backend integration", func() { Context("Flow detection and backend integration", func() {
It("should attempt to get passphrase with valid parameters", func() { It("should attempt to get passphrase with valid parameters", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid-12345", "--partition-uuid=test-uuid-12345",
"--partition-label=test-label", "--partition-label=test-label",
@@ -80,7 +72,7 @@ var _ = Describe("CLI Interface", func() {
}) })
// We expect this to fail since there's no server, but it should reach the backend logic // We expect this to fail since there's no server, but it should reach the backend logic
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Should show flow detection in the log (if created) // Should show flow detection in the log (if created)
logContent, readErr := os.ReadFile("/tmp/kcrypt-challenger-client.log") logContent, readErr := os.ReadFile("/tmp/kcrypt-challenger-client.log")
@@ -93,7 +85,8 @@ var _ = Describe("CLI Interface", func() {
It("should use the correct backend client logic", func() { It("should use the correct backend client logic", func() {
// Test that the CLI mode uses the same GetPassphrase method // Test that the CLI mode uses the same GetPassphrase method
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
@@ -101,32 +94,11 @@ var _ = Describe("CLI Interface", func() {
}) })
// Should fail but attempt to use the client // Should fail but attempt to use the client
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// The important thing is that it reaches the backend and doesn't crash // The important thing is that it reaches the backend and doesn't crash
}) })
}) })
Context("Event validation", func() {
It("should correctly identify valid events", func() {
// Test that discovery.password is recognized as a valid event
Expect(isEventDefined("discovery.password")).To(BeTrue())
Expect(isEventDefined(string(bus.EventDiscoveryPassword))).To(BeTrue())
})
It("should reject invalid events", func() {
// Test that invalid events are rejected
Expect(isEventDefined("invalid.event")).To(BeFalse())
Expect(isEventDefined("")).To(BeFalse())
Expect(isEventDefined(123)).To(BeFalse())
})
It("should route to plugin mode for valid events", func() {
// This would be the behavior when called with discovery.password
isValid := isEventDefined("discovery.password")
Expect(isValid).To(BeTrue())
})
})
Context("Configuration overrides with debug logging", func() { Context("Configuration overrides with debug logging", func() {
var tempDir string var tempDir string
var originalLogFile string var originalLogFile string
@@ -180,7 +152,8 @@ var _ = Describe("CLI Interface", func() {
}) })
It("should read and use original configuration values without overrides", func() { It("should read and use original configuration values without overrides", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
@@ -189,7 +162,7 @@ var _ = Describe("CLI Interface", func() {
}) })
// Should fail at passphrase retrieval but config parsing should work // Should fail at passphrase retrieval but config parsing should work
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Check that original configuration values are logged // Check that original configuration values are logged
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -210,7 +183,8 @@ var _ = Describe("CLI Interface", func() {
}) })
It("should show configuration file values being overridden by CLI flags", func() { It("should show configuration file values being overridden by CLI flags", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
@@ -222,7 +196,7 @@ var _ = Describe("CLI Interface", func() {
}) })
// Should fail at passphrase retrieval but config parsing and overrides should work // Should fail at passphrase retrieval but config parsing and overrides should work
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Check that both original and overridden values are logged // Check that both original and overridden values are logged
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -248,7 +222,8 @@ var _ = Describe("CLI Interface", func() {
}) })
It("should apply CLI flag overrides and log configuration changes", func() { It("should apply CLI flag overrides and log configuration changes", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
@@ -260,7 +235,7 @@ var _ = Describe("CLI Interface", func() {
}) })
// Should fail at passphrase retrieval but flag parsing should work // Should fail at passphrase retrieval but flag parsing should work
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Check if debug log exists and contains configuration information // Check if debug log exists and contains configuration information
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -275,7 +250,8 @@ var _ = Describe("CLI Interface", func() {
}) })
It("should show original vs final configuration in debug mode", func() { It("should show original vs final configuration in debug mode", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
@@ -285,7 +261,7 @@ var _ = Describe("CLI Interface", func() {
}) })
// Should fail but debug information should be logged // Should fail but debug information should be logged
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Check for original and final configuration logging // Check for original and final configuration logging
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -298,7 +274,8 @@ var _ = Describe("CLI Interface", func() {
}) })
It("should log partition details in debug mode", func() { It("should log partition details in debug mode", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/custom-partition", "--partition-name=/dev/custom-partition",
"--partition-uuid=custom-uuid-123", "--partition-uuid=custom-uuid-123",
"--partition-label=custom-label-456", "--partition-label=custom-label-456",
@@ -306,7 +283,7 @@ var _ = Describe("CLI Interface", func() {
"--attempts=2", "--attempts=2",
}) })
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Check for partition details in debug log // Check for partition details in debug log
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -321,14 +298,15 @@ var _ = Describe("CLI Interface", func() {
}) })
It("should not log debug information without debug flag", func() { It("should not log debug information without debug flag", func() {
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
"--attempts=1", "--attempts=1",
}) })
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Debug log should not exist or should not contain detailed debug info // Debug log should not exist or should not contain detailed debug info
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -344,7 +322,8 @@ var _ = Describe("CLI Interface", func() {
// Remove the config file to test default behavior // Remove the config file to test default behavior
_ = os.RemoveAll(configDir) _ = os.RemoveAll(configDir)
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/test", "--partition-name=/dev/test",
"--partition-uuid=test-uuid", "--partition-uuid=test-uuid",
"--partition-label=test-label", "--partition-label=test-label",
@@ -353,7 +332,7 @@ var _ = Describe("CLI Interface", func() {
}) })
// Should fail at passphrase retrieval but not due to config parsing // Should fail at passphrase retrieval but not due to config parsing
Expect(exitCode).To(Equal(1)) Expect(err).To(HaveOccurred())
// Check that default/empty configuration values are logged // Check that default/empty configuration values are logged
logContent, readErr := os.ReadFile(testLogFile) logContent, readErr := os.ReadFile(testLogFile)
@@ -374,25 +353,22 @@ var _ = Describe("CLI Interface", func() {
It("should parse all arguments correctly", func() { It("should parse all arguments correctly", func() {
// This will fail at the client creation/server connection, // This will fail at the client creation/server connection,
// but should successfully parse all arguments // but should successfully parse all arguments
exitCode := RunCLIMode([]string{ err := ExecuteWithArgs([]string{
"get",
"--partition-name=/dev/custom", "--partition-name=/dev/custom",
"--partition-uuid=custom-uuid-999", "--partition-uuid=custom-uuid-999",
"--partition-label=custom-label", "--partition-label=custom-label",
"--attempts=5", "--attempts=5",
}) })
Expect(exitCode).To(Equal(1)) // Fails due to no server Expect(err).To(HaveOccurred()) // Fails due to no server
// The important thing is that flag parsing worked and it reached the backend // The important thing is that flag parsing worked and it reached the backend
}) })
It("should handle boolean flags correctly", func() { It("should handle boolean flags correctly", func() {
// Test help flag // Test help flag
exitCode := RunCLIMode([]string{"-help"}) err := ExecuteWithArgs([]string{"--help"})
Expect(exitCode).To(Equal(0)) Expect(err).To(BeNil())
// Test version flag
exitCode = RunCLIMode([]string{"-version"})
Expect(exitCode).To(Equal(0))
}) })
}) })
}) })

View File

@@ -207,6 +207,15 @@ func main() {
} }
} }
// ExecuteWithArgs executes the root command with the given arguments.
// This function is used by tests to simulate CLI execution.
func ExecuteWithArgs(args []string) error {
// Set command arguments (this overrides os.Args)
rootCmd.SetArgs(args)
return rootCmd.Execute()
}
// runTPMHash handles the root command - TPM hash generation // runTPMHash handles the root command - TPM hash generation
func runTPMHash() error { func runTPMHash() error {
// Create logger based on debug flag // Create logger based on debug flag

View File

@@ -69,8 +69,7 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil()) Expect(k8sClient).NotTo(BeNil())
})
}, 60)
var _ = AfterSuite(func() { var _ = AfterSuite(func() {
By("tearing down the test environment") By("tearing down the test environment")