|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package tests |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/miekg/dns" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + "k8s.io/apimachinery/pkg/util/wait" |
| 27 | + |
| 28 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 29 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + ConformanceTests = append(ConformanceTests, UDPRouteTest) |
| 34 | +} |
| 35 | + |
| 36 | +var UDPRouteTest = suite.ConformanceTest{ |
| 37 | + ShortName: "UDPRoute", |
| 38 | + Description: "Make sure UDPRoute is working", |
| 39 | + Manifests: []string{"tests/udproute-simple.yaml"}, |
| 40 | + Features: []suite.SupportedFeature{ |
| 41 | + suite.SupportGateway, |
| 42 | + suite.SupportUDPRoute, |
| 43 | + }, |
| 44 | + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { |
| 45 | + t.Run("Simple UDP request matching UDPRoute should reach coredns backend", func(t *testing.T) { |
| 46 | + namespace := "gateway-conformance-infra" |
| 47 | + domain := "foo.bar.com." |
| 48 | + routeNN := types.NamespacedName{Name: "udp-coredns", Namespace: namespace} |
| 49 | + gwNN := types.NamespacedName{Name: "udp-gateway", Namespace: namespace} |
| 50 | + gwAddr := kubernetes.GatewayAndUDPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN) |
| 51 | + |
| 52 | + msg := new(dns.Msg) |
| 53 | + msg.SetQuestion(domain, dns.TypeA) |
| 54 | + |
| 55 | + if err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, true, |
| 56 | + func(_ context.Context) (done bool, err error) { |
| 57 | + t.Logf("performing DNS query %s on %s", domain, gwAddr) |
| 58 | + _, err = dns.Exchange(msg, gwAddr) |
| 59 | + if err != nil { |
| 60 | + t.Logf("failed to perform a UDP query: %v", err) |
| 61 | + return false, nil |
| 62 | + } |
| 63 | + return true, nil |
| 64 | + }); err != nil { |
| 65 | + t.Errorf("failed to perform DNS query: %v", err) |
| 66 | + } |
| 67 | + }) |
| 68 | + }, |
| 69 | +} |
0 commit comments