Skip to content

test(go): remove unused controller-runtime Manager #4626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions core/server/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ func TestMain(m *testing.M) {
"../../tools/testcrds",
})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to start test environment: %v\n", err)
os.Exit(1)
panic(err)
}

code := m.Run()
if k8sEnv != nil {
k8sEnv.Stop() // No return value to handle here
}

k8sEnv.Stop()

os.Exit(code)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/run/install/install_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ var _ = BeforeSuite(func() {
cleanupK8s = k8sEnv.Stop
k8sClient = k8sEnv.Client
})

var _ = AfterSuite(func() {
cleanupK8s()
})
4 changes: 4 additions & 0 deletions pkg/run/run_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ var _ = BeforeSuite(func() {
cleanupK8s = k8sEnv.Stop
k8sClient = k8sEnv.Client
})

var _ = AfterSuite(func() {
cleanupK8s()
})
6 changes: 3 additions & 3 deletions pkg/server/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ func TestRateLimit(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(res1).To(HaveHTTPStatus(http.StatusOK))

res2, err := http.Post(s.URL+"/oauth2/sign_in", "application/json", bytes.NewReader([]byte(`{"password":"my-secret-password"}`)))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(res2).To(HaveHTTPStatus(http.StatusTooManyRequests))
g.Eventually(func() (*http.Response, error) {
return http.Post(s.URL+"/oauth2/sign_in", "application/json", bytes.NewReader([]byte(`{"password":"my-secret-password"}`)))
}).Should(HaveHTTPStatus(http.StatusTooManyRequests))

time.Sleep(time.Second)

Expand Down
77 changes: 6 additions & 71 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ import (
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

kustomizev2 "github.com/fluxcd/kustomize-controller/api/v1"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
"github.com/go-logr/logr"
"github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
Expand All @@ -31,16 +24,12 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"

"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/vendorfakes/fakelogr"
)

const BaseURI = "https://weave.works/api"

var k8sEnv *K8sTestEnv

type K8sTestEnv struct {
Expand Down Expand Up @@ -80,57 +69,30 @@ func StartK8sTestEnvironment(crdPaths []string) (*K8sTestEnv, error) {
return nil, fmt.Errorf("could not create scheme: %w", err)
}

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: []client.Object{
&corev1.Namespace{},
&corev1.Secret{},
&appsv1.Deployment{},
&corev1.ConfigMap{},
&kustomizev2.Kustomization{},
&sourcev1.GitRepository{},
&v1.CustomResourceDefinition{},
},
},
Scheme: scheme,
},
})
if err != nil {
return nil, fmt.Errorf("could not create controller manager: %w", err)
}

ctrlCtx, ctrlCancel := context.WithCancel(context.Background())

go func() {
err := k8sManager.Start(ctrlCtx)
if err != nil {
log.Fatal(err.Error())
}
}()

dc, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
ctrlCancel()
return nil, fmt.Errorf("failed to initialize discovery client: %w", err)
}

mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))

dyn, err := dynamic.NewForConfig(cfg)
if err != nil {
ctrlCancel()
return nil, fmt.Errorf("failed to initialize dynamic client: %w", err)
}

k8sClient, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("failed to initialize client: %w", err)
}

k8sEnv = &K8sTestEnv{
Env: testEnv,
Client: k8sManager.GetClient(),
Client: k8sClient,
DynClient: dyn,
RestMapper: mapper,
Rest: cfg,
Stop: func() {
ctrlCancel()
err := testEnv.Stop()
if err != nil {
log.Fatal(err.Error())
Expand All @@ -141,33 +103,6 @@ func StartK8sTestEnvironment(crdPaths []string) (*K8sTestEnv, error) {
return k8sEnv, nil
}

// MakeFakeLogr returns an API compliant logr object that can be used for unit testing.
func MakeFakeLogr() (logr.Logger, *fakelogr.LogSink) {
sink := &fakelogr.LogSink{}
sink.WithValuesStub = func(i ...interface{}) logr.LogSink {
return sink
}
sink.EnabledStub = func(i int) bool {
return true
}

return logr.New(sink), sink
}

func Setenv(k, v string) func() {
prev := os.Environ()
os.Setenv(k, v)

return func() {
os.Unsetenv(k)

for _, kv := range prev {
parts := strings.SplitN(kv, "=", 2)
os.Setenv(parts[0], parts[1])
}
}
}

// MakeRSAPrivateKey generates and returns an RSA Private Key.
func MakeRSAPrivateKey(t *testing.T) *rsa.PrivateKey {
t.Helper()
Expand Down
Loading