|
| 1 | +// Copyright (c) 2019-2022 Red Hat, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package controllers_test |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + dwv1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha1" |
| 24 | + dwv2 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 25 | + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 26 | + appsv1 "k8s.io/api/apps/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "sigs.k8s.io/yaml" |
| 29 | + |
| 30 | + workspacecontroller "github.com/devfile/devworkspace-operator/controllers/workspace" |
| 31 | + "github.com/devfile/devworkspace-operator/pkg/cache" |
| 32 | + "github.com/devfile/devworkspace-operator/pkg/config" |
| 33 | + "github.com/devfile/devworkspace-operator/pkg/infrastructure" |
| 34 | + . "github.com/onsi/ginkgo/v2" |
| 35 | + . "github.com/onsi/gomega" |
| 36 | + corev1 "k8s.io/api/core/v1" |
| 37 | + "k8s.io/client-go/kubernetes/scheme" |
| 38 | + "k8s.io/client-go/rest" |
| 39 | + ctrl "sigs.k8s.io/controller-runtime" |
| 40 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 41 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 42 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 43 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 44 | +) |
| 45 | + |
| 46 | +const ( |
| 47 | + testNamespace = "devworkspace-test" |
| 48 | + devWorkspaceName = "test-devworkspace" |
| 49 | +) |
| 50 | + |
| 51 | +var ( |
| 52 | + cfg *rest.Config |
| 53 | + k8sClient client.Client |
| 54 | + testEnv *envtest.Environment |
| 55 | + ctx context.Context |
| 56 | + cancel context.CancelFunc |
| 57 | + testControllerCfg = &controllerv1alpha1.OperatorConfiguration{ |
| 58 | + Routing: &controllerv1alpha1.RoutingConfig{ |
| 59 | + ClusterHostSuffix: "test-environment-cluster-suffix", |
| 60 | + }, |
| 61 | + } |
| 62 | +) |
| 63 | + |
| 64 | +func TestAPIs(t *testing.T) { |
| 65 | + // if os.Getenv("TEST_CONTROLLER") != "true" { |
| 66 | + // t.Skip() |
| 67 | + // } |
| 68 | + |
| 69 | + RegisterFailHandler(Fail) |
| 70 | + |
| 71 | + RunSpecs(t, "Controller Suite") |
| 72 | +} |
| 73 | + |
| 74 | +var _ = BeforeSuite(func() { |
| 75 | + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) |
| 76 | + |
| 77 | + ctx, cancel = context.WithCancel(context.TODO()) |
| 78 | + |
| 79 | + By("setting up controller environment") |
| 80 | + Expect(setupEnvVars()).To(Succeed()) |
| 81 | + |
| 82 | + By("bootstrapping test environment") |
| 83 | + testEnv = &envtest.Environment{ |
| 84 | + CRDDirectoryPaths: []string{filepath.Join("..", "..", "deploy", "templates", "crd", "bases")}, |
| 85 | + ErrorIfCRDPathMissing: true, |
| 86 | + } |
| 87 | + |
| 88 | + cfg, err := testEnv.Start() |
| 89 | + Expect(err).NotTo(HaveOccurred()) |
| 90 | + Expect(cfg).NotTo(BeNil()) |
| 91 | + |
| 92 | + infrastructure.InitializeForTesting(infrastructure.Kubernetes) |
| 93 | + |
| 94 | + err = controllerv1alpha1.AddToScheme(scheme.Scheme) |
| 95 | + Expect(err).NotTo(HaveOccurred()) |
| 96 | + err = dwv1.AddToScheme(scheme.Scheme) |
| 97 | + Expect(err).NotTo(HaveOccurred()) |
| 98 | + err = dwv2.AddToScheme(scheme.Scheme) |
| 99 | + Expect(err).NotTo(HaveOccurred()) |
| 100 | + |
| 101 | + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) |
| 102 | + Expect(err).NotTo(HaveOccurred()) |
| 103 | + Expect(k8sClient).NotTo(BeNil()) |
| 104 | + |
| 105 | + // Replicate controller setup similarly to how we do for main.go |
| 106 | + cacheFunc, err := cache.GetCacheFunc() |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + |
| 109 | + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 110 | + Scheme: scheme.Scheme, |
| 111 | + Port: 9443, |
| 112 | + NewCache: cacheFunc, |
| 113 | + }) |
| 114 | + Expect(err).NotTo(HaveOccurred()) |
| 115 | + // Use default config |
| 116 | + config.SetConfigForTesting(testControllerCfg) |
| 117 | + |
| 118 | + nonCachingClient, err := client.New(mgr.GetConfig(), client.Options{Scheme: scheme.Scheme}) |
| 119 | + Expect(err).NotTo(HaveOccurred()) |
| 120 | + |
| 121 | + err = mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Event{}, "involvedObject.name", func(obj client.Object) []string { |
| 122 | + ev := obj.(*corev1.Event) |
| 123 | + return []string{ev.InvolvedObject.Name} |
| 124 | + }) |
| 125 | + Expect(err).NotTo(HaveOccurred()) |
| 126 | + |
| 127 | + // Don't set up DevWorkspaceRouting Reconciler so that we can manage routings |
| 128 | + |
| 129 | + err = (&workspacecontroller.DevWorkspaceReconciler{ |
| 130 | + Client: mgr.GetClient(), |
| 131 | + NonCachingClient: nonCachingClient, |
| 132 | + Log: ctrl.Log.WithName("controllers").WithName("DevWorkspace"), |
| 133 | + Scheme: mgr.GetScheme(), |
| 134 | + }).SetupWithManager(mgr) |
| 135 | + Expect(err).NotTo(HaveOccurred()) |
| 136 | + |
| 137 | + // Skip trying to set up / test webhooks for now |
| 138 | + |
| 139 | + By("Creating Namespace for the DevWorkspace") |
| 140 | + ns := &corev1.Namespace{ |
| 141 | + ObjectMeta: metav1.ObjectMeta{ |
| 142 | + Name: testNamespace, |
| 143 | + }, |
| 144 | + } |
| 145 | + Expect(k8sClient.Create(ctx, ns)).Should(Succeed()) |
| 146 | + |
| 147 | + go func() { |
| 148 | + defer GinkgoRecover() |
| 149 | + err = mgr.Start(ctx) |
| 150 | + Expect(err).ToNot(HaveOccurred(), "failed to run manager") |
| 151 | + }() |
| 152 | +}) |
| 153 | + |
| 154 | +var _ = AfterSuite(func() { |
| 155 | + cancel() |
| 156 | + By("tearing down the test environment") |
| 157 | + err := testEnv.Stop() |
| 158 | + Expect(err).NotTo(HaveOccurred()) |
| 159 | +}) |
| 160 | + |
| 161 | +func setupEnvVars() error { |
| 162 | + bytes, err := os.ReadFile(filepath.Join("..", "..", "deploy", "templates", "components", "manager", "manager.yaml")) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + deploy := &appsv1.Deployment{} |
| 167 | + if err := yaml.Unmarshal(bytes, deploy); err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + var dwContainer *corev1.Container |
| 172 | + for _, container := range deploy.Spec.Template.Spec.Containers { |
| 173 | + if container.Name == "devworkspace-controller" { |
| 174 | + dwContainer = &container |
| 175 | + break |
| 176 | + } |
| 177 | + } |
| 178 | + if dwContainer == nil { |
| 179 | + return fmt.Errorf("could not read devworkspace-controller container from manager.yaml") |
| 180 | + } |
| 181 | + |
| 182 | + for _, envvar := range dwContainer.Env { |
| 183 | + if err := os.Setenv(envvar.Name, envvar.Value); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return nil |
| 189 | +} |
0 commit comments