Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INLONG-9909][Agent] Add unit test for installer #9916

Merged
merged 13 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions inlong-agent/agent-installer/conf/installer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
#######################
# common config
#######################
local.ip=127.0.0.1
agent.local.ip=127.0.0.1
agent.enable.oom.exit=false

############################
# manager config
############################
manager.addr=http://127.0.0.1:8083
manager.auth.secretId=
manager.auth.secretKey=
agent.manager.addr=http://127.0.0.1:8083
agent.manager.auth.secretId=
agent.manager.auth.secretKey=

############################
# cluster config for automatically report and register
############################
cluster.tag=default_cluster
cluster.name=default_agent
agent.cluster.tag=default_cluster
agent.cluster.name=default_agent

############################
# audit config
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
18 changes: 18 additions & 0 deletions inlong-agent/agent-installer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.inlong.agent.constant.AgentConstants.AGENT_CLUSTER_NAME;
import static org.apache.inlong.agent.constant.AgentConstants.AGENT_CLUSTER_TAG;
import static org.apache.inlong.agent.constant.AgentConstants.AGENT_LOCAL_IP;
import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_FETCHER_INTERVAL;
import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_RETURN_PARAM_DATA;
import static org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_AGENT_FETCHER_INTERVAL;
import static org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_INSTALLER_MANAGER_CONFIG_HTTP_PATH;
import static org.apache.inlong.agent.constant.FetcherConstants.INSTALLER_MANAGER_CONFIG_HTTP_PATH;
import static org.apache.inlong.agent.installer.ManagerResultFormatter.getResultData;
import static org.apache.inlong.agent.utils.AgentUtils.fetchLocalIp;
import static org.apache.inlong.agent.utils.AgentUtils.fetchLocalUuid;

