Skip to content

Commit 5b61ee7

Browse files
authored
BIGTOP-4363: Add some unit tests for stack core module (#178)
1 parent 6c3119c commit 5b61ee7

File tree

19 files changed

+1890
-8
lines changed

19 files changed

+1890
-8
lines changed

bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/spi/hook/AddHook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public String getName() {
4646
return NAME;
4747
}
4848

49-
private void addUserAndGroup(Params params) {
49+
protected void addUserAndGroup(Params params) {
5050
String user = params.user();
5151
String group = params.group();
5252

bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/LocalSettings.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Object configurations(String service, String type, String key, Obj
4343
public static Map<String, Object> configurations(String service, String type) {
4444

4545
Map<String, Object> configDataMap = new HashMap<>();
46-
File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.CONFIGURATIONS_INFO);
46+
File file = createFile(ProjectPathUtils.getAgentCachePath() + CacheFiles.CONFIGURATIONS_INFO);
4747
try {
4848
if (file.exists()) {
4949
Map<String, Map<String, Object>> configJson = JsonUtils.readFromFile(file, new TypeReference<>() {});
@@ -67,7 +67,7 @@ public static List<String> hosts(String componentName) {
6767
public static Map<String, List<String>> hosts() {
6868

6969
Map<String, List<String>> hostJson = new HashMap<>();
70-
File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.HOSTS_INFO);
70+
File file = createFile(ProjectPathUtils.getAgentCachePath() + CacheFiles.HOSTS_INFO);
7171
if (file.exists()) {
7272
hostJson = JsonUtils.readFromFile(file, new TypeReference<>() {});
7373
}
@@ -77,7 +77,7 @@ public static Map<String, List<String>> hosts() {
7777
public static Map<String, Object> basicInfo() {
7878

7979
Map<String, Object> settings = new HashMap<>();
80-
File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.SETTINGS_INFO);
80+
File file = createFile(ProjectPathUtils.getAgentCachePath() + CacheFiles.SETTINGS_INFO);
8181
if (file.exists()) {
8282
settings = JsonUtils.readFromFile(file, new TypeReference<>() {});
8383
}
@@ -87,7 +87,7 @@ public static Map<String, Object> basicInfo() {
8787
public static Map<String, String> users() {
8888

8989
Map<String, String> userMap = new HashMap<>();
90-
File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.USERS_INFO);
90+
File file = createFile(ProjectPathUtils.getAgentCachePath() + CacheFiles.USERS_INFO);
9191
if (file.exists()) {
9292
userMap = JsonUtils.readFromFile(file, new TypeReference<>() {});
9393
}
@@ -101,7 +101,7 @@ public static List<String> packages() {
101101
public static List<RepoInfo> repos() {
102102

103103
List<RepoInfo> repoInfoList = List.of();
104-
File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.REPOS_INFO);
104+
File file = createFile(ProjectPathUtils.getAgentCachePath() + CacheFiles.REPOS_INFO);
105105
if (file.exists()) {
106106
repoInfoList = JsonUtils.readFromFile(file, new TypeReference<>() {});
107107
}
@@ -111,10 +111,14 @@ public static List<RepoInfo> repos() {
111111
public static ClusterInfo cluster() {
112112

113113
ClusterInfo clusterInfo = new ClusterInfo();
114-
File file = new File(ProjectPathUtils.getAgentCachePath() + CacheFiles.CLUSTER_INFO);
114+
File file = createFile(ProjectPathUtils.getAgentCachePath() + CacheFiles.CLUSTER_INFO);
115115
if (file.exists()) {
116116
clusterInfo = JsonUtils.readFromFile(file, new TypeReference<>() {});
117117
}
118118
return clusterInfo;
119119
}
120+
121+
protected static File createFile(String fileName) {
122+
return new File(fileName);
123+
}
120124
}

bigtop-manager-stack/bigtop-manager-stack-core/src/main/java/org/apache/bigtop/manager/stack/core/utils/linux/LinuxAccountUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public static String getUserPrimaryGroup(String user) {
328328
return null;
329329
}
330330

331-
private static ShellResult sudoExecCmd(List<String> params) throws IOException {
331+
protected static ShellResult sudoExecCmd(List<String> params) throws IOException {
332332
if ("root".equals(System.getProperty("user.name"))) {
333333
return ShellExecutor.execCommand(params);
334334
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.stack.core.spi.hook;
20+
21+
import org.apache.bigtop.manager.stack.core.spi.param.Params;
22+
import org.apache.bigtop.manager.stack.core.utils.linux.LinuxAccountUtils;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.mockito.Mock;
27+
import org.mockito.MockedStatic;
28+
import org.mockito.junit.jupiter.MockitoExtension;
29+
30+
import static org.mockito.Mockito.any;
31+
import static org.mockito.Mockito.doCallRealMethod;
32+
import static org.mockito.Mockito.doNothing;
33+
import static org.mockito.Mockito.mockStatic;
34+
import static org.mockito.Mockito.never;
35+
import static org.mockito.Mockito.times;
36+
import static org.mockito.Mockito.verify;
37+
import static org.mockito.Mockito.when;
38+
39+
@ExtendWith(MockitoExtension.class)
40+
public class AddHookTest {
41+
42+
@Mock
43+
private AddHook addHook;
44+
45+
@Mock
46+
private Params params;
47+
48+
@Test
49+
public void testDoBefore() {
50+
doNothing().when(addHook).addUserAndGroup(any());
51+
doCallRealMethod().when(addHook).doBefore(any());
52+
addHook.doBefore(params);
53+
verify(addHook, times(1)).addUserAndGroup(params);
54+
}
55+
56+
@Test
57+
public void testDoAfter() {
58+
doCallRealMethod().when(addHook).doAfter(any());
59+
addHook.doAfter(params);
60+
verify(addHook, never()).addUserAndGroup(params);
61+
}
62+
63+
@Test
64+
public void testGetName() {
65+
doCallRealMethod().when(addHook).getName();
66+
String name = addHook.getName();
67+
assert name.equals("add");
68+
}
69+
70+
@Test
71+
public void testAddUserAndGroup() {
72+
try (MockedStatic<LinuxAccountUtils> linuxAccountUtilsMockedStatic = mockStatic(LinuxAccountUtils.class)) {
73+
when(params.user()).thenReturn("testUser");
74+
when(params.group()).thenReturn("testGroup1");
75+
doCallRealMethod().when(addHook).addUserAndGroup(any());
76+
77+
linuxAccountUtilsMockedStatic
78+
.when(() -> LinuxAccountUtils.userAdd(any(), any()))
79+
.thenAnswer(invocation -> null);
80+
linuxAccountUtilsMockedStatic
81+
.when(() -> LinuxAccountUtils.groupAdd(any()))
82+
.thenAnswer(invocation -> null);
83+
84+
linuxAccountUtilsMockedStatic
85+
.when(() -> LinuxAccountUtils.getUserPrimaryGroup(any()))
86+
.thenReturn("testGroup1");
87+
addHook.addUserAndGroup(params);
88+
linuxAccountUtilsMockedStatic.verify(() -> LinuxAccountUtils.userAdd(any(), any()), never());
89+
90+
linuxAccountUtilsMockedStatic
91+
.when(() -> LinuxAccountUtils.getUserPrimaryGroup(any()))
92+
.thenReturn("testGroup2");
93+
addHook.addUserAndGroup(params);
94+
linuxAccountUtilsMockedStatic.verify(() -> LinuxAccountUtils.userAdd(any(), any()), times(1));
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.stack.core.spi.hook;
20+
21+
import org.apache.bigtop.manager.stack.core.spi.param.Params;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.jupiter.MockitoExtension;
27+
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.Mockito.doCallRealMethod;
30+
import static org.mockito.Mockito.times;
31+
import static org.mockito.Mockito.verify;
32+
33+
@ExtendWith(MockitoExtension.class)
34+
public class RestartHookTest {
35+
36+
@Mock
37+
private RestartHook restartHook;
38+
39+
@Mock
40+
private Params params;
41+
42+
@Test
43+
public void testDoBefore() {
44+
doCallRealMethod().when(restartHook).doBefore(any());
45+
restartHook.doBefore(params);
46+
verify(restartHook, times(1)).doBefore(params);
47+
}
48+
49+
@Test
50+
public void testDoAfter() {
51+
doCallRealMethod().when(restartHook).doAfter(any());
52+
restartHook.doAfter(params);
53+
verify(restartHook, times(1)).doAfter(params);
54+
}
55+
56+
@Test
57+
public void testGetName() {
58+
doCallRealMethod().when(restartHook).getName();
59+
String name = restartHook.getName();
60+
assert name.equals("restart");
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.stack.core.spi.hook;
20+
21+
import org.apache.bigtop.manager.stack.core.spi.param.Params;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.jupiter.MockitoExtension;
27+
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.Mockito.doCallRealMethod;
30+
import static org.mockito.Mockito.times;
31+
import static org.mockito.Mockito.verify;
32+
33+
@ExtendWith(MockitoExtension.class)
34+
public class StartHookTest {
35+
36+
@Mock
37+
private StartHook startHook;
38+
39+
@Mock
40+
private Params params;
41+
42+
@Test
43+
public void testDoBefore() {
44+
doCallRealMethod().when(startHook).doBefore(any());
45+
startHook.doBefore(params);
46+
verify(startHook, times(1)).doBefore(params);
47+
}
48+
49+
@Test
50+
public void testDoAfter() {
51+
doCallRealMethod().when(startHook).doAfter(any());
52+
startHook.doAfter(params);
53+
verify(startHook, times(1)).doAfter(params);
54+
}
55+
56+
@Test
57+
public void testGetName() {
58+
doCallRealMethod().when(startHook).getName();
59+
String name = startHook.getName();
60+
assert name.equals("start");
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bigtop.manager.stack.core.spi.hook;
20+
21+
import org.apache.bigtop.manager.stack.core.spi.param.Params;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.jupiter.MockitoExtension;
27+
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.Mockito.doCallRealMethod;
30+
import static org.mockito.Mockito.times;
31+
import static org.mockito.Mockito.verify;
32+
33+
@ExtendWith(MockitoExtension.class)
34+
public class StopHookTest {
35+
36+
@Mock
37+
private StopHook stopHook;
38+
39+
@Mock
40+
private Params params;
41+
42+
@Test
43+
public void testDoBefore() {
44+
doCallRealMethod().when(stopHook).doBefore(any());
45+
stopHook.doBefore(params);
46+
verify(stopHook, times(1)).doBefore(params);
47+
}
48+
49+
@Test
50+
public void testDoAfter() {
51+
doCallRealMethod().when(stopHook).doAfter(any());
52+
stopHook.doAfter(params);
53+
verify(stopHook, times(1)).doAfter(params);
54+
}
55+
56+
@Test
57+
public void testGetName() {
58+
doCallRealMethod().when(stopHook).getName();
59+
String name = stopHook.getName();
60+
assert name.equals("stop");
61+
}
62+
}

0 commit comments

Comments
 (0)