From 81fd66e634825a37f7905f75650fe53f478b6ad3 Mon Sep 17 00:00:00 2001 From: panshuxiao Date: Wed, 14 May 2025 10:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E6=97=B6=E5=88=A0=E9=99=A4=E4=BA=86mo?= =?UTF-8?q?dules/k8s/controller/devcontainer/suite=5Ftest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- .../devcontainerapp_controller_test.go | 94 ------------------ .../k8s/controller/devcontainer/suite_test.go | 96 ------------------- 3 files changed, 1 insertion(+), 191 deletions(-) delete mode 100644 modules/k8s/controller/devcontainer/devcontainerapp_controller_test.go delete mode 100644 modules/k8s/controller/devcontainer/suite_test.go diff --git a/Makefile b/Makefile index add78894b8..ffffe60363 100644 --- a/Makefile +++ b/Makefile @@ -469,7 +469,7 @@ watch-backend: go-check test: test-frontend test-backend .PHONY: test-backend -test-backend:k8s-download-test-bins +test-backend: @echo "Running go test with $(GOTESTFLAGS) -tags '$(TEST_TAGS)'..." @$(GO) test $(GOTESTFLAGS) -tags='$(TEST_TAGS)' $(GO_TEST_PACKAGES) diff --git a/modules/k8s/controller/devcontainer/devcontainerapp_controller_test.go b/modules/k8s/controller/devcontainer/devcontainerapp_controller_test.go deleted file mode 100644 index f729673b78..0000000000 --- a/modules/k8s/controller/devcontainer/devcontainerapp_controller_test.go +++ /dev/null @@ -1,94 +0,0 @@ -/* -Copyright 2024. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package devcontainer - -import ( - "context" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/reconcile" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - devcontainerv1 "code.gitea.io/gitea/modules/k8s/api/v1" -) - -var _ = Describe("DevcontainerApp Controller", func() { - Context("When reconciling a resource", func() { - const resourceName = "test-resource" - - ctx := context.Background() - - typeNamespacedName := types.NamespacedName{ - Name: resourceName, - Namespace: "default", // TODO(user):Modify as needed - } - devcontainerapp := &devcontainerv1.DevcontainerApp{} - - BeforeEach(func() { - By("creating the custom resource for the Kind DevcontainerApp") - err := k8sClient.Get(ctx, typeNamespacedName, devcontainerapp) - if err != nil && errors.IsNotFound(err) { - resource := &devcontainerv1.DevcontainerApp{ - ObjectMeta: metav1.ObjectMeta{ - Name: resourceName, - Namespace: "default", - }, - // TODO(user): Specify other spec details if needed. - Spec: devcontainerv1.DevcontainerAppSpec{ - StatefulSet: devcontainerv1.StatefulSetSpec{ - // 添加必要的命令 - Command: []string{"/bin/sh", "-c", "sleep infinity"}, - // 添加 SSH 公钥列表 - SSHPublicKeyList: []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/test-key"}, - // 其他需要的字段 - Image: "busybox:latest", - }, - }, - } - Expect(k8sClient.Create(ctx, resource)).To(Succeed()) - } - }) - - AfterEach(func() { - // TODO(user): Cleanup logic after each test, like removing the resource instance. - resource := &devcontainerv1.DevcontainerApp{} - err := k8sClient.Get(ctx, typeNamespacedName, resource) - Expect(err).NotTo(HaveOccurred()) - - By("Cleanup the specific resource instance DevcontainerApp") - Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) - }) - It("should successfully reconcile the resource", func() { - By("Reconciling the created resource") - controllerReconciler := &DevcontainerAppReconciler{ - Client: k8sClient, - Scheme: k8sClient.Scheme(), - } - - _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ - NamespacedName: typeNamespacedName, - }) - Expect(err).NotTo(HaveOccurred()) - // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. - // Example: If you expect a certain status condition after reconciliation, verify it here. - }) - }) -}) diff --git a/modules/k8s/controller/devcontainer/suite_test.go b/modules/k8s/controller/devcontainer/suite_test.go deleted file mode 100644 index 94905d2951..0000000000 --- a/modules/k8s/controller/devcontainer/suite_test.go +++ /dev/null @@ -1,96 +0,0 @@ -/* -Copyright 2024. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package devcontainer - -import ( - "context" - "fmt" - "path/filepath" - "runtime" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" - - devcontainerv1 "code.gitea.io/gitea/modules/k8s/api/v1" - // +kubebuilder:scaffold:imports -) - -// These tests use Ginkgo (BDD-style Go testing framework). Refer to -// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. - -var cfg *rest.Config -var k8sClient client.Client -var testEnv *envtest.Environment -var ctx context.Context -var cancel context.CancelFunc - -func TestControllers(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecs(t, "Controller Suite") -} - -var _ = BeforeSuite(func() { - logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - - ctx, cancel = context.WithCancel(context.TODO()) - - By("bootstrapping test environment") - testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "devcontainer", "crd", "bases")}, - ErrorIfCRDPathMissing: true, - - // The BinaryAssetsDirectory is only required if you want to run the tests directly - // without call the makefile target test. If not informed it will look for the - // default path defined in controller-runtime which is /usr/local/kubebuilder/. - // Note that you must have the required binaries setup under the bin directory to perform - // the tests directly. When we run make test it will be setup and used automatically. - BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", - fmt.Sprintf("1.31.0-%s-%s", runtime.GOOS, runtime.GOARCH)), - } - - var err error - // cfg is defined in this file globally. - cfg, err = testEnv.Start() - Expect(err).NotTo(HaveOccurred()) - Expect(cfg).NotTo(BeNil()) - - err = devcontainerv1.AddToScheme(scheme.Scheme) - Expect(err).NotTo(HaveOccurred()) - - // +kubebuilder:scaffold:scheme - - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) - Expect(err).NotTo(HaveOccurred()) - Expect(k8sClient).NotTo(BeNil()) - -}) - -var _ = AfterSuite(func() { - By("tearing down the test environment") - cancel() - err := testEnv.Stop() - Expect(err).NotTo(HaveOccurred()) -})