Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
野花太放肆
jenkins
提交
09722a98
J
jenkins
项目概览
野花太放肆
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
09722a98
编写于
12月 09, 2015
作者:
J
Jesse Glick
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Converting BootFailureTest from Groovy to Java.
上级
54374fc2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
137 addition
and
0 deletion
+137
-0
test/src/test/java/hudson/util/BootFailureTest.java
test/src/test/java/hudson/util/BootFailureTest.java
+137
-0
未找到文件。
test/src/test/
groovy/hudson/util/BootFailureTest.groovy
→
test/src/test/
java/hudson/util/BootFailureTest.java
浏览文件 @
09722a98
package
hudson.util
package
hudson.util
;
import
hudson.WebAppMain
import
hudson.WebAppMain
;
import
hudson.model.Hudson
import
hudson.model.Hudson
;
import
hudson.model.listeners.ItemListener
import
hudson.model.listeners.ItemListener
;
import
jenkins.model.Jenkins
import
static
hudson
.
util
.
BootFailureTest
.
makeBootFail
;
import
org.junit.After
import
java.io.File
;
import
org.junit.Rule
import
java.io.IOException
;
import
org.junit.Test
import
java.util.ArrayList
;
import
org.junit.rules.TemporaryFolder
import
java.util.Arrays
;
import
org.jvnet.hudson.test.HudsonHomeLoader
import
java.util.List
;
import
org.jvnet.hudson.test.JenkinsRule
import
javax.servlet.ServletContext
;
import
org.jvnet.hudson.test.TestEnvironment
import
javax.servlet.ServletContextEvent
;
import
org.jvnet.hudson.test.TestExtension
import
jenkins.model.Jenkins
;
import
org.kohsuke.stapler.WebApp
import
org.apache.commons.io.FileUtils
;
import
org.junit.After
;
import
javax.servlet.ServletContextEvent
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.TemporaryFolder
;
import
org.jvnet.hudson.test.HudsonHomeLoader
;
import
org.jvnet.hudson.test.JenkinsRule
;
import
org.jvnet.hudson.test.TestEnvironment
;
import
org.jvnet.hudson.test.TestExtension
;
import
org.kohsuke.stapler.WebApp
;
/**
/**
*
*
*
*
* @author Kohsuke Kawaguchi
* @author Kohsuke Kawaguchi
*/
*/
class
BootFailureTest
{
public
class
BootFailureTest
{
@Rule
@Rule
public
TemporaryFolder
tmpDir
=
new
TemporaryFolder
();
public
TemporaryFolder
tmpDir
=
new
TemporaryFolder
();
static
boolean
makeBootFail
=
true
;
static
boolean
makeBootFail
=
true
;
@Rule
static
class
CustomRule
extends
JenkinsRule
{
public
JenkinsRule
j
=
new
JenkinsRule
()
{
@Override
@Override
void
before
()
throws
Throwable
{
public
void
before
()
throws
Throwable
{
env
=
new
TestEnvironment
(
testDescription
);
env
=
new
TestEnvironment
(
testDescription
);
env
.
pin
();
env
.
pin
();
// don't let Jenkins start automatically
// don't let Jenkins start automatically
...
@@ -38,78 +45,93 @@ class BootFailureTest {
...
@@ -38,78 +45,93 @@ class BootFailureTest {
@Override
@Override
public
Hudson
newHudson
()
throws
Exception
{
public
Hudson
newHudson
()
throws
Exception
{
def
ws
=
createWebServer
()
ServletContext
ws
=
createWebServer
();
def
wa
=
new
WebAppMain
()
{
WebAppMain
wa
=
new
WebAppMain
()
{
@Override
@Override
WebAppMain
.
FileAndDescription
getHomeDir
(
ServletContextEvent
event
)
{
public
WebAppMain
.
FileAndDescription
getHomeDir
(
ServletContextEvent
event
)
{
return
new
WebAppMain
.
FileAndDescription
(
homeLoader
.
allocate
(),
"test"
);
try
{
return
new
WebAppMain
.
FileAndDescription
(
homeLoader
.
allocate
(),
"test"
);
}
catch
(
Exception
x
)
{
throw
new
AssertionError
(
x
);
}
}
}
}
}
;
wa
.
contextInitialized
(
new
ServletContextEvent
(
ws
));
wa
.
contextInitialized
(
new
ServletContextEvent
(
ws
));
wa
.
joinInit
();
wa
.
joinInit
();
def
a
=
WebApp
.
get
(
ws
).
app
;
Object
a
=
WebApp
.
get
(
ws
).
getApp
();
if
(
a
instanceof
Jenkins
)
if
(
a
instanceof
Hudson
)
{
return
a
;
return
(
Hudson
)
a
;
}
return
null
;
// didn't boot
return
null
;
// didn't boot
}
}
}
}
@Rule
public
CustomRule
j
=
new
CustomRule
();
@After
@After
public
void
tearDown
()
{
public
void
tearDown
()
{
Jenkins
.
getInstance
()?.
cleanUp
()
Jenkins
j
=
Jenkins
.
getInstance
();
if
(
j
!=
null
)
{
j
.
cleanUp
();
}
}
}
public
static
class
SeriousError
extends
Error
{}
public
static
class
SeriousError
extends
Error
{}
@TestExtension
()
@TestExtension
(
"runBootFailureScript"
)
public
static
class
InduceBootFailure
extends
ItemListener
{
public
static
class
InduceBootFailure
extends
ItemListener
{
@Override
@Override
void
onLoaded
()
{
public
void
onLoaded
()
{
if
(
makeBootFail
)
if
(
makeBootFail
)
throw
new
SeriousError
();
throw
new
SeriousError
();
}
}
}
}
@Test
@Test
void
runBootFailureScript
()
{
public
void
runBootFailureScript
()
throws
Exception
{
final
def
home
=
tmpDir
.
newFolder
()
final
File
home
=
tmpDir
.
newFolder
();
j
.
with
({
->
home
}
as
HudsonHomeLoader
)
j
.
with
(
new
HudsonHomeLoader
()
{
@Override
public
File
allocate
()
throws
Exception
{
return
home
;
}
});
// creates a script
// creates a script
new
File
(
home
,
"boot-failure.groovy"
).
text
=
"hudson.util.BootFailureTest.problem = exception"
;
FileUtils
.
write
(
new
File
(
home
,
"boot-failure.groovy"
),
"hudson.util.BootFailureTest.problem = exception"
)
;
def
d
=
new
File
(
home
,
"boot-failure.groovy.d"
)
File
d
=
new
File
(
home
,
"boot-failure.groovy.d"
);
d
.
mkdirs
();
d
.
mkdirs
();
new
File
(
d
,
"1.groovy"
).
text
=
"hudson.util.BootFailureTest.runRecord << '1'"
;
FileUtils
.
write
(
new
File
(
d
,
"1.groovy"
),
"hudson.util.BootFailureTest.runRecord << '1'"
)
;
new
File
(
d
,
"2.groovy"
).
text
=
"hudson.util.BootFailureTest.runRecord << '2'"
;
FileUtils
.
write
(
new
File
(
d
,
"2.groovy"
),
"hudson.util.BootFailureTest.runRecord << '2'"
)
;
// first failed boot
// first failed boot
makeBootFail
=
true
;
makeBootFail
=
true
;
assert
j
.
newHudson
()==
null
;
assert
Null
(
j
.
newHudson
())
;
assert
bootFailures
(
home
)==
1
;
assert
Equals
(
1
,
bootFailures
(
home
))
;
// second failed boot
// second failed boot
problem
=
null
;
problem
=
null
;
runRecord
=
[]
;
runRecord
=
new
ArrayList
<
String
>()
;
assert
j
.
newHudson
()==
null
;
assert
Null
(
j
.
newHudson
())
;
assert
bootFailures
(
home
)==
2
;
assert
Equals
(
2
,
bootFailures
(
home
))
;
assert
runRecord
==[
"1"
,
"2"
]
assert
Equals
(
Arrays
.
asList
(
"1"
,
"2"
),
runRecord
);
// make sure the script has actually run
// make sure the script has actually run
assert
problem
.
cause
instanceof
SeriousError
assert
Equals
(
SeriousError
.
class
,
problem
.
getCause
().
getClass
());
// if it boots well, the failure record should be gone
// if it boots well, the failure record should be gone
makeBootFail
=
false
;
makeBootFail
=
false
;
assert
j
.
newHudson
()!=
null
;
assert
NotNull
(
j
.
newHudson
())
;
assert
!
BootFailure
.
getBootFailureFile
(
home
).
exists
()
assert
False
(
BootFailure
.
getBootFailureFile
(
home
).
exists
());
}
}
private
static
int
bootFailures
(
File
home
)
{
private
static
int
bootFailures
(
File
home
)
throws
IOException
{
return
BootFailure
.
getBootFailureFile
(
home
).
readLines
().
size
()
return
FileUtils
.
readLines
(
BootFailure
.
getBootFailureFile
(
home
)).
size
();
}
}
// to be set by the script
// to be set by the script
public
static
Exception
problem
;
public
static
Exception
problem
;
public
static
def
runRecord
=
[]
;
public
static
List
<
String
>
runRecord
=
new
ArrayList
<
String
>()
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录