|
| 1 | +/* |
| 2 | +Copyright (C) 2022-2026 ApeCloud Co., Ltd |
| 3 | +
|
| 4 | +This file is part of KubeBlocks project |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU Affero General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU Affero General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU Affero General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +package common |
| 21 | + |
| 22 | +import ( |
| 23 | + "testing" |
| 24 | + |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | +) |
| 29 | + |
| 30 | +func TestGetFieldRef(t *testing.T) { |
| 31 | + pod := &corev1.Pod{ |
| 32 | + ObjectMeta: metav1.ObjectMeta{ |
| 33 | + Name: "pod-0", |
| 34 | + Namespace: "default", |
| 35 | + UID: types.UID("uid-1"), |
| 36 | + Labels: map[string]string{"app": "mysql", "role": "leader"}, |
| 37 | + Annotations: map[string]string{"config": "enabled"}, |
| 38 | + }, |
| 39 | + Spec: corev1.PodSpec{ |
| 40 | + NodeName: "node-1", |
| 41 | + ServiceAccountName: "kb-sa", |
| 42 | + }, |
| 43 | + Status: corev1.PodStatus{ |
| 44 | + HostIP: "192.168.0.1", |
| 45 | + PodIP: "10.0.0.1", |
| 46 | + PodIPs: []corev1.PodIP{{IP: "10.0.0.1"}, {IP: "fd00::1"}}, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + cases := []struct { |
| 51 | + name string |
| 52 | + fieldPath string |
| 53 | + want string |
| 54 | + wantErr bool |
| 55 | + }{ |
| 56 | + {name: "annotation subscript", fieldPath: "metadata.annotations['config']", want: "enabled"}, |
| 57 | + {name: "label subscript", fieldPath: "metadata.labels['role']", want: "leader"}, |
| 58 | + {name: "annotations map is sorted", fieldPath: "metadata.annotations", want: `config="enabled"`}, |
| 59 | + {name: "labels map is sorted", fieldPath: "metadata.labels", want: "app=\"mysql\"\nrole=\"leader\""}, |
| 60 | + {name: "name", fieldPath: "metadata.name", want: "pod-0"}, |
| 61 | + {name: "namespace", fieldPath: "metadata.namespace", want: "default"}, |
| 62 | + {name: "uid", fieldPath: "metadata.uid", want: "uid-1"}, |
| 63 | + {name: "node name", fieldPath: "spec.nodeName", want: "node-1"}, |
| 64 | + {name: "service account", fieldPath: "spec.serviceAccountName", want: "kb-sa"}, |
| 65 | + {name: "host ip", fieldPath: "status.hostIP", want: "192.168.0.1"}, |
| 66 | + {name: "pod ip", fieldPath: "status.podIP", want: "10.0.0.1"}, |
| 67 | + {name: "pod ips", fieldPath: "status.podIPs", want: "10.0.0.1,fd00::1"}, |
| 68 | + {name: "unsupported field", fieldPath: "spec.unsupported", wantErr: true}, |
| 69 | + {name: "unsupported subscript", fieldPath: "spec.containers['main']", wantErr: true}, |
| 70 | + {name: "invalid label key", fieldPath: "metadata.labels['bad/key/again']", wantErr: true}, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tc := range cases { |
| 74 | + t.Run(tc.name, func(t *testing.T) { |
| 75 | + got, err := GetFieldRef(pod, &corev1.EnvVarSource{ |
| 76 | + FieldRef: &corev1.ObjectFieldSelector{FieldPath: tc.fieldPath}, |
| 77 | + }) |
| 78 | + if tc.wantErr { |
| 79 | + if err == nil { |
| 80 | + t.Fatalf("expected error") |
| 81 | + } |
| 82 | + return |
| 83 | + } |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("unexpected error: %v", err) |
| 86 | + } |
| 87 | + if got != tc.want { |
| 88 | + t.Fatalf("expected %q, got %q", tc.want, got) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestSplitMaybeSubscriptedPath(t *testing.T) { |
| 95 | + cases := []struct { |
| 96 | + input string |
| 97 | + wantPath string |
| 98 | + wantKey string |
| 99 | + wantFound bool |
| 100 | + }{ |
| 101 | + {input: "metadata.annotations['myKey']", wantPath: "metadata.annotations", wantKey: "myKey", wantFound: true}, |
| 102 | + {input: "metadata.annotations['a[b]c']", wantPath: "metadata.annotations", wantKey: "a[b]c", wantFound: true}, |
| 103 | + {input: "metadata.labels", wantPath: "metadata.labels"}, |
| 104 | + {input: "['missingPath']", wantPath: "['missingPath']"}, |
| 105 | + {input: "metadata.labels['unterminated", wantPath: "metadata.labels['unterminated"}, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tc := range cases { |
| 109 | + t.Run(tc.input, func(t *testing.T) { |
| 110 | + path, key, found := splitMaybeSubscriptedPath(tc.input) |
| 111 | + if path != tc.wantPath || key != tc.wantKey || found != tc.wantFound { |
| 112 | + t.Fatalf("expected (%q, %q, %v), got (%q, %q, %v)", |
| 113 | + tc.wantPath, tc.wantKey, tc.wantFound, path, key, found) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments