|
| 1 | +package orchestrator |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 7 | + "sigs.k8s.io/yaml" |
| 8 | +) |
| 9 | + |
| 10 | +const subaccountCR = ` |
| 11 | +apiVersion: account.btp.sap.crossplane.io/v1alpha1 |
| 12 | +kind: Subaccount |
| 13 | +metadata: |
| 14 | + annotations: |
| 15 | + crossplane.io/external-name: test-subaccount |
| 16 | + name: test-subaccount |
| 17 | +spec: |
| 18 | + deletionPolicy: Delete |
| 19 | +status: |
| 20 | + conditions: |
| 21 | + - lastTransitionTime: "2025-09-12T15:57:41Z" |
| 22 | + observedGeneration: 1 |
| 23 | + reason: ReconcileSuccess |
| 24 | + status: "True" |
| 25 | + type: Synced |
| 26 | + - lastTransitionTime: "2025-09-09T14:33:38Z" |
| 27 | + reason: Available |
| 28 | + status: "True" |
| 29 | + type: Ready |
| 30 | +` |
| 31 | + |
| 32 | +func TestNestedPrimitiveValue(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + resourceYaml string |
| 36 | + path string |
| 37 | + wantValue string |
| 38 | + wantFound bool |
| 39 | + wantError bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "top level value retrieval", |
| 43 | + resourceYaml: subaccountCR, |
| 44 | + path: "kind", |
| 45 | + wantValue: "Subaccount", |
| 46 | + wantFound: true, |
| 47 | + wantError: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "nested value retrieval with name selector", |
| 51 | + resourceYaml: subaccountCR, |
| 52 | + path: "spec.deletionPolicy", |
| 53 | + wantValue: "Delete", |
| 54 | + wantFound: true, |
| 55 | + wantError: false, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "nested value retrieval with escaped name selector", |
| 59 | + resourceYaml: subaccountCR, |
| 60 | + path: "metadata.annotations.crossplane\\.io/external-name", |
| 61 | + wantValue: "test-subaccount", |
| 62 | + wantFound: true, |
| 63 | + wantError: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "nested value retrieval with index selector", |
| 67 | + resourceYaml: subaccountCR, |
| 68 | + path: "status.conditions[1].status", |
| 69 | + wantValue: "True", |
| 70 | + wantFound: true, |
| 71 | + wantError: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "nested value retrieval with filter selector", |
| 75 | + resourceYaml: subaccountCR, |
| 76 | + path: "status.conditions[?(@.type=='Ready')].status", |
| 77 | + wantValue: "True", |
| 78 | + wantFound: true, |
| 79 | + wantError: false, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "nested value retrieval with array slice selector", |
| 83 | + resourceYaml: subaccountCR, |
| 84 | + path: "status.conditions[0:1].status", |
| 85 | + wantValue: "True", |
| 86 | + wantFound: true, |
| 87 | + wantError: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "nested value retrieval with wildcard selector; collection results are not supported", |
| 91 | + resourceYaml: subaccountCR, |
| 92 | + path: "status.conditions[*].status", |
| 93 | + wantValue: "", |
| 94 | + wantFound: true, |
| 95 | + wantError: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "non-existent value", |
| 99 | + resourceYaml: subaccountCR, |
| 100 | + path: "metadata.labels.app", |
| 101 | + wantValue: "", |
| 102 | + wantFound: false, |
| 103 | + wantError: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "nested non-string value retrieval with default print format", |
| 107 | + resourceYaml: subaccountCR, |
| 108 | + path: "status.conditions[0].observedGeneration", |
| 109 | + wantValue: "1", |
| 110 | + wantFound: true, |
| 111 | + wantError: false, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "retrieval of collection types is not supported", |
| 115 | + resourceYaml: subaccountCR, |
| 116 | + path: "status.conditions[0]", |
| 117 | + wantValue: "", |
| 118 | + wantFound: true, |
| 119 | + wantError: true, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "invalid array index returns an error", |
| 123 | + resourceYaml: subaccountCR, |
| 124 | + path: "status.conditions[abc].status", |
| 125 | + wantValue: "", |
| 126 | + wantFound: false, |
| 127 | + wantError: true, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "invalid path syntax returns an error", |
| 131 | + resourceYaml: subaccountCR, |
| 132 | + path: "$.[status.conditions[0].status]", |
| 133 | + wantValue: "", |
| 134 | + wantFound: false, |
| 135 | + wantError: true, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tt := range tests { |
| 140 | + t.Run(tt.name, func(t *testing.T) { |
| 141 | + obj := toUnstructured(t, tt.resourceYaml) |
| 142 | + value, ok, err := nestedPrimitiveValue(obj, tt.path) |
| 143 | + |
| 144 | + if (err != nil) != tt.wantError { |
| 145 | + t.Errorf("unexpected error: got %v, wantErr %v", err, tt.wantError) |
| 146 | + } |
| 147 | + if ok != tt.wantFound { |
| 148 | + t.Errorf("unexpected ok result: got %v, want %v", ok, tt.wantFound) |
| 149 | + } |
| 150 | + if value != tt.wantValue { |
| 151 | + t.Errorf("unexpected value: got %v, want %v", value, tt.wantValue) |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func toUnstructured(t *testing.T, resourceYaml string) unstructured.Unstructured { |
| 158 | + t.Helper() |
| 159 | + var object map[string]interface{} |
| 160 | + if err := yaml.Unmarshal([]byte(resourceYaml), &object); err != nil { |
| 161 | + t.Fatalf("failed to unmarshal YAML: %v", err) |
| 162 | + } |
| 163 | + return unstructured.Unstructured{Object: object} |
| 164 | +} |
0 commit comments