github.com/solo-io/service-mesh-hub@v0.9.2/test/e2e/istio/traffic_policy_test.go (about)

     1  package istio_test
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/solo-io/service-mesh-hub/pkg/api/networking.smh.solo.io/v1alpha2"
     7  	"github.com/solo-io/service-mesh-hub/pkg/mesh-networking/translation/utils/metautils"
     8  	"github.com/solo-io/service-mesh-hub/test/e2e"
     9  	"github.com/solo-io/service-mesh-hub/test/utils"
    10  	v1 "github.com/solo-io/skv2/pkg/api/core.skv2.solo.io/v1"
    11  	istionetworkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
    12  	"k8s.io/apimachinery/pkg/api/errors"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  	"sigs.k8s.io/controller-runtime/pkg/client"
    15  
    16  	"github.com/solo-io/service-mesh-hub/test/data"
    17  
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("TrafficPolicy", func() {
    23  	var (
    24  		err      error
    25  		manifest utils.Manifest
    26  		ctx      = context.Background()
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		manifest, err = utils.NewManifest("bookinfo-policies.yaml")
    31  		Expect(err).NotTo(HaveOccurred())
    32  	})
    33  
    34  	AfterEach(func() {
    35  		manifest.Cleanup(BookinfoNamespace)
    36  	})
    37  
    38  	It("applies traffic shift policies to local subsets", func() {
    39  
    40  		By("initially curling reviews should return both reviews-v1 and reviews-v2", func() {
    41  			Eventually(curlReviews, "1m", "1s").Should(ContainSubstring(`"color": "black"`))
    42  			Eventually(curlReviews, "1m", "1s").ShouldNot(ContainSubstring(`"color": "black"`))
    43  		})
    44  
    45  		By("creating a TrafficPolicy with traffic shift to reviews-v2 should consistently shift traffic", func() {
    46  			trafficShiftReviewsV2 := data.LocalTrafficShiftPolicy("bookinfo-policy", BookinfoNamespace, &v1.ClusterObjectRef{
    47  				Name:        "reviews",
    48  				Namespace:   BookinfoNamespace,
    49  				ClusterName: mgmtClusterName,
    50  			}, map[string]string{"version": "v2"}, 9080)
    51  
    52  			err = manifest.AppendResources(trafficShiftReviewsV2)
    53  			Expect(err).NotTo(HaveOccurred())
    54  			err = manifest.KubeApply(BookinfoNamespace)
    55  			Expect(err).NotTo(HaveOccurred())
    56  
    57  			// ensure status is updated
    58  			utils.AssertTrafficPolicyStatuses(dynamicClient, BookinfoNamespace)
    59  			// check we consistently hit the v2 subset
    60  			Consistently(curlReviews, "10s", "0.1s").Should(ContainSubstring(`"color": "black"`))
    61  		})
    62  
    63  		By("delete TrafficPolicy should remove traffic shift", func() {
    64  			err = manifest.KubeDelete(BookinfoNamespace)
    65  			Expect(err).NotTo(HaveOccurred())
    66  
    67  			Eventually(curlReviews, "1m", "1s").Should(ContainSubstring(`"color": "black"`))
    68  			Eventually(curlReviews, "1m", "1s").ShouldNot(ContainSubstring(`"color": "black"`))
    69  		})
    70  	})
    71  
    72  	It("disables mTLS for traffic target", func() {
    73  		var getReviewsDestinationRule = func() (*istionetworkingv1alpha3.DestinationRule, error) {
    74  			env := e2e.GetEnv()
    75  			destRuleClient := env.Management.DestinationRuleClient
    76  			meta := metautils.TranslatedObjectMeta(
    77  				&v1.ClusterObjectRef{
    78  					Name:        "reviews",
    79  					Namespace:   BookinfoNamespace,
    80  					ClusterName: mgmtClusterName,
    81  				},
    82  				nil,
    83  			)
    84  			return destRuleClient.GetDestinationRule(ctx, client.ObjectKey{Name: meta.Name, Namespace: meta.Namespace})
    85  		}
    86  
    87  		By("initially ensure that DestinationRule exists for mgmt reviews traffic target", func() {
    88  			Eventually(func() *istionetworkingv1alpha3.DestinationRule {
    89  				destRule, err := getReviewsDestinationRule()
    90  				if err != nil {
    91  					return nil
    92  				}
    93  				return destRule
    94  			}, "30s", "1s").ShouldNot(BeNil())
    95  		})
    96  
    97  		By("creating TrafficPolicy that overrides default mTLS settings for reviews traffic target", func() {
    98  			tp := &v1alpha2.TrafficPolicy{
    99  				TypeMeta: metav1.TypeMeta{
   100  					Kind:       "TrafficPolicy",
   101  					APIVersion: v1alpha2.SchemeGroupVersion.String(),
   102  				},
   103  				ObjectMeta: metav1.ObjectMeta{
   104  					Name:        "mtls-disable",
   105  					Namespace:   BookinfoNamespace,
   106  					ClusterName: mgmtClusterName,
   107  				},
   108  				Spec: v1alpha2.TrafficPolicySpec{
   109  					Mtls: &v1alpha2.TrafficPolicySpec_MTLS{
   110  						Istio: &v1alpha2.TrafficPolicySpec_MTLS_Istio{
   111  							TlsMode: v1alpha2.TrafficPolicySpec_MTLS_Istio_DISABLE,
   112  						},
   113  					},
   114  				},
   115  			}
   116  			err = manifest.AppendResources(tp)
   117  			Expect(err).NotTo(HaveOccurred())
   118  			err = manifest.KubeApply(BookinfoNamespace)
   119  			Expect(err).NotTo(HaveOccurred())
   120  
   121  			// ensure status is updated
   122  			utils.AssertTrafficPolicyStatuses(dynamicClient, BookinfoNamespace)
   123  
   124  			// Check that DestinationRule for reviews no longer exists
   125  			Eventually(func() bool {
   126  				_, err := getReviewsDestinationRule()
   127  				return errors.IsNotFound(err)
   128  			}, "30s", "1s").Should(BeTrue())
   129  		})
   130  
   131  		By("first ensure that DestinationRule for mgmt reviews traffic target exists", func() {
   132  			err = manifest.KubeDelete(BookinfoNamespace)
   133  			Expect(err).NotTo(HaveOccurred())
   134  
   135  			Eventually(func() *istionetworkingv1alpha3.DestinationRule {
   136  				destRule, _ := getReviewsDestinationRule()
   137  				return destRule
   138  			}, "30s", "1s").ShouldNot(BeNil())
   139  		})
   140  	})
   141  })