Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Chu Peng 楚鹏
minikube
提交
bfb2f848
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,发现更多精彩内容 >>
提交
bfb2f848
编写于
4月 26, 2016
作者:
D
Dan Lorenc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add more testing of cluster.go.
Refactor a bit to make testing easier.
上级
482cc762
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
116 addition
and
32 deletion
+116
-32
cli/cluster/cluster.go
cli/cluster/cluster.go
+7
-11
cli/cluster/cluster_test.go
cli/cluster/cluster_test.go
+72
-0
cli/cmd/root.go
cli/cmd/root.go
+8
-6
cli/cmd/root_test.go
cli/cmd/root_test.go
+6
-6
cli/cmd/start.go
cli/cmd/start.go
+10
-2
cli/constants/constants.go
cli/constants/constants.go
+3
-1
cli/tests/api_mock.go
cli/tests/api_mock.go
+10
-0
cli/tests/driver_mock.go
cli/tests/driver_mock.go
+0
-6
未找到文件。
cli/cluster/cluster.go
浏览文件 @
bfb2f848
...
...
@@ -17,7 +17,6 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/docker/machine/drivers/virtualbox"
...
...
@@ -42,15 +41,12 @@ func StartHost(api libmachine.API) (*host.Host, error) {
}
}
// StartCluster starts as k8s cluster on the specified Host.
func
StartCluster
(
h
*
host
.
Host
)
(
string
,
error
)
{
host
,
err
:=
h
.
Driver
.
GetURL
()
if
err
!=
nil
{
return
""
,
err
}
kubeHost
:=
strings
.
Replace
(
host
,
"tcp://"
,
"http://"
,
-
1
)
kubeHost
=
strings
.
Replace
(
kubeHost
,
":2376"
,
":8080"
,
-
1
)
type
sshAble
interface
{
RunSSHCommand
(
string
)
(
string
,
error
)
}
// StartCluster starts as k8s cluster on the specified Host.
func
StartCluster
(
h
sshAble
)
error
{
for
_
,
cmd
:=
range
[]
string
{
// Download and install weave, if it doesn't exist.
`if [ ! -e /usr/local/bin/weave ];
...
...
@@ -75,11 +71,11 @@ func StartCluster(h *host.Host) (string, error) {
output
,
err
:=
h
.
RunSSHCommand
(
cmd
)
log
.
Println
(
output
)
if
err
!=
nil
{
return
""
,
err
return
err
}
}
return
kubeHost
,
nil
return
nil
}
func
createHost
(
api
libmachine
.
API
)
(
*
host
.
Host
,
error
)
{
...
...
cli/cluster/cluster_test.go
浏览文件 @
bfb2f848
package
cluster
import
(
"fmt"
"testing"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state"
"github.com/kubernetes/minikube/cli/constants"
"github.com/kubernetes/minikube/cli/tests"
...
...
@@ -37,3 +39,73 @@ func TestCreateHost(t *testing.T) {
t
.
Fatalf
(
"Wrong driver name: %v. Should be virtualbox."
,
h
.
DriverName
)
}
}
type
mockHost
struct
{
Commands
[]
string
}
func
(
m
mockHost
)
RunSSHCommand
(
cmd
string
)
(
string
,
error
)
{
m
.
Commands
=
append
(
m
.
Commands
,
cmd
)
return
""
,
nil
}
func
TestStartCluster
(
t
*
testing
.
T
)
{
h
:=
mockHost
{}
err
:=
StartCluster
(
h
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error starting cluster: %s"
,
err
)
}
}
type
mockHostError
struct
{}
func
(
m
mockHostError
)
RunSSHCommand
(
cmd
string
)
(
string
,
error
)
{
return
""
,
fmt
.
Errorf
(
"Error calling command: %s"
,
cmd
)
}
func
TestStartClusterError
(
t
*
testing
.
T
)
{
h
:=
mockHostError
{}
err
:=
StartCluster
(
h
)
if
err
==
nil
{
t
.
Fatal
(
"Error not thrown starting cluster."
)
}
}
func
TestStartHostExists
(
t
*
testing
.
T
)
{
api
:=
&
tests
.
MockAPI
{}
// Create an initial host.
_
,
err
:=
createHost
(
api
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error creating host: %v"
,
err
)
}
// Make sure the next call to Create will fail, to assert it doesn't get called again.
api
.
CreateError
=
true
if
err
:=
api
.
Create
(
&
host
.
Host
{});
err
==
nil
{
t
.
Fatal
(
"api.Create did not fail, but should have."
)
}
// This should pass without calling Create because the host exists already.
h
,
err
:=
StartHost
(
api
)
if
err
!=
nil
{
t
.
Fatal
(
"Error starting host."
)
}
if
h
.
Name
!=
constants
.
MachineName
{
t
.
Fatalf
(
"Machine created with incorrect name: %s"
,
h
.
Name
)
}
}
func
TestStartHost
(
t
*
testing
.
T
)
{
api
:=
&
tests
.
MockAPI
{}
h
,
err
:=
StartHost
(
api
)
if
err
!=
nil
{
t
.
Fatal
(
"Error starting host."
)
}
if
h
.
Name
!=
constants
.
MachineName
{
t
.
Fatalf
(
"Machine created with incorrect name: %s"
,
h
.
Name
)
}
if
exists
,
_
:=
api
.
Exists
(
h
.
Name
);
!
exists
{
t
.
Fatal
(
"Machine not saved."
)
}
}
cli/cmd/root.go
浏览文件 @
bfb2f848
...
...
@@ -39,6 +39,14 @@ clusters optimized for development workflows.
`
,
PersistentPreRun
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
localbinary
.
CurrentBinaryIsDockerMachine
=
true
for
_
,
path
:=
range
dirs
{
if
err
:=
os
.
MkdirAll
(
path
,
0777
);
err
!=
nil
{
log
.
Panicf
(
"Error creating minikube directory: %s"
,
err
)
}
}
},
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
if
os
.
Getenv
(
localbinary
.
PluginEnvKey
)
==
localbinary
.
PluginEnvVal
{
driverName
:=
os
.
Getenv
(
localbinary
.
PluginEnvDriverName
)
switch
driverName
{
...
...
@@ -50,12 +58,6 @@ clusters optimized for development workflows.
}
return
}
for
_
,
path
:=
range
dirs
{
if
err
:=
os
.
MkdirAll
(
path
,
0777
);
err
!=
nil
{
log
.
Panicf
(
"Error creating minikube directory: %s"
,
err
)
}
}
},
}
...
...
cli/cmd/root_test.go
浏览文件 @
bfb2f848
...
...
@@ -22,10 +22,10 @@ func makeTempDir() string {
return
tempDir
}
func
runCommand
()
{
func
runCommand
(
f
func
(
*
cobra
.
Command
,
[]
string
)
)
{
cmd
:=
cobra
.
Command
{}
var
args
[]
string
RootCmd
.
PersistentPreRun
(
&
cmd
,
args
)
f
(
&
cmd
,
args
)
}
func
TestPreRunDirectories
(
t
*
testing
.
T
)
{
...
...
@@ -33,7 +33,7 @@ func TestPreRunDirectories(t *testing.T) {
tempDir
:=
makeTempDir
()
defer
os
.
RemoveAll
(
tempDir
)
runCommand
()
runCommand
(
RootCmd
.
PersistentPreRun
)
for
_
,
dir
:=
range
dirs
{
_
,
err
:=
os
.
Stat
(
dir
)
...
...
@@ -46,13 +46,13 @@ func TestPreRunDirectories(t *testing.T) {
func
TestPreRunNotDriver
(
t
*
testing
.
T
)
{
tempDir
:=
makeTempDir
()
defer
os
.
RemoveAll
(
tempDir
)
runCommand
()
runCommand
(
RootCmd
.
PersistentPreRun
)
if
!
localbinary
.
CurrentBinaryIsDockerMachine
{
t
.
Fatal
(
"CurrentBinaryIsDockerMachine not set. This will break driver initialization."
)
}
}
func
Test
Pre
RunDriver
(
t
*
testing
.
T
)
{
func
TestRunDriver
(
t
*
testing
.
T
)
{
// This test is a bit complicated. It verifies that when the root command is
// called with the proper environment variables, we setup the libmachine driver.
...
...
@@ -70,7 +70,7 @@ func TestPreRunDriver(t *testing.T) {
}()
// Run the command asynchronously. It should listen on a port for connections.
go
runCommand
()
go
runCommand
(
RootCmd
.
Run
)
// The command will write out what port it's listening on over stdout.
reader
:=
bufio
.
NewReader
(
r
)
...
...
cli/cmd/start.go
浏览文件 @
bfb2f848
...
...
@@ -17,6 +17,7 @@ import (
"fmt"
"log"
"os"
"strings"
"github.com/docker/machine/libmachine"
"github.com/kubernetes/minikube/cli/cluster"
...
...
@@ -43,11 +44,18 @@ func runStart(cmd *cobra.Command, args []string) {
log
.
Println
(
"Error starting host: "
,
err
)
os
.
Exit
(
1
)
}
kubeHost
,
err
:=
cluster
.
StartCluster
(
host
)
if
err
!=
nil
{
if
err
:=
cluster
.
StartCluster
(
host
);
err
!=
nil
{
log
.
Println
(
"Error starting cluster: "
,
err
)
os
.
Exit
(
1
)
}
kubeHost
,
err
:=
host
.
Driver
.
GetURL
()
if
err
!=
nil
{
log
.
Println
(
"Error connecting to cluster: "
,
err
)
}
kubeHost
=
strings
.
Replace
(
kubeHost
,
"tcp://"
,
"http://"
,
-
1
)
kubeHost
=
strings
.
Replace
(
kubeHost
,
":2376"
,
":8080"
,
-
1
)
log
.
Printf
(
"Kubernetes is available at %s.
\n
"
,
kubeHost
)
log
.
Println
(
"Run this command to use the cluster: "
)
log
.
Printf
(
"kubectl config set-cluster minikube --insecure-skip-tls-verify=true --server=%s
\n
"
,
kubeHost
)
...
...
cli/constants/constants.go
浏览文件 @
bfb2f848
...
...
@@ -5,11 +5,13 @@ import (
"path/filepath"
)
// MachineName is the name to use for the VM.
const
MachineName
=
"minikubeVM"
// Fix for windows
var
Minipath
=
filepath
.
Join
(
os
.
Getenv
(
"HOME"
),
"minikube"
)
var
Minipath
=
filepath
.
Join
(
os
.
Getenv
(
"HOME"
),
"
.
minikube"
)
// MakeMiniPath is a utility to calculate a relative path to our directory.
func
MakeMiniPath
(
fileName
string
)
string
{
return
filepath
.
Join
(
Minipath
,
fileName
)
}
cli/tests/api_mock.go
浏览文件 @
bfb2f848
...
...
@@ -2,6 +2,7 @@ package tests
import
(
"encoding/json"
"fmt"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/auth"
...
...
@@ -10,6 +11,12 @@ import (
"github.com/docker/machine/libmachine/state"
)
// MockAPI is a struct used to mock out libmachine.API
type
MockAPI
struct
{
Hosts
[]
*
host
.
Host
CreateError
bool
}
// Close closes the API.
func
(
api
*
MockAPI
)
Close
()
error
{
return
nil
...
...
@@ -33,6 +40,9 @@ func (api *MockAPI) NewHost(driverName string, rawDriver []byte) (*host.Host, er
// Create creates the actual host.
func
(
api
*
MockAPI
)
Create
(
h
*
host
.
Host
)
error
{
if
api
.
CreateError
{
return
fmt
.
Errorf
(
"Error creating host."
)
}
return
h
.
Driver
.
Create
()
}
...
...
cli/tests/driver_mock.go
浏览文件 @
bfb2f848
...
...
@@ -2,16 +2,10 @@ package tests
import
(
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state"
)
// MockAPI is a struct used to mock out libmachine.API
type
MockAPI
struct
{
Hosts
[]
*
host
.
Host
}
// MockDriver is a struct used to mock out libmachine.Driver
type
MockDriver
struct
{
drivers
.
BaseDriver
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录