From adff6989f55c4f8087dc4269f79ec7f945b01452 Mon Sep 17 00:00:00 2001 From: mathetake Date: Tue, 10 Mar 2020 11:42:44 +0900 Subject: [PATCH] pkg/router/istio: add test for mirrorWeight --- pkg/router/istio_test.go | 109 +++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/pkg/router/istio_test.go b/pkg/router/istio_test.go index e48967f0..a2ac03a2 100644 --- a/pkg/router/istio_test.go +++ b/pkg/router/istio_test.go @@ -91,69 +91,78 @@ func TestIstioRouter_SetRoutes(t *testing.T) { err := router.Reconcile(mocks.canary) require.NoError(t, err) - p, c, m, err := router.GetRoutes(mocks.canary) - require.NoError(t, err) - - p = 60 - c = 40 - m = false - - err = router.SetRoutes(mocks.canary, p, c, m) - require.NoError(t, err) - - vs, err := mocks.meshClient.NetworkingV1alpha3().VirtualServices("default").Get("podinfo", metav1.GetOptions{}) - require.NoError(t, err) - pHost := fmt.Sprintf("%s-primary", mocks.canary.Spec.TargetRef.Name) cHost := fmt.Sprintf("%s-canary", mocks.canary.Spec.TargetRef.Name) - pRoute := istiov1alpha3.DestinationWeight{} - cRoute := istiov1alpha3.DestinationWeight{} - var mirror *istiov1alpha3.Destination - for _, http := range vs.Spec.Http { - for _, route := range http.Route { - if route.Destination.Host == pHost { - pRoute = route - } - if route.Destination.Host == cHost { - cRoute = route - mirror = http.Mirror + t.Run("normal", func(t *testing.T) { + p, c := 60, 40 + err := router.SetRoutes(mocks.canary, p, c, false) + require.NoError(t, err) + + vs, err := mocks.meshClient.NetworkingV1alpha3().VirtualServices("default").Get("podinfo", metav1.GetOptions{}) + require.NoError(t, err) + + var pRoute, cRoute istiov1alpha3.DestinationWeight + var mirror *istiov1alpha3.Destination + for _, http := range vs.Spec.Http { + for _, route := range http.Route { + if route.Destination.Host == pHost { + pRoute = route + } + if route.Destination.Host == cHost { + cRoute = route + mirror = http.Mirror + } } } - } - assert.Equal(t, p, pRoute.Weight) - assert.Equal(t, c, cRoute.Weight) - assert.Nil(t, mirror) + assert.Equal(t, p, pRoute.Weight) + assert.Equal(t, c, cRoute.Weight) + assert.Nil(t, mirror) - mirror = nil - p = 100 - c = 0 - m = true + }) - err = router.SetRoutes(mocks.canary, p, c, m) - require.NoError(t, err) + t.Run("mirror", func(t *testing.T) { + for _, w := range []float64{0, 10, 50} { + p, c := 100, 0 - vs, err = mocks.meshClient.NetworkingV1alpha3().VirtualServices("default").Get("podinfo", metav1.GetOptions{}) - require.NoError(t, err) + // set mirror weight + mocks.canary.Spec.Analysis.MirrorWeight = w + err := router.SetRoutes(mocks.canary, p, c, true) + require.NoError(t, err) - for _, http := range vs.Spec.Http { - for _, route := range http.Route { - if route.Destination.Host == pHost { - pRoute = route + vs, err := mocks.meshClient.NetworkingV1alpha3().VirtualServices("default").Get("podinfo", metav1.GetOptions{}) + require.NoError(t, err) + + var pRoute, cRoute istiov1alpha3.DestinationWeight + var mirror *istiov1alpha3.Destination + var mirrorWeight *istiov1alpha3.Percent + for _, http := range vs.Spec.Http { + for _, route := range http.Route { + if route.Destination.Host == pHost { + pRoute = route + } + if route.Destination.Host == cHost { + cRoute = route + mirror = http.Mirror + mirrorWeight = http.MirrorPercentage + } + } } - if route.Destination.Host == cHost { - cRoute = route - mirror = http.Mirror + + assert.Equal(t, p, pRoute.Weight) + assert.Equal(t, c, cRoute.Weight) + if assert.NotNil(t, mirror) { + assert.Equal(t, cHost, mirror.Host) + } + + if w > 0 && assert.NotNil(t, mirrorWeight) { + assert.Equal(t, w, mirrorWeight.Value) + } else { + assert.Nil(t, mirrorWeight) } } - } - - assert.Equal(t, p, pRoute.Weight) - assert.Equal(t, c, cRoute.Weight) - if assert.NotNil(t, mirror) { - assert.Equal(t, cHost, mirror.Host) - } + }) } func TestIstioRouter_GetRoutes(t *testing.T) {