squakez commented on a change in pull request #2577: URL: https://github.com/apache/camel-k/pull/2577#discussion_r699021504
########## File path: e2e/common/traits/route_test.go ########## @@ -0,0 +1,124 @@ +// +build integration + +// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "knative" + +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You 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 traits + +import ( + "bytes" + "crypto/tls" + "fmt" + "io/ioutil" + "net/http" + "testing" + "time" + + . "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + . "github.com/apache/camel-k/e2e/support" + "github.com/apache/camel-k/pkg/util/openshift" +) + +const( + secretName = "my-combined-tls" + keyName = "my-key" + certName = "my-cert" + keyFilePath = "files/key.key" + certFilePath = "files/crt.crt" +) + +func TestRunRouteTLS(t *testing.T) { + WithNewTestNamespace(t, func(ns string) { + ocp, err := openshift.IsOpenShift(TestClient()) + if !ocp { + t.Skip("This test requires route object which is available on OpenShift only.") + return + } + assert.Nil(t, err) + + createSecrets(ns) + + refKey := secretName + "/" + keyName + refCert := secretName + "/" + certName + Expect(Kamel("install", "-n", ns).Execute()).To(Succeed()) + Expect(Kamel("run", "-n", ns, "files/NettyServer.java", + "-t", "route.enabled=true", + "-t", "route.tls-termination=edge", + "-t", "route.tls-certificate-secret=" + refCert, + "-t", "route.tls-key-secret=" + refKey, + ).Execute()).To(Succeed()) + Eventually(IntegrationPodPhase(ns, "netty-server"), TestTimeoutMedium).Should(Equal(corev1.PodRunning)) + + t.Run("Route https works", func(t *testing.T) { + route := Route(ns, "netty-server") + Eventually(route, TestTimeoutMedium).ShouldNot(BeNil()) + // must wait a little time after route is created, before an http request + time.Sleep(3 * time.Second) + response := httpsRequest(t, fmt.Sprintf("https://%s/hello", route().Spec.Host)) + assert.Equal(t, "Hello World", response) + }) + // Cleanup + Expect(Kamel("delete", "--all", "-n", ns).Execute()).Should(BeNil()) + // }) + }) +} + +func httpsRequest(t *testing.T, url string) string { + transCfg := &http.Transport{ + // ignore self signed SSL certificates + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client := &http.Client{Transport: transCfg} + response, err := client.Get(url) + defer func() { + if response != nil { + _ = response.Body.Close() + } + }() + assert.Nil(t, err) + buf := new(bytes.Buffer) + _, err = buf.ReadFrom(response.Body) + assert.Nil(t, err) + return buf.String() +} + +func createSecrets(ns string) error { Review comment: Yeah, I think it would be cleaner to have a `func NewBinarySecret(ns string, name string, data map[string][]byte) error` method which generically takes in a binary content. Then you use it from your private method with the certificates created for the purpose. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org