|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package describe |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + appsv1 "k8s.io/api/apps/v1" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + describecmd "k8s.io/kubectl/pkg/describe" |
| 29 | + |
| 30 | + "github.com/karmada-io/karmada/pkg/karmadactl/options" |
| 31 | +) |
| 32 | + |
| 33 | +var deployment = &appsv1.Deployment{ |
| 34 | + TypeMeta: metav1.TypeMeta{ |
| 35 | + Kind: "Deployment", |
| 36 | + APIVersion: "apps/v1", |
| 37 | + }, |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "test-deployment", |
| 40 | + Namespace: "default", |
| 41 | + }, |
| 42 | + Spec: appsv1.DeploymentSpec{ |
| 43 | + Selector: &metav1.LabelSelector{ |
| 44 | + MatchLabels: map[string]string{"app": "nginx"}, |
| 45 | + }, |
| 46 | + Template: corev1.PodTemplateSpec{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{}, |
| 48 | + Spec: corev1.PodSpec{ |
| 49 | + Containers: []corev1.Container{ |
| 50 | + { |
| 51 | + Image: "nginx", |
| 52 | + ImagePullPolicy: "Always", |
| 53 | + Name: "nginx", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +type ResourceDescribe struct{} |
| 62 | + |
| 63 | +func (rd ResourceDescribe) Describe(string, string, describecmd.DescriberSettings) (output string, err error) { |
| 64 | + // Serialize the deployment response to JSON. |
| 65 | + bodyBytes, err := json.Marshal(deployment) |
| 66 | + if err != nil { |
| 67 | + return "", fmt.Errorf("failed to serialize deployment response: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Return the serialized deployment as a string (pretty-printed JSON). |
| 71 | + return string(bodyBytes), nil |
| 72 | +} |
| 73 | + |
| 74 | +func TestValidate(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + describeOpts *CommandDescribeOptions |
| 78 | + wantErr bool |
| 79 | + errMsg string |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "Validate_WithMemberOperationScopeAndWithoutCluster_MemberClusterOptMustBeSpecified", |
| 83 | + describeOpts: &CommandDescribeOptions{OperationScope: options.Members}, |
| 84 | + wantErr: true, |
| 85 | + errMsg: "must specify a member cluster", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "Validate__WithMemberOperationScopeAndWithCluster_Validated", |
| 89 | + describeOpts: &CommandDescribeOptions{ |
| 90 | + OperationScope: options.Members, |
| 91 | + Cluster: "test-cluster", |
| 92 | + }, |
| 93 | + wantErr: false, |
| 94 | + }, |
| 95 | + } |
| 96 | + for _, test := range tests { |
| 97 | + t.Run(test.name, func(t *testing.T) { |
| 98 | + err := test.describeOpts.Validate() |
| 99 | + if err == nil && test.wantErr { |
| 100 | + t.Fatal("expected an error, but got none") |
| 101 | + } |
| 102 | + if err != nil && !test.wantErr { |
| 103 | + t.Errorf("unexpected error, got: %v", err) |
| 104 | + } |
| 105 | + if err != nil && test.wantErr && !strings.Contains(err.Error(), test.errMsg) { |
| 106 | + t.Errorf("expected error message %s to be in %s", test.errMsg, err.Error()) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments