Skip to content

Commit

Permalink
chore: adding count test to ensure expected count of yaml objects
Browse files Browse the repository at this point in the history
  • Loading branch information
karlderkaefer committed Nov 30, 2024
1 parent 312b77c commit 012ca75
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
36 changes: 36 additions & 0 deletions helm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"fmt"
"os/exec"
"strings"
"testing"
"time"

Expand All @@ -17,6 +20,10 @@ func TestUpdateDependenciesWithRefresh(t *testing.T) {
duration := time.Since(startTime)
assert.NoError(t, err)
t.Logf("UpdateChart took with refresh %s", duration)

numberObject, err := countObjects(chartPath)
assert.NoError(t, err)
assert.Equal(t, 40, numberObject, "Number of objects in umbrella chart")
}

func TestUpdateDependenciesWithoutRefresh(t *testing.T) {
Expand All @@ -32,4 +39,33 @@ func TestUpdateDependenciesWithoutRefresh(t *testing.T) {
duration := time.Since(startTime)
assert.NoError(t, err)
t.Logf("UpdateChart took with refresh %s", duration)

numberObject, err := countObjects(chartPath)
assert.NoError(t, err)
assert.Equal(t, 40, numberObject, "Number of objects in umbrella chart")
}

// render helm template of chart and count yaml objects
func countObjects(chartPath string) (int, error) {
output, err := runHelmTemplate(chartPath)
if err != nil {
return 0, err
}
yamlDocs := strings.Split(output, "\n---\n")
count := 0
for _, doc := range yamlDocs {
if strings.TrimSpace(doc) != "" {
count++
}
}
return count, nil
}

func runHelmTemplate(chartPath string) (string, error) {
cmd := exec.Command("helm", "template", chartPath)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to run helm template: %w", err)
}
return string(output), nil
}
4 changes: 4 additions & 0 deletions kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (r *RegistryHelper) InitKubeClient() error {
}

func (r *RegistryHelper) UpdateRegistryInfo() error {
if len(r.Registries) == 0 {
log.Printf("No secrets provided, skipping registry update from kubeclient")
return nil
}
err := r.InitKubeClient()
if err != nil {
return err
Expand Down
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,31 @@ import (
)

func main() {
// chartPath := flag.String("chartPath", ".", "path to the chart")
chartPath := flag.String("chartPath", ".", "path to the chart")
secretNamespace := flag.String("secretNamespace", "argocd", "namespace where the secret is located")
secretNames := flag.String("registries", os.Getenv("HELM_DEPS_SECRET_NAMES"), "comma separated list of registries to update")
// DEPRECATED: this flag not required anymore, since the repo secret contains OCI flag already
// if repo credentials have this flag we need to use registry login insteaad of repo add
addRegistries := flag.String("add-registries", os.Getenv("HELM_DEPS_SECRET_NAMES_REPO_ADD"), "DEPRECATED only registries flag is required: comma separated list of registries using 'helm repo add'")
skipGlobalRefresh := flag.Bool("skip-global-refresh", false, "skip global refresh of all registries at the start. This can improve performance in combination with HELM_DEPS_SKIP_REFRESH=true")
flag.Parse()

comnbinedSecretNames := strings.Join([]string{*secretNames, *addRegistries}, ",")
registryHelper := NewRegistryHelper(comnbinedSecretNames, *secretNamespace)
err := registryHelper.LoginAll()
registryHelper.UpdateRegistryInfo()

Check failure on line 22 in main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `registryHelper.UpdateRegistryInfo` is not checked (errcheck)

if !*skipGlobalRefresh {
err := registryHelper.LoginAll()
if err != nil {
log.Fatal(err)
}
defer registryHelper.LogoutAll() // nolint: errcheck
}
updater := HelmUpdater{
registryHelper: registryHelper,
}
err := updater.UpdateChart(*chartPath)
if err != nil {
log.Fatal(err)
}
defer registryHelper.LogoutAll() // nolint: errcheck

// err := updateDependencies(*chartPath, 1)
// if err != nil {
// log.Fatal(err)
// }
}

0 comments on commit 012ca75

Please sign in to comment.