Skip to content
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
6 changes: 4 additions & 2 deletions api/v1alpha1/flavor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ type FlavorResourceSpec struct {
Description *OpenStackDescription `json:"description,omitempty"`

// RAM is the memory of the flavor, measured in MB.
RAM int32 `json:"ram,omitempty"`
// +kubebuilder:validation:Minimum=1
RAM int32 `json:"ram"`

// Vcpus is the number of vcpus for the flavor.
Vcpus int32 `json:"vcpus,omitempty"`
// +kubebuilder:validation:Minimum=1
Vcpus int32 `json:"vcpus"`

// Disk is the size of the root disk that will be created in GiB. If 0
// the root disk will be set to exactly the size of the image used to
Expand Down
3 changes: 3 additions & 0 deletions cmd/models-schema/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/crd/bases/openstack.k-orc.cloud_flavors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ spec:
ram:
description: RAM is the memory of the flavor, measured in MB.
format: int32
minimum: 1
type: integer
swap:
description: |-
Expand All @@ -202,7 +203,11 @@ spec:
vcpus:
description: Vcpus is the number of vcpus for the flavor.
format: int32
minimum: 1
type: integer
required:
- ram
- vcpus
type: object
required:
- cloudCredentialsRef
Expand Down
2 changes: 2 additions & 0 deletions pkg/clients/applyconfiguration/internal/internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions test/apivalidations/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024 The ORC Authors.

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 apivalidations

import (
applyconfigv1alpha1 "github.com/k-orc/openstack-resource-controller/pkg/clients/applyconfiguration/api/v1alpha1"
)

func testCredentials() *applyconfigv1alpha1.CloudCredentialsReferenceApplyConfiguration {
return applyconfigv1alpha1.CloudCredentialsReference().
WithSecretName("openstack-credentials").
WithCloudName("openstack")
}
89 changes: 89 additions & 0 deletions test/apivalidations/flavor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2024 The ORC Authors.

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 apivalidations

import (
"context"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

orcv1alpha1 "github.com/k-orc/openstack-resource-controller/api/v1alpha1"
applyconfigv1alpha1 "github.com/k-orc/openstack-resource-controller/pkg/clients/applyconfiguration/api/v1alpha1"
)

const flavorName = "flavor"

func flavorStub(namespace *corev1.Namespace) *orcv1alpha1.Flavor {
obj := &orcv1alpha1.Flavor{}
obj.Name = flavorName
obj.Namespace = namespace.Name
return obj
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing blank line below. Surprised go fmt didn't complain about that 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


func baseFlavorPatch(flavor client.Object) *applyconfigv1alpha1.FlavorApplyConfiguration {
return applyconfigv1alpha1.Flavor(flavor.GetName(), flavor.GetNamespace()).
WithSpec(applyconfigv1alpha1.FlavorSpec().
WithCloudCredentialsRef(testCredentials()))
}

var _ = Describe("ORC Flavor API validations", func() {
var namespace *corev1.Namespace
BeforeEach(func() {
namespace = createNamespace()
})

It("should allow to create a minimal flavor and managementPolicy should default to managed", func(ctx context.Context) {
flavor := flavorStub(namespace)
patch := baseFlavorPatch(flavor)
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec().WithRAM(1).WithVcpus(1))
Expect(applyObj(ctx, flavor, patch)).To(Succeed())
Expect(flavor.Spec.ManagementPolicy).To(Equal(orcv1alpha1.ManagementPolicyManaged))
})

It("should reject a flavor without required fields", func(ctx context.Context) {
flavor := flavorStub(namespace)
patch := baseFlavorPatch(flavor)
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec())
Expect(applyObj(ctx, flavor, patch)).NotTo(Succeed())
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec().WithRAM(1))
Expect(applyObj(ctx, flavor, patch)).NotTo(Succeed())
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec().WithVcpus(1))
Expect(applyObj(ctx, flavor, patch)).NotTo(Succeed())
})

It("should reject a flavor with values less than minimal", func(ctx context.Context) {
flavor := flavorStub(namespace)
patch := baseFlavorPatch(flavor)
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec().WithRAM(1).WithVcpus(0))
Expect(applyObj(ctx, flavor, patch)).NotTo(Succeed())
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec().WithRAM(0).WithVcpus(1))
Expect(applyObj(ctx, flavor, patch)).NotTo(Succeed())
})

It("should reject a flavor with values greater than max", func(ctx context.Context) {
flavor := flavorStub(namespace)
patch := baseFlavorPatch(flavor)
maxString := orcv1alpha1.OpenStackDescription(strings.Repeat("a", 1025))
patch.Spec.WithResource(applyconfigv1alpha1.FlavorResourceSpec().WithRAM(1).WithVcpus(1).WithDescription(maxString))
Expect(applyObj(ctx, flavor, patch)).NotTo(Succeed())

})
})
6 changes: 0 additions & 6 deletions test/apivalidations/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ func imageStub(name string, namespace *corev1.Namespace) *orcv1alpha1.Image {
return obj
}

func testCredentials() *applyconfigv1alpha1.CloudCredentialsReferenceApplyConfiguration {
return applyconfigv1alpha1.CloudCredentialsReference().
WithSecretName("openstack-credentials").
WithCloudName("openstack")
}

func testResource() *applyconfigv1alpha1.ImageResourceSpecApplyConfiguration {
return applyconfigv1alpha1.ImageResourceSpec().
WithContent(applyconfigv1alpha1.ImageContent().
Expand Down
Loading