Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
chore: add test verifying cluster config updates(CapacityExhausted fl…
Browse files Browse the repository at this point in the history
…ag) (#777)
  • Loading branch information
MatousJobanek authored and alexeykazakov committed Oct 31, 2019
1 parent d444fc3 commit d215aa6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
34 changes: 34 additions & 0 deletions cluster/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cluster_test

import (
"context"
"fmt"
"gopkg.in/h2non/gock.v1"
"math/rand"
"sync"
"testing"
Expand All @@ -20,6 +22,38 @@ import (
"github.com/stretchr/testify/require"
)

func TestUpdateCluster(t *testing.T) {
// given
defer gock.Off()
testdoubles.MockCommunicationWithAuthSettingCapacityFlag(testsupport.ClusterURL, false, false)
testdoubles.MockCommunicationWithAuthSettingCapacityFlag(testsupport.ClusterURL, false, false)

// when
clusterService, _, _, reset := testdoubles.PrepareConfigClusterAndAuthServiceWithRefreshInt(time.Second, t)
defer reset()

// then
err := clusterService.Start()
require.NoError(t, err)
clusters := clusterService.GetClusters(context.Background())
require.Len(t, clusters, 1)
assert.False(t, clusters[0].CapacityExhausted)

// and when
testdoubles.MockCommunicationWithAuthSettingCapacityFlag(testsupport.ClusterURL, true, true)

// then
err = testsupport.WaitWithTimeout(3 * time.Second).Until(func() error {
clusters := clusterService.GetClusters(context.Background())
require.Len(t, clusters, 1)
if !clusters[0].CapacityExhausted {
return fmt.Errorf("CapacityExhausted flag is still false")
}
return nil
})
assert.NoError(t, err)
}

func TestResolveCluster(t *testing.T) {

// given
Expand Down
6 changes: 5 additions & 1 deletion test/doubles/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func createTokenMock(tenantId string, persist bool) {
}

func PrepareConfigClusterAndAuthService(t *testing.T) (cluster.Service, auth.Service, *configuration.Data, func()) {
return PrepareConfigClusterAndAuthServiceWithRefreshInt(time.Hour, t)
}

func PrepareConfigClusterAndAuthServiceWithRefreshInt(refreshInt time.Duration, t *testing.T) (cluster.Service, auth.Service, *configuration.Data, func()) {
saToken, err := test.NewToken(
map[string]interface{}{
"sub": "tenant_service",
Expand All @@ -100,7 +104,7 @@ func PrepareConfigClusterAndAuthService(t *testing.T) (cluster.Service, auth.Ser
resetConf()
}

clusterService := cluster.NewClusterService(time.Hour, authService)
clusterService := cluster.NewClusterService(refreshInt, authService)
err = clusterService.Start()
require.NoError(t, err)
return clusterService, authService, config, reset
Expand Down
39 changes: 25 additions & 14 deletions test/doubles/test_doubles.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
"net/http"
"strconv"
"strings"
"testing"
)
Expand Down Expand Up @@ -349,14 +350,20 @@ func GetMappedVersions(envTypes ...environment.Type) map[environment.Type]string
}

func MockCommunicationWithAuth(cluster string, otherClusters ...string) {
MockCommunicationWithAuthSettingCapacityFlag(cluster, false, true, otherClusters...)
}

func MockCommunicationWithAuthSettingCapacityFlag(cluster string, capacityExhausted, persist bool, otherClusters ...string) {
clusterList := ""
var clusters []string
clusters = append(otherClusters, cluster)
for _, cl := range clusters {
gock.New("http://authservice").
Get("/api/token").
Persist().
MatchParam("for", cl+"/").
mockAuth := gock.New("http://authservice").
Get("/api/token")
if persist {
mockAuth = mockAuth.Persist()
}
mockAuth.MatchParam("for", cl+"/").
MatchParam("force_pull", "false").
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("tenant_service"))).
Reply(200).
Expand All @@ -366,11 +373,13 @@ func MockCommunicationWithAuth(cluster string, otherClusters ...string) {
"username": "devtools-sre"
}`)

gock.New(cl).
mockUsers := gock.New(cl).
Get("/apis/user.openshift.io/v1/users/~").
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("devtools-sre"))).
Persist().
Reply(200).
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("devtools-sre")))
if persist {
mockUsers = mockUsers.Persist()
}
mockUsers.Reply(200).
BodyString(`{
"kind":"User",
"apiVersion":"user.openshift.io/v1",
Expand All @@ -393,16 +402,18 @@ func MockCommunicationWithAuth(cluster string, otherClusters ...string) {
"metrics-url": "%s/",
"logging-url": "%s/",
"app-dns": "foo",
"capacity-exhausted": false
},`, cl, cl, cl, cl)
"capacity-exhausted": %s
},`, cl, cl, cl, cl, strconv.FormatBool(capacityExhausted))

}

gock.New("http://authservice").
mockClusters := gock.New("http://authservice").
Get("/api/clusters/").
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("tenant_service"))).
Persist().
Reply(200).
SetMatcher(test.ExpectRequest(test.HasJWTWithSub("tenant_service")))
if persist {
mockClusters = mockClusters.Persist()
}
mockClusters.Reply(200).
BodyString(fmt.Sprintf(`{
"data":[
%s
Expand Down

0 comments on commit d215aa6

Please sign in to comment.