Skip to content

Commit

Permalink
Add test coverage for local Helm repo functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Rascao <luis.rascao@gmail.com>
  • Loading branch information
lrascao committed Dec 6, 2023
1 parent ef33cf6 commit 2838a92
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tests/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,10 @@ func installTest(details VersionDetails, opts TestOptions) func(t *testing.T) {
}
if !details.UseDaprLatestVersion {
// TODO: Pass dashboard-version also when charts are released.
args = append(args, "--runtime-version", details.RuntimeVersion)
args = append(args, []string{
"--runtime-version", details.RuntimeVersion,
"--dashboard-version", details.DashboardVersion,
}...)
}
if opts.HAEnabled {
args = append(args, "--enable-ha")
Expand Down
131 changes: 131 additions & 0 deletions tests/e2e/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ limitations under the License.
package kubernetes_test

import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/dapr/cli/tests/e2e/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestKubernetesNonHAModeMTLSDisabled(t *testing.T) {
Expand Down Expand Up @@ -500,3 +508,126 @@ func TestK8sInstallwithoutRuntimeVersionwithMarinerImagesFlag(t *testing.T) {
t.Run(tc.Name, tc.Callable)
}
}

func TestKubernetesLocalFileHelmRepoInstall(t *testing.T) {
// ensure clean env for test
ensureCleanEnv(t, false)

// create a temp dir to store the helm repo
helmRepoPath, err := os.MkdirTemp("", "dapr-e2e-kube-with-env-*")
assert.NoError(t, err)
// defer os.RemoveAll(helmRepoPath) // clean up

// copy all .tar.gz files from testdata dir and uncompress them
copyAndUncompressTarGzFiles(t, helmRepoPath)

// point the env var to the dir containing both dapr and dapr-dashboard helm charts
t.Setenv("DAPR_HELM_REPO_URL", helmRepoPath)

// setup tests
tests := []common.TestCase{}
tests = append(tests, common.GetTestsOnInstall(currentVersionDetails, common.TestOptions{
HAEnabled: false,
MTLSEnabled: false,
ApplyComponentChanges: true,
CheckResourceExists: map[common.Resource]bool{
common.CustomResourceDefs: true,
common.ClusterRoles: true,
common.ClusterRoleBindings: true,
},
})...)

tests = append(tests, common.GetTestsOnUninstall(currentVersionDetails, common.TestOptions{
CheckResourceExists: map[common.Resource]bool{
common.CustomResourceDefs: true,
common.ClusterRoles: false,
common.ClusterRoleBindings: false,
},
})...)

// execute tests
for _, tc := range tests {
t.Run(tc.Name, tc.Callable)
}
}

func copyAndUncompressTarGzFiles(t *testing.T, destination string) {
// find all .tar.gz files in testdata dir
files, err := filepath.Glob(filepath.Join("testdata", "*.tgz"))
require.NoError(t, err)

for _, file := range files {
// untar the dapr/dashboard helm .tar.gz, get back the root dir of the untarred files
// it's either 'dapr' or 'dapr-dashboard'
rootDir, err := untarDaprHelmGzFile(file, destination)
require.NoError(t, err)

// rename the root dir to the base name of the .tar.gz file
// (eg. /var/folders/4s/w0gdrc957k11vbkgyhjrk12w0000gn/T/dapr-e2e-kube-with-env-404115459/dapr-1.12.0)
base := filepath.Base(strings.TrimSuffix(file, filepath.Ext(file)))
err = os.Rename(filepath.Join(destination, rootDir), filepath.Join(destination, base))
require.NoError(t, err)
}
}

func untarDaprHelmGzFile(file string, destination string) (string, error) {
// open the tar.gz file
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()

// create a gzip reader
gr, err := gzip.NewReader(f)
if err != nil {
return "", err
}
defer gr.Close()

// create a tar reader
tr := tar.NewReader(gr)

rootDir := ""
// iterate through all the files in the tarball
for {
hdr, err := tr.Next()
if err == io.EOF {
break // end of tarball
}
if err != nil {
return "", err
}

// build the full destination path
filename := filepath.Join(destination, hdr.Name)

// ensure the destination directory exists
dir := filepath.Dir(filename)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0700); err != nil {
return "", err
}
}

// the root dir for all files is the same
rootDir = strings.FieldsFunc(hdr.Name,
func(c rune) bool {
return os.PathSeparator == c
})[0]

// create the destination file
dstFile, err := os.Create(filename)
if err != nil {
return "", err
}
defer dstFile.Close()

// copy the file contents
if _, err := io.Copy(dstFile, tr); err != nil {
return "", err
}
}

return rootDir, nil
}
Binary file added tests/e2e/kubernetes/testdata/dapr-1.12.0.tgz
Binary file not shown.
Binary file not shown.

0 comments on commit 2838a92

Please sign in to comment.