Skip to content

Commit 78cb19d

Browse files
committed
Make sure reports are not run in test environments
1 parent d5526a4 commit 78cb19d

File tree

7 files changed

+179
-4
lines changed

7 files changed

+179
-4
lines changed

src/main/java/com/gitblit/instance/GitblitInstance.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.gitblit.instance;
22

3+
import com.gitblit.IStoredSettings;
34
import com.gitblit.manager.IRuntimeManager;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ScheduledExecutorService;
7+
import java.util.concurrent.TimeUnit;
48

59
public class GitblitInstance
610
{
11+
private final static String STATS_URL = "https://instats.gitblit.dev/hiitsme/";
712

813
private IRuntimeManager runtimeManager;
914

@@ -50,5 +55,95 @@ else if (runtimeManager.getStatus().isGO){
5055

5156
public void start()
5257
{
58+
if (shouldRunReports()) {
59+
startReports();
60+
}
61+
}
62+
63+
64+
/**
65+
* Determine if the reporting task should run.
66+
*
67+
* We do not want to report anything, i.e. the reporting task to run,
68+
* if we are running unit tests or integration tests.
69+
* Instance reports should only be sent for production instances or released versions.
70+
* Therefore we also check if the Gitblit version is a SNAPSHOT version,
71+
* or if the docker image is not a release version, when running from a docker image.
72+
* A docker image running under GOSS should also not report anything.
73+
*/
74+
boolean shouldRunReports()
75+
{
76+
// We can only run reports when we have been initialized
77+
if (this.report == null || this.runtimeManager == null) {
78+
return false;
79+
}
80+
81+
// Check if we are running in a test environment
82+
IStoredSettings settings = this.runtimeManager.getSettings();
83+
if (! settings.getString("gitblit.testReportingURL", "").isEmpty()) {
84+
// Force reporting to run overriding any test settings
85+
return true;
86+
}
87+
if (settings.getBoolean("gitblit.testRun", false)) {
88+
return false;
89+
}
90+
91+
// Check if we are running a SNAPSHOT version
92+
if (this.runtimeManager.getStatus().version.endsWith("SNAPSHOT")) {
93+
return false;
94+
}
95+
96+
if (this.report.instanceStat.instanceType == GitblitInstanceStat.GitblitInstanceType.DOCKER) {
97+
// Check if we are running a docker image that is not a release version
98+
if (! settings.getString("container.imageType", "").equals("release")) {
99+
return false;
100+
}
101+
102+
// Check if we are running a docker image under GOSS
103+
if (System.getenv("GITBLIT_GOSS_TEST") != null) {
104+
return false;
105+
}
106+
}
107+
108+
return true;
53109
}
110+
111+
112+
/**
113+
* Start the reporting task.
114+
*
115+
* This will start a thread that runs once a day and sends the instance
116+
* report to the popularity report server.
117+
*/
118+
private void startReports()
119+
{
120+
this.executor = Executors.newSingleThreadScheduledExecutor();
121+
122+
String baseUrl = STATS_URL;
123+
int delay = 24;
124+
int period = 24 * 60; // 24 hours in minutes
125+
TimeUnit unit = TimeUnit.MINUTES;
126+
127+
this.executor.scheduleAtFixedRate(new Runnable()
128+
{
129+
@Override
130+
public void run()
131+
{
132+
sendMyStats(baseUrl + instanceId);
133+
}
134+
}, delay, period, unit);
135+
}
136+
137+
/**
138+
* Send the instance report to the popularity report server.
139+
*
140+
* This will send a JSON object to the server with the instance report.
141+
*
142+
* @param server
143+
* The URL to send the report to.
144+
*/
145+
private void sendMyStats(String server)
146+
{
147+
}
148+
54149
}

src/main/java/com/gitblit/models/ServerStatus.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public class ServerStatus implements Serializable {
5151

5252
public String servletContainer;
5353

54-
public ServerStatus() {
54+
public ServerStatus(String version) {
5555
this.bootDate = new Date();
56-
this.version = Constants.getVersion();
56+
this.version = version;
5757
this.releaseDate = Constants.getBuildDate();
5858

5959
this.heapMaximum = Runtime.getRuntime().maxMemory();
@@ -76,6 +76,10 @@ public ServerStatus() {
7676
put("os.version");
7777
}
7878

79+
public ServerStatus() {
80+
this(Constants.getVersion());
81+
}
82+
7983
private void put(String key) {
8084
systemProperties.put(key, System.getProperty(key));
8185
}

src/test/config/test-gitblit.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#
22
# Gitblit Unit Testing properties
33
#
4+
gitblit.testRun = true
45
git.allowAnonymousPushes = true
56
git.defaultAccessRestriction = NONE
67
git.repositoriesFolder = ${baseFolder}/git
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.gitblit.instance;
2+
3+
import com.gitblit.manager.IRuntimeManager;
4+
5+
import com.gitblit.models.ServerStatus;
6+
import com.gitblit.tests.mock.MockRuntimeManager;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
13+
14+
public class GitblitInstanceTest
15+
{
16+
@Test
17+
public void testShouldNotReportUnintialized()
18+
{
19+
GitblitInstance instance = new GitblitInstance();
20+
assertFalse(instance.shouldRunReports());
21+
}
22+
23+
@Test
24+
public void testShouldNotReportInTests()
25+
{
26+
GitblitInstance instance = new GitblitInstance();
27+
instance.init(new MockRuntimeManager());
28+
assertFalse(instance.shouldRunReports());
29+
}
30+
31+
@Test
32+
public void testShouldNotReportInSnapshotVersion()
33+
{
34+
GitblitInstance instance = new GitblitInstance();
35+
IRuntimeManager runtimeManager = new MockRuntimeManager();
36+
runtimeManager.getSettings().overrideSetting("gitblit.testRun", "false");
37+
instance.init(runtimeManager);
38+
assertFalse(instance.shouldRunReports());
39+
}
40+
41+
@Test
42+
public void testShouldReportIfForced()
43+
{
44+
GitblitInstance instance = new GitblitInstance();
45+
IRuntimeManager runtimeManager = new MockRuntimeManager();
46+
runtimeManager.getSettings().overrideSetting("gitblit.testRunReporting", "true");
47+
instance.init(runtimeManager);
48+
assertTrue(instance.shouldRunReports());
49+
}
50+
51+
@Test
52+
public void testShouldReportInReleaseVersion()
53+
{
54+
ServerStatus serverStatus = new ServerStatus("1.10.123");
55+
MockRuntimeManager runtimeManager = new MockRuntimeManager();
56+
runtimeManager.setStatus(serverStatus);
57+
runtimeManager.getSettings().overrideSetting("gitblit.testRun", "false");
58+
59+
GitblitInstance instance = new GitblitInstance();
60+
instance.init(runtimeManager);
61+
assertTrue(instance.shouldRunReports());
62+
}
63+
64+
}

src/test/java/com/gitblit/tests/GitBlitSuite.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.gitblit.instance.GitblitInstanceIdTest;
3030
import com.gitblit.instance.GitblitInstanceStatTest;
31+
import com.gitblit.instance.GitblitInstanceTest;
3132
import com.gitblit.utils.TimeUtilsTest;
3233
import org.eclipse.jgit.api.Git;
3334
import org.eclipse.jgit.lib.Repository;
@@ -77,7 +78,7 @@
7778
BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class,
7879
SshKeysDispatcherTest.class, UITicketTest.class, PathUtilsTest.class, SshKerberosAuthenticationTest.class,
7980
GravatarTest.class, FilestoreManagerTest.class, FilestoreServletTest.class, TicketReferenceTest.class,
80-
GitblitInstanceIdTest.class, GitblitInstanceStatTest.class })
81+
GitblitInstanceIdTest.class, GitblitInstanceStatTest.class, GitblitInstanceTest.class })
8182
public class GitBlitSuite {
8283

8384
public static final File BASEFOLDER = new File("data");

src/test/java/com/gitblit/tests/GitblitUnitTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
public class GitblitUnitTest extends org.junit.Assert {
3232

3333
public static IStoredSettings settings() {
34-
return runtime().getSettings();
34+
IStoredSettings settings = runtime().getSettings();
35+
// Insert marker that this is running as a test
36+
settings.overrideSetting("gitblit.testRun", "true");
37+
return settings;
3538
}
3639

3740
public static IRuntimeManager runtime() {

src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public MockRuntimeManager(Map<String, Object> settings) {
5252

5353
public MockRuntimeManager(IStoredSettings settings) {
5454
this.settings = settings;
55+
// Insert marker that this is running as a test
56+
settings.overrideSetting("gitblit.testRun", "true");
5557

5658
this.serverStatus = new ServerStatus();
5759
this.serverStatus.servletContainer = "MockServer";
@@ -94,6 +96,11 @@ public Date getBootDate() {
9496
return serverStatus.bootDate;
9597
}
9698

99+
public void setStatus(ServerStatus status)
100+
{
101+
this.serverStatus = status;
102+
}
103+
97104
@Override
98105
public ServerStatus getStatus() {
99106
// update heap memory status

0 commit comments

Comments
 (0)