Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing the broken gtp tests #226

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions admiral/pkg/controller/admiral/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package admiral

import (
"errors"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
_ "github.com/istio-ecosystem/admiral/admiral/pkg/test"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/fake"
"testing"
"time"
)

func init() {
Expand All @@ -28,7 +29,7 @@ func init() {

p.LabelSet.WorkloadIdentityKey = "identity"
p.LabelSet.GlobalTrafficDeploymentLabel = "identity"

p.LabelSet.EnvKey = "admiral.io/env"
common.InitializeConfig(p)
}

Expand Down
54 changes: 32 additions & 22 deletions admiral/pkg/controller/admiral/globaltraffic_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package admiral

import (
"reflect"
"sync"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/model"
v1 "github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/v1"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"github.com/istio-ecosystem/admiral/admiral/pkg/test"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"reflect"
"sync"
"testing"
"time"
)

func TestNewGlobalTrafficController(t *testing.T) {
Expand Down Expand Up @@ -187,17 +188,8 @@ func TestGlobalTrafficController_Deleted(t *testing.T) {
}

func TestGlobalTrafficController_Added(t *testing.T) {

var (
gth = test.MockGlobalTrafficHandler{}
cache = gtpCache{
cache: make(map[string]map[string]map[string]*v1.GlobalTrafficPolicy),
mutex: &sync.Mutex{},
}
gtpController = GlobalTrafficController{
GlobalTrafficHandler: &gth,
Cache: &cache,
}
gth = test.MockGlobalTrafficHandler{}
gtp = v1.GlobalTrafficPolicy{ObjectMeta: v12.ObjectMeta{Name: "gtp", Namespace: "namespace1", Labels: map[string]string{"identity": "id", "admiral.io/env": "stage"}}}
gtpWithIgnoreLabels = v1.GlobalTrafficPolicy{ObjectMeta: v12.ObjectMeta{Name: "gtpWithIgnoreLabels", Namespace: "namespace2", Labels: map[string]string{"identity": "id2", "admiral.io/env": "stage"}, Annotations: map[string]string{"admiral.io/ignore": "true"}}}

Expand All @@ -208,35 +200,53 @@ func TestGlobalTrafficController_Added(t *testing.T) {

testCases := []struct {
name string
gtp *v1.GlobalTrafficPolicy
gtpKey string
namespace string
gtp []*v1.GlobalTrafficPolicy
expectedGtps []*v1.GlobalTrafficPolicy
}{
{
name: "Gtp should be added to the cache",
gtp: &gtp,
gtpKey: "stage.id",
namespace: "namespace1",
gtp: []*v1.GlobalTrafficPolicy{&gtp},
expectedGtps: []*v1.GlobalTrafficPolicy{&gtp},
},
{
name: "Gtp with ignore annotation should not be added to the cache",
gtp: &gtpWithIgnoreLabels,
gtpKey: "stage.id2",
namespace: "namespace2",
gtp: []*v1.GlobalTrafficPolicy{&gtpWithIgnoreLabels},
expectedGtps: []*v1.GlobalTrafficPolicy{},
},
{
name: "Should cache multiple gtps in a namespace",
gtp: &gtp2,
gtpKey: "stage.id",
namespace: "namespace1",
gtp: []*v1.GlobalTrafficPolicy{&gtp, &gtp2},
expectedGtps: []*v1.GlobalTrafficPolicy{&gtp, &gtp2},
},
{
name: "Should cache gtps in from multiple namespaces",
gtp: &gtp3,
gtpKey: "stage.id",
namespace: "namespace3",
gtp: []*v1.GlobalTrafficPolicy{&gtp, &gtp3},
expectedGtps: []*v1.GlobalTrafficPolicy{&gtp3},
},
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
gtpController.Added(c.gtp)
gtpKey := common.GetGtpKey(c.gtp)
matchedGtps := gtpController.Cache.Get(gtpKey, c.gtp.Namespace)
gtpController := GlobalTrafficController{
GlobalTrafficHandler: &gth,
Cache: &gtpCache{
cache: make(map[string]map[string]map[string]*v1.GlobalTrafficPolicy),
mutex: &sync.Mutex{},
},
}
for _, g := range c.gtp {
gtpController.Added(g)
}
matchedGtps := gtpController.Cache.Get(c.gtpKey, c.namespace)
if !reflect.DeepEqual(c.expectedGtps, matchedGtps) {
t.Errorf("Test %s failed; expected %v, got %v", c.name, c.expectedGtps, matchedGtps)
}
Expand Down
1 change: 1 addition & 0 deletions admiral/pkg/controller/admiral/rollouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func NewRolloutsController(clusterID string, stopCh <-chan struct{}, handler Rol

roController := RolloutController{}
roController.RolloutHandler = handler
roController.labelSet = common.GetLabelSet()

rolloutCache := rolloutCache{}
rolloutCache.cache = make(map[string]*RolloutClusterEntry)
Expand Down