Files
ProjectOctopus/agent-operator/CmiiK8sOperator_test.go
2024-04-11 10:36:19 +08:00

286 lines
8.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"testing"
"time"
"wdd.io/agent-common/assert"
"wdd.io/agent-common/message_pusher"
"wdd.io/agent-common/utils"
)
var CmiiDevNamespaceList = []string{
"uavcloud-dev",
"uavcloud-devflight",
"uavcloud-devoperation",
}
func TestFindAppNotHealthyOrRestartCountGreaterThanN(t *testing.T) {
deploymentRestartCountGreaterThanN := FindAppNotHealthyOrRestartCountGreaterThanN("devflight", 10)
deploymentRestartCountGreaterThanN = FilterAllCmiiAppSoft(deploymentRestartCountGreaterThanN)
for _, deploymentInterface := range deploymentRestartCountGreaterThanN {
println()
utils.BeautifulPrint(deploymentInterface)
println()
}
}
func TestFindDeploymentReplicasSmallerThanN(t *testing.T) {
deploymentReplicasSmallerThanN := FindDeploymentReplicasSmallerThanN("devflight", 0)
for _, deploymentInterface := range deploymentReplicasSmallerThanN {
println()
utils.BeautifulPrint(deploymentInterface)
println()
}
}
func TestFindCmiiMiddlewarePodInterface(t *testing.T) {
middlewarePodInterface := FindCmiiMiddlewarePodInterface(integration)
for _, middlePod := range middlewarePodInterface {
println()
utils.BeautifulPrint(middlePod)
println()
}
}
func TestRollBackCmiiDeploymentFromUpdateLog(t *testing.T) {
updateLog := RollBackCmiiDeploymentFromUpdateLog("2024-01-10-14-37-07 uavcloud-devflight cmii-uav-depotautoreturn 12345678 123sdsa45678")
assert.Equal(t, updateLog, true, "roll back from update log failed !")
}
func TestRestartCmiiBackendDeployment(t *testing.T) {
RestartCmiiBackendDeployment("dev")
}
func TestRestartCmiiFrontendDeployment(t *testing.T) {
RestartCmiiFrontendDeployment("dev")
}
func TestFindDeploymentNotHealthy(t *testing.T) {
for _, devNamespace := range CmiiDevNamespaceList {
notHealthy := FindDeploymentNotHealthy(devNamespace)
notHealthy = FilterAllCmiiAppSoft(notHealthy)
for _, deploymentInterface := range notHealthy {
utils.BeautifulPrint(deploymentInterface)
}
}
}
func TestFindAllNodeNotHealthy(t *testing.T) {
start := time.Now()
allNodeNotHealthy := FindAllNodeNotHealthy()
elapsed := time.Since(start).Milliseconds()
fmt.Printf("执行耗时: %d ms\n", elapsed)
allNodeNotHealthy = FilterAllCmiiNodeSoft(allNodeNotHealthy)
assert.Equal(t, len(allNodeNotHealthy), 0, "have unhealthy pod !")
for _, nodeInterface := range allNodeNotHealthy {
println()
utils.BeautifulPrint(nodeInterface)
println()
}
}
func TestFindPodNotHealthy(t *testing.T) {
podNotHealthy := FindPodNotHealthy("devfl")
podNotHealthy = FilterAllCmiiPodSoft(podNotHealthy)
for _, podInterface := range podNotHealthy {
t.Logf("[%s] [%s]", podInterface.Name, podInterface.PodPhase)
}
}
func TestFindPodNotHealthy_And_Delete(t *testing.T) {
podNotHealthy := FindPodNotHealthy("devf")
podNotHealthy = FilterAllCmiiPodSoft(podNotHealthy)
for _, podInterface := range podNotHealthy {
t.Logf("[%s] [%s]", podInterface.Name, podInterface.PodPhase)
podDelete := CmiiOperator.PodDelete(podInterface.Namespace, podInterface.Name)
assert.Equal(t, podDelete, true, "delete of ", podInterface.Namespace, podInterface.Name, " failed !")
}
}
func TestFilterAllCmiiAppStrict(t *testing.T) {
allInterface := CmiiOperator.DeploymentAllInterface("devflight")
FilterAllCmiiAppStrict(allInterface)
}
func TestRestartDeploymentFromList(t *testing.T) {
allInterface := CmiiOperator.DeploymentAllInterface("devflight")
allInterface = FilterAllCmiiAppSoft(allInterface)
RestartDeploymentFromList(allInterface)
}
func TestBackupAllCmiiDeploymentToMap(t *testing.T) {
backendMap, frontendMap, srsMap := BackupAllCmiiDeploymentToMap(demo)
utils.BeautifulPrint(backendMap)
utils.BeautifulPrint(frontendMap)
utils.BeautifulPrint(srsMap)
}
func TestUpdateCmiiImageTagFromNameTagMap(t *testing.T) {
cmii530BackendMap := map[string]string{
"cmii-admin-data": "5.3.0",
"cmii-admin-gateway": "5.3.0",
"cmii-admin-user": "5.3.0",
"cmii-open-gateway": "5.3.0",
"cmii-suav-supervision": "5.3.0",
"cmii-uav-airspace": "5.3.0",
"cmii-uav-alarm": "5.3.0",
"cmii-uav-brain": "5.3.0",
"cmii-uav-cloud-live": "5.3.0",
"cmii-uav-cms": "5.3.0",
"cmii-uav-data-post-process": "5.3.0",
"cmii-uav-developer": "5.3.0",
"cmii-uav-device": "5.3.0",
"cmii-uav-emergency": "5.3.0",
"cmii-uav-gateway": "5.3.0",
"cmii-uav-gis-server": "5.3.0",
"cmii-uav-industrial-portfolio": "5.3.0",
"cmii-uav-integration": "5.3.0",
"cmii-uav-logger": "5.3.0",
"cmii-uav-material-warehouse": "5.3.0",
"cmii-uav-mission": "5.3.0",
"cmii-uav-mqtthandler": "5.3.0",
"cmii-uav-notice": "5.3.0",
"cmii-uav-oauth": "5.3.0",
"cmii-uav-process": "5.3.0",
"cmii-uav-surveillance": "5.3.0",
"cmii-uav-threedsimulation": "5.3.0",
"cmii-uav-tower": "5.3.0",
"cmii-uav-user": "5.3.0",
"cmii-uav-waypoint": "5.3.0",
//"cmii-uav-grid-datasource": "5.2.0-24810",
//"cmii-uav-grid-engine": "5.1.0",
//"cmii-uav-grid-manage": "5.1.0",
}
cmii530FrontendMap := map[string]string{
"cmii-suav-platform-supervision": "5.3.0",
"cmii-suav-platform-supervisionh5": "5.3.0",
"cmii-uav-platform": "5.3.0",
"cmii-uav-platform-ai-brain": "5.3.0",
"cmii-uav-platform-armypeople": "5.3.0",
"cmii-uav-platform-base": "5.3.0",
"cmii-uav-platform-cms-portal": "5.3.0",
"cmii-uav-platform-detection": "5.3.0",
"cmii-uav-platform-emergency-rescue": "5.3.0",
"cmii-uav-platform-logistics": "5.3.0",
"cmii-uav-platform-media": "5.3.0",
"cmii-uav-platform-multiterminal": "5.3.0",
"cmii-uav-platform-mws": "5.3.0",
"cmii-uav-platform-oms": "5.3.0",
"cmii-uav-platform-open": "5.3.0",
"cmii-uav-platform-securityh5": "5.3.0",
"cmii-uav-platform-seniclive": "5.3.0",
"cmii-uav-platform-share": "5.3.0",
"cmii-uav-platform-splice": "5.3.0",
"cmii-uav-platform-threedsimulation": "5.3.0",
"cmii-uav-platform-visualization": "5.3.0",
//"cmii-uav-platform-security": "4.1.6",
}
result := UpdateCmiiImageTagFromNameTagMap("demo", cmii530BackendMap)
utils.BeautifulPrint(result)
result = UpdateCmiiImageTagFromNameTagMap("demo", cmii530FrontendMap)
utils.BeautifulPrint(result)
}
func TestScaleCmiiFrontendDeploymentToDesiredReplicas(t *testing.T) {
ScaleCmiiFrontendDeploymentToDesiredReplicas(uat, 0)
}
func TestScaleCmiiBackendDeploymentToDesiredReplicas(t *testing.T) {
ScaleCmiiBackendDeploymentToDesiredReplicas(uat, 0)
}
func TestBackupAllDeploymentFromEnv(t *testing.T) {
BackupAllDeploymentFromEnv(demo)
}
func TestBackUpAllCmiiAppImageNameFromEnv(t *testing.T) {
BackUpAllCmiiAppImageNameFromEnv(integration)
}
func TestRestartCmiiDeployment(t *testing.T) {
cmiiEnv := integration
appName := "cmii-uav-platform"
kill := CmiiOperator.DeploymentRestartByKill(cmiiEnv, appName)
assert.Equal(t, kill, true, "have unhealthy pod !")
check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
assert.Equal(t, check, true, "DeploymentStatusCheck failed !")
}
func TestUpdateCmiiDeploymentImageTag(t *testing.T) {
// 计算20:00的时间
now := time.Now()
targetTime := time.Date(now.Year(), now.Month(), now.Day(), 16, 12, 0, 0, now.Location())
duration := time.Duration(0)
if !now.After(targetTime) {
// 计算到20:00的剩余时间
duration = targetTime.Sub(now)
} else {
// 如果当前时间已经过了20:00那么就跳到下一天
//targetTime = targetTime.Add(24 * time.Hour)
}
fmt.Println(duration)
// 等待到20:00
time.Sleep(duration)
cmiiEnv := devFlight
//appName := "cmii-uav-platform"
//newTag := "5.4.0-032601"
appNameTagMap := map[string]string{
"cmii-uav-industrial-portfolio": "5.4.0-041001",
}
for appName, newTag := range appNameTagMap {
tag := UpdateCmiiDeploymentImageTag(cmiiEnv, appName, newTag)
assert.Equal(t, tag, true, "update image tag failed !")
utils.SplitLinePrint()
check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 300)
assert.Equal(t, check, true, "deployment run failed!")
// push message
message_pusher.PushCmiiUpdateMessage(cmiiEnv, appName, newTag, check)
}
}