/
resourceid.go
86 lines (70 loc) · 3.29 KB
/
resourceid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2022 Google LLC
//
// 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 testcontroller
import (
"strings"
"testing"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func AssertServerGeneratedResourceIDMatch(t *testing.T, reconciledResourceID string, appliedUnstruct *unstructured.Unstructured) {
t.Helper()
appliedResourceID, foundInAppliedSpec := GetResourceID(t, appliedUnstruct)
if foundInAppliedSpec && reconciledResourceID != appliedResourceID {
t.Fatalf("resourceID should never be changed if specified; "+
"resourceID before reconciliation: '%s', resourceID after "+
"reconciliation: '%s'", appliedResourceID, reconciledResourceID)
}
}
func AssertUserSpecifiedResourceIDMatch(t *testing.T, reconciledResourceID string, appliedUnstruct *unstructured.Unstructured) {
t.Helper()
appliedResourceID, foundInAppliedSpec := GetResourceID(t, appliedUnstruct)
if !foundInAppliedSpec && reconciledResourceID != appliedUnstruct.GetName() {
t.Fatalf("resourceID must be the same as the value of "+
"'metadata.name' if '%s' is not specified; 'metadata.name': "+
"'%s', resourceID after reconciliation: '%s'",
k8s.ResourceIDFieldPath, appliedUnstruct.GetName(),
reconciledResourceID)
}
if foundInAppliedSpec && reconciledResourceID != appliedResourceID {
t.Fatalf("resourceID should never be changed if specified; "+
"resourceID before reconciliation: '%s', resourceID after "+
"reconciliation: '%s'", appliedResourceID, reconciledResourceID)
}
}
// TODO(kcc-eng): Clean up all the duplicate SupportsResourceIDField functions.
func SupportsResourceIDField(rc *v1alpha1.ResourceConfig) bool {
return rc.ResourceID.TargetField != ""
}
// TODO(kcc-eng): Clean up all the duplicate IsResourceIDFieldServerGenerated functions.
func IsResourceIDFieldServerGenerated(rc *v1alpha1.ResourceConfig) bool {
return rc.ResourceID.TargetField == rc.ServerGeneratedIDField &&
rc.ServerGeneratedIDField != ""
}
func GetResourceID(t *testing.T, resourceUnstruct *unstructured.Unstructured) (string, bool) {
resourceID, found, err := unstructured.NestedString(resourceUnstruct.Object, "spec", k8s.ResourceIDFieldName)
if err != nil {
t.Fatalf("error getting '%s': %v", k8s.ResourceIDFieldPath, err)
}
return resourceID, found
}
func SetResourceID(t *testing.T, u *unstructured.Unstructured, val string) {
if err := unstructured.SetNestedField(u.Object, val, strings.Split(k8s.ResourceIDFieldPath, ".")...); err != nil {
t.Fatalf("error setting '%s' on unstruct: %v", k8s.ResourceIDFieldPath, err)
}
}
func RemoveResourceID(u *unstructured.Unstructured) {
unstructured.RemoveNestedField(u.Object, strings.Split(k8s.ResourceIDFieldPath, ".")...)
}