Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Chu Peng 楚鹏
minikube
提交
ef912eae
M
minikube
项目概览
Chu Peng 楚鹏
/
minikube
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
minikube
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
ef912eae
编写于
8月 29, 2019
作者:
M
Marcin Niemira
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve test coverage for proxy package
上级
fb50cf86
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
116 addition
and
0 deletion
+116
-0
pkg/minikube/proxy/proxy.go
pkg/minikube/proxy/proxy.go
+5
-0
pkg/minikube/proxy/proxy_test.go
pkg/minikube/proxy/proxy_test.go
+111
-0
未找到文件。
pkg/minikube/proxy/proxy.go
浏览文件 @
ef912eae
...
...
@@ -57,6 +57,11 @@ func isInBlock(ip string, block string) (bool, error) {
// ExcludeIP will exclude the ip from the http(s)_proxy
func
ExcludeIP
(
ip
string
)
error
{
if
netIp
:=
net
.
ParseIP
(
ip
);
netIp
==
nil
{
if
_
,
_
,
err
:=
net
.
ParseCIDR
(
ip
);
err
!=
nil
{
return
fmt
.
Errorf
(
"ExcludeIP(%v) requires IP as a parameter or CIDR"
,
ip
)
}
}
return
updateEnv
(
ip
,
"NO_PROXY"
)
}
...
...
pkg/minikube/proxy/proxy_test.go
浏览文件 @
ef912eae
...
...
@@ -18,8 +18,11 @@ package proxy
import
(
"fmt"
"net/http"
"os"
"testing"
"k8s.io/client-go/rest"
)
func
TestIsValidEnv
(
t
*
testing
.
T
)
{
...
...
@@ -55,6 +58,7 @@ func TestIsInBlock(t *testing.T) {
{
"192.168.0.2"
,
"192.168.0.1/32"
,
false
,
false
},
{
"192.168.0.1"
,
"192.168.0.1/18"
,
true
,
false
},
{
"abcd"
,
"192.168.0.1/18"
,
false
,
true
},
{
"192.168.0.1"
,
"foo"
,
false
,
true
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
fmt
.
Sprintf
(
"%s in %s"
,
tc
.
ip
,
tc
.
block
),
func
(
t
*
testing
.
T
)
{
...
...
@@ -124,6 +128,7 @@ func TestCheckEnv(t *testing.T) {
{
"192.168.0.13"
,
"NO_PROXY"
,
true
,
"192.168.0.13/22"
},
{
"192.168.0.13"
,
"NO_PROXY"
,
true
,
"10.10.0.13,192.168.0.13"
},
{
"192.168.0.13"
,
"NO_PROXY"
,
false
,
"10.10.0.13/22"
},
{
"10.10.10.4"
,
"NO_PROXY"
,
true
,
"172.168.0.0/30,10.10.10.0/24"
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
fmt
.
Sprintf
(
"%s in %s"
,
tc
.
ip
,
tc
.
envName
),
func
(
t
*
testing
.
T
)
{
...
...
@@ -149,3 +154,109 @@ func TestCheckEnv(t *testing.T) {
}
}
func
TestIsIPExcluded
(
t
*
testing
.
T
)
{
var
testCases
=
[]
struct
{
ip
,
env
string
excluded
bool
}{
{
"1.2.3.4"
,
"7.7.7.7"
,
false
},
{
"1.2.3.4"
,
"1.2.3.4"
,
true
},
{
"1.2.3.4"
,
""
,
false
},
{
"foo"
,
""
,
false
},
{
"foo"
,
"bar"
,
false
},
{
"foo"
,
"1.2.3.4"
,
false
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
fmt
.
Sprintf
(
"exclude %s NO_PROXY(%v)"
,
tc
.
ip
,
tc
.
env
),
func
(
t
*
testing
.
T
)
{
originalEnv
:=
os
.
Getenv
(
"NO_PROXY"
)
defer
func
()
{
// revert to pre-test env var
err
:=
os
.
Setenv
(
"NO_PROXY"
,
originalEnv
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error reverting env NO_PROXY to its original value (%s) var after test "
,
originalEnv
)
}
}()
if
err
:=
os
.
Setenv
(
"NO_PROXY"
,
tc
.
env
);
err
!=
nil
{
t
.
Errorf
(
"Error during setting env: %v"
,
err
)
}
if
excluded
:=
IsIPExcluded
(
tc
.
ip
);
excluded
!=
tc
.
excluded
{
t
.
Fatalf
(
"IsIPExcluded(%v) should return %v. NO_PROXY=%v"
,
tc
.
ip
,
tc
.
excluded
,
tc
.
env
)
}
})
}
}
func
TestExcludeIP
(
t
*
testing
.
T
)
{
var
testCases
=
[]
struct
{
ip
,
env
string
wantAErr
bool
}{
{
"1.2.3.4"
,
""
,
false
},
{
""
,
""
,
true
},
{
"7.7.7.7"
,
"7.7.7.7"
,
false
},
{
"7.7.7.7"
,
"1.2.3.4"
,
false
},
{
"foo"
,
""
,
true
},
{
"foo"
,
"1.2.3.4"
,
true
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
fmt
.
Sprintf
(
"exclude %s NO_PROXY(%s)"
,
tc
.
ip
,
tc
.
env
),
func
(
t
*
testing
.
T
)
{
originalEnv
:=
os
.
Getenv
(
"NO_PROXY"
)
defer
func
()
{
// revert to pre-test env var
err
:=
os
.
Setenv
(
"NO_PROXY"
,
originalEnv
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error reverting env NO_PROXY to its original value (%s) var after test "
,
originalEnv
)
}
}()
if
err
:=
os
.
Setenv
(
"NO_PROXY"
,
tc
.
env
);
err
!=
nil
{
t
.
Errorf
(
"Error during setting env: %v"
,
err
)
}
err
:=
ExcludeIP
(
tc
.
ip
)
if
err
!=
nil
&&
!
tc
.
wantAErr
{
t
.
Errorf
(
"ExcludeIP(%v) returned unexpected error %v"
,
tc
.
ip
,
err
)
}
if
err
==
nil
&&
tc
.
wantAErr
{
t
.
Errorf
(
"ExcludeIP(%v) should return error but error is %v"
,
tc
.
ip
,
err
)
}
})
}
}
func
TestUpdateTransport
(
t
*
testing
.
T
)
{
t
.
Run
(
"new"
,
func
(
t
*
testing
.
T
)
{
rc
:=
rest
.
Config
{}
c
:=
UpdateTransport
(
&
rc
)
tr
:=
&
http
.
Transport
{}
tr
.
RegisterProtocol
(
"file"
,
http
.
NewFileTransport
(
http
.
Dir
(
"/tmp"
)))
rt
:=
c
.
WrapTransport
(
tr
)
if
_
,
ok
:=
rt
.
(
http
.
RoundTripper
);
!
ok
{
t
.
Fatalf
(
"Cannot cast rt(%v) to http.RoundTripper"
,
rt
)
}
})
t
.
Run
(
"existing"
,
func
(
t
*
testing
.
T
)
{
// rest config initialized with WrapTransport function
rc
:=
rest
.
Config
{
WrapTransport
:
func
(
http
.
RoundTripper
)
http
.
RoundTripper
{
rt
:=
&
http
.
Transport
{}
rt
.
RegisterProtocol
(
"file"
,
http
.
NewFileTransport
(
http
.
Dir
(
"/tmp"
)))
return
rt
}}
transport
:=
&
http
.
Transport
{}
transport
.
RegisterProtocol
(
"file"
,
http
.
NewFileTransport
(
http
.
Dir
(
"/"
)))
c
:=
UpdateTransport
(
&
rc
)
rt
:=
c
.
WrapTransport
(
nil
)
if
rt
==
rc
.
WrapTransport
(
transport
)
{
t
.
Fatalf
(
"Excpected to reuse existing RoundTripper(%v) but found %v"
,
rt
,
transport
)
}
})
t
.
Run
(
"nil"
,
func
(
t
*
testing
.
T
)
{
rc
:=
rest
.
Config
{}
c
:=
UpdateTransport
(
&
rc
)
rt
:=
c
.
WrapTransport
(
nil
)
if
rt
!=
nil
{
t
.
Fatalf
(
"Expected RoundTripper nil for invocation WrapTransport(nil)"
)
}
})
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录