forked from TencentBlueKing/bk-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: 修复StorageManager创建Node超时后误删存储 TencentBlueKing#2133
* feat: 记录存储中的文件用于存储失败回滚 TencentBlueKing#2133 * feat: 记录存储中的文件用于存储失败回滚 TencentBlueKing#2133 * feat: 支持删除制品的同时删除staging文件 TencentBlueKing#2133 * feat: 增加存储回滚定时任务 TencentBlueKing#2133 * feat: 增加存储回滚定时任务 TencentBlueKing#2133 * feat: 移除创建node超时回滚操作 TencentBlueKing#2133 * feat: 增加存储回滚定时任务 TencentBlueKing#2133 * feat: 增加存储回滚定时任务 TencentBlueKing#2133 * feat: 增加存储回滚定时任务 TencentBlueKing#2133 * feat: 使用findAndModify创建引用,增加单元测试 TencentBlueKing#2133 * feat: 增加存储回滚任务单元测试 TencentBlueKing#2133 * feat: 修复代码检查错误 TencentBlueKing#2133 * feat: 移除FileReference创建方法 TencentBlueKing#2133 * feat: 移除FileReference创建方法 TencentBlueKing#2133 * feat: 恢复NODE_CREATE_TIMEOUT TencentBlueKing#2133 * feat: 移除存储回滚相关代码 TencentBlueKing#2133 * feat: 移除存储回滚相关代码 TencentBlueKing#2133 * feat: 移除存储回滚相关代码 TencentBlueKing#2133 * feat: 增加StorageManager单元测试 TencentBlueKing#2133
- Loading branch information
Showing
24 changed files
with
276 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...-service/src/test/kotlin/com/tencent/bkrepo/common/artifact/manager/StorageManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.tencent.bkrepo.common.artifact.manager | ||
|
||
import com.tencent.bkrepo.common.api.constant.StringPool | ||
import com.tencent.bkrepo.common.api.pojo.Response | ||
import com.tencent.bkrepo.common.artifact.api.ArtifactFile | ||
import com.tencent.bkrepo.common.artifact.api.FileSystemArtifactFile | ||
import com.tencent.bkrepo.common.artifact.util.Constant.UT_PROJECT_ID | ||
import com.tencent.bkrepo.common.artifact.util.Constant.UT_REPO_NAME | ||
import com.tencent.bkrepo.common.artifact.util.Constant.UT_SHA256 | ||
import com.tencent.bkrepo.common.artifact.util.Constant.UT_USER | ||
import com.tencent.bkrepo.common.storage.core.StorageService | ||
import com.tencent.bkrepo.repository.api.FileReferenceClient | ||
import com.tencent.bkrepo.repository.api.NodeClient | ||
import com.tencent.bkrepo.repository.pojo.node.NodeDetail | ||
import com.tencent.bkrepo.repository.pojo.node.NodeInfo | ||
import com.tencent.bkrepo.repository.pojo.node.service.NodeCreateRequest | ||
import com.tencent.devops.plugin.api.PluginManager | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import org.junit.jupiter.api.assertThrows | ||
import org.mockito.ArgumentMatchers.anyString | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.anyOrNull | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.SpringBootConfiguration | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.test.context.TestPropertySource | ||
import org.springframework.util.ReflectionUtils | ||
import java.io.File | ||
|
||
@DisplayName("存储管理器测试") | ||
@DataMongoTest | ||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
@Import(StorageManager::class) | ||
@TestPropertySource( | ||
locations = ["classpath:bootstrap-ut.properties"] | ||
) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class StorageManagerTest @Autowired constructor( | ||
private val storageManager: StorageManager, | ||
) { | ||
@MockBean | ||
private lateinit var fileReferenceClient: FileReferenceClient | ||
|
||
@MockBean | ||
private lateinit var nodeClient: NodeClient | ||
|
||
@MockBean | ||
private lateinit var pluginManager: PluginManager | ||
|
||
@MockBean | ||
private lateinit var storageService: StorageService | ||
|
||
@MockBean | ||
private lateinit var nodeResourceFactory: NodeResourceFactory | ||
|
||
@BeforeEach | ||
fun beforeEach() { | ||
whenever(storageService.store(anyString(), any(), anyOrNull(), anyOrNull())) | ||
.thenReturn(1) | ||
whenever(nodeClient.createNode(any())) | ||
.thenReturn(Response(code = 0, data = buildNodeDetail(UT_SHA256))) | ||
whenever(fileReferenceClient.increment(anyString(), anyOrNull(), any())) | ||
.thenReturn(Response(code = 0, data = true)) | ||
} | ||
|
||
@Test | ||
fun testStoreSuccess() { | ||
store() | ||
verify(nodeClient, times(1)).createNode(any()) | ||
verify(fileReferenceClient, times(0)).increment(anyString(), anyOrNull(), any()) | ||
} | ||
|
||
@Test | ||
fun `test store failed`() { | ||
// mock | ||
val storageService = mock<StorageService>() | ||
whenever(storageService.store(anyString(), any(), anyOrNull(), anyOrNull())).then { throw RuntimeException() } | ||
val field = ReflectionUtils.findField(StorageManager::class.java, "storageService")!! | ||
field.isAccessible = true | ||
val oldStorageService = field.get(storageManager) | ||
field.set(storageManager, storageService) | ||
|
||
// store failed | ||
assertThrows<RuntimeException> { store() } | ||
verify(nodeClient, times(0)).createNode(any()) | ||
verify(fileReferenceClient, times(0)).increment(anyString(), anyOrNull(), any()) | ||
|
||
// reset mock | ||
field.set(storageManager, oldStorageService) | ||
} | ||
|
||
@Test | ||
fun `test create node failed`() { | ||
// mock | ||
whenever(nodeClient.createNode(any())).then { throw RuntimeException() } | ||
|
||
// store failed | ||
assertThrows<RuntimeException> { store() } | ||
verify(nodeClient, times(1)).createNode(any()) | ||
verify(fileReferenceClient, times(1)).increment(anyString(), anyOrNull(), any()) | ||
} | ||
|
||
private fun store(): String { | ||
val file = createTempArtifactFile() | ||
val sha256 = file.getFileSha256() | ||
val req = buildNodeCreateRequest("/a/b/c.txt", 10240L, sha256) | ||
try { | ||
storageManager.storeArtifactFile(req, file, null) | ||
} finally { | ||
file.delete() | ||
} | ||
return sha256 | ||
} | ||
|
||
private fun createTempArtifactFile(size: Long = 10240L): ArtifactFile { | ||
val tempFile = File.createTempFile("tmp", "") | ||
val content = StringPool.randomString(size.toInt()) | ||
content.byteInputStream().use { input -> | ||
tempFile.outputStream().use { output -> | ||
input.copyTo(output) | ||
} | ||
} | ||
return FileSystemArtifactFile(tempFile) | ||
} | ||
|
||
private fun buildNodeCreateRequest(fullPath: String, size: Long, sha256: String) = NodeCreateRequest( | ||
projectId = UT_PROJECT_ID, | ||
repoName = UT_REPO_NAME, | ||
folder = false, | ||
fullPath = fullPath, | ||
expires = 0, | ||
overwrite = false, | ||
size = size, | ||
sha256 = sha256, | ||
md5 = "md5", | ||
) | ||
|
||
private fun buildNodeDetail(sha256: String): NodeDetail { | ||
val nodeInfo = NodeInfo( | ||
createdBy = UT_USER, | ||
createdDate = "", | ||
lastModifiedBy = UT_USER, | ||
lastModifiedDate = "", | ||
folder = false, | ||
sha256 = sha256, | ||
path = "/a/b", | ||
name = "c.txt", | ||
fullPath = "/a/b/c.txt", | ||
size = 10240L, | ||
projectId = UT_PROJECT_ID, | ||
repoName = UT_REPO_NAME | ||
) | ||
return NodeDetail(nodeInfo) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...fact/artifact-service/src/test/kotlin/com/tencent/bkrepo/common/artifact/util/Constant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. | ||
* | ||
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. | ||
* | ||
* A copy of the MIT License is included in this file. | ||
* | ||
* | ||
* Terms of the MIT License: | ||
* --------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.tencent.bkrepo.common.artifact.util | ||
|
||
object Constant { | ||
const val UT_PROJECT_ID = "ut-project" | ||
const val UT_REPO_NAME = "ut-repo" | ||
const val UT_USER = "system" | ||
const val UT_SHA256 = "688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.