Skip to content

Commit

Permalink
Merge pull request #157 from rabi/functional
Browse files Browse the repository at this point in the history
Add initial functional tests
  • Loading branch information
openshift-merge-bot[bot] authored Apr 17, 2024
2 parents 7b9ec5a + 3fe374a commit 679bfaa
Show file tree
Hide file tree
Showing 7 changed files with 1,687 additions and 5 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ vet: gowork ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ginkgo ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) --coverprofile cover.out --cover --covermode=atomic --coverpkg=../../pkg/...,../../controllers,../../api/v1beta1 --trace ./...
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) --trace --cover --coverpkg=../../pkg/...,../../controllers,../../api/v1beta1 --coverprofile cover.out --covermode=atomic ${PROC_CMD} $(GINKGO_ARGS) ./tests/...

##@ Build

Expand Down Expand Up @@ -337,9 +337,7 @@ govet: get-ci-tools
GOWORK=off $(CI_TOOLS_REPO_DIR)/test-runner/govet.sh ./api

# Run go test against code
gotest: get-ci-tools
GOWORK=off $(CI_TOOLS_REPO_DIR)/test-runner/gotest.sh
GOWORK=off $(CI_TOOLS_REPO_DIR)/test-runner/gotest.sh ./api
gotest: test

.PHONY: golangci-lint
golangci-lint:
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/go-logr/logr v1.4.1
github.com/golang/glog v1.2.0
github.com/google/uuid v1.6.0
github.com/metal3-io/baremetal-operator/apis v0.5.1
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.32.0
Expand Down Expand Up @@ -38,17 +39,18 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/metal3-io/baremetal-operator/pkg/hardwareutils v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 h1:VzM3TYHDgqPkettiP6I6q2jOeQFL4nrJM+UcAc4f6Fs=
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0/go.mod h1:nqCI7aelBJU61wiBeeZWJ6oi4bJy5nrjkM6lWIMA4j0=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -94,6 +96,8 @@ github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
github.com/onsi/gomega v1.32.0/go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg=
github.com/openshift/api v3.9.0+incompatible h1:fJ/KsefYuZAjmrr3+5U9yZIZbTOpVkDDLDLFresAeYs=
github.com/openshift/api v3.9.0+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240412091425-bb628ded5eb8 h1:5ywsORdn4y7vNO732ODwYlmfRXnEphfoqB6PCYWt9r8=
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240412091425-bb628ded5eb8/go.mod h1:gqByVGUdKQB/NkhKV4eD+8NWYkHq961nC96rTCB3ywE=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
150 changes: 150 additions & 0 deletions tests/functional/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
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

import (
"strings"

metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
. "github.com/onsi/gomega" //revive:disable:dot-imports
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
baremetalv1 "github.com/openstack-k8s-operators/openstack-baremetal-operator/api/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)

// Create OpenstackBaremetalSet in k8s and test that no errors occur
func CreateBaremetalSet(name types.NamespacedName, spec map[string]interface{}) *unstructured.Unstructured {
instance := DefaultBaremetalSetTemplate(name, spec)
return th.CreateUnstructured(instance)
}

// Build OpenStackBaremetalSet struct and fill it with preset values
func DefaultBaremetalSetTemplate(name types.NamespacedName, spec map[string]interface{}) map[string]interface{} {
return map[string]interface{}{

"apiVersion": "baremetal.openstack.org/v1beta1",
"kind": "OpenStackBaremetalSet",
"metadata": map[string]interface{}{
"name": name.Name,
"namespace": name.Namespace,
},
"spec": spec,
}
}

// Build BaremetalSetSpec struct and fill it with preset values
func DefaultBaremetalSetSpec(name types.NamespacedName, withProvInterface bool) map[string]interface{} {
spec := map[string]interface{}{
"baremetalHosts": map[string]interface{}{
"compute-0": map[string]interface{}{
"ctlPlaneIP": "10.0.0.1",
},
},
"deploymentSSHSecret": "mysecret",
"ctlplaneInterface": "eth0",
"bmhNamespace": name.Namespace,
}
if withProvInterface {
spec["provisioningInterface"] = "eth1"
spec["osContainerImageUrl"] = "quay.io/podified-antelope-centos9/edpm-hardened-uefi@latest"
spec["agentImageUrl"] = "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent@latest"
spec["apacheImageUrl"] = "registry.redhat.io/rhel8/httpd-24@latest"
spec["osImage"] = "edpm-hardened-uefi.qcow2"
}

return spec

}

// Default BMH Template with preset values
func DefaultBMHTemplate(name types.NamespacedName) map[string]interface{} {
return map[string]interface{}{
"apiVersion": "metal3.io/v1alpha1",
"kind": "BareMetalHost",
"metadata": map[string]interface{}{
"name": name.Name,
"namespace": name.Namespace,
"annotations": map[string]interface{}{
"inspect.metal3.io": "disabled",
},
},
"spec": map[string]interface{}{
"bmc": map[string]interface{}{
"address": "fake_address",
"credentialsName": "fake_credential",
},
"bootMACAddress": "52:54:00:39:a7:44",
"bootMode": "UEFI",
"online": false,
},
}
}

// Get BaremetalSet
func GetBaremetalSet(name types.NamespacedName) *baremetalv1.OpenStackBaremetalSet {
instance := &baremetalv1.OpenStackBaremetalSet{}
Eventually(func(g Gomega) error {
g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed())
return nil
}, timeout, interval).Should(Succeed())
return instance
}

// Create BaremetalHost
func CreateBaremetalHost(name types.NamespacedName) *unstructured.Unstructured {
instance := DefaultBMHTemplate(name)
return th.CreateUnstructured(instance)
}

// Get BaremetalHost
func GetBaremetalHost(name types.NamespacedName) *metal3v1.BareMetalHost {
instance := &metal3v1.BareMetalHost{}
Eventually(func(g Gomega) error {
g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed())
return nil
}, timeout, interval).Should(Succeed())
return instance
}

// Get BaremetalSet conditions
func BaremetalSetConditionGetter(name types.NamespacedName) condition.Conditions {
instance := GetBaremetalSet(name)
return instance.Status.Conditions
}

// Create DeploymentSSHSecret
func CreateSSHSecret(name types.NamespacedName) *corev1.Secret {
return th.CreateSecret(
types.NamespacedName{Namespace: name.Namespace, Name: name.Name},
map[string][]byte{
"ssh-privatekey": []byte("blah"),
"authorized_keys": []byte("blih"),
},
)
}

// Get ProvisionServer
func GetProvisionServer(name types.NamespacedName) *baremetalv1.OpenStackProvisionServer {
instance := &baremetalv1.OpenStackProvisionServer{}
name.Name = strings.Join([]string{name.Name, "provisionserver"}, "-")
Eventually(func(g Gomega) error {
g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed())
return nil
}, timeout, interval).Should(Succeed())
return instance
}
Loading

0 comments on commit 679bfaa

Please sign in to comment.