github.com/polarismesh/polaris@v1.17.8/plugin/whitelist/ip_whitelist_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package whitelist 19 20 import ( 21 "errors" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 26 "github.com/polarismesh/polaris/plugin" 27 ) 28 29 func Test_ipWhitelist_Name(t *testing.T) { 30 tests := []struct { 31 name string 32 want string 33 }{ 34 { 35 name: "get name", 36 want: PluginName, 37 }, 38 } 39 for _, tt := range tests { 40 t.Run(tt.name, func(t *testing.T) { 41 i := &ipWhitelist{} 42 got := i.Name() 43 assert.Equal(t, tt.want, got) 44 }) 45 } 46 } 47 48 func Test_ipWhitelist_Initialize(t *testing.T) { 49 type args struct { 50 conf *plugin.ConfigEntry 51 } 52 tests := []struct { 53 name string 54 args args 55 wantIPs map[string]bool 56 wantErr error 57 }{ 58 { 59 name: "initialize success", 60 args: args{ 61 conf: &plugin.ConfigEntry{ 62 Name: "whitelist", 63 Option: map[string]interface{}{ 64 "ip": []interface{}{ 65 "127.0.0.1", 66 "192.168.0.1", 67 }, 68 }, 69 }, 70 }, 71 wantIPs: map[string]bool{ 72 "127.0.0.1": true, 73 "192.168.0.1": true, 74 }, 75 wantErr: nil, 76 }, 77 { 78 name: "initialize fail", 79 args: args{ 80 conf: &plugin.ConfigEntry{ 81 Name: "whitelist", 82 Option: map[string]interface{}{ 83 "ip": "127.0.0.1", 84 }, 85 }, 86 }, 87 wantIPs: map[string]bool{}, 88 wantErr: errors.New("whitelist plugin initialize error"), 89 }, 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 i := &ipWhitelist{} 94 err := i.Initialize(tt.args.conf) 95 assert.Equal(t, tt.wantErr, err) 96 assert.Equal(t, tt.wantIPs, i.ips) 97 }) 98 } 99 } 100 101 func Test_ipWhitelist_Destroy(t *testing.T) { 102 tests := []struct { 103 name string 104 }{ 105 { 106 name: "destroy", 107 }, 108 } 109 for _, tt := range tests { 110 t.Run(tt.name, func(t *testing.T) { 111 i := &ipWhitelist{} 112 err := i.Destroy() 113 assert.Nil(t, err) 114 }) 115 } 116 } 117 118 func Test_ipWhitelist_Contain(t *testing.T) { 119 type args struct { 120 elem interface{} 121 } 122 tests := []struct { 123 name string 124 args args 125 want bool 126 }{ 127 { 128 name: "contain true", 129 args: args{ 130 elem: "127.0.0.1", 131 }, 132 want: true, 133 }, 134 { 135 name: "contain false", 136 args: args{ 137 elem: "0.0.0.0", 138 }, 139 want: false, 140 }, 141 } 142 for _, tt := range tests { 143 t.Run(tt.name, func(t *testing.T) { 144 i := &ipWhitelist{ 145 ips: map[string]bool{ 146 "127.0.0.1": true, 147 "192.168.0.1": true, 148 }, 149 } 150 got := i.Contain(tt.args.elem) 151 assert.Equal(t, tt.want, got) 152 }) 153 } 154 }