Skip to content

Commit

Permalink
Add functional tests for Octavia
Browse files Browse the repository at this point in the history
Add the base for running tests, add partial coverage for Octavia and
OctaviaAPI Controllers

JIRA: RHOSP-5725
  • Loading branch information
gthiemonge committed Sep 9, 2024
1 parent d049901 commit 63095c0
Show file tree
Hide file tree
Showing 13 changed files with 1,629 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ golangci-lint: vet
$(LOCALBIN)/golangci-lint run --fix

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

##@ Build

Expand Down Expand Up @@ -181,6 +181,7 @@ $(LOCALBIN):
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GINKGO ?= $(LOCALBIN)/ginkgo

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
Expand Down Expand Up @@ -224,6 +225,11 @@ OPERATOR_SDK = $(shell which operator-sdk)
endif
endif

.PHONY: ginkgo
ginkgo: $(GINKGO) ## Download ginkgo locally if necessary.
$(GINKGO): $(LOCALBIN)
test -s $(LOCALBIN)/ginkgo || GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo

.PHONY: bundle
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
$(OPERATOR_SDK) generate kustomize manifests -q
Expand Down
1 change: 1 addition & 0 deletions controllers/octaviaapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ func (r *OctaviaAPIReconciler) generateServiceConfigMaps(

cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(octavia.ServiceName), map[string]string{})

// TODO(gthiemonge) Check 3rd parameter, it's probably DatabaseCRName
db, err := mariadbv1.GetDatabaseByNameAndAccount(ctx, h, octavia.DatabaseName, instance.Spec.DatabaseAccount, instance.Namespace)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ require (
)

require (
github.com/google/uuid v1.6.0
github.com/openstack-k8s-operators/lib-common/modules/test v0.4.1-0.20240730142703-7fd3da600c9d
golang.org/x/crypto v0.26.0
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
)
Expand All @@ -46,7 +48,6 @@ 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-20240727154555-813a5fbdbec8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -63,6 +64,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.23.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.2024090512
github.com/openstack-k8s-operators/lib-common/modules/common v0.4.1-0.20240905123813-174296c09ec6/go.mod h1:6zxa5xg9uvpObVKFSJa/SA+vDDlgh0Q1aswxDB2XbxU=
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.4.1-0.20240805121733-1c08e6b7e260 h1:K+2TH5If/WR+ls92EYH6IcbWcLY15wlR1adGq/prA30=
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.4.1-0.20240805121733-1c08e6b7e260/go.mod h1:Z9QhWZexP9fYcZrBRI5rrcRwTh6LSsd5XB7NWzdphaE=
github.com/openstack-k8s-operators/lib-common/modules/test v0.4.1-0.20240730142703-7fd3da600c9d h1:P5tF3/shBcQf0HkPHWcr5Ri+jsRStMPbGOPQw4qRHRI=
github.com/openstack-k8s-operators/lib-common/modules/test v0.4.1-0.20240730142703-7fd3da600c9d/go.mod h1:0h76CxD9g0z2Hk7fGFOZcjnzT1tQQ/yRNv3OXng+S/A=
github.com/openstack-k8s-operators/mariadb-operator/api v0.4.1-0.20240805141244-ff694b3aaeda h1:Oy45/CWDZ6EQ3+4pO4+TO6fDSmnZsbce+MvNvvRbbO0=
github.com/openstack-k8s-operators/mariadb-operator/api v0.4.1-0.20240805141244-ff694b3aaeda/go.mod h1:J3jD6GZQlRQMu11LUs3e8JCdrqNcxpc2nH5Fqj7cEQY=
github.com/openstack-k8s-operators/ovn-operator/api v0.4.1-0.20240805122347-074f3d876f74 h1:KjT4AElMkei63wArWuXV5E7i+waBp9TSm2VvCsr72Xg=
Expand Down Expand Up @@ -124,6 +126,8 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbR
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
2 changes: 2 additions & 0 deletions pkg/octavia/amphora_certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func EnsureAmphoraCerts(
return fmt.Errorf("Error retrieving secret %s - %w", certsSecretName, err)
}

// TODO(gthiemonge) not sure that instance.Name is supposed to be part of the key
// it should always be "octavia"
serverCAPassSecretName := fmt.Sprintf("%s-ca-passphrase", instance.Name)
cAPassSecret, _, err := secret.GetSecret(
ctx, h, serverCAPassSecretName, instance.Namespace)
Expand Down
2 changes: 2 additions & 0 deletions pkg/octavia/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func GetAdminServiceClient(
return nil, ctrl.Result{}, err
}
if (ctrlResult != ctrl.Result{}) {
// TODO(gthiemonge) callers are ignoring these return values
// It means that this function can return a nil client when ketystone is not fully initialized
return nil, ctrlResult, nil
}

Expand Down
209 changes: 209 additions & 0 deletions tests/functional/api_fixture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
Copyright 2024.
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 (
"encoding/json"
"fmt"
"net/http"

"github.com/go-logr/logr"
"github.com/google/uuid"

. "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports

"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
keystone_helpers "github.com/openstack-k8s-operators/keystone-operator/api/test/helpers"

api "github.com/openstack-k8s-operators/lib-common/modules/test/apis"
)

var (
keystoneProjects = []projects.Project{
{
Name: "admin",
ID: uuid.New().String(),
},
{
Name: "service",
ID: uuid.New().String(),
},
}
)

// ResponseHandleToken responds with a valid keystone token and the computeURL in the catalog
func ResponseHandleToken(
keystoneURL string,
novaURL string,
neutronURL string,
) string {
return fmt.Sprintf(
`
{
"token":{
"catalog":[
{
"endpoints":[
{
"id":"9141a10b3d964db2ac2ce36fec7d32e0",
"interface":"internal",
"region_id":"RegionOne",
"url":"%s",
"region":"RegionOne"
},
{
"id":"e6ec29ecce164c3084ef308478080127",
"interface":"public",
"region_id":"RegionOne",
"url":"%s",
"region":"RegionOne"
}
],
"id":"edad7277e52a47b3bfb2b7004f77110f",
"type":"identity",
"name":"keystone"
},
{
"endpoints":[
{
"id":"3474478de5cf4f4bb6289cadde383522",
"interface":"internal",
"region_id":"RegionOne",
"url":"%s",
"region":"RegionOne"
},
{
"id":"a32b1b4d579a402b8e998f65285ffecc",
"interface":"public",
"region_id":"RegionOne",
"url":"%s",
"region":"RegionOne"
}
],
"id":"7d1edf61386547039c4046438f77e8be",
"type":"compute",
"name":"nova"
},
{
"endpoints":[
{
"id":"3474478de5cf4f4bb6289cadde383522",
"interface":"internal",
"region_id":"RegionOne",
"url":"%s",
"region":"RegionOne"
},
{
"id":"a32b1b4d579a402b8e998f65285ffecc",
"interface":"public",
"region_id":"RegionOne",
"url":"%s",
"region":"RegionOne"
}
],
"id":"7d1edf61386547039c4046438f77e8be",
"type":"network",
"name":"neutron"
}
]
}
}
`, keystoneURL, keystoneURL, novaURL, novaURL, neutronURL, neutronURL)
}

func keystoneHandleProjects(
f *keystone_helpers.KeystoneAPIFixture,
w http.ResponseWriter,
r *http.Request,
) {
f.LogRequest(r)
switch r.Method {
case "GET":
keystoneGetProject(f, w, r)
default:
f.UnexpectedRequest(w, r)
return
}
}

func GetProject(name string) *projects.Project {
for _, p := range keystoneProjects {
if p.Name == name {
return &p
}
}
return nil
}

func keystoneGetProject(
f *keystone_helpers.KeystoneAPIFixture,
w http.ResponseWriter,
r *http.Request,
) {
nameFilter := r.URL.Query().Get("name")
var s struct {
Projects []projects.Project `json:"projects"`
}
project := GetProject(nameFilter)
s.Projects = []projects.Project{*project}

bytes, err := json.Marshal(&s)
if err != nil {
f.InternalError(err, "Error during marshalling response", w, r)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprint(w, string(bytes))
f.APIFixture.Log.Info(fmt.Sprintf("GetProject returns %s", string(bytes)))
}

func SetupAPIFixtures(logger logr.Logger) (
*keystone_helpers.KeystoneAPIFixture,
*NovaAPIFixture,
*NeutronAPIFixture,
) {
nova := NewNovaAPIFixtureWithServer(logger)
nova.Setup()
DeferCleanup(nova.Cleanup)
novaURL := nova.Endpoint()

neutron := NewNeutronAPIFixtureWithServer(logger)
neutron.Setup()
DeferCleanup(neutron.Cleanup)
neutronURL := neutron.Endpoint()

keystone := keystone_helpers.NewKeystoneAPIFixtureWithServer(logger)
keystone.Setup(
api.Handler{Pattern: "/", Func: keystone.HandleVersion},
api.Handler{Pattern: "/v3/users", Func: keystone.HandleUsers},
api.Handler{Pattern: "/v3/domains", Func: keystone.HandleDomains},
api.Handler{Pattern: "/v3/projects", Func: func(w http.ResponseWriter, r *http.Request) {
keystoneHandleProjects(keystone, w, r)
}},
api.Handler{Pattern: "/v3/auth/tokens", Func: func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(202)
// ensure keystone returns the simulator endpoints in its catalog
fmt.Fprint(w, ResponseHandleToken(keystone.Endpoint(), novaURL, neutronURL))
}
}})
DeferCleanup(keystone.Cleanup)
return keystone, nova, neutron
}
Loading

0 comments on commit 63095c0

Please sign in to comment.