Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cmd

import (
"reflect"
"testing"
)

func TestMapFromSlice(t *testing.T) {
tests := []struct {
name string
input []string
expected map[string]string
}{
{
name: "empty slice",
input: []string{},
expected: map[string]string{},
},
{
name: "single element",
input: []string{"step1"},
expected: map[string]string{"step1": ""},
},
{
name: "multiple elements",
input: []string{"step1", "step2", "step3"},
expected: map[string]string{"step1": "", "step2": "", "step3": ""},
},
{
name: "duplicate elements",
input: []string{"step1", "step1", "step2"},
expected: map[string]string{"step1": "", "step2": ""},
},
{
name: "case sensitivity",
input: []string{"Step1", "step1", "STEP1"},
expected: map[string]string{"Step1": "", "step1": "", "STEP1": ""},
},
{
name: "special characters",
input: []string{"step-1", "step_2", "step@3"},
expected: map[string]string{"step-1": "", "step_2": "", "step@3": ""},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := mapFromSlice(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("mapFromSlice(%v) = %v, want %v", tt.input, result, tt.expected)
}
})
}
}

func TestGlobalVariables(t *testing.T) {
// Test global variables are accessible and modifiable
profile = "test-profile"
if profile != "test-profile" {
t.Errorf("profile = %s, want test-profile", profile)
}

skipSteps = []string{"step1", "step2"}
if len(skipSteps) != 2 || skipSteps[0] != "step1" || skipSteps[1] != "step2" {
t.Errorf("skipSteps = %v, want [step1 step2]", skipSteps)
}

outputFormat = "json"
if outputFormat != "json" {
t.Errorf("outputFormat = %s, want json", outputFormat)
}

Config = "config.yaml"
if Config != "config.yaml" {
t.Errorf("Config = %s, want config.yaml", Config)
}
}

func TestMapFromSlice_NilInput(t *testing.T) {
result := mapFromSlice(nil)
if result == nil {
t.Error("mapFromSlice(nil) should not return nil map")
}
if len(result) != 0 {
t.Errorf("mapFromSlice(nil) = %v, want empty map", result)
}
}
143 changes: 143 additions & 0 deletions pkg/cmd-util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package pkg

import (
"testing"

"github.com/kubeslice/kubeslice-cli/pkg/internal"
)

func TestSetCliOptions(t *testing.T) {
cliParams := CliParams{
ObjectType: "project",
ObjectName: "test-project",
Namespace: "test-namespace",
FileName: "test-file.yaml",
Config: "",
OutputFormat: "json",
}
//set
SetCliOptions(cliParams)

if CliOptions.ObjectType != "project" {
t.Errorf("Expected ObjectType to be 'project', got %s", CliOptions.ObjectType)
}
if CliOptions.ObjectName != "test-project" {
t.Errorf("Expected ObjectName to be 'test-project', got %s", CliOptions.ObjectName)
}
if CliOptions.Namespace != "test-namespace" {
t.Errorf("Expected Namespace to be 'test-namespace', got %s", CliOptions.Namespace)
}
}

func TestReadAndValidateConfiguration_WithDefaults(t *testing.T) {
if defaultConfiguration == nil {
t.Fatal("Expected defaultConfiguration to not be nil")
}
if defaultConfiguration.Configuration.ClusterConfiguration.Profile != "full-demo" {
t.Errorf("Expected profile to be 'full-demo', got %s", defaultConfiguration.Configuration.ClusterConfiguration.Profile)
}
if defaultConfiguration.Configuration.KubeSliceConfiguration.ProjectName != "demo" {
t.Errorf("Expected project name to be 'demo', got %s", defaultConfiguration.Configuration.KubeSliceConfiguration.ProjectName)
}
}

func TestReadAndValidateConfiguration_WithEntProfile(t *testing.T) {
if defaultEntConfiguration == nil {
t.Fatal("Expected defaultEntConfiguration to not be nil")
}
if defaultEntConfiguration.RepoAlias != "kubeslice-ent-demo" {
t.Errorf("Expected repo alias to be 'kubeslice-ent-demo', got %s", defaultEntConfiguration.RepoAlias)
}
if defaultEntConfiguration.UIChart.ChartName != "kubeslice-ui" {
t.Errorf("Expected UI chart name to be 'kubeslice-ui', got %s", defaultEntConfiguration.UIChart.ChartName)
}
}

func TestValidateConfiguration_ValidConfig(t *testing.T) {
specs := &internal.ConfigurationSpecs{
Configuration: internal.Configuration{
ClusterConfiguration: internal.ClusterConfiguration{
ControllerCluster: internal.Cluster{
Name: "controller",
KubeConfigPath: "/path/to/kubeconfig",
ContextName: "controller-context",
},
WorkerClusters: []internal.Cluster{
{
Name: "worker1",
KubeConfigPath: "/path/to/kubeconfig",
ContextName: "worker1-context",
},
{
Name: "worker2",
KubeConfigPath: "/path/to/kubeconfig",
ContextName: "worker2-context",
},
},
},
KubeSliceConfiguration: internal.KubeSliceConfiguration{
ProjectName: "test-project",
},
HelmChartConfiguration: internal.HelmChartConfiguration{
RepoAlias: "test-repo",
RepoUrl: "https://test.com",
CertManagerChart: internal.HelmChart{
ChartName: "cert-manager",
},
ControllerChart: internal.HelmChart{
ChartName: "controller",
},
WorkerChart: internal.HelmChart{
ChartName: "worker",
},
},
},
}

errors := validateConfiguration(specs)
if len(errors) > 0 {
t.Errorf("Expected no validation errors, got %d errors: %v", len(errors), errors)
}
}

func TestValidateConfiguration_InvalidProfile(t *testing.T) {
specs := &internal.ConfigurationSpecs{
Configuration: internal.Configuration{
ClusterConfiguration: internal.ClusterConfiguration{
Profile: "invalid-profile",
},
},
}

errors := validateConfiguration(specs)
if len(errors) == 0 {
t.Error("Expected validation errors for invalid profile, got none")
}
}

func TestValidateConfiguration_MissingControllerName(t *testing.T) {
specs := &internal.ConfigurationSpecs{
Configuration: internal.Configuration{
ClusterConfiguration: internal.ClusterConfiguration{
ControllerCluster: internal.Cluster{
Name: "", // Missing name
},
},
KubeSliceConfiguration: internal.KubeSliceConfiguration{
ProjectName: "test",
},
HelmChartConfiguration: internal.HelmChartConfiguration{
RepoAlias: "test",
RepoUrl: "https://test.com",
CertManagerChart: internal.HelmChart{ChartName: "cert"},
ControllerChart: internal.HelmChart{ChartName: "ctrl"},
WorkerChart: internal.HelmChart{ChartName: "worker"},
},
},
}

errors := validateConfiguration(specs)
if len(errors) == 0 {
t.Error("Expected validation error for missing controller name")
}
}
Loading