Skip to content

Commit

Permalink
Minor improvements to the integration test framework.
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Hallgren <[email protected]>
  • Loading branch information
thallgren committed Nov 23, 2023
1 parent 7255412 commit ae1afe9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

func Test_Integration(t *testing.T) {
moduleRoot, err := filepath.Abs("..")
ossRoot, err := filepath.Abs(".")
if err != nil {
t.Fatalf("unable to get absolute path of .oss: %v", err)
}
itest.RunTests(itest.TestContext(t, moduleRoot, moduleRoot))
itest.RunTests(itest.TestContext(t, ossRoot, filepath.Dir(ossRoot)))
}
4 changes: 2 additions & 2 deletions integration_test/itest/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (s *cluster) Initialize(ctx context.Context) context.Context {
func (s *cluster) tearDown(ctx context.Context) {
s.ensureQuit(ctx)
if s.kubeConfig != "" {
ctx = WithWorkingDir(ctx, filepath.Join(GetOSSRoot(ctx), "integration_test"))
ctx = WithWorkingDir(ctx, GetOSSRoot(ctx))
_ = Run(ctx, "kubectl", "delete", "-f", filepath.Join("testdata", "k8s", "client_rbac.yaml"))
_ = Run(ctx, "kubectl", "delete", "--wait=false", "ns", "-l", "purpose=tp-cli-testing")
}
Expand Down Expand Up @@ -569,7 +569,7 @@ func (s *cluster) InstallTrafficManagerVersion(ctx context.Context, version stri
func (s *cluster) installChart(ctx context.Context, release bool, chartFilename string, values map[string]string) error {
settings := s.self.GetValuesForHelm(ctx, values, release)

ctx = WithWorkingDir(ctx, filepath.Join(GetOSSRoot(ctx), "integration_test"))
ctx = WithWorkingDir(ctx, GetOSSRoot(ctx))
nss := GetNamespaces(ctx)
args := []string{"install", "-n", nss.Namespace, "--wait"}
args = append(args, settings...)
Expand Down
4 changes: 2 additions & 2 deletions integration_test/itest/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func GetOSSRoot(ctx context.Context) string {
if dir, ok := ctx.Value(ossRootKey{}).(string); ok {
return dir
}
moduleRoot, err := os.Getwd()
ossRoot, err := os.Getwd()
if err != nil {
panic("failed to get current directory")
}
return filepath.Dir(moduleRoot)
return ossRoot
}

// SetOSSRoot sets the OSS module root for the given context to dir.
Expand Down
23 changes: 13 additions & 10 deletions integration_test/itest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ func (h *harness) HarnessContext() context.Context {
}

func (h *harness) RunSuite(s TestingSuite) {
ctx := h.HarnessContext()
if suiteEnabled(h.HarnessContext(), s) {
h.HarnessT().Run(s.SuiteName(), func(t *testing.T) { suite.Run(t, s) })
}
}

func suiteEnabled(ctx context.Context, s TestingSuite) bool {
suiteRx := dos.Getenv(ctx, "TEST_SUITE")
if suiteRx != "" {
r, err := regexp.Compile(suiteRx)
if err != nil {
getT(ctx).Fatal(err)
}
if !r.MatchString(s.SuiteName()) {
return
}
if suiteRx == "" {
return true
}
r, err := regexp.Compile(suiteRx)
if err != nil {
getT(ctx).Fatal(err)
}
h.HarnessT().Run(s.SuiteName(), func(t *testing.T) { suite.Run(t, s) })
return r.MatchString(s.SuiteName())
}

// SetupSuite calls all functions that has been added with AddSetup in the order they
Expand Down
2 changes: 1 addition & 1 deletion integration_test/itest/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *nsPair) setup(ctx context.Context) bool {
if t.Failed() {
return false
}
err := Kubectl(ctx, s.Namespace, "apply", "-f", filepath.Join(GetOSSRoot(ctx), "integration_test", "testdata", "k8s", "client_sa.yaml"))
err := Kubectl(ctx, s.Namespace, "apply", "-f", filepath.Join(GetOSSRoot(ctx), "testdata", "k8s", "client_sa.yaml"))
assert.NoError(t, err, "failed to create connect ServiceAccount")
return !t.Failed()
}
Expand Down
8 changes: 5 additions & 3 deletions integration_test/itest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ func (r *runner) RunTests(c context.Context) { //nolint:gocognit
t := getT(c)
for _, f := range r.withCluster {
s := f(c)
t.Run(s.SuiteName(), func(t *testing.T) {
suite.Run(t, f(c))
})
if suiteEnabled(c, s) {
t.Run(s.SuiteName(), func(t *testing.T) {
suite.Run(t, f(c))
})
}
}
}()
for s, sr := range r.withSuffix {
Expand Down
14 changes: 10 additions & 4 deletions integration_test/itest/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package itest
import (
"context"
"encoding/json"
"errors"

"github.com/stretchr/testify/require"

Expand All @@ -13,18 +14,23 @@ import (
type StatusResponse struct {
RootDaemon cmd.RootDaemonStatus `json:"root_daemon,omitempty"`
UserDaemon cmd.UserDaemonStatus `json:"user_daemon,omitempty"`
Error string `json:"err,omitempty"`
}

func TelepresenceStatus(ctx context.Context, args ...string) (*StatusResponse, error) {
stdout, stderr, err := Telepresence(ctx, append([]string{"status", "--output", "json"}, args...)...)
var status StatusResponse
jErr := json.Unmarshal([]byte(stdout), &status)
if err != nil {
if jErr == nil && status.Error != "" {
dlog.Error(ctx, status.Error)
return nil, errors.New(status.Error)
}
dlog.Error(ctx, stderr)
return nil, err
}
var status StatusResponse
err = json.Unmarshal([]byte(stdout), &status)
if err != nil {
return nil, err
if jErr != nil {
return nil, jErr
}
return &status, nil
}
Expand Down
2 changes: 1 addition & 1 deletion integration_test/multi_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *multiConnectSuite) SetupSuite() {
}()

ctx2 := itest.WithNamespaces(ctx, &itest.Namespaces{Namespace: s.mgrSpace2, ManagedNamespaces: []string{s.appSpace2}})
err := itest.Kubectl(ctx2, s.mgrSpace2, "apply", "-f", filepath.Join(itest.GetOSSRoot(ctx2), "integration_test", "testdata", "k8s", "client_sa.yaml"))
err := itest.Kubectl(ctx2, s.mgrSpace2, "apply", "-f", filepath.Join(itest.GetOSSRoot(ctx2), "testdata", "k8s", "client_sa.yaml"))
require.NoError(err, "failed to create connect ServiceAccount")

ctx2 = itest.WithUser(ctx2, s.mgrSpace2+":"+itest.TestUser)
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/cli/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type UserDaemonStatus struct {
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
ManagerNamespace string `json:"manager_namespace,omitempty" yaml:"manager_namespace,omitempty"`
MappedNamespaces []string `json:"mapped_namespaces,omitempty" yaml:"mapped_namespaces,omitempty"`
Intercepts []connectStatusIntercept `json:"intercepts,omitempty" yaml:"intercepts,omitempty"`
Intercepts []ConnectStatusIntercept `json:"intercepts,omitempty" yaml:"intercepts,omitempty"`
}

type connectStatusIntercept struct {
type ConnectStatusIntercept struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Client string `json:"client,omitempty" yaml:"client,omitempty"`
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func BasicGetStatusInfo(ctx context.Context) (ioutil.WriterTos, error) {
us.KubernetesServer = status.ClusterServer
us.KubernetesContext = status.ClusterContext
for _, icept := range status.GetIntercepts().GetIntercepts() {
us.Intercepts = append(us.Intercepts, connectStatusIntercept{
us.Intercepts = append(us.Intercepts, ConnectStatusIntercept{
Name: icept.Spec.Name,
Client: icept.Spec.Client,
})
Expand Down

0 comments on commit ae1afe9

Please sign in to comment.