Skip to content

Commit

Permalink
chore(internal/it): replace kubectl apply bash script with direct exec (
Browse files Browse the repository at this point in the history
#163)

Fixes: #79
  • Loading branch information
uhthomas authored May 9, 2024
1 parent 4fb8541 commit acd2e77
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions internal/it/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package it

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

"github.com/go-logr/stdr"
"github.com/jodevsa/wireguard-operator/pkg/api/v1alpha1"
. "github.com/onsi/ginkgo"
Expand All @@ -12,17 +18,11 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"log"
"os"
"os/exec"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
kind "sigs.k8s.io/kind/pkg/cluster"
log2 "sigs.k8s.io/kind/pkg/log"
"strings"
"testing"
"time"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -97,20 +97,21 @@ func WaitForPeerToBeReady(name string, namespace string) {
}

func KubectlApply(resource string, namespace string) (string, error) {
bashCommand := fmt.Sprintf("echo \"%s\" | kubectl apply -n %s --context %s -f -", strings.TrimSpace(strings.ReplaceAll(resource, "\"", "\\\"")), namespace, testKindContextName)

cmd := exec.Command("bash", "-c", bashCommand)

o, err := cmd.Output()

if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return string(exitError.Stderr), err
}
return "", err
cmd := exec.Command("kubectl", "apply",
"--context", testKindContextName,
"-n", namespace,
"-f", "-",
)
cmd.Stdin = strings.NewReader(resource)

var stdout, stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
return stderr.String(), err
}

return strings.TrimSpace(string(o)), nil
return strings.TrimSpace(stdout.String()), nil
}

var _ = BeforeSuite(func() {
Expand Down

0 comments on commit acd2e77

Please sign in to comment.