|
| 1 | +/* |
| 2 | + * Copyright 2018- The Pixie 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "errors" |
| 23 | + "net" |
| 24 | + "testing" |
| 25 | + |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/watch" |
| 29 | +) |
| 30 | + |
| 31 | +func TestWatchForExternalIPUsesResolvedHostname(t *testing.T) { |
| 32 | + restoreDNSState := setTestDNSState() |
| 33 | + defer restoreDNSState() |
| 34 | + |
| 35 | + oldLookupIP := lookupIP |
| 36 | + lookupIP = func(host string) ([]net.IP, error) { |
| 37 | + if host != "example.com" { |
| 38 | + t.Fatalf("unexpected hostname: %s", host) |
| 39 | + } |
| 40 | + return []net.IP{net.ParseIP("203.0.113.10")}, nil |
| 41 | + } |
| 42 | + defer func() { lookupIP = oldLookupIP }() |
| 43 | + |
| 44 | + events := make(chan watch.Event, 1) |
| 45 | + out := make(chan svcInfo, 1) |
| 46 | + events <- serviceEvent("cloud-proxy-service", "", "example.com") |
| 47 | + close(events) |
| 48 | + |
| 49 | + if err := watchForExternalIP(events, out); err != nil { |
| 50 | + t.Fatalf("watchForExternalIP returned error: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + got := <-out |
| 54 | + if got.SvcName != "cloud-proxy-service" { |
| 55 | + t.Fatalf("unexpected service name: %s", got.SvcName) |
| 56 | + } |
| 57 | + if got.Addr != "203.0.113.10" { |
| 58 | + t.Fatalf("unexpected address: %s", got.Addr) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestWatchForExternalIPSkipsUnresolvedHostname(t *testing.T) { |
| 63 | + restoreDNSState := setTestDNSState() |
| 64 | + defer restoreDNSState() |
| 65 | + |
| 66 | + oldLookupIP := lookupIP |
| 67 | + lookupIP = func(host string) ([]net.IP, error) { |
| 68 | + return nil, errors.New("lookup failed") |
| 69 | + } |
| 70 | + defer func() { lookupIP = oldLookupIP }() |
| 71 | + |
| 72 | + events := make(chan watch.Event, 1) |
| 73 | + out := make(chan svcInfo, 1) |
| 74 | + events <- serviceEvent("cloud-proxy-service", "", "example.com") |
| 75 | + close(events) |
| 76 | + |
| 77 | + if err := watchForExternalIP(events, out); err != nil { |
| 78 | + t.Fatalf("watchForExternalIP returned error: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + if len(out) != 0 { |
| 82 | + t.Fatalf("expected no service updates, got %d", len(out)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func setTestDNSState() func() { |
| 87 | + oldDNSEntries := dnsEntriesByService |
| 88 | + dnsEntriesByService = map[string][]string{ |
| 89 | + "cloud-proxy-service": {"dev.withpixie.dev"}, |
| 90 | + } |
| 91 | + return func() { |
| 92 | + dnsEntriesByService = oldDNSEntries |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func serviceEvent(name string, ip string, hostname string) watch.Event { |
| 97 | + return watch.Event{ |
| 98 | + Object: &v1.Service{ |
| 99 | + ObjectMeta: metav1.ObjectMeta{ |
| 100 | + Name: name, |
| 101 | + }, |
| 102 | + Status: v1.ServiceStatus{ |
| 103 | + LoadBalancer: v1.LoadBalancerStatus{ |
| 104 | + Ingress: []v1.LoadBalancerIngress{ |
| 105 | + { |
| 106 | + IP: ip, |
| 107 | + Hostname: hostname, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
0 commit comments