Skip to content

Commit

Permalink
utils tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>

test

Signed-off-by: Tullio Sebastiani <[email protected]>

test fix

Signed-off-by: Tullio Sebastiani <[email protected]>

utils test

Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani committed Nov 29, 2024
1 parent 88c70b9 commit 81c2561
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
if err != nil {
return err
}
if kubeconfigPath == nil {
return fmt.Errorf("kubeconfig not found: %s", kubeconfig)
}
volumes[*kubeconfigPath] = config.KubeconfigPath

if metricsProfile != "" {
Expand Down
3 changes: 3 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
if err != nil {
return err
}
if kubeconfigPath == nil {
return fmt.Errorf("kubeconfig not found: %s", *foundKubeconfig)
}
volumes[*kubeconfigPath] = config.KubeconfigPath
if metricsProfile != nil {
volumes[*metricsProfile] = config.MetricsProfilePath
Expand Down
28 changes: 28 additions & 0 deletions pkg/scenario_orchestrator/factory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package factory

import (
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/docker"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/podman"
"github.com/stretchr/testify/assert"
"testing"
)

func TestScenarioOrchestratorFactory_NewInstance(t *testing.T) {
typeProviderPodman := &podman.ScenarioOrchestrator{}
typeProviderDocker := &docker.ScenarioOrchestrator{}
conf, err := config.LoadConfig()
assert.Nil(t, err)
assert.NotNil(t, conf)
factory := NewScenarioOrchestratorFactory(conf)
assert.NotNil(t, factory)
scenarioDocker := factory.NewInstance(models.Docker)
assert.NotNil(t, scenarioDocker)
assert.IsType(t, scenarioDocker, typeProviderDocker)

scenarioPodman := factory.NewInstance(models.Podman)
assert.NotNil(t, scenarioPodman)
assert.IsType(t, scenarioPodman, typeProviderPodman)

}
45 changes: 39 additions & 6 deletions pkg/scenario_orchestrator/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/krkn-chaos/krknctl/internal/config"
orchestatormodels "github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models"
"github.com/krkn-chaos/krknctl/pkg/text"
"io/fs"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
Expand All @@ -26,25 +27,36 @@ type SigTermChannel struct {

func PrepareKubeconfig(kubeconfigPath *string, config config.Config) (*string, error) {
var configPath string

var configFolder string
if kubeconfigPath == nil || *kubeconfigPath == "" {
configPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
} else {
path, err := ExpandHomeFolder(*kubeconfigPath)
path, err := ExpandFolder(*kubeconfigPath, nil)
if err != nil {
return nil, err
}
configPath = *path
}

configFolder = filepath.Dir(configPath)

kubeconfig, err := clientcmd.LoadFromFile(configPath)
if err != nil {
var fserr *fs.PathError
if errors.As(err, &fserr) {
return nil, nil
}
return nil, fmt.Errorf("failed to load kubeconfig: %v", err)
}

for clusterName, cluster := range kubeconfig.Clusters {
if cluster.CertificateAuthorityData == nil && cluster.CertificateAuthority != "" {
certData, err := os.ReadFile(cluster.CertificateAuthority)
path, err := ExpandFolder(cluster.CertificateAuthority, &configFolder)
if err != nil {
return nil, err
}

certData, err := os.ReadFile(*path)
if err != nil {
return nil, fmt.Errorf("failed to load cluster certificate '%s': %v", clusterName, err)
}
Expand All @@ -55,7 +67,11 @@ func PrepareKubeconfig(kubeconfigPath *string, config config.Config) (*string, e

for authName, auth := range kubeconfig.AuthInfos {
if auth.ClientCertificateData == nil && auth.ClientCertificate != "" {
certData, err := os.ReadFile(auth.ClientCertificate)
path, err := ExpandFolder(auth.ClientCertificate, &configFolder)
if err != nil {
return nil, err
}
certData, err := os.ReadFile(*path)
if err != nil {
return nil, fmt.Errorf("failed to load user certificate '%s': %v", authName, err)
}
Expand All @@ -64,7 +80,11 @@ func PrepareKubeconfig(kubeconfigPath *string, config config.Config) (*string, e
}

if auth.ClientKeyData == nil && auth.ClientKey != "" {
keyData, err := os.ReadFile(auth.ClientKey)
path, err := ExpandFolder(auth.ClientKey, &configFolder)
if err != nil {
return nil, err
}
keyData, err := os.ReadFile(*path)
if err != nil {
return nil, fmt.Errorf("failed to load user key '%s': %v", authName, err)
}
Expand Down Expand Up @@ -141,7 +161,7 @@ func CleanLogFiles(config config.Config) (*int, error) {
return &deletedFiles, nil
}

func ExpandHomeFolder(folder string) (*string, error) {
func ExpandFolder(folder string, basePath *string) (*string, error) {
if strings.HasPrefix(folder, "~/") {
home, err := os.UserHomeDir()
if err != nil {
Expand All @@ -151,6 +171,19 @@ func ExpandHomeFolder(folder string) (*string, error) {
expandedPath := filepath.Join(home, replacedHome)
return &expandedPath, nil
}
if filepath.IsAbs(folder) == false {
if basePath != nil {
path := filepath.Join(*basePath, folder)
return &path, nil
} else {
path, err := filepath.Abs(folder)
if err != nil {
return nil, err
}
return &path, nil
}

}
return &folder, nil
}

Expand Down
Loading

0 comments on commit 81c2561

Please sign in to comment.