Skip to content

Commit

Permalink
GH action
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Mar 27, 2024
1 parent 2e9f5bb commit facf8f1
Show file tree
Hide file tree
Showing 47 changed files with 154 additions and 812 deletions.
62 changes: 62 additions & 0 deletions pkg/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package test

import (
"fmt"
"os"
"path/filepath"
"regexp"
"sigs.k8s.io/yaml"
"strings"
"testing"

Expand Down Expand Up @@ -140,3 +142,63 @@ func IgnoreLeadingComments(s string) string {
}
return strings.TrimSpace(strings.Join(out, "\n")) + "\n"
}

func CompareGoldenObject(path string, got []byte) error {
writeGoldenOutput := os.Getenv("WRITE_GOLDEN_OUTPUT") != ""
want, err := os.ReadFile(path)
if err != nil {
if writeGoldenOutput && os.IsNotExist(err) {
// Expected when creating output for the first time
if err := os.WriteFile(path, got, 0644); err != nil {
return fmt.Errorf("failed to write golden output %s: %w", path, err)
}
fmt.Printf("wrote updated golden output to %s", path)
return nil
} else {
return fmt.Errorf("failed to read golden file %q: %w", path, err)
}
}
var wantMap, gotMap map[string]interface{}
err = yaml.Unmarshal(want, &wantMap)
if err != nil {
return err
}
err = yaml.Unmarshal(got, &gotMap)
if err != nil {
return err
}
if ok, err := compareNestedFields(wantMap, gotMap); !ok {
return err
}
return nil
}

func compareNestedFields(wantMap, gotMap map[string]interface{}) (bool, error) {
for wantKey := range wantMap {
if _, exists := gotMap[wantKey]; !exists {
err := fmt.Errorf("field %s in the golden file is missing", wantKey)
return false, err
}
}

for gotKey := range gotMap {
if _, exists := wantMap[gotKey]; !exists {
err := fmt.Errorf("field %s does not exist in golden file", gotKey)
return false, err
}
}

// Check nested structures recursively
for wantKey, wantVal := range wantMap {
if gotVal, exists := gotMap[wantKey]; exists {
if wantNestedMap, ok1 := wantVal.(map[string]interface{}); ok1 {
if gotNestedMap, ok2 := gotVal.(map[string]interface{}); ok2 {
if ok, err := compareNestedFields(wantNestedMap, gotNestedMap); !ok {
return false, err
}
}
}
}
}
return true, nil
}
29 changes: 29 additions & 0 deletions scripts/github-actions/ga-object-schema-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# Copyright 2024 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.
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT="$(git rev-parse --show-toplevel)"
source ${REPO_ROOT}/scripts/shared-vars-public.sh
cd ${REPO_ROOT}
source ${REPO_ROOT}/scripts/fetch_ext_bins.sh && \
fetch_tools && \
setup_envs

cd ${REPO_ROOT}/
echo "Running mock e2e object schema tests..."
E2E_KUBE_TARGET=envtest \
RUN_E2E=1 E2E_GCP_TARGET=mock \
go test -test.count=1 -timeout 3600s -v ./tests/e2e -run TestKRMObject/fixtures 2>&1
11 changes: 11 additions & 0 deletions tests/e2e/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package e2e

import (
"k8s.io/apimachinery/pkg/types"
"path/filepath"
"strings"

Expand Down Expand Up @@ -95,3 +96,13 @@ func exportResourceAsUnstructured(h *create.Harness, obj *unstructured.Unstructu
}
return u
}

func getKRMObject(h *create.Harness, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(obj.GroupVersionKind())
id := types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}
if err := h.GetClient().Get(h.Ctx, id, u); err != nil {
return nil, err
}
return u, nil
}
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_apikeyskeybasic.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: apikeys.cnrm.cloud.google.com/v1alpha1
kind: APIKeysKey
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_billingaccountiampolicy.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPolicy
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_billingaccountiampolicymember.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPolicyMember
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: certificatemanager.cnrm.cloud.google.com/v1beta1
kind: CertificateManagerCertificate
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_certificatemanagercertificatemap.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: certificatemanager.cnrm.cloud.google.com/v1beta1
kind: CertificateManagerCertificateMap
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: certificatemanager.cnrm.cloud.google.com/v1beta1
kind: CertificateManagerCertificateMapEntry
metadata:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: certificatemanager.cnrm.cloud.google.com/v1beta1
kind: CertificateManagerCertificate
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_certificatemanagerdnsauthorization.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: certificatemanager.cnrm.cloud.google.com/v1beta1
kind: CertificateManagerDNSAuthorization
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_computediskfromsourcedisk.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeDisk
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_computenetwork.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeNetwork
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_computenodegroup.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeNodeGroup
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_computenodetemplate.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeNodeTemplate
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_computesubnetwork.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeSubnetwork
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_containerattachedcluster.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: containerattached.cnrm.cloud.google.com/v1beta1
kind: ContainerAttachedCluster
metadata:
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/golden/object/_edgecontainercluster.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2024 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.

apiVersion: edgecontainer.cnrm.cloud.google.com/v1beta1
kind: EdgeContainerCluster
metadata:
Expand Down
Loading

0 comments on commit facf8f1

Please sign in to comment.