|
| 1 | +/* |
| 2 | +Copyright 2025 Red Hat, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package machinemigration |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + |
| 27 | + configv1builder "github.com/openshift/cluster-api-actuator-pkg/testutils/resourcebuilder/config/v1" |
| 28 | + "github.com/openshift/cluster-capi-operator/pkg/test" |
| 29 | + |
| 30 | + "k8s.io/apimachinery/pkg/api/meta" |
| 31 | + "k8s.io/apimachinery/pkg/runtime" |
| 32 | + "k8s.io/client-go/rest" |
| 33 | + "k8s.io/klog/v2" |
| 34 | + "k8s.io/klog/v2/textlogger" |
| 35 | + |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 39 | + "sigs.k8s.io/controller-runtime/pkg/envtest/komega" |
| 40 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 41 | + //+kubebuilder:scaffold:imports |
| 42 | +) |
| 43 | + |
| 44 | +const ( |
| 45 | + timeout = time.Second * 2 |
| 46 | +) |
| 47 | + |
| 48 | +var cfg *rest.Config |
| 49 | +var k8sClient client.Client |
| 50 | +var testEnv *envtest.Environment |
| 51 | +var testScheme *runtime.Scheme |
| 52 | +var testRESTMapper meta.RESTMapper |
| 53 | +var ctx = context.Background() |
| 54 | + |
| 55 | +func TestAPIs(t *testing.T) { |
| 56 | + RegisterFailHandler(Fail) |
| 57 | + |
| 58 | + RunSpecs(t, "Controller Suite") |
| 59 | +} |
| 60 | + |
| 61 | +var _ = BeforeSuite(func() { |
| 62 | + klog.SetOutput(GinkgoWriter) |
| 63 | + |
| 64 | + logf.SetLogger(textlogger.NewLogger(textlogger.NewConfig())) |
| 65 | + |
| 66 | + By("bootstrapping test environment") |
| 67 | + var err error |
| 68 | + testEnv = &envtest.Environment{} |
| 69 | + cfg, k8sClient, err = test.StartEnvTest(testEnv) |
| 70 | + |
| 71 | + Expect(err).NotTo(HaveOccurred()) |
| 72 | + Expect(cfg).NotTo(BeNil()) |
| 73 | + Expect(k8sClient).NotTo(BeNil()) |
| 74 | + |
| 75 | + infrastructure := configv1builder.Infrastructure().AsAWS("test", "eu-west-2").WithName("cluster").Build() |
| 76 | + Expect(k8sClient.Create(ctx, infrastructure)).To(Succeed()) |
| 77 | + |
| 78 | + httpClient, err := rest.HTTPClientFor(cfg) |
| 79 | + Expect(err).NotTo(HaveOccurred()) |
| 80 | + Expect(httpClient).NotTo(BeNil()) |
| 81 | + |
| 82 | + testRESTMapper, err = apiutil.NewDynamicRESTMapper(cfg, httpClient) |
| 83 | + Expect(err).NotTo(HaveOccurred()) |
| 84 | + |
| 85 | + komega.SetClient(k8sClient) |
| 86 | + komega.SetContext(ctx) |
| 87 | +}) |
| 88 | + |
| 89 | +var _ = AfterSuite(func() { |
| 90 | + By("tearing down the test environment") |
| 91 | + err := testEnv.Stop() |
| 92 | + Expect(err).NotTo(HaveOccurred()) |
| 93 | +}) |
0 commit comments