Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #170 from sususama/installBiz
Browse files Browse the repository at this point in the history
fix installBiz need bizName and bizVersion
  • Loading branch information
lvjing2 authored Dec 9, 2023
2 parents c8a7bd5 + f374172 commit ec783fe
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
*/
package com.alipay.sofa.serverless.arklet.core.command.builtin.handler;

import com.alipay.sofa.ark.api.ArkClient;
import com.alipay.sofa.ark.api.ClientResponse;
import com.alipay.sofa.ark.api.ResponseCode;
import com.alipay.sofa.ark.common.util.FileUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.spi.model.Biz;
import com.alipay.sofa.ark.spi.service.biz.BizFactoryService;
import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand;
import com.alipay.sofa.serverless.arklet.core.command.meta.AbstractCommandHandler;
import com.alipay.sofa.serverless.arklet.core.command.meta.Command;
Expand All @@ -27,11 +31,15 @@
import com.alipay.sofa.serverless.arklet.core.command.meta.bizops.ArkBizOps;
import com.alipay.sofa.serverless.arklet.core.common.exception.ArkletRuntimeException;
import com.alipay.sofa.serverless.arklet.core.common.exception.CommandValidationException;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.net.URL;
import java.util.List;

/**
Expand All @@ -43,7 +51,6 @@ public class InstallBizHandler
AbstractCommandHandler<InstallBizHandler.Input, InstallBizHandler.InstallBizClientResponse>
implements
ArkBizOps {

@Override
public Output<InstallBizClientResponse> handle(Input input) {
MemoryPoolMXBean metaSpaceMXBean = getMetaSpaceMXBean();
Expand Down Expand Up @@ -87,11 +94,41 @@ public Command command() {

@Override
public void validate(Input input) throws CommandValidationException {
notBlank(input.getBizName(), "bizName should not be blank");
notBlank(input.getBizVersion(), "bizVersion should not be blank");
isTrue(!input.isAsync() || !StringUtils.isEmpty(input.getRequestId()),
"requestId should not be blank when async is true");
notBlank(input.getBizUrl(), "bizUrl should not be blank");

if (StringUtils.isEmpty(input.getBizName()) && StringUtils.isEmpty(input.getBizVersion())) {
// if bizName and bizVersion is blank, it means that we should parse them from the jar. this will cost io operation.
try {
refreshBizInfoFromJar(input);
} catch (IOException e) {
throw new CommandValidationException(String.format(
"refresh biz info from jar failed: %s", e.getMessage()));
}
} else if (!StringUtils.isEmpty(input.getBizName())
&& !StringUtils.isEmpty(input.getBizVersion())) {
// if bizName and bizVersion is not blank, it means that we should install the biz with the given bizName and bizVersion.
// do nothing.
} else {
// if bizName or bizVersion is blank, it is invalid, throw exception.
throw new CommandValidationException(
"bizName and bizVersion should be both blank or both not blank.");
}
}

private void refreshBizInfoFromJar(Input input) throws IOException {
// 如果入参里没有jar,例如模块卸载,这里就直接返回
if (StringUtils.isEmpty(input.getBizUrl())) {
return;
}
BizFactoryService bizFactoryService = ArkClient.getBizFactoryService();
URL url = new URL(input.getBizUrl());
File bizFile = ArkClient.createBizSaveFile(input.getBizName(), input.getBizVersion());
FileUtils.copyInputStreamToFile(url.openStream(), bizFile);
Biz biz = bizFactoryService.createBiz(bizFile);
input.setBizName(biz.getBizName());
input.setBizVersion(biz.getBizVersion());
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,31 @@
*/
package com.alipay.sofa.serverless.arklet.core.command.handler;

import com.alipay.sofa.ark.api.ArkClient;
import com.alipay.sofa.ark.spi.archive.BizArchive;
import com.alipay.sofa.ark.spi.model.Biz;
import com.alipay.sofa.ark.spi.model.BizOperation;
import com.alipay.sofa.ark.spi.service.biz.BizFactoryService;
import com.alipay.sofa.serverless.arklet.core.command.builtin.BuiltinCommand;
import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler;
import com.alipay.sofa.serverless.arklet.core.command.builtin.handler.InstallBizHandler.Input;
import com.alipay.sofa.serverless.arklet.core.command.meta.Output;
import com.alipay.sofa.serverless.arklet.core.common.exception.CommandValidationException;
import com.alipay.sofa.serverless.arklet.core.health.custom.model.CustomBiz;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

/**
Expand All @@ -35,6 +51,8 @@ public class InstallBizHandlerTest extends BaseHandlerTest {

private InstallBizHandler handler;

public BizFactoryService bizFactoryService = Mockito.mock(BizFactoryService.class);

@Before
public void setupInstallBizHandler() {
handler = (InstallBizHandler) commandService.getHandler(BuiltinCommand.INSTALL_BIZ);
Expand Down Expand Up @@ -90,6 +108,64 @@ public void testValidate_BlankBizVersion() throws CommandValidationException {
handler.validate(input);
}

@Test
public void testValidate_BlankBizName_BlankBizVersion() throws CommandValidationException, IOException {
// 准备测试数据
URL url = this.getClass().getClassLoader().getResource("test-biz.jar");
String bizName = "sofa-ark-sample-springboot-ark";
String bizVersion = "0.3.0";

Input input = new Input();
input.setBizName("");
input.setBizVersion("");
input.setBizUrl("file://" + url.getFile());


when(bizFactoryService.createBiz(any(File.class))).thenReturn(new CustomBiz(bizName, bizVersion));
arkClient.when(ArkClient::getBizFactoryService).thenReturn(bizFactoryService);
arkClient.when(() -> ArkClient.createBizSaveFile(anyString(), anyString())).thenReturn(new File(url.getFile()));

// 执行测试
handler.validate(input);
Assert.assertEquals(bizName, input.getBizName());
Assert.assertEquals(bizVersion, input.getBizVersion());
}

@Test(expected = CommandValidationException.class)
public void testValidate_BizName_BlankBizVersion() throws CommandValidationException,
IOException {
// 准备测试数据
URL url = this.getClass().getClassLoader().getResource("test-biz.jar");
String bizName = "sofa-ark-sample-springboot-ark";
String bizVersion = "0.3.0";

Input input = new Input();
input.setBizName(bizName);
input.setBizVersion("");
input.setBizUrl("file://" + url.getFile());

// 执行测试
handler.validate(input);
}

@Test
public void testValidate_BizName_BizVersion() throws CommandValidationException {
// 准备测试数据
URL url = this.getClass().getClassLoader().getResource("test-biz.jar");
String bizName = "sofa-ark-sample-springboot-ark";
String bizVersion = "0.3.0";

Input input = new Input();
input.setBizName(bizName);
input.setBizVersion(bizVersion);
input.setBizUrl("file://" + url.getFile());

// 执行测试
handler.validate(input);
// 测试验证 ArkClient.createBizSaveFile 被调用 0 次
arkClient.verify(() -> ArkClient.createBizSaveFile(anyString(), anyString()), times(0));
}

@Test(expected = CommandValidationException.class)
public void testValidate_BlankRequestId() throws CommandValidationException {
// 执行测试
Expand Down

0 comments on commit ec783fe

Please sign in to comment.