|
| 1 | +// Copyright 2026 HAProxy Technologies LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build e2e_parallel |
| 16 | + |
| 17 | +package differentports |
| 18 | + |
| 19 | +import ( |
| 20 | + "io" |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/haproxytech/kubernetes-ingress/deploy/tests/e2e" |
| 25 | +) |
| 26 | + |
| 27 | +func (suite *DifferentPortsSuite) Test_Different_Ports() { |
| 28 | + suite.Run("different port added without scaling", func() { |
| 29 | + suite.tmplData.Instances = append(suite.tmplData.Instances, instanceTmplData{Port: 8001, Replicas: 1}) |
| 30 | + suite.Require().NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 31 | + suite.Eventually(suite.everyReplicaReachable, e2e.WaitDuration, e2e.TickDuration) |
| 32 | + }) |
| 33 | + |
| 34 | + suite.Run("different port added with scaling", func() { |
| 35 | + suite.tmplData.Instances = append(suite.tmplData.Instances, instanceTmplData{Port: 8002, Replicas: 8}) |
| 36 | + suite.Require().NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 37 | + suite.Eventually(suite.everyReplicaReachable, e2e.WaitDuration, e2e.TickDuration) |
| 38 | + }) |
| 39 | + |
| 40 | + suite.Run("different port added while previous port removed", func() { |
| 41 | + suite.tmplData.Instances = append(suite.tmplData.Instances, instanceTmplData{Port: 8003, Replicas: 1}) |
| 42 | + suite.tmplData.Instances[len(suite.tmplData.Instances)-2].Replicas = 0 |
| 43 | + suite.Require().NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 44 | + suite.Eventually(suite.everyReplicaReachable, e2e.WaitDuration, e2e.TickDuration) |
| 45 | + }) |
| 46 | + |
| 47 | + suite.Run("standalone different ports", func() { |
| 48 | + suite.tmplData.Instances = append(suite.tmplData.Instances, instanceTmplData{Port: 8004, Replicas: 1}) |
| 49 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{{Key: "haproxy.org/standalone-backend", Value: "true"}} |
| 50 | + suite.Require().NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 51 | + suite.Require().NoError(suite.test.Apply("config/ingress.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 52 | + suite.Eventually(suite.everyReplicaReachable, e2e.WaitDuration, e2e.TickDuration) |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func (suite *DifferentPortsSuite) everyReplicaReachable() bool { |
| 57 | + totalReplicas := 0 |
| 58 | + counter := map[int]map[string]int{} |
| 59 | + for instanceIndex, instance := range suite.tmplData.Instances { |
| 60 | + totalReplicas += instance.Replicas |
| 61 | + counter[instanceIndex] = make(map[string]int) |
| 62 | + } |
| 63 | + |
| 64 | + for i := 0; i < 2*totalReplicas; i++ { |
| 65 | + func() { |
| 66 | + res, cls, err := suite.client.Do() |
| 67 | + if err != nil { |
| 68 | + suite.T().Log(err.Error()) |
| 69 | + } |
| 70 | + defer cls() |
| 71 | + if res.StatusCode == 200 { |
| 72 | + body, err := io.ReadAll(res.Body) |
| 73 | + if err != nil { |
| 74 | + suite.T().Log(err.Error()) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + pod := strings.TrimSpace(string(body)) |
| 79 | + instanceIndex, err := strconv.Atoi(strings.Split(pod, "-")[2]) // http-echo-<index>-<hash>-<random> |
| 80 | + if err != nil { |
| 81 | + suite.T().Log(err.Error()) |
| 82 | + return |
| 83 | + } |
| 84 | + counter[instanceIndex][pod]++ |
| 85 | + } |
| 86 | + }() |
| 87 | + } |
| 88 | + |
| 89 | + for instanceIndex, instance := range suite.tmplData.Instances { |
| 90 | + if len(counter[instanceIndex]) != instance.Replicas { |
| 91 | + return false |
| 92 | + } |
| 93 | + for _, v := range counter[instanceIndex] { |
| 94 | + if v != 2 { |
| 95 | + return false |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return true |
| 100 | +} |
0 commit comments