Skip to content

Commit

Permalink
Improve basic functional test
Browse files Browse the repository at this point in the history
As a follow up of PR#239, this patch is supposed to improve the envTest
coverage, providing more tests and the basic data structures required
to simplify the logic.

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Aug 3, 2023
1 parent 17d3db3 commit a09c6e3
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.51.2
$(LOCALBIN)/golangci-lint run --fix

PROCS?=$(shell expr $(shell nproc --ignore 2) / 2)
PROCS?=$(shell expr $(shell nproc --ignore 2) / 4)
PROC_CMD = --procs ${PROCS}

.PHONY: test
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/openstack-k8s-operators/lib-common/modules/common v0.1.0
github.com/openstack-k8s-operators/lib-common/modules/database v0.1.0
github.com/openstack-k8s-operators/lib-common/modules/storage v0.1.0
github.com/openstack-k8s-operators/lib-common/modules/test v0.1.1
github.com/openstack-k8s-operators/mariadb-operator/api v0.1.0
k8s.io/api v0.26.7
k8s.io/apimachinery v0.26.7
Expand All @@ -24,11 +23,13 @@ require (
require (
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
github.com/openstack-k8s-operators/cinder-operator/api v0.1.0
github.com/openstack-k8s-operators/lib-common/modules/test v0.1.1
)

require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/tools v0.9.3 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
134 changes: 131 additions & 3 deletions tests/functional/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package functional_test
package functional

import (
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
Expand All @@ -34,12 +36,25 @@ func GetGlance(name types.NamespacedName) *glancev1.Glance {
return instance
}

func GetGlanceAPI(name types.NamespacedName) *glancev1.GlanceAPI {
instance := &glancev1.GlanceAPI{}
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed())
}, timeout, interval).Should(Succeed())
return instance
}

func GlanceConditionGetter(name types.NamespacedName) condition.Conditions {
instance := GetGlance(name)
return instance.Status.Conditions
}

func CreateGlance(name types.NamespacedName) client.Object {
func GlanceAPIConditionGetter(name types.NamespacedName) condition.Conditions {
instance := GetGlanceAPI(name)
return instance.Status.Conditions
}

func CreateDefaultGlance(name types.NamespacedName) client.Object {
raw := map[string]interface{}{
"apiVersion": "glance.openstack.org/v1beta1",
"kind": "Glance",
Expand All @@ -49,8 +64,121 @@ func CreateGlance(name types.NamespacedName) client.Object {
},
"spec": map[string]interface{}{
"databaseInstance": "openstack",
"storageRequest": "10G",
"storageRequest": glanceTest.GlancePVCSize,
},
}
return th.CreateUnstructured(raw)
}

func GetGlanceEmptySpec() map[string]interface{} {
return map[string]interface{}{
"secret": SecretName,
"spec": map[string]interface{}{
"databaseInstance": "openstack",
"storageRequest": glanceTest.GlancePVCSize,
},
}
}

func GetGlanceDefaultSpec() map[string]interface{} {
return map[string]interface{}{
"databaseInstance": "openstack",
"databaseUser": glanceTest.GlanceDatabaseUser,
"serviceUser": glanceName.Name,
"secret": SecretName,
"glanceAPIInternal": GetGlanceAPIDefaultSpec(GlanceAPITypeInternal),
"glanceAPIExternal": GetGlanceAPIDefaultSpec(GlanceAPITypeExternal),
"storageRequest": glanceTest.GlancePVCSize,
}
}

func GetGlanceDefaultSpecWithQuota() map[string]interface{} {
return map[string]interface{}{
"databaseInstance": "openstack",
"databaseUser": glanceTest.GlanceDatabaseUser,
"serviceUser": glanceName.Name,
"secret": SecretName,
"glanceAPIInternal": GetGlanceAPIDefaultSpec(GlanceAPITypeInternal),
"glanceAPIExternal": GetGlanceAPIDefaultSpec(GlanceAPITypeExternal),
"storageRequest": glanceTest.GlancePVCSize,
"quotas": glanceTest.GlanceQuotas,
}
}

func GetGlanceAPIDefaultSpec(apiType APIType) map[string]interface{} {
return map[string]interface{}{
"replicas": 1,
}
}

func CreateGlance(name types.NamespacedName, spec map[string]interface{}) client.Object {

raw := map[string]interface{}{
"apiVersion": "glance.openstack.org/v1beta1",
"kind": "Glance",
"metadata": map[string]interface{}{
"name": name.Name,
"namespace": name.Namespace,
},
"spec": spec,
}
return th.CreateUnstructured(raw)
}

func CreateGlanceAPI(name types.NamespacedName, spec map[string]interface{}) client.Object {
raw := map[string]interface{}{
"apiVersion": "glance.openstack.org/v1beta1",
"kind": "GlanceAPI",
"metadata": map[string]interface{}{
"name": name.Name,
"namespace": name.Namespace,
},
"spec": spec,
}
return th.CreateUnstructured(raw)
}

func CreateGlanceSecret(namespace string, name string) *corev1.Secret {
return th.CreateSecret(
types.NamespacedName{Namespace: namespace, Name: name},
map[string][]byte{
"GlancePassword": []byte(glanceTest.GlancePassword),
"GlanceDatabasePassword": []byte(glanceTest.GlancePassword),
},
)
}

func GetDefaultGlanceSpec() map[string]interface{} {
return map[string]interface{}{
"databaseInstance": "openstack",
"secret": SecretName,
"glanceAPIInternal": GetDefaultGlanceAPISpec(),
"glanceAPIExternal": GetDefaultGlanceAPISpec(),
}
}

func GetDefaultGlanceAPISpec() map[string]interface{} {
return map[string]interface{}{
"secret": SecretName,
"replicas": 1,
"containerImage": "test://glance",
"serviceAccount": glanceTest.GlanceSA.Name,
"databaseInstance": "openstack",
}
}

func GlanceAPINotExists(name types.NamespacedName) {
Consistently(func(g Gomega) {
instance := &glancev1.GlanceAPI{}
err := k8sClient.Get(ctx, name, instance)
g.Expect(k8s_errors.IsNotFound(err)).To(BeTrue())
}, timeout, interval).Should(Succeed())
}

func GlanceAPIExists(name types.NamespacedName) {
Consistently(func(g Gomega) {
instance := &glancev1.GlanceAPI{}
err := k8sClient.Get(ctx, name, instance)
g.Expect(k8s_errors.IsNotFound(err)).To(BeFalse())
}, timeout, interval).Should(Succeed())
}
Loading

0 comments on commit a09c6e3

Please sign in to comment.