Skip to content

Commit

Permalink
Unit test to ensure install pkg do not import cloud providers.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Sep 3, 2024
1 parent 8ae667e commit 30a9224
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package install

import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

goruntime "runtime"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -147,3 +153,45 @@ func TestDaemonSetIsReady(t *testing.T) {
require.NoError(t, err)
assert.True(t, ready)
}

// Prevent https://github.com/vmware-tanzu/velero/issues/8157
func TestPkgImportNoCloudProvider(t *testing.T) {
_, filename, _, ok := goruntime.Caller(0)
if !ok {
t.Fatalf("No caller information")
}
t.Logf("Current test file path: %s", filename)
t.Logf("Current test directory: %s", filepath.Dir(filename)) // should be "pkg/install"
// go list -f {{.Deps}} ./pkg/install/
cmd := exec.Command(
"go",
"list",
"-f",
"{{.Deps}}",
".",
)
// set cmd.Dir to this package even if executed from different dir
cmd.Dir = filepath.Dir(filename)
output, err := cmd.Output()
require.NoError(t, err)
// split dep by line, replace space with newline
deps := strings.ReplaceAll(string(output), " ", "\n")
require.NotEmpty(t, deps)
// ignore k8s.io
k8sio, err := regexp.Compile("^k8s.io")
require.NoError(t, err)
cloudProvider, err := regexp.Compile("aws|gcp|azure")
require.NoError(t, err)
// depsArr :=[]string{}
cloudProviderDeps := []string{}
for _, dep := range strings.Split(deps, "\n") {
if !k8sio.MatchString(dep) {
// depsArr = append(depsArr, dep)
if cloudProvider.MatchString(dep) {
cloudProviderDeps = append(cloudProviderDeps, dep)
}
}
}
// TODO: expected to fail until #8145 rebase
require.Empty(t, cloudProviderDeps)
}

0 comments on commit 30a9224

Please sign in to comment.