Skip to content

Commit

Permalink
unit-test: add unit test for pkg/ingress/kube/annotations (alibaba#156)
Browse files Browse the repository at this point in the history
Signed-off-by: charlie <qianglin98@qq.com>
  • Loading branch information
Charlie17Li authored Feb 2, 2023
1 parent 1092402 commit 7ceec94
Show file tree
Hide file tree
Showing 6 changed files with 479 additions and 1 deletion.
103 changes: 103 additions & 0 deletions pkg/ingress/kube/annotations/canary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,112 @@ import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"

networking "istio.io/api/networking/v1alpha3"
)

func TestCanaryParse(t *testing.T) {
parser := canary{}

testCases := []struct {
name string
input Annotations
expect *CanaryConfig
}{
{
name: "Don't contain the 'enableCanary' key",
input: Annotations{},
expect: nil,
},
{
name: "the 'enableCanary' is false",
input: Annotations{
buildNginxAnnotationKey(enableCanary): "false",
},
expect: &CanaryConfig{
Enabled: false,
WeightTotal: defaultCanaryWeightTotal,
},
},
{
name: "By header",
input: Annotations{
buildNginxAnnotationKey(enableCanary): "true",
buildNginxAnnotationKey(canaryByHeader): "header",
},
expect: &CanaryConfig{
Enabled: true,
Header: "header",
WeightTotal: defaultCanaryWeightTotal,
},
},
{
name: "By headerValue",
input: Annotations{
buildNginxAnnotationKey(enableCanary): "true",
buildNginxAnnotationKey(canaryByHeader): "header",
buildNginxAnnotationKey(canaryByHeaderValue): "headerValue",
},
expect: &CanaryConfig{
Enabled: true,
Header: "header",
HeaderValue: "headerValue",
WeightTotal: defaultCanaryWeightTotal,
},
},
{
name: "By headerPattern",
input: Annotations{
buildNginxAnnotationKey(enableCanary): "true",
buildNginxAnnotationKey(canaryByHeader): "header",
buildNginxAnnotationKey(canaryByHeaderPattern): "headerPattern",
},
expect: &CanaryConfig{
Enabled: true,
Header: "header",
HeaderPattern: "headerPattern",
WeightTotal: defaultCanaryWeightTotal,
},
},
{
name: "By cookie",
input: Annotations{
buildNginxAnnotationKey(enableCanary): "true",
buildNginxAnnotationKey(canaryByCookie): "cookie",
},
expect: &CanaryConfig{
Enabled: true,
Cookie: "cookie",
WeightTotal: defaultCanaryWeightTotal,
},
},
{
name: "By weight",
input: Annotations{
buildNginxAnnotationKey(enableCanary): "true",
buildNginxAnnotationKey(canaryWeight): "50",
buildNginxAnnotationKey(canaryWeightTotal): "100",
},
expect: &CanaryConfig{
Enabled: true,
Weight: 50,
WeightTotal: 100,
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
config := &Ingress{}
_ = parser.Parse(tt.input, config, nil)
if diff := cmp.Diff(tt.expect, config.Canary); diff != "" {
t.Fatalf("TestCanaryParse() mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestApplyWeight(t *testing.T) {
route := &networking.HTTPRoute{
Headers: &networking.Headers{
Expand Down
5 changes: 4 additions & 1 deletion pkg/ingress/kube/annotations/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type DestinationConfig struct {

type destination struct{}

func (a destination) Parse(annotations Annotations, config *Ingress, globalContext *GlobalContext) error {
func (a destination) Parse(annotations Annotations, config *Ingress, _ *GlobalContext) error {
if !needDestinationConfig(annotations) {
return nil
}
Expand All @@ -55,6 +55,9 @@ func (a destination) Parse(annotations Annotations, config *Ingress, globalConte
pairs := strings.Fields(line)
var weight int64 = 100
var addrIndex int
if len(pairs) == 0 {
continue
}
if strings.HasSuffix(pairs[0], "%") {
weight, err = strconv.ParseInt(strings.TrimSuffix(pairs[0], "%"), 10, 32)
if err != nil {
Expand Down
98 changes: 98 additions & 0 deletions pkg/ingress/kube/annotations/destination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed 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 annotations

import (
"testing"

"github.com/google/go-cmp/cmp"

networking "istio.io/api/networking/v1alpha3"
)

func TestDestinationParse(t *testing.T) {
parser := destination{}

testCases := []struct {
input Annotations
expect *DestinationConfig
}{
{
input: Annotations{},
expect: nil,
},
{
input: Annotations{
buildHigressAnnotationKey(destinationKey): "",
},
expect: nil,
},
{
input: Annotations{
buildHigressAnnotationKey(destinationKey): "100% my-svc.DEFAULT-GROUP.xxxx.nacos:8080 v1",
},
expect: &DestinationConfig{
McpDestination: []*networking.HTTPRouteDestination{
{
Destination: &networking.Destination{
Host: "my-svc.DEFAULT-GROUP.xxxx.nacos",
Subset: "v1",
Port: &networking.PortSelector{Number: 8080},
},
Weight: 100,
},
},
WeightSum: 100,
},
},
{
input: Annotations{
buildHigressAnnotationKey(destinationKey): "50% my-svc.DEFAULT-GROUP.xxxx.nacos:8080 v1\n\n" +
"50% my-svc.DEFAULT-GROUP.xxxx.nacos:8080 v2",
},
expect: &DestinationConfig{
McpDestination: []*networking.HTTPRouteDestination{
{
Destination: &networking.Destination{
Host: "my-svc.DEFAULT-GROUP.xxxx.nacos",
Subset: "v1",
Port: &networking.PortSelector{Number: 8080},
},
Weight: 50,
},
{
Destination: &networking.Destination{
Host: "my-svc.DEFAULT-GROUP.xxxx.nacos",
Subset: "v2",
Port: &networking.PortSelector{Number: 8080},
},
Weight: 50,
},
},
WeightSum: 100,
},
},
}

for _, testCase := range testCases {
t.Run("", func(t *testing.T) {
config := &Ingress{}
_ = parser.Parse(testCase.input, config, nil)
if diff := cmp.Diff(config.Destination, testCase.expect); diff != "" {
t.Fatalf("TestDestinationParse() mismatch: (-want +got)\n%s", diff)
}
})
}
}
89 changes: 89 additions & 0 deletions pkg/ingress/kube/annotations/redirect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed 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 annotations

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestRedirectParse(t *testing.T) {
parser := redirect{}

testCases := []struct {
name string
input Annotations
expect *RedirectConfig
}{
{
name: "Don't contain any redirect keys",
input: Annotations{},
expect: nil,
},
{
name: "By appRoot",
input: Annotations{
buildHigressAnnotationKey(appRoot): "/root",
buildHigressAnnotationKey(sslRedirect): "true",
buildHigressAnnotationKey(forceSSLRedirect): "true",
},
expect: &RedirectConfig{
AppRoot: "/root",
httpsRedirect: true,
Code: defaultPermanentRedirectCode,
},
},
{
name: "By temporalRedirect",
input: Annotations{
buildHigressAnnotationKey(temporalRedirect): "http://www.xxx.org",
},
expect: &RedirectConfig{
URL: "http://www.xxx.org",
Code: defaultTemporalRedirectCode,
},
},
{
name: "By temporalRedirect with invalid url",
input: Annotations{
buildHigressAnnotationKey(temporalRedirect): "tcp://www.xxx.org",
},
expect: &RedirectConfig{
Code: defaultPermanentRedirectCode,
},
},
{
name: "By permanentRedirect",
input: Annotations{
buildHigressAnnotationKey(permanentRedirect): "http://www.xxx.org",
},
expect: &RedirectConfig{
URL: "http://www.xxx.org",
Code: defaultPermanentRedirectCode,
},
},
}

for _, tt := range testCases {
t.Run("", func(t *testing.T) {
config := &Ingress{}
_ = parser.Parse(tt.input, config, nil)
if diff := cmp.Diff(tt.expect, config.Redirect, cmp.AllowUnexported(RedirectConfig{})); diff != "" {
t.Fatalf("TestRedirectParse() mismatch (-want +got):\n%s", diff)
}
})
}
}
Loading

0 comments on commit 7ceec94

Please sign in to comment.