/**
* Fetch command from Inlong-Manager
*/
public class ManagerFetcher extends AbstractDaemon implements ProfileFetcher {

public static final String CLUSTER_NAME = "cluster.name";
public static final String CLUSTER_TAG = "cluster.tag";
private static final Logger LOGGER = LoggerFactory.getLogger(ManagerFetcher.class);
private static final GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
private static final Gson GSON = gsonBuilder.create();
Expand All @@ -68,8 +68,8 @@ public ManagerFetcher(Manager manager) {
httpManager = manager.getModuleManager().getHttpManager(conf);
baseManagerUrl = httpManager.getBaseUrl();
staticConfigUrl = buildStaticConfigUrl(baseManagerUrl);
clusterTag = conf.get(CLUSTER_TAG);
clusterName = conf.get(CLUSTER_NAME);
clusterTag = conf.get(AGENT_CLUSTER_TAG);
clusterName = conf.get(AGENT_CLUSTER_NAME);
}

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ private Runnable configFetchThread() {
@Override
public void start() throws Exception {
// when agent start, check local ip and fetch manager ip list;
localIp = fetchLocalIp();
localIp = conf.get(AGENT_LOCAL_IP);
uuid = fetchLocalUuid();
submitWorker(configFetchThread());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Collectors;

import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_ADDR;
import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_AUTH_SECRET_ID;
import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_AUTH_SECRET_KEY;
import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_REQUEST_TIMEOUT;
import static org.apache.inlong.agent.constant.FetcherConstants.AGENT_MANAGER_VIP_HTTP_PREFIX_PATH;
import static org.apache.inlong.agent.constant.FetcherConstants.DEFAULT_AGENT_MANAGER_REQUEST_TIMEOUT;
Expand All @@ -70,9 +73,6 @@
*/
public class ModuleManager extends AbstractDaemon {

public static final String MANAGER_ADDR = "manager.addr";
public static final String MANAGER_AUTH_SECRET_ID = "manager.auth.secretId";
public static final String MANAGER_AUTH_SECRET_KEY = "manager.auth.secretKey";
public static final int CONFIG_QUEUE_CAPACITY = 1;
public static final int CORE_THREAD_SLEEP_TIME = 10000;
public static final int DOWNLOAD_PACKAGE_READ_BUFF_SIZE = 1024 * 1024;
Expand All @@ -97,18 +97,18 @@ public ModuleManager() {
}

public HttpManager getHttpManager(InstallerConfiguration conf) {
String managerAddr = conf.get(MANAGER_ADDR);
String managerAddr = conf.get(AGENT_MANAGER_ADDR);
String managerHttpPrefixPath = conf.get(AGENT_MANAGER_VIP_HTTP_PREFIX_PATH,
DEFAULT_AGENT_MANAGER_VIP_HTTP_PREFIX_PATH);
int timeout = conf.getInt(AGENT_MANAGER_REQUEST_TIMEOUT,
DEFAULT_AGENT_MANAGER_REQUEST_TIMEOUT);
String secretId = conf.get(MANAGER_AUTH_SECRET_ID);
String secretKey = conf.get(MANAGER_AUTH_SECRET_KEY);
String secretId = conf.get(AGENT_MANAGER_AUTH_SECRET_ID);
String secretKey = conf.get(AGENT_MANAGER_AUTH_SECRET_KEY);
return new HttpManager(managerAddr, managerHttpPrefixPath, timeout, secretId, secretKey);
}

private boolean requiredKeys(InstallerConfiguration conf) {
return conf.hasKey(MANAGER_ADDR);
return conf.hasKey(AGENT_MANAGER_ADDR);
}

public void submitConfig(ConfigResult config) {
Expand Down Expand Up @@ -318,12 +318,12 @@ private void deleteModule(ModuleConfig module) {
private void updateModule(ModuleConfig localModule, ModuleConfig managerModule) {
LOGGER.info("update module {} start", localModule.getId());
if (localModule.getPackageConfig().getMd5().equals(managerModule.getPackageConfig().getMd5())) {
LOGGER.info("package md5 no change, will restart", localModule.getId());
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
restartModule(localModule, managerModule);
} else {
LOGGER.info("package md5 changed, will reinstall", localModule.getId());
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
deleteModule(localModule);
addModule(managerModule);
} else {
LOGGER.info("package md5 no chang, will restart", localModule.getId());
restartModule(localModule, managerModule);
}
LOGGER.info("update module {} end", localModule.getId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package installer;

import org.apache.inlong.agent.conf.TaskProfile;
import org.apache.inlong.agent.constant.AgentConstants;
import org.apache.inlong.agent.constant.FetcherConstants;
import org.apache.inlong.agent.installer.conf.InstallerConfiguration;
import org.apache.inlong.agent.pojo.FileTask.FileTaskConfig;
import org.apache.inlong.common.enums.TaskStateEnum;
import org.apache.inlong.common.pojo.agent.DataConfig;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* common environment setting up for test cases.
*/
public class BaseTestsHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(BaseTestsHelper.class);
private static final GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
private static final Gson GSON = gsonBuilder.create();
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
private final String className;
private Path testRootDir;

public BaseTestsHelper(String className) {
this.className = className;
}

public BaseTestsHelper setupAgentHome() {
testRootDir = Paths
.get("/tmp", BaseTestsHelper.class.getSimpleName(), className);
teardownAgentHome();
boolean result = testRootDir.toFile().mkdirs();
LOGGER.info("try to create {}, result is {}", testRootDir, result);
InstallerConfiguration.getInstallerConf().set(AgentConstants.AGENT_HOME, testRootDir.toString());
InstallerConfiguration.getInstallerConf().set(FetcherConstants.AGENT_MANAGER_ADDR, "");
return this;
}

public Path getTestRootDir() {
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
return testRootDir;
}

public void teardownAgentHome() {
if (testRootDir != null) {
try {
FileUtils.deleteDirectory(testRootDir.toFile());
} catch (Exception ignored) {
LOGGER.warn("deleteDirectory error ", ignored);
}
}
}

public TaskProfile getTaskProfile(int taskId, String pattern, boolean retry, Long startTime, Long endTime,
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
TaskStateEnum state, String timeZone) {
DataConfig dataConfig = getDataConfig(taskId, pattern, retry, startTime, endTime, state, timeZone);
TaskProfile profile = TaskProfile.convertToTaskProfile(dataConfig);
return profile;
justinwwhuang marked this conversation as resolved.
Show resolved Hide resolved
}

private DataConfig getDataConfig(int taskId, String pattern, boolean retry, Long startTime, Long endTime,
TaskStateEnum state, String timeZone) {
DataConfig dataConfig = new DataConfig();
dataConfig.setInlongGroupId("testGroupId");
dataConfig.setInlongStreamId("testStreamId");
dataConfig.setDataReportType(1);
dataConfig.setTaskType(3);
dataConfig.setTaskId(taskId);
dataConfig.setTimeZone(timeZone);
dataConfig.setState(state.ordinal());
FileTaskConfig fileTaskConfig = new FileTaskConfig();
fileTaskConfig.setPattern(pattern);
fileTaskConfig.setTimeOffset("0h");
fileTaskConfig.setMaxFileCount(100);
fileTaskConfig.setCycleUnit("h");
fileTaskConfig.setRetry(retry);
fileTaskConfig.setStartTime(startTime);
fileTaskConfig.setEndTime(endTime);
dataConfig.setExtParams(GSON.toJson(fileTaskConfig));
return dataConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package installer;

/**
* Enum of module action.
*/
public enum ModuleActionTypeEnum {

DOWNLOAD(0),
INSTALL(1),
UNINSTALL(2),
START(3),
STOP(4);

private final int type;

ModuleActionTypeEnum(int state) {
this.type = state;
}

public static ModuleActionTypeEnum getTaskState(int state) {
switch (state) {
case 0:
return DOWNLOAD;
case 1:
return INSTALL;
case 2:
return UNINSTALL;
case 3:
return START;
case 4:
return STOP;
default:
throw new RuntimeException("Unsupported module action " + state);
}
}

public int getType() {
return type;
}
}
Loading
Loading