|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. 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 | +package upstream_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + |
| 22 | + "github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations" |
| 23 | + "github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations/upstream" |
| 24 | +) |
| 25 | + |
| 26 | +func TestIPRestrictionHandler(t *testing.T) { |
| 27 | + anno := map[string]string{ |
| 28 | + annotations.AnnotationsUpstreamScheme: "grpcs", |
| 29 | + } |
| 30 | + u := upstream.NewParser() |
| 31 | + |
| 32 | + out, err := u.Parse(annotations.NewExtractor(anno)) |
| 33 | + ups, ok := out.(upstream.Upstream) |
| 34 | + if !ok { |
| 35 | + t.Fatalf("could not parse upstream") |
| 36 | + } |
| 37 | + assert.Nil(t, err, "checking given error") |
| 38 | + assert.Equal(t, "grpcs", ups.Scheme) |
| 39 | + |
| 40 | + anno[annotations.AnnotationsUpstreamScheme] = "gRPC" |
| 41 | + out, err = u.Parse(annotations.NewExtractor(anno)) |
| 42 | + ups, ok = out.(upstream.Upstream) |
| 43 | + if !ok { |
| 44 | + t.Fatalf("could not parse upstream") |
| 45 | + } |
| 46 | + assert.Nil(t, err, "checking given error") |
| 47 | + assert.Equal(t, "grpc", ups.Scheme) |
| 48 | + |
| 49 | + anno[annotations.AnnotationsUpstreamScheme] = "nothing" |
| 50 | + out, err = u.Parse(annotations.NewExtractor(anno)) |
| 51 | + assert.NotNil(t, err, "checking given error") |
| 52 | + assert.Nil(t, out, "checking given output") |
| 53 | +} |
| 54 | + |
| 55 | +func TestRetryParsing(t *testing.T) { |
| 56 | + anno := map[string]string{ |
| 57 | + annotations.AnnotationsUpstreamRetry: "2", |
| 58 | + } |
| 59 | + u := upstream.NewParser() |
| 60 | + out, err := u.Parse(annotations.NewExtractor(anno)) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf(err.Error()) |
| 63 | + } |
| 64 | + ups, ok := out.(upstream.Upstream) |
| 65 | + if !ok { |
| 66 | + t.Fatalf("could not parse upstream") |
| 67 | + } |
| 68 | + assert.Nil(t, err, "checking given error") |
| 69 | + assert.Equal(t, 2, ups.Retry) |
| 70 | + |
| 71 | + anno[annotations.AnnotationsUpstreamRetry] = "asdf" |
| 72 | + out, err = u.Parse(annotations.NewExtractor(anno)) |
| 73 | + assert.NotNil(t, err, "checking given error") |
| 74 | +} |
| 75 | + |
| 76 | +func TestTimeoutParsing(t *testing.T) { |
| 77 | + anno := map[string]string{ |
| 78 | + annotations.AnnotationsUpstreamTimeoutConnect: "2s", |
| 79 | + annotations.AnnotationsUpstreamTimeoutRead: "3s", |
| 80 | + annotations.AnnotationsUpstreamTimeoutSend: "4s", |
| 81 | + } |
| 82 | + u := upstream.NewParser() |
| 83 | + out, err := u.Parse(annotations.NewExtractor(anno)) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf(err.Error()) |
| 86 | + } |
| 87 | + ups, ok := out.(upstream.Upstream) |
| 88 | + if !ok { |
| 89 | + t.Fatalf("could not parse upstream") |
| 90 | + } |
| 91 | + assert.Nil(t, err, "checking given error") |
| 92 | + assert.Equal(t, 2, ups.TimeoutConnect) |
| 93 | + assert.Equal(t, 3, ups.TimeoutRead) |
| 94 | + assert.Equal(t, 4, ups.TimeoutSend) |
| 95 | + anno[annotations.AnnotationsUpstreamRetry] = "asdf" |
| 96 | + out, err = u.Parse(annotations.NewExtractor(anno)) |
| 97 | + assert.NotNil(t, err, "checking given error") |
| 98 | +} |
0 commit comments