...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/dynamic/common_test_functions.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/dynamic

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dynamic
    16  
    17  import (
    18  	"io/ioutil"
    19  	"log"
    20  	"testing"
    21  
    22  	condition "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"
    23  
    24  	"github.com/ghodss/yaml"
    25  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    26  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  )
    29  
    30  func UnmarshalFileToCRD(t *testing.T, fileName string) *apiextensions.CustomResourceDefinition {
    31  	t.Helper()
    32  	bytes, err := ioutil.ReadFile(fileName)
    33  	if err != nil {
    34  		t.Fatalf("error reading file '%v': %v", fileName, err)
    35  	}
    36  	o := &apiextensions.CustomResourceDefinition{}
    37  	err = yaml.Unmarshal(bytes, o)
    38  	if err != nil {
    39  		t.Fatalf("error unmarshalling bytes to CRD: %v", err)
    40  	}
    41  	return o
    42  }
    43  
    44  func UnmarshalToCRD(fileName string) *apiextensions.CustomResourceDefinition {
    45  	bytes, err := ioutil.ReadFile(fileName)
    46  	if err != nil {
    47  		log.Fatalf("error reading file '%v': %v", fileName, err)
    48  	}
    49  	o := &apiextensions.CustomResourceDefinition{}
    50  	err = yaml.Unmarshal(bytes, o)
    51  	if err != nil {
    52  		log.Fatalf("error unmarshalling bytes to CRD: %v", err)
    53  	}
    54  	return o
    55  }
    56  
    57  func GetConditions(t *testing.T, kccResource *unstructured.Unstructured) []condition.Condition {
    58  	// Simple types with the fields we care about, so that we can use the libraries
    59  	type withConditions struct {
    60  		Conditions []condition.Condition `json:"conditions"`
    61  	}
    62  	type withStatusConditions struct {
    63  		Status withConditions `json:"status"`
    64  	}
    65  	var obj withStatusConditions
    66  
    67  	// Convert into the above simplifed types
    68  	if err := runtime.DefaultUnstructuredConverter.FromUnstructured(kccResource.UnstructuredContent(), &obj); err != nil {
    69  		t.Errorf("error converting to object with status.conditions: %v", err)
    70  	}
    71  
    72  	return obj.Status.Conditions
    73  }
    74  

View as plain text