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.
feat: 支持多种维度取回项目归档的数据 TencentBlueKing#2219
* feat: 支持多种维度取回项目归档的数据 TencentBlueKing#2219 * feat: 增加单元测试 TencentBlueKing#2219 * feat: 增加单元测试 TencentBlueKing#2219
- Loading branch information
Showing
6 changed files
with
234 additions
and
13 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/backend/job/api-job/src/main/kotlin/com/tencent/bkrepo/job/pojo/ArchiveRestoreRequest.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,50 @@ | ||
/* | ||
* 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.job.pojo | ||
|
||
/** | ||
* 恢复已归档或已压缩的制品请求 | ||
*/ | ||
data class ArchiveRestoreRequest( | ||
/** | ||
* 制品所属项目 | ||
*/ | ||
val projectId: String, | ||
/** | ||
* 制品所属仓库 | ||
*/ | ||
val repoName: String? = null, | ||
/** | ||
* 制品路径前缀 | ||
*/ | ||
val prefix: String? = null, | ||
/** | ||
* 制品元数据,所有元数据均匹配才会恢复 | ||
*/ | ||
val metadata: Map<String, String> = emptyMap(), | ||
) |
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
142 changes: 142 additions & 0 deletions
142
.../biz-job/src/test/kotlin/com/tencent/bkrepo/job/service/impl/ArchiveJobServiceImplTest.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,142 @@ | ||
package com.tencent.bkrepo.job.service.impl | ||
|
||
import com.tencent.bkrepo.archive.api.ArchiveClient | ||
import com.tencent.bkrepo.job.UT_MD5 | ||
import com.tencent.bkrepo.job.UT_PROJECT_ID | ||
import com.tencent.bkrepo.job.UT_REPO_NAME | ||
import com.tencent.bkrepo.job.UT_SHA256 | ||
import com.tencent.bkrepo.job.batch.task.archive.IdleNodeArchiveJob | ||
import com.tencent.bkrepo.job.migrate.MigrateRepoStorageService | ||
import com.tencent.bkrepo.job.model.TNode | ||
import com.tencent.bkrepo.job.pojo.ArchiveRestoreRequest | ||
import com.tencent.bkrepo.repository.pojo.metadata.MetadataModel | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import org.springframework.beans.factory.annotation.Autowired | ||
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.data.mongodb.core.MongoTemplate | ||
import org.springframework.data.mongodb.core.query.Query | ||
import java.time.LocalDateTime | ||
|
||
@DisplayName("归档任务服务测试") | ||
@DataMongoTest | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
@Import(ArchiveJobServiceImpl::class) | ||
class ArchiveJobServiceImplTest @Autowired constructor( | ||
private val mongoTemplate: MongoTemplate, | ||
private val service: ArchiveJobServiceImpl, | ||
) { | ||
@MockBean | ||
private lateinit var archiveJob: IdleNodeArchiveJob | ||
|
||
@MockBean | ||
private lateinit var archiveClient: ArchiveClient | ||
|
||
@MockBean | ||
private lateinit var migrateRepoStorageService: MigrateRepoStorageService | ||
|
||
@Test | ||
fun test() { | ||
mockNode() | ||
val req = ArchiveRestoreRequest( | ||
projectId = UT_PROJECT_ID, | ||
repoName = UT_REPO_NAME, | ||
prefix = "/a", | ||
) | ||
// 根据projectId筛选 | ||
var nodes = findNodes(req) | ||
assertEquals(1, nodes.size) | ||
|
||
nodes = findNodes(req.copy(projectId = "other")) | ||
assertEquals(2, nodes.size) | ||
|
||
// 根据repo筛选 | ||
nodes = findNodes(req.copy(repoName = "other")) | ||
assertEquals(2, nodes.size) | ||
|
||
// 根据路径前缀筛选 | ||
nodes = findNodes(req.copy(prefix = "/e/f/")) | ||
assertEquals(2, nodes.size) | ||
|
||
// 根据元数据筛选 | ||
nodes = findNodes( | ||
req.copy( | ||
projectId = "metadata-test", | ||
metadata = mapOf( | ||
"pid" to "ppp", | ||
"bid" to "bbb" | ||
) | ||
) | ||
) | ||
assertEquals(2, nodes.size) | ||
} | ||
|
||
private fun findNodes(request: ArchiveRestoreRequest): List<TNode> { | ||
return mongoTemplate.find(Query(service.buildCriteria(request)), TNode::class.java, COLLECTION_NAME) | ||
} | ||
|
||
private fun mockNode() { | ||
val node = TNode( | ||
id = null, | ||
projectId = UT_PROJECT_ID, | ||
repoName = UT_REPO_NAME, | ||
fullPath = "/a/b/c.txt", | ||
size = 100L, | ||
sha256 = UT_SHA256, | ||
md5 = UT_MD5, | ||
createdDate = LocalDateTime.now(), | ||
folder = false, | ||
archived = true, | ||
compressed = true, | ||
) | ||
|
||
mongoTemplate.insert(node, COLLECTION_NAME) | ||
mongoTemplate.insert(node.copy(projectId = "other"), COLLECTION_NAME) | ||
mongoTemplate.insert(node.copy(projectId = "other"), COLLECTION_NAME) | ||
mongoTemplate.insert(node.copy(repoName = "other"), COLLECTION_NAME) | ||
mongoTemplate.insert(node.copy(repoName = "other"), COLLECTION_NAME) | ||
mongoTemplate.insert(node.copy(fullPath = "/e/f/g.txt"), COLLECTION_NAME) | ||
mongoTemplate.insert(node.copy(fullPath = "/e/f/t.txt"), COLLECTION_NAME) | ||
mongoTemplate.insert( | ||
node.copy( | ||
projectId = "metadata-test", | ||
metadata = listOf( | ||
MetadataModel(key = "pid", value = "ppp"), | ||
MetadataModel(key = "bid", value = "bbb"), | ||
MetadataModel(key = "xxx", value = "yyy"), | ||
) | ||
), | ||
COLLECTION_NAME | ||
) | ||
mongoTemplate.insert( | ||
node.copy( | ||
projectId = "metadata-test", | ||
metadata = listOf( | ||
MetadataModel(key = "pid", value = "ppp"), | ||
MetadataModel(key = "bid", value = "bbb"), | ||
MetadataModel(key = "xxx", value = "yyy"), | ||
) | ||
), | ||
COLLECTION_NAME | ||
) | ||
mongoTemplate.insert( | ||
node.copy( | ||
projectId = "metadata-test", | ||
metadata = listOf( | ||
MetadataModel(key = "pid", value = "ppp"), | ||
MetadataModel(key = "bid", value = "bbb2"), | ||
MetadataModel(key = "xxx", value = "yyy"), | ||
) | ||
), | ||
COLLECTION_NAME | ||
) | ||
} | ||
|
||
companion object { | ||
private const val COLLECTION_NAME = "node_1" | ||
} | ||
} |