@@ -0,0 +1,13 @@
|
||||
// Package ansible provides utilities for working with Ansible
|
||||
package ansible
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestAnsiblePlaybookExistence tests that the RunPlaybook function exists with the right signature
|
||||
func TestAnsiblePlaybookExistence(t *testing.T) {
|
||||
// Just check that the function exists with the right signature
|
||||
var _ func(string, string, []string) error = RunPlaybook
|
||||
}
|
||||
|
||||
@@ -47,4 +47,3 @@ func cleanupInteractive() {
|
||||
fmt.Println("Invalid choice")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRootCmd(t *testing.T) {
|
||||
// Check that rootCmd has the expected attributes
|
||||
if rootCmd == nil {
|
||||
t.Fatalf("rootCmd is nil")
|
||||
}
|
||||
|
||||
if rootCmd.Use != "provision" {
|
||||
t.Errorf("rootCmd.Use = %q, want \"provision\"", rootCmd.Use)
|
||||
}
|
||||
|
||||
if rootCmd.Short == "" {
|
||||
t.Errorf("rootCmd.Short is empty")
|
||||
}
|
||||
|
||||
if rootCmd.Long == "" {
|
||||
t.Errorf("rootCmd.Long is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubCommands(t *testing.T) {
|
||||
// Check that the subcommands are properly registered
|
||||
commands := rootCmd.Commands()
|
||||
|
||||
// Helper function to find a command by name
|
||||
findCmd := func(name string) bool {
|
||||
for _, cmd := range commands {
|
||||
if cmd.Use == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Check provision command
|
||||
if !findCmd("provision") {
|
||||
t.Errorf("\"provision\" command not found in rootCmd")
|
||||
}
|
||||
|
||||
// Check cleanup command
|
||||
if !findCmd("cleanup") {
|
||||
t.Errorf("\"cleanup\" command not found in rootCmd")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteFunction(t *testing.T) {
|
||||
// Testing Execute() directly will run the entire CLI, which isn't suitable for unit testing
|
||||
// Instead, we check that the function exists with the correct signature
|
||||
var _ func() error = Execute
|
||||
}
|
||||
|
||||
// We can't really test exitWithError because it calls os.Exit, which would terminate the test
|
||||
func TestExitWithErrorSignature(t *testing.T) {
|
||||
// Just check the function signature
|
||||
var _ func(string, error) = exitWithError
|
||||
}
|
||||
|
||||
func TestProvisionCmd(t *testing.T) {
|
||||
// Check that provisionCmd has the expected attributes
|
||||
if provisionCmd == nil {
|
||||
t.Fatalf("provisionCmd is nil")
|
||||
}
|
||||
|
||||
if provisionCmd.Use != "provision" {
|
||||
t.Errorf("provisionCmd.Use = %q, want \"provision\"", provisionCmd.Use)
|
||||
}
|
||||
|
||||
if provisionCmd.Short == "" {
|
||||
t.Errorf("provisionCmd.Short is empty")
|
||||
}
|
||||
|
||||
if provisionCmd.Long == "" {
|
||||
t.Errorf("provisionCmd.Long is empty")
|
||||
}
|
||||
|
||||
// Check that the Run function is defined
|
||||
if provisionCmd.Run == nil {
|
||||
t.Errorf("provisionCmd.Run is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupCmd(t *testing.T) {
|
||||
// Check that cleanupCmd has the expected attributes
|
||||
if cleanupCmd == nil {
|
||||
t.Fatalf("cleanupCmd is nil")
|
||||
}
|
||||
|
||||
if cleanupCmd.Use != "cleanup" {
|
||||
t.Errorf("cleanupCmd.Use = %q, want \"cleanup\"", cleanupCmd.Use)
|
||||
}
|
||||
|
||||
if cleanupCmd.Short == "" {
|
||||
t.Errorf("cleanupCmd.Short is empty")
|
||||
}
|
||||
|
||||
if cleanupCmd.Long == "" {
|
||||
t.Errorf("cleanupCmd.Long is empty")
|
||||
}
|
||||
|
||||
// Check that the Run function is defined
|
||||
if cleanupCmd.Run == nil {
|
||||
t.Errorf("cleanupCmd.Run is nil")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetRepoRoot(t *testing.T) {
|
||||
// Test basic functionality
|
||||
root, err := GetRepoRoot()
|
||||
if err != nil {
|
||||
t.Fatalf("GetRepoRoot() error = %v", err)
|
||||
}
|
||||
if root == "" {
|
||||
t.Fatalf("GetRepoRoot() returned empty string")
|
||||
}
|
||||
|
||||
// Reset the cache to test multiple calls
|
||||
repoRootCache = ""
|
||||
repoRootOnce = sync.Once{}
|
||||
|
||||
// First call
|
||||
root1, err := GetRepoRoot()
|
||||
if err != nil {
|
||||
t.Fatalf("GetRepoRoot() first call error = %v", err)
|
||||
}
|
||||
|
||||
// Second call should return the same result
|
||||
root2, err := GetRepoRoot()
|
||||
if err != nil {
|
||||
t.Fatalf("GetRepoRoot() second call error = %v", err)
|
||||
}
|
||||
|
||||
// Results should be the same (cached)
|
||||
if root1 != root2 {
|
||||
t.Errorf("GetRepoRoot() cache failed: got %q then %q", root1, root2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirExists(t *testing.T) {
|
||||
// Create a temporary directory
|
||||
tempDir, err := os.MkdirTemp("", "dir-test")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Test with existing directory
|
||||
if !dirExists(tempDir) {
|
||||
t.Errorf("dirExists(%q) = false, want true", tempDir)
|
||||
}
|
||||
|
||||
// Test with non-existent directory
|
||||
nonExistentDir := filepath.Join(tempDir, "non-existent")
|
||||
if dirExists(nonExistentDir) {
|
||||
t.Errorf("dirExists(%q) = true, want false", nonExistentDir)
|
||||
}
|
||||
|
||||
// Create a file and test
|
||||
filePath := filepath.Join(tempDir, "testfile")
|
||||
if err := os.WriteFile(filePath, []byte("test"), 0o644); err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// A file should return false
|
||||
if dirExists(filePath) {
|
||||
t.Errorf("dirExists(%q) for a file = true, want false", filePath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
// Create a temporary directory
|
||||
tempDir, err := os.MkdirTemp("", "file-test")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Test with non-existent file
|
||||
nonExistentFile := filepath.Join(tempDir, "non-existent")
|
||||
if fileExists(nonExistentFile) {
|
||||
t.Errorf("fileExists(%q) = true, want false", nonExistentFile)
|
||||
}
|
||||
|
||||
// Create a file and test
|
||||
filePath := filepath.Join(tempDir, "testfile")
|
||||
if err := os.WriteFile(filePath, []byte("test"), 0o644); err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// The file should exist
|
||||
if !fileExists(filePath) {
|
||||
t.Errorf("fileExists(%q) = false, want true", filePath)
|
||||
}
|
||||
|
||||
// A directory should return false
|
||||
if fileExists(tempDir) {
|
||||
t.Errorf("fileExists(%q) for a directory = true, want false", tempDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAnsiblePath(t *testing.T) {
|
||||
// Skip this test as it depends on specific directory structure
|
||||
t.Skip("Skipping path test that depends on specific directory structure")
|
||||
}
|
||||
|
||||
func TestGetScriptsPath(t *testing.T) {
|
||||
// Skip this test as it depends on specific directory structure
|
||||
t.Skip("Skipping path test that depends on specific directory structure")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package interactive
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Testing interactive prompts is challenging since they require user input
|
||||
// Here we test the function signatures and basic structure
|
||||
|
||||
func TestPromptFunctionsExist(t *testing.T) {
|
||||
// Verify that the functions have the expected signatures
|
||||
var _ func(string, string) (string, error) = PromptText
|
||||
var _ func(string) (string, error) = PromptPassword
|
||||
var _ func(string, []string) (string, error) = PromptSelect
|
||||
var _ func(string) (bool, error) = PromptConfirm
|
||||
var _ func(string, int) (int, error) = PromptInt
|
||||
var _ func(string, int, int, int) (int, error) = PromptIntWithRange
|
||||
var _ func(map[string]interface{}) (map[string]interface{}, error) = PromptYamlVariables
|
||||
}
|
||||
|
||||
// Test the PromptIntWithRange function without mocking
|
||||
func TestPromptIntWithRange_Validation(t *testing.T) {
|
||||
// Since we can't replace the PromptInt function for testing,
|
||||
// we'll test only the behaviors we can verify:
|
||||
|
||||
// 1. Check the function exists and has the correct signature
|
||||
var promptIntRangeFn func(string, int, int, int) (int, error) = PromptIntWithRange
|
||||
|
||||
if promptIntRangeFn == nil {
|
||||
t.Error("PromptIntWithRange function is nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPromptYamlVariablesTypes tests the variable handling logic
|
||||
func TestPromptYamlVariablesTypes(t *testing.T) {
|
||||
// We can't test the full function without mocking, but we can
|
||||
// verify that it exists with the right signature
|
||||
|
||||
var promptYamlVarsFn func(map[string]interface{}) (map[string]interface{}, error) = PromptYamlVariables
|
||||
|
||||
if promptYamlVarsFn == nil {
|
||||
t.Error("PromptYamlVariables function is nil")
|
||||
}
|
||||
|
||||
// Skip the actual testing since we can't mock the dependencies
|
||||
t.Skip("PromptYamlVariables requires user input, skipping detailed testing")
|
||||
}
|
||||
|
||||
// For better testability in the future, consider refactoring the prompt functions like this:
|
||||
/*
|
||||
// Interfaces for prompt functionality that can be mocked for testing
|
||||
type Prompter interface {
|
||||
PromptText(message string, defaultValue string) (string, error)
|
||||
PromptInt(message string, defaultValue int) (int, error)
|
||||
PromptConfirm(message string) (bool, error)
|
||||
// ...other methods
|
||||
}
|
||||
|
||||
// Implementation using survey
|
||||
type SurveyPrompter struct{}
|
||||
|
||||
func (p *SurveyPrompter) PromptText(message string, defaultValue string) (string, error) {
|
||||
// Real implementation
|
||||
}
|
||||
|
||||
// Then in other code:
|
||||
type Handler struct {
|
||||
Prompter Prompter
|
||||
}
|
||||
|
||||
func (h *Handler) PromptIntWithRange(message string, defaultValue, min, max int) (int, error) {
|
||||
// Use h.Prompter.PromptInt
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestLoadDefaultConfig(t *testing.T) {
|
||||
// This test depends on the actual defaults file existing
|
||||
// Skip the test if we get an error, which likely means we're not in the right environment
|
||||
t.Skip("Skipping test that depends on specific file location")
|
||||
}
|
||||
|
||||
func TestSaveConfig(t *testing.T) {
|
||||
// Create a sample config
|
||||
config := &Config{
|
||||
KubernetesVersion: "1.24",
|
||||
PodCIDR: "192.168.0.0/16",
|
||||
ServiceCIDR: "10.96.0.0/16",
|
||||
CNIPlugin: "calico",
|
||||
CalicoVersion: "v3.24.1",
|
||||
ControlPlaneCPUs: 2,
|
||||
ControlPlaneMemory: "4G",
|
||||
ControlPlaneDisk: "20G",
|
||||
WorkerCPUs: 2,
|
||||
WorkerMemory: "2G",
|
||||
WorkerDisk: "20G",
|
||||
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
|
||||
KubernetesPackages: []string{"kubelet", "kubeadm", "kubectl"},
|
||||
}
|
||||
|
||||
// Save the config
|
||||
path, err := SaveConfig(config)
|
||||
if err != nil {
|
||||
t.Fatalf("SaveConfig() error = %v", err)
|
||||
}
|
||||
defer os.RemoveAll(filepath.Dir(path)) // Clean up temporary directory
|
||||
|
||||
// Check that the file exists
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatalf("SaveConfig() file not accessible: %v", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
t.Fatalf("SaveConfig() created a directory instead of a file")
|
||||
}
|
||||
|
||||
// Read the file contents
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read config file: %v", err)
|
||||
}
|
||||
|
||||
// Parse the YAML directly to ensure it's valid YAML
|
||||
var parsedConfig map[string]interface{}
|
||||
err = yaml.Unmarshal(data, &parsedConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse YAML: %v", err)
|
||||
}
|
||||
|
||||
// Check specific fields
|
||||
if version, ok := parsedConfig["kubernetes_version"].(string); !ok || version != "1.24" {
|
||||
t.Errorf("kubernetes_version = %v, want \"1.24\"", parsedConfig["kubernetes_version"])
|
||||
}
|
||||
|
||||
if podCIDR, ok := parsedConfig["pod_cidr"].(string); !ok || podCIDR != "192.168.0.0/16" {
|
||||
t.Errorf("pod_cidr = %v, want \"192.168.0.0/16\"", parsedConfig["pod_cidr"])
|
||||
}
|
||||
|
||||
if serviceCIDR, ok := parsedConfig["service_cidr"].(string); !ok || serviceCIDR != "10.96.0.0/16" {
|
||||
t.Errorf("service_cidr = %v, want \"10.96.0.0/16\"", parsedConfig["service_cidr"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestProvisionInteractive is difficult to test because it requires user input
|
||||
// and interactions with external systems.
|
||||
func TestProvisionInteractive_Existence(t *testing.T) {
|
||||
// Just check that the function exists and has the right signature
|
||||
var _ func() error = ProvisionInteractive
|
||||
}
|
||||
|
||||
// TestCleanup is difficult to test because it runs an external script.
|
||||
func TestCleanup_Existence(t *testing.T) {
|
||||
// Just check that the function exists and has the right signature
|
||||
var _ func() error = Cleanup
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Testing main() is challenging because it doesn't return and may call os.Exit
|
||||
// We can only test that the package compiles and builds correctly
|
||||
|
||||
func TestMainPackageCompiles(t *testing.T) {
|
||||
// Simply having this test compile confirms the main package compiles
|
||||
t.Log("main package compiles successfully")
|
||||
}
|
||||
|
||||
func TestMainCanBeBuilt(t *testing.T) {
|
||||
// Skip if go is not in PATH
|
||||
_, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
t.Skip("go command not found in PATH, skipping build test")
|
||||
}
|
||||
|
||||
// Save current working directory
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working directory: %v", err)
|
||||
}
|
||||
|
||||
// Create a temporary directory for the build
|
||||
tempDir, err := os.MkdirTemp("", "provision-build")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// Prepare build command that won't actually run the binary
|
||||
cmd := exec.Command("go", "build", "-o", tempDir)
|
||||
cmd.Dir = wd
|
||||
|
||||
// Execute the build command
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("Build failed: %v\nOutput: %s", err, output)
|
||||
}
|
||||
|
||||
// Check that the binary was created
|
||||
files, err := os.ReadDir(tempDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read build directory: %v", err)
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
t.Errorf("No binary was produced by the build")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user