mirror of
https://github.com/vmware-tanzu/velero.git
synced 2025-12-23 06:15:21 +00:00
440 lines
12 KiB
Go
440 lines
12 KiB
Go
/*
|
|
Copyright the Velero contributors.
|
|
|
|
Licensed 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 repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
v1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
|
"github.com/vmware-tanzu/velero/pkg/util/kube"
|
|
)
|
|
|
|
func TestGenerateJobName1(t *testing.T) {
|
|
testCases := []struct {
|
|
repo string
|
|
expectedStart string
|
|
}{
|
|
{
|
|
repo: "example",
|
|
expectedStart: "example-maintain-job-",
|
|
},
|
|
{
|
|
repo: strings.Repeat("a", 60),
|
|
expectedStart: "repo-maintain-job-",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.repo, func(t *testing.T) {
|
|
// Call the function to test
|
|
jobName := GenerateJobName(tc.repo)
|
|
|
|
// Check if the generated job name starts with the expected prefix
|
|
if !strings.HasPrefix(jobName, tc.expectedStart) {
|
|
t.Errorf("generated job name does not start with expected prefix")
|
|
}
|
|
|
|
// Check if the length of the generated job name exceeds the Kubernetes limit
|
|
if len(jobName) > 63 {
|
|
t.Errorf("generated job name exceeds Kubernetes limit")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestDeleteOldMaintenanceJobs(t *testing.T) {
|
|
// Set up test repo and keep value
|
|
repo := "test-repo"
|
|
keep := 2
|
|
|
|
// Create some maintenance jobs for testing
|
|
var objs []client.Object
|
|
// Create a newer job
|
|
newerJob := &batchv1.Job{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "job1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{RepositoryNameLabel: repo},
|
|
},
|
|
Spec: batchv1.JobSpec{},
|
|
}
|
|
objs = append(objs, newerJob)
|
|
// Create older jobs
|
|
for i := 2; i <= 3; i++ {
|
|
olderJob := &batchv1.Job{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: fmt.Sprintf("job%d", i),
|
|
Namespace: "default",
|
|
Labels: map[string]string{RepositoryNameLabel: repo},
|
|
CreationTimestamp: metav1.Time{
|
|
Time: metav1.Now().Add(time.Duration(-24*i) * time.Hour),
|
|
},
|
|
},
|
|
Spec: batchv1.JobSpec{},
|
|
}
|
|
objs = append(objs, olderJob)
|
|
}
|
|
// Create a fake Kubernetes client
|
|
scheme := runtime.NewScheme()
|
|
_ = batchv1.AddToScheme(scheme)
|
|
cli := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
|
|
|
|
// Call the function
|
|
err := DeleteOldMaintenanceJobs(cli, repo, keep)
|
|
assert.NoError(t, err)
|
|
|
|
// Get the remaining jobs
|
|
jobList := &batchv1.JobList{}
|
|
err = cli.List(context.TODO(), jobList, client.MatchingLabels(map[string]string{RepositoryNameLabel: repo}))
|
|
assert.NoError(t, err)
|
|
|
|
// We expect the number of jobs to be equal to 'keep'
|
|
assert.Len(t, jobList.Items, keep)
|
|
|
|
// We expect that the oldest jobs were deleted
|
|
// Job3 should not be present in the remaining list
|
|
assert.NotContains(t, jobList.Items, objs[2])
|
|
|
|
// Job2 should also not be present in the remaining list
|
|
assert.NotContains(t, jobList.Items, objs[1])
|
|
}
|
|
|
|
func TestWaitForJobComplete(t *testing.T) {
|
|
// Set up test job
|
|
job := &batchv1.Job{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "test-job",
|
|
Namespace: "default",
|
|
},
|
|
Status: batchv1.JobStatus{},
|
|
}
|
|
|
|
// Define test cases
|
|
tests := []struct {
|
|
description string // Test case description
|
|
jobStatus batchv1.JobStatus // Job status to set for the test
|
|
expectError bool // Whether an error is expected
|
|
}{
|
|
{
|
|
description: "Job Succeeded",
|
|
jobStatus: batchv1.JobStatus{
|
|
Succeeded: 1,
|
|
},
|
|
expectError: false,
|
|
},
|
|
{
|
|
description: "Job Failed",
|
|
jobStatus: batchv1.JobStatus{
|
|
Failed: 1,
|
|
},
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
// Run tests
|
|
for _, tc := range tests {
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
// Set the job status
|
|
job.Status = tc.jobStatus
|
|
// Create a fake Kubernetes client
|
|
cli := fake.NewClientBuilder().WithObjects(job).Build()
|
|
// Call the function
|
|
err := WaitForJobComplete(context.Background(), cli, job)
|
|
|
|
// Check if the error matches the expectation
|
|
if tc.expectError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetMaintenanceResultFromJob(t *testing.T) {
|
|
// Set up test job
|
|
job := &batchv1.Job{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "test-job",
|
|
Namespace: "default",
|
|
},
|
|
}
|
|
|
|
// Set up test pod
|
|
pod := &v1.Pod{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "test-pod",
|
|
Namespace: "default",
|
|
Labels: map[string]string{"job-name": job.Name},
|
|
},
|
|
Status: v1.PodStatus{
|
|
ContainerStatuses: []v1.ContainerStatus{
|
|
{
|
|
State: v1.ContainerState{
|
|
Terminated: &v1.ContainerStateTerminated{
|
|
Message: "test message",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Create a fake Kubernetes client
|
|
cli := fake.NewClientBuilder().WithObjects(job, pod).Build()
|
|
|
|
// Call the function
|
|
result, err := GetMaintenanceResultFromJob(cli, job)
|
|
|
|
// Check if the result and error match the expectation
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "test message", result)
|
|
}
|
|
|
|
func TestGetLatestMaintenanceJob(t *testing.T) {
|
|
// Set up test repo
|
|
repo := "test-repo"
|
|
|
|
// Create some maintenance jobs for testing
|
|
var objs []client.Object
|
|
// Create a newer job
|
|
newerJob := &batchv1.Job{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "job1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{RepositoryNameLabel: repo},
|
|
CreationTimestamp: metav1.Time{
|
|
Time: metav1.Now().Add(time.Duration(-24) * time.Hour),
|
|
},
|
|
},
|
|
Spec: batchv1.JobSpec{},
|
|
}
|
|
objs = append(objs, newerJob)
|
|
|
|
// Create an older job
|
|
olderJob := &batchv1.Job{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "job2",
|
|
Namespace: "default",
|
|
Labels: map[string]string{RepositoryNameLabel: repo},
|
|
},
|
|
Spec: batchv1.JobSpec{},
|
|
}
|
|
objs = append(objs, olderJob)
|
|
|
|
// Create a fake Kubernetes client
|
|
scheme := runtime.NewScheme()
|
|
_ = batchv1.AddToScheme(scheme)
|
|
cli := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
|
|
|
|
// Call the function
|
|
job, err := GetLatestMaintenanceJob(cli, "default")
|
|
assert.NoError(t, err)
|
|
|
|
// We expect the returned job to be the newer job
|
|
assert.Equal(t, newerJob.Name, job.Name)
|
|
}
|
|
|
|
func TestGetMaintenanceJobConfig(t *testing.T) {
|
|
ctx := context.Background()
|
|
logger := logrus.New()
|
|
veleroNamespace := "velero"
|
|
repoMaintenanceJobConfig := "repo-maintenance-job-config"
|
|
repo := &velerov1api.BackupRepository{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: veleroNamespace,
|
|
Name: repoMaintenanceJobConfig,
|
|
},
|
|
Spec: velerov1api.BackupRepositorySpec{
|
|
BackupStorageLocation: "default",
|
|
RepositoryType: "kopia",
|
|
VolumeNamespace: "test",
|
|
},
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
repoJobConfig *v1.ConfigMap
|
|
expectedConfig *JobConfigs
|
|
expectedError error
|
|
}{
|
|
{
|
|
name: "Config not exist",
|
|
expectedConfig: nil,
|
|
expectedError: nil,
|
|
},
|
|
{
|
|
name: "Invalid JSON",
|
|
repoJobConfig: &v1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: veleroNamespace,
|
|
Name: repoMaintenanceJobConfig,
|
|
},
|
|
Data: map[string]string{
|
|
"test-default-kopia": "{\"cpuRequest:\"100m\"}",
|
|
},
|
|
},
|
|
expectedConfig: nil,
|
|
expectedError: fmt.Errorf("fail to unmarshal configs from %s", repoMaintenanceJobConfig),
|
|
},
|
|
{
|
|
name: "Find config specific for BackupRepository",
|
|
repoJobConfig: &v1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: veleroNamespace,
|
|
Name: repoMaintenanceJobConfig,
|
|
},
|
|
Data: map[string]string{
|
|
"test-default-kopia": "{\"podResources\":{\"cpuRequest\":\"100m\",\"cpuLimit\":\"200m\",\"memoryRequest\":\"100Mi\",\"memoryLimit\":\"200Mi\"},\"loadAffinity\":[{\"nodeSelector\":{\"matchExpressions\":[{\"key\":\"cloud.google.com/machine-family\",\"operator\":\"In\",\"values\":[\"e2\"]}]}}]}",
|
|
},
|
|
},
|
|
expectedConfig: &JobConfigs{
|
|
PodResources: &kube.PodResources{
|
|
CPURequest: "100m",
|
|
CPULimit: "200m",
|
|
MemoryRequest: "100Mi",
|
|
MemoryLimit: "200Mi",
|
|
},
|
|
LoadAffinities: []*kube.LoadAffinity{
|
|
{
|
|
NodeSelector: metav1.LabelSelector{
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
|
{
|
|
Key: "cloud.google.com/machine-family",
|
|
Operator: metav1.LabelSelectorOpIn,
|
|
Values: []string{"e2"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: nil,
|
|
},
|
|
{
|
|
name: "Find config specific for global",
|
|
repoJobConfig: &v1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: veleroNamespace,
|
|
Name: repoMaintenanceJobConfig,
|
|
},
|
|
Data: map[string]string{
|
|
GlobalKeyForRepoMaintenanceJobCM: "{\"podResources\":{\"cpuRequest\":\"50m\",\"cpuLimit\":\"100m\",\"memoryRequest\":\"50Mi\",\"memoryLimit\":\"100Mi\"},\"loadAffinity\":[{\"nodeSelector\":{\"matchExpressions\":[{\"key\":\"cloud.google.com/machine-family\",\"operator\":\"In\",\"values\":[\"n2\"]}]}}]}",
|
|
},
|
|
},
|
|
expectedConfig: &JobConfigs{
|
|
PodResources: &kube.PodResources{
|
|
CPURequest: "50m",
|
|
CPULimit: "100m",
|
|
MemoryRequest: "50Mi",
|
|
MemoryLimit: "100Mi",
|
|
},
|
|
LoadAffinities: []*kube.LoadAffinity{
|
|
{
|
|
NodeSelector: metav1.LabelSelector{
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
|
{
|
|
Key: "cloud.google.com/machine-family",
|
|
Operator: metav1.LabelSelectorOpIn,
|
|
Values: []string{"n2"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: nil,
|
|
},
|
|
{
|
|
name: "Specific config supersede global config",
|
|
repoJobConfig: &v1.ConfigMap{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Namespace: veleroNamespace,
|
|
Name: repoMaintenanceJobConfig,
|
|
},
|
|
Data: map[string]string{
|
|
GlobalKeyForRepoMaintenanceJobCM: "{\"podResources\":{\"cpuRequest\":\"50m\",\"cpuLimit\":\"100m\",\"memoryRequest\":\"50Mi\",\"memoryLimit\":\"100Mi\"},\"loadAffinity\":[{\"nodeSelector\":{\"matchExpressions\":[{\"key\":\"cloud.google.com/machine-family\",\"operator\":\"In\",\"values\":[\"n2\"]}]}}]}",
|
|
"test-default-kopia": "{\"podResources\":{\"cpuRequest\":\"100m\",\"cpuLimit\":\"200m\",\"memoryRequest\":\"100Mi\",\"memoryLimit\":\"200Mi\"},\"loadAffinity\":[{\"nodeSelector\":{\"matchExpressions\":[{\"key\":\"cloud.google.com/machine-family\",\"operator\":\"In\",\"values\":[\"e2\"]}]}}]}",
|
|
},
|
|
},
|
|
expectedConfig: &JobConfigs{
|
|
PodResources: &kube.PodResources{
|
|
CPURequest: "100m",
|
|
CPULimit: "200m",
|
|
MemoryRequest: "100Mi",
|
|
MemoryLimit: "200Mi",
|
|
},
|
|
LoadAffinities: []*kube.LoadAffinity{
|
|
{
|
|
NodeSelector: metav1.LabelSelector{
|
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
|
{
|
|
Key: "cloud.google.com/machine-family",
|
|
Operator: metav1.LabelSelectorOpIn,
|
|
Values: []string{"e2"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedError: nil,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var fakeClient client.Client
|
|
if tc.repoJobConfig != nil {
|
|
fakeClient = velerotest.NewFakeControllerRuntimeClient(t, tc.repoJobConfig)
|
|
} else {
|
|
fakeClient = velerotest.NewFakeControllerRuntimeClient(t)
|
|
}
|
|
|
|
jobConfig, err := GetMaintenanceJobConfig(
|
|
ctx,
|
|
fakeClient,
|
|
logger,
|
|
veleroNamespace,
|
|
repoMaintenanceJobConfig,
|
|
repo,
|
|
)
|
|
|
|
if tc.expectedError != nil {
|
|
require.ErrorContains(t, err, tc.expectedError.Error())
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
require.Equal(t, tc.expectedConfig, jobConfig)
|
|
})
|
|
}
|
|
}
|