Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 18, 2024
1 parent 7dc1340 commit ab26976
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.21"
- uses: golangci/golangci-lint-action@v3
with:
version: v1.52.2
version: v1.55
- run: make test

integration:
Expand All @@ -34,7 +34,7 @@ jobs:
version: v3.7.0
- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.21"
- uses: actions/cache@v2
with:
path: |
Expand Down Expand Up @@ -194,7 +194,7 @@ jobs:
EOF
- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.21"
- uses: goreleaser/goreleaser-action@v4
with:
version: latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20-alpine3.18 AS builder
FROM golang:1.21-alpine3.18 AS builder
COPY . /go/src/github.com/tsuru/rpaas-operator
WORKDIR /go/src/github.com/tsuru/rpaas-operator
RUN set -x \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.operator
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20-alpine3.18 AS builder
FROM golang:1.21-alpine3.18 AS builder
COPY . /go/src/github.com/tsuru/rpaas-operator
WORKDIR /go/src/github.com/tsuru/rpaas-operator
RUN apk add --update gcc git make musl-dev && \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.purger
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20-alpine3.18 AS builder
FROM golang:1.21-alpine3.18 AS builder
COPY . /go/src/github.com/tsuru/rpaas-operator
WORKDIR /go/src/github.com/tsuru/rpaas-operator
RUN apk add --update gcc git make musl-dev && \
Expand Down
12 changes: 8 additions & 4 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -881,20 +882,23 @@ func (r *RpaasInstanceReconciler) getNginx(ctx context.Context, instance *v1alph
return found, err
}

func (r *RpaasInstanceReconciler) getNginxExternalAddressses(ctx context.Context, nginx *nginxv1alpha1.Nginx) (v1alpha1.RpaasInstanceExternalAddressesStatus, error) {
func externalAddresssesFromNginx(nginx *nginxv1alpha1.Nginx) v1alpha1.RpaasInstanceExternalAddressesStatus {
ingressesStatus := v1alpha1.RpaasInstanceExternalAddressesStatus{}

for _, service := range nginx.Status.Services {
ingressesStatus.IPs = append(ingressesStatus.IPs, service.Hostnames...)
ingressesStatus.IPs = append(ingressesStatus.IPs, service.IPs...)
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, service.Hostnames...)
}

for _, ingress := range nginx.Status.Ingresses {
ingressesStatus.IPs = append(ingressesStatus.IPs, ingress.Hostnames...)
ingressesStatus.IPs = append(ingressesStatus.IPs, ingress.IPs...)
ingressesStatus.Hostnames = append(ingressesStatus.Hostnames, ingress.Hostnames...)
}

return ingressesStatus, nil
slices.Sort(ingressesStatus.IPs)
slices.Sort(ingressesStatus.Hostnames)

return ingressesStatus
}

func (r *RpaasInstanceReconciler) reconcileNginx(ctx context.Context, instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) error {
Expand Down
41 changes: 41 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,47 @@ func TestRpaasInstanceController_Reconcile_Suspended(t *testing.T) {
assert.Equal(t, "Warning RpaasInstanceSuspended no modifications will be done by RPaaS controller", <-fr.Events)
}

func TestExternalAddresssesFromNginx(t *testing.T) {
externalAddresses := externalAddresssesFromNginx(&nginxv1alpha1.Nginx{
Status: nginxv1alpha1.NginxStatus{
Ingresses: []nginxv1alpha1.IngressStatus{
{
Name: "ing1",
IPs: []string{"1.1.1.3", "1.1.1.1"},
},
{
Name: "ing2",
IPs: []string{"1.1.1.2", "1.1.1.4"},
},
{
Name: "ing3",
Hostnames: []string{"host2", "host1"},
},
},
Services: []nginxv1alpha1.ServiceStatus{
{
Name: "svc",
IPs: []string{"8.1.1.3", "8.1.1.1"},
},
{
Name: "svc2",
IPs: []string{"8.1.1.2", "8.1.1.4"},
},
{
Name: "svc3",
Hostnames: []string{"host9", "host8"},
},
},
},
})

assert.Equal(t, v1alpha1.RpaasInstanceExternalAddressesStatus{
IPs: []string{"1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4", "8.1.1.1", "8.1.1.2", "8.1.1.3", "8.1.1.4"},
Hostnames: []string{"host1", "host2", "host8", "host9"},
}, externalAddresses)

}

func newRpaasInstanceReconciler(objs ...runtime.Object) *RpaasInstanceReconciler {
return &RpaasInstanceReconciler{
Client: fake.NewClientBuilder().WithScheme(extensionsruntime.NewScheme()).WithRuntimeObjects(objs...).Build(),
Expand Down
7 changes: 1 addition & 6 deletions controllers/rpaasinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,12 @@ func (r *RpaasInstanceReconciler) refreshStatus(ctx context.Context, instance *v
return err
}

externalAddresses, err := r.getNginxExternalAddressses(ctx, existingNginx)
if err != nil {
return err
}

newStatus := v1alpha1.RpaasInstanceStatus{
ObservedGeneration: instance.Generation,
WantedNginxRevisionHash: newHash,
ObservedNginxRevisionHash: existingHash,
NginxUpdated: newHash == existingHash,
ExternalAddresses: externalAddresses,
ExternalAddresses: externalAddresssesFromNginx(existingNginx),
}

if existingNginx != nil {
Expand Down

0 comments on commit ab26976

Please sign in to comment.