Skip to content

Commit

Permalink
test: add ability to override/add extra helm flags for ginkgo tests.
Browse files Browse the repository at this point in the history
This adds -cilium.install-helm-overrides flag which provides a way to
add or override test flags without modifying the test code.

This is useful for forked projects to extend testing by providing their
own custom flags to the test program.

For example, you could extend the tests by doing:

-cilium.install-helm-overrides="foo=bar,\"zap={a,b,c}\"" which would
add: --set foo=bar and --set zap={a,b,c} to the cilium install params.

Signed-off-by: Tom Hadlaw <tom.hadlaw@isovalent.com>
  • Loading branch information
tommyp1ckles authored and rolinh committed Aug 1, 2024
1 parent b189c57 commit 2aabd55
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type CiliumTestConfigType struct {
Kubeconfig string
KubectlPath string
RegistryCredentials string
InstallHelmOverrides string

// Multinode enables the running of tests that involve more than one
// node. If false, some tests will silently skip multinode checks.
Multinode bool
Expand Down Expand Up @@ -97,6 +99,9 @@ func (c *CiliumTestConfigType) ParseFlags() {
flagset.BoolVar(&c.RunQuarantined, "cilium.runQuarantined", false,
"Run tests that are under quarantine.")
flagset.BoolVar(&c.Help, "cilium.help", false, "Display this help message.")
flagset.StringVar(&c.InstallHelmOverrides, "cilium.install-helm-overrides", "",
"Comma separated list of cilium install helm --set overrides. "+
"*note*: This will take precedence over any other value set by the tests")

args := make([]string, 0, len(os.Args))
for index, flag := range os.Args {
Expand Down
36 changes: 36 additions & 0 deletions test/helpers/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package helpers
import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"os"
"path"
Expand Down Expand Up @@ -78,6 +80,9 @@ const (
)

var (
// cliOverrideOptions are populated from -cilium.install-helm-overrides.
cliOverrideOptions = map[string]string{}

// defaultHelmOptions are passed to helm in ciliumInstallHelm, unless
// overridden by options passed in at invocation. In those cases, the test
// has a specific need to override the option.
Expand Down Expand Up @@ -280,6 +285,33 @@ func Init() {
// preflight must match the cilium agent image (that's the point)
defaultHelmOptions["preflight.image.repository"] = defaultHelmOptions["image.repository"]
defaultHelmOptions["preflight.image.tag"] = defaultHelmOptions["image.tag"]

if config.CiliumTestConfig.InstallHelmOverrides != "" {
parseHelmOverrides(config.CiliumTestConfig.InstallHelmOverrides, cliOverrideOptions)
}
}

func parseHelmOverrides(overridesStr string, overrides map[string]string) {
rdr := csv.NewReader(strings.NewReader(overridesStr))
rdr.LazyQuotes = true
for {
record, err := rdr.Read()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
fmt.Fprintf(os.Stderr, "failed to parse install-helm-overrides %q: %v\n", config.CiliumTestConfig.InstallHelmOverrides, err)
os.Exit(1)
}
for _, row := range record {
toks := strings.Split(row, "=")
if len(toks) != 2 {
fmt.Fprintf(os.Stderr, "failed to parse install-helm-overrides value %q, unexpected format (should be x.y.z=foo)\n", row)
os.Exit(1)
}
overrides[toks[0]] = toks[1]
}
}
}

// GetCurrentK8SEnv returns the value of K8S_VERSION from the OS environment.
Expand Down Expand Up @@ -2602,6 +2634,10 @@ func (kub *Kubectl) overwriteHelmOptions(options map[string]string) error {
options["ipv6.enabled"] = "false"
}

for k, v := range cliOverrideOptions {
options[k] = v
}

return nil
}

Expand Down
24 changes: 24 additions & 0 deletions test/helpers/kubectl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package helpers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_parseHelmOverrides(t *testing.T) {
// This should be able to parse key value pairs, including those
// where the value has commas but is delimited by quotes.
overridesStr := "key1=value1,\"key2={a,b,c}\",\"key3={}\""
expected := map[string]string{
"key1": "value1",
"key2": "{a,b,c}",
"key3": "{}",
}
overrides := map[string]string{}
parseHelmOverrides(overridesStr, overrides)
assert.Equal(t, expected, overrides)
}

0 comments on commit 2aabd55

Please sign in to comment.