-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
316 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2023. | ||
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 functional_test | ||
|
||
import ( | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
openstackclientv1 "github.com/openstack-k8s-operators/openstack-operator/apis/client/v1beta1" | ||
) | ||
|
||
func GetDefaultOpenStackClientSpec() map[string]interface{} { | ||
return map[string]interface{}{} | ||
} | ||
|
||
func CreateOpenStackClient(name types.NamespacedName, spec map[string]interface{}) client.Object { | ||
|
||
raw := map[string]interface{}{ | ||
"apiVersion": "client.openstack.org/v1beta1", | ||
"kind": "OpenStackClient", | ||
"metadata": map[string]interface{}{ | ||
"name": name.Name, | ||
"namespace": name.Namespace, | ||
}, | ||
"spec": spec, | ||
} | ||
return th.CreateUnstructured(raw) | ||
} | ||
|
||
func GetOpenStackClient(name types.NamespacedName) *openstackclientv1.OpenStackClient { | ||
instance := &openstackclientv1.OpenStackClient{} | ||
Eventually(func(g Gomega) { | ||
g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed()) | ||
}, timeout, interval).Should(Succeed()) | ||
return instance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2023. | ||
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 functional_test | ||
|
||
import ( | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
openstackclientv1 "github.com/openstack-k8s-operators/openstack-operator/apis/client/v1beta1" | ||
) | ||
|
||
var _ = Describe("OpenStackClient Webhook", func() { | ||
|
||
var openstackclientName types.NamespacedName | ||
|
||
BeforeEach(func() { | ||
|
||
openstackclientName = types.NamespacedName{ | ||
Name: "foo", | ||
Namespace: namespace, | ||
} | ||
|
||
err := os.Setenv("OPERATOR_TEMPLATES", "../../templates") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
When("A OpenStackClient instance is created without container images", func() { | ||
BeforeEach(func() { | ||
DeferCleanup(th.DeleteInstance, CreateOpenStackClient(openstackclientName, GetDefaultOpenStackClientSpec())) | ||
}) | ||
|
||
It("should have the defaults initialized by webhook", func() { | ||
OpenStackClient := GetOpenStackClient(openstackclientName) | ||
Expect(OpenStackClient.Spec.ContainerImage).Should(Equal( | ||
openstackclientv1.OpenStackClientContainerImage, | ||
)) | ||
}) | ||
}) | ||
|
||
When("A OpenStackClient instance is created with container images", func() { | ||
BeforeEach(func() { | ||
openstackclientSpec := GetDefaultOpenStackClientSpec() | ||
openstackclientSpec["containerImage"] = "api-container-image" | ||
DeferCleanup(th.DeleteInstance, CreateOpenStackClient(openstackclientName, openstackclientSpec)) | ||
}) | ||
|
||
It("should use the given values", func() { | ||
OpenStackClient := GetOpenStackClient(openstackclientName) | ||
Expect(OpenStackClient.Spec.ContainerImage).Should(Equal( | ||
"api-container-image", | ||
)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package functional_test | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"net" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/google/uuid" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"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" | ||
|
||
routev1 "github.com/openshift/api/route/v1" | ||
test "github.com/openstack-k8s-operators/lib-common/modules/test" | ||
openstackclientv1 "github.com/openstack-k8s-operators/openstack-operator/apis/client/v1beta1" | ||
|
||
client_ctrl "github.com/openstack-k8s-operators/openstack-operator/controllers/client" | ||
|
||
common_test "github.com/openstack-k8s-operators/lib-common/modules/test/helpers" | ||
//+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 | ||
k8sClient client.Client | ||
testEnv *envtest.Environment | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
logger logr.Logger | ||
th *common_test.TestHelper | ||
namespace string | ||
) | ||
|
||
const ( | ||
timeout = time.Second * 2 | ||
|
||
SecretName = "test-osp-secret" | ||
|
||
interval = time.Millisecond * 200 | ||
) | ||
|
||
func TestAPIs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecs(t, "Controller Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { | ||
o.Development = true | ||
o.TimeEncoder = zapcore.ISO8601TimeEncoder | ||
})) | ||
|
||
ctx, cancel = context.WithCancel(context.TODO()) | ||
|
||
routev1CRDs, err := test.GetOpenShiftCRDDir("route/v1", "../../go.mod") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
By("bootstrapping test environment") | ||
testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{ | ||
filepath.Join("..", "..", "config", "crd", "bases"), | ||
routev1CRDs, | ||
}, | ||
ErrorIfCRDPathMissing: true, | ||
WebhookInstallOptions: envtest.WebhookInstallOptions{ | ||
Paths: []string{filepath.Join("..", "..", "config", "webhook")}, | ||
// NOTE(gibi): if localhost is resolved to ::1 (ipv6) then starting | ||
// the webhook fails as it try to parse the address as ipv4 and | ||
// failing on the colons in ::1 | ||
LocalServingHost: "127.0.0.1", | ||
}, | ||
} | ||
|
||
// cfg is defined in this file globally. | ||
cfg, err = testEnv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cfg).NotTo(BeNil()) | ||
|
||
err = openstackclientv1.AddToScheme(scheme.Scheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
err = routev1.AddToScheme(scheme.Scheme) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
//+kubebuilder:scaffold:scheme | ||
|
||
logger = ctrl.Log.WithName("---Test---") | ||
|
||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(k8sClient).NotTo(BeNil()) | ||
th = common_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) | ||
Expect(th).NotTo(BeNil()) | ||
|
||
// Start the controller-manager if goroutine | ||
webhookInstallOptions := &testEnv.WebhookInstallOptions | ||
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ | ||
Scheme: scheme.Scheme, | ||
// NOTE(gibi): disable metrics reporting in test to allow | ||
// parallel test execution. Otherwise each instance would like to | ||
// bind to the same port | ||
MetricsBindAddress: "0", | ||
Host: webhookInstallOptions.LocalServingHost, | ||
Port: webhookInstallOptions.LocalServingPort, | ||
CertDir: webhookInstallOptions.LocalServingCertDir, | ||
LeaderElection: false, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
kclient, err := kubernetes.NewForConfig(cfg) | ||
Expect(err).ToNot(HaveOccurred(), "failed to create kclient") | ||
|
||
err = (&openstackclientv1.OpenStackClient{}).SetupWebhookWithManager(k8sManager) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
openstackclientv1.SetupDefaults() | ||
|
||
err = (&client_ctrl.OpenStackClientReconciler{ | ||
Client: k8sManager.GetClient(), | ||
Scheme: k8sManager.GetScheme(), | ||
Kclient: kclient, | ||
}).SetupWithManager(k8sManager) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
err = k8sManager.Start(ctx) | ||
Expect(err).ToNot(HaveOccurred(), "failed to run manager") | ||
}() | ||
|
||
// wait for the webhook server to get ready | ||
dialer := &net.Dialer{Timeout: 10 * time.Second} | ||
addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) | ||
Eventually(func() error { | ||
conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) | ||
if err != nil { | ||
return err | ||
} | ||
conn.Close() | ||
return nil | ||
}).Should(Succeed()) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
By("tearing down the test environment") | ||
cancel() | ||
err := testEnv.Stop() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
var _ = BeforeEach(func() { | ||
// NOTE(gibi): We need to create a unique namespace for each test run | ||
// as namespaces cannot be deleted in a locally running envtest. See | ||
// https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation | ||
namespace = uuid.New().String() | ||
th.CreateNamespace(namespace) | ||
// We still request the delete of the Namespace to properly cleanup if | ||
// we run the test in an existing cluster. | ||
DeferCleanup(th.DeleteNamespace, namespace) | ||
}) |