<commit_before>// +build e2e istio

/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"fmt"
	"math/big"
	"net"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"testing"
	"time"

	"golang.org/x/sync/errgroup"
	"k8s.io/apimachinery/pkg/watch"

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"knative.dev/pkg/apis/istio/v1alpha3"
	pkgTest "knative.dev/pkg/test"
	"knative.dev/pkg/test/logstream"
	"knative.dev/pkg/test/spoof"
	"knative.dev/serving/pkg/apis/networking"
	"knative.dev/serving/test"
	"knative.dev/serving/test/e2e"
	v1a1test "knative.dev/serving/test/v1alpha1"
)

var (
	namespace = "knative-serving"
)

func TestIstioProbing(t *testing.T) {
	cancel := logstream.Start(t)
	defer cancel()

	clients := e2e.Setup(t)

	// Save the current Gateway to restore it after the test
	oldGateway, err := clients.SharedClient.NetworkingV1alpha3().Gateways(namespace).Get(networking.KnativeIngressGateway, metav1.GetOptions{})
	if err != nil {
		t.Fatalf("Failed to get Gateway %s/%s", namespace, networking.KnativeIngressGateway)
	}
	restore := func() {
		curGateway, err := clients.SharedClient.NetworkingV1alpha3().Gateways(namespace).Get(networking.KnativeIngressGateway, metav1.GetOptions{})
		if err != nil {
			t.Fatalf("Failed to get Gateway %s/%s", namespace, networking.KnativeIngressGateway)
		}
		curGateway.Spec.Servers = oldGateway.Spec.Servers
		if _, err := clients.SharedClient.NetworkingV1alpha3().Gateways(namespace).Update(curGateway); err != nil {
			t.Fatalf("Failed to restore Gateway %s/%s: %v", namespace, networking.KnativeIngressGateway, err)
		}
	}
	test.CleanupOnInterrupt(restore)
	defer restore()

	// Create a dummy service to get the domain name
	var domain string
	func() {
		names := test.ResourceNames{
			Service: test.ObjectNameForTest(t),
			Image:   "helloworld",
		}
		test.CleanupOnInterrupt(func() { test.TearDown(clients, names) })
		defer test.TearDown(clients, names)
		objects, _, err := v1a1test.CreateRunLatestServiceReady(t, clients, &names,
			false /* https TODO(taragu) turn this on after helloworld test running with https */)
		if err != nil {
			t.Fatalf("Failed to create Service %s: %v", names.Service, err)
		}
		domain = strings.SplitN(objects.Route.Status.URL.Host, ".", 2)[1]
	}()

	tlsOptions := &v1alpha3.TLSOptions{
		Mode:              v1alpha3.TLSModeSimple,
		PrivateKey:        "/etc/istio/ingressgateway-certs/tls.key",
		ServerCertificate: "/etc/istio/ingressgateway-certs/tls.crt",
	}

	cases := []struct {
		name    string
		servers []v1alpha3.Server
		urls    []string
	}{{
		name: "HTTP",
		servers: []v1alpha3.Server{{
			Hosts: []string{"*"},
			Port: v1alpha3.Port{
				Name:     "standard-http",
				Number:   80,
				Protocol: v1alpha3.ProtocolHTTP,
			},
		}},
		urls: []string{"http://%s/"},
	}, {
		name: "HTTP2",
		servers: []v1alpha3.Server{{
			Hosts: []string{"*"},
			Port: v1alpha3.Port{
				Name:     "standard-http2",<commit_msg>Start testing Istio 1.4.0 (#6097)<commit_after>// +build e2e istio

/*
Copyright 2019 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"fmt"
	"math/big"
	"net"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"testing"
	"time"

	"golang.org/x/sync/errgroup"
	"k8s.io/apimachinery/pkg/watch"

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"knative.dev/pkg/apis/istio/v1alpha3"
	pkgTest "knative.dev/pkg/test"
	"knative.dev/pkg/test/logstream"
	"knative.dev/pkg/test/spoof"
	"knative.dev/serving/pkg/apis/networking"
	"knative.dev/serving/test"
	"knative.dev/serving/test/e2e"
	v1a1test "knative.dev/serving/test/v1alpha1"
)

var (
	namespace = "knative-serving"
)

func TestIstioProbing(t *testing.T) {
	cancel := logstream.Start(t)
	defer cancel()

	clients := e2e.Setup(t)

	// Save the current Gateway to restore it after the test
	oldGateway, err := clients.SharedClient.NetworkingV1alpha3().Gateways(namespace).Get(networking.KnativeIngressGateway, metav1.GetOptions{})
	if err != nil {
		t.Fatalf("Failed to get Gateway %s/%s", namespace, networking.KnativeIngressGateway)
	}
	restore := func() {
		curGateway, err := clients.SharedClient.NetworkingV1alpha3().Gateways(namespace).Get(networking.KnativeIngressGateway, metav1.GetOptions{})
		if err != nil {
			t.Fatalf("Failed to get Gateway %s/%s", namespace, networking.KnativeIngressGateway)
		}
		curGateway.Spec.Servers = oldGateway.Spec.Servers
		if _, err := clients.SharedClient.NetworkingV1alpha3().Gateways(namespace).Update(curGateway); err != nil {
			t.Fatalf("Failed to restore Gateway %s/%s: %v", namespace, networking.KnativeIngressGateway, err)
		}
	}
	test.CleanupOnInterrupt(restore)
	defer restore()

	// Create a dummy service to get the domain name
	var domain string
	func() {
		names := test.ResourceNames{
			Service: test.ObjectNameForTest(t),
			Image:   "helloworld",
		}
		test.CleanupOnInterrupt(func() { test.TearDown(clients, names) })
		defer test.TearDown(clients, names)
		objects, _, err := v1a1test.CreateRunLatestServiceReady(t, clients, &names,
			false /* https TODO(taragu) turn this on after helloworld test running with https */)
		if err != nil {
			t.Fatalf("Failed to create Service %s: %v", names.Service, err)
		}
		domain = strings.SplitN(objects.Route.Status.URL.Host, ".", 2)[1]
	}()

	tlsOptions := &v1alpha3.TLSOptions{
		Mode:              v1alpha3.TLSModeSimple,
		PrivateKey:        "/etc/istio/ingressgateway-certs/tls.key",
		ServerCertificate: "/etc/istio/ingressgateway-certs/tls.crt",
		SubjectAltNames:   []string{},
	}

	cases := []struct {
		name    string
		servers []v1alpha3.Server
		urls    []string
	}{{
		name: "HTTP",
		servers: []v1alpha3.Server{{
			Hosts: []string{"*"},
			Port: v1alpha3.Port{
				Name:     "standard-http",
				Number:   80,
				Protocol: v1alpha3.ProtocolHTTP,
			},
		}},
		urls: []string{"http://%s/"},
	}, {
		name: "HTTP2",
		servers: []v1alpha3.Server{{
			Hosts: []string{"*"},
			Port: v1alpha3.Port{
				Name:     "standard-http2",
{'fraction_non_alphanumeric': 0.13906185139061852, 'fraction_numerical': 0.018403210184032102, 'mean_word_length': 4.058082575227432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2862058', 'n_tokens_mistral': 2770, 'n_tokens_neox': 2610, 'n_words': 587}