From 7138fc5d6fd6405180ffa5d5d43ce9052fac4026 Mon Sep 17 00:00:00 2001 From: Yashvardhan Kukreja Date: Wed, 1 May 2024 12:56:04 +0530 Subject: [PATCH] chore: squash json and yaml printer tests Signed-off-by: Yashvardhan Kukreja --- gwctl/pkg/printer/common.go | 2 +- gwctl/pkg/printer/gatewayclasses_test.go | 70 +++----------- gwctl/pkg/printer/gateways_test.go | 51 ++++++++-- gwctl/pkg/printer/httproutes_test.go | 92 +++--------------- gwctl/pkg/printer/namespace_test.go | 63 +++--------- gwctl/pkg/printer/policies_test.go | 118 +++-------------------- 6 files changed, 92 insertions(+), 304 deletions(-) diff --git a/gwctl/pkg/printer/common.go b/gwctl/pkg/printer/common.go index bc8e8c9302..6fec2f199e 100644 --- a/gwctl/pkg/printer/common.go +++ b/gwctl/pkg/printer/common.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "os" + "sort" "strings" "text/tabwriter" @@ -28,7 +29,6 @@ import ( "k8s.io/utils/clock" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" - "sort" ) // DescriberKV stores key-value pairs that are used with Describing a resource. diff --git a/gwctl/pkg/printer/gatewayclasses_test.go b/gwctl/pkg/printer/gatewayclasses_test.go index ca1829beea..65f7b5cab3 100644 --- a/gwctl/pkg/printer/gatewayclasses_test.go +++ b/gwctl/pkg/printer/gatewayclasses_test.go @@ -38,7 +38,7 @@ import ( "sigs.k8s.io/gateway-api/gwctl/pkg/utils" ) -func TestGatewayClassesPrinter_Print(t *testing.T) { +func TestGatewayClassesPrinter_PrintTable(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) objects := []runtime.Object{ &gatewayv1.GatewayClass{ @@ -253,8 +253,8 @@ Status: {} } } -// TestGatewayClassesPrinter_PrintJson tests the -o json output of the `get` subcommand -func TestGatewayClassesPrinter_PrintJson(t *testing.T) { +// TestGatewayClassesPrinter_PrintJsonYaml tests the -o json/yaml output of the `get` subcommand +func TestGatewayClassesPrinter_PrintJsonYaml(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) creationTime := fakeClock.Now().Add(-365 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON @@ -304,8 +304,8 @@ func TestGatewayClassesPrinter_PrintJson(t *testing.T) { } Print(gcp, resourceModel, utils.OutputFormatJSON) - got := common.JSONString(params.Out.(*bytes.Buffer).String()) - want := common.JSONString(fmt.Sprintf(` + gotJson := common.JSONString(params.Out.(*bytes.Buffer).String()) + wantJson := common.JSONString(fmt.Sprintf(` { "apiVersion": "v1", "items": [ @@ -339,66 +339,20 @@ func TestGatewayClassesPrinter_PrintJson(t *testing.T) { ], "kind": "List" }`, creationTime.Format(time.RFC3339))) - diff, err := want.CmpDiff(got) + diff, err := wantJson.CmpDiff(gotJson) if err != nil { t.Fatalf("Failed to compare the json diffs: %v", diff) } if diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) - } -} - -// TestGatewayClassesPrinter_PrintYaml tests the -o yaml output of the `get` subcommand -func TestGatewayClassesPrinter_PrintYaml(t *testing.T) { - fakeClock := testingclock.NewFakeClock(time.Now()) - creationTime := fakeClock.Now().Add(-365 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON - - gtwName := "foo-com-internal-gateway-class" - gtwApplyConfig := apisv1beta1.GatewayClass(gtwName) - - object := &gatewayv1.GatewayClass{ - TypeMeta: metav1.TypeMeta{ - APIVersion: *gtwApplyConfig.APIVersion, - Kind: *gtwApplyConfig.Kind, - }, - ObjectMeta: metav1.ObjectMeta{ - Name: gtwName, - Labels: map[string]string{"app": "foo", "env": "internal"}, - CreationTimestamp: metav1.Time{ - Time: creationTime, - }, - }, - Spec: gatewayv1.GatewayClassSpec{ - ControllerName: gatewayv1.GatewayController(gtwName + "/controller"), - }, - Status: gatewayv1.GatewayClassStatus{ - Conditions: []metav1.Condition{ - { - Type: "Accepted", - Status: metav1.ConditionTrue, - }, - }, - }, - } - params := utils.MustParamsForTest(t, common.MustClientsForTest(t, object)) - discoverer := resourcediscovery.Discoverer{ - K8sClients: params.K8sClients, - PolicyManager: params.PolicyManager, - } - resourceModel, err := discoverer.DiscoverResourcesForGatewayClass(resourcediscovery.Filter{}) - if err != nil { - t.Fatalf("Failed to construct resourceModel: %v", resourceModel) + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotJson, wantJson, diff) } - gcp := &GatewayClassesPrinter{ - Writer: params.Out, - Clock: fakeClock, - } + gcp.Writer = &bytes.Buffer{} Print(gcp, resourceModel, utils.OutputFormatYAML) - got := common.YamlString(params.Out.(*bytes.Buffer).String()) - want := common.YamlString(fmt.Sprintf(` + gotYaml := common.YamlString(gcp.Writer.(*bytes.Buffer).String()) + wantYaml := common.YamlString(fmt.Sprintf(` apiVersion: v1 items: - apiVersion: gateway.networking.k8s.io/v1beta1 @@ -420,7 +374,7 @@ items: status: "True" type: Accepted kind: List`, creationTime.Format(time.RFC3339))) - if diff := cmp.Diff(want, got, common.YamlStringTransformer); diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) + if diff := cmp.Diff(wantYaml, gotYaml, common.YamlStringTransformer); diff != "" { + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotYaml, wantYaml, diff) } } diff --git a/gwctl/pkg/printer/gateways_test.go b/gwctl/pkg/printer/gateways_test.go index 6d8a8126cc..c31a61045b 100644 --- a/gwctl/pkg/printer/gateways_test.go +++ b/gwctl/pkg/printer/gateways_test.go @@ -39,7 +39,7 @@ import ( "sigs.k8s.io/gateway-api/gwctl/pkg/utils" ) -func TestGatewaysPrinter_Print(t *testing.T) { +func TestGatewaysPrinter_PrintTable(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) objects := []runtime.Object{ &gatewayv1.GatewayClass{ @@ -425,8 +425,8 @@ Events: } } -// TestGatewaysPrinter_PrintJson tests the -o json output of the `get` subcommand -func TestGatewaysPrinter_PrintJson(t *testing.T) { +// TestGatewaysPrinter_PrintJsonYaml tests the -o json/yaml output of the `get` subcommand +func TestGatewaysPrinter_PrintJsonYaml(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) creationTime := fakeClock.Now().Add(-5 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON gcName := "gateway-1" @@ -498,8 +498,8 @@ func TestGatewaysPrinter_PrintJson(t *testing.T) { } Print(gp, resourceModel, utils.OutputFormatJSON) - got := common.JSONString(params.Out.(*bytes.Buffer).String()) - want := common.JSONString(fmt.Sprintf(` + gotJson := common.JSONString(params.Out.(*bytes.Buffer).String()) + wantJson := common.JSONString(fmt.Sprintf(` { "apiVersion": "v1", "items": [ @@ -545,12 +545,49 @@ func TestGatewaysPrinter_PrintJson(t *testing.T) { ], "kind": "List" }`, creationTime.Format(time.RFC3339))) - diff, err := want.CmpDiff(got) + diff, err := wantJson.CmpDiff(gotJson) if err != nil { t.Fatalf("Failed to compare the json diffs: %v", diff) } if diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotJson, wantJson, diff) + } + + gp.Writer = &bytes.Buffer{} + + Print(gp, resourceModel, utils.OutputFormatYAML) + + gotYaml := common.YamlString(gp.Writer.(*bytes.Buffer).String()) + wantYaml := common.YamlString(fmt.Sprintf(` +apiVersion: v1 +items: +- apiVersion: gateway.networking.k8s.io/v1beta1 + kind: Gateway + metadata: + creationTimestamp: "%s" + labels: + app: foo + env: internal + name: gateway-1 + resourceVersion: "999" + spec: + gatewayClassName: gatewayclass-1 + listeners: + - name: http-8080 + port: 8080 + protocol: HTTP + status: + addresses: + - value: 192.168.100.5 + conditions: + - lastTransitionTime: null + message: "" + reason: "" + status: "False" + type: Programmed +kind: List`, creationTime.Format(time.RFC3339))) + if diff := cmp.Diff(wantYaml, gotYaml, common.YamlStringTransformer); diff != "" { + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotYaml, wantYaml, diff) } } diff --git a/gwctl/pkg/printer/httproutes_test.go b/gwctl/pkg/printer/httproutes_test.go index 15575557f8..07299e9a52 100644 --- a/gwctl/pkg/printer/httproutes_test.go +++ b/gwctl/pkg/printer/httproutes_test.go @@ -39,7 +39,7 @@ import ( "sigs.k8s.io/gateway-api/gwctl/pkg/utils" ) -func TestHTTPRoutesPrinter_Print(t *testing.T) { +func TestHTTPRoutesPrinter_PrintTable(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) objects := []runtime.Object{ &gatewayv1.GatewayClass{ @@ -438,8 +438,8 @@ EffectivePolicies: } } -// TestHTTPRoutesPrinter_PrintJson tests the correctness of JSON output associated with -o json of `get` subcommand -func TestHTTPRoutesPrinter_PrintJson(t *testing.T) { +// TestHTTPRoutesPrinter_PrintJsonYaml tests the correctness of JSON/YAML output associated with -o json/yaml of `get` subcommand +func TestHTTPRoutesPrinter_PrintJsonYaml(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) creationTime := fakeClock.Now().Add(-24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON @@ -509,8 +509,8 @@ func TestHTTPRoutesPrinter_PrintJson(t *testing.T) { } Print(hp, resourceModel, utils.OutputFormatJSON) - got := common.JSONString(params.Out.(*bytes.Buffer).String()) - want := common.JSONString(fmt.Sprintf(` + gotJson := common.JSONString(params.Out.(*bytes.Buffer).String()) + wantJson := common.JSONString(fmt.Sprintf(` { "apiVersion": "v1", "items": [ @@ -544,88 +544,20 @@ func TestHTTPRoutesPrinter_PrintJson(t *testing.T) { ], "kind": "List" }`, creationTime.Format(time.RFC3339))) - diff, err := want.CmpDiff(got) + diff, err := wantJson.CmpDiff(gotJson) if err != nil { t.Fatalf("Failed to compare the json diffs: %v", diff) } if diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotJson, wantJson, diff) } -} - -// TestHTTPRoutesPrinter_PrintYaml tests the correctness of YAML output associated with -o yaml of `get` subcommand -func TestHTTPRoutesPrinter_PrintYaml(t *testing.T) { - fakeClock := testingclock.NewFakeClock(time.Now()) - creationTime := fakeClock.Now().Add(-24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON - hrName, hrNamespace := "httproute-1", "default" - hrConfig := apisv1beta1.HTTPRoute(hrName, hrNamespace) - - objects := []runtime.Object{ - &gatewayv1.GatewayClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: "gatewayclass-1", - }, - Spec: gatewayv1.GatewayClassSpec{ - ControllerName: "example.net/gateway-controller", - Description: common.PtrTo("random"), - }, - }, + hp.Writer = &bytes.Buffer{} - &gatewayv1.Gateway{ - ObjectMeta: metav1.ObjectMeta{ - Name: "gateway-1", - Namespace: "default", - }, - Spec: gatewayv1.GatewaySpec{ - GatewayClassName: "gatewayclass-1", - }, - }, - &gatewayv1.HTTPRoute{ - TypeMeta: metav1.TypeMeta{ - APIVersion: *hrConfig.APIVersion, - Kind: *hrConfig.Kind, - }, - ObjectMeta: metav1.ObjectMeta{ - Name: hrName, - Namespace: hrNamespace, - CreationTimestamp: metav1.Time{ - Time: creationTime, - }, - Labels: map[string]string{"app": "foo", "env": "internal"}, - }, - Spec: gatewayv1.HTTPRouteSpec{ - Hostnames: []gatewayv1.Hostname{"example.com"}, - CommonRouteSpec: gatewayv1.CommonRouteSpec{ - ParentRefs: []gatewayv1.ParentReference{ - { - Name: "gateway-1", - }, - }, - }, - }, - }, - } - - params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...)) - discoverer := resourcediscovery.Discoverer{ - K8sClients: params.K8sClients, - PolicyManager: params.PolicyManager, - } - - resourceModel, err := discoverer.DiscoverResourcesForHTTPRoute(resourcediscovery.Filter{}) - if err != nil { - t.Fatalf("Failed to discover resources: %v", err) - } - - hp := &HTTPRoutesPrinter{ - Writer: params.Out, - Clock: fakeClock, - } Print(hp, resourceModel, utils.OutputFormatYAML) - got := common.YamlString(params.Out.(*bytes.Buffer).String()) - want := common.YamlString(fmt.Sprintf(` + gotYaml := common.YamlString(hp.Writer.(*bytes.Buffer).String()) + wantYaml := common.YamlString(fmt.Sprintf(` apiVersion: v1 items: - apiVersion: gateway.networking.k8s.io/v1beta1 @@ -646,7 +578,7 @@ items: status: parents: null kind: List`, creationTime.Format(time.RFC3339))) - if diff := cmp.Diff(want, got, common.YamlStringTransformer); diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) + if diff := cmp.Diff(wantYaml, gotYaml, common.YamlStringTransformer); diff != "" { + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotYaml, wantYaml, diff) } } diff --git a/gwctl/pkg/printer/namespace_test.go b/gwctl/pkg/printer/namespace_test.go index e78a4f49c9..571789c1c1 100644 --- a/gwctl/pkg/printer/namespace_test.go +++ b/gwctl/pkg/printer/namespace_test.go @@ -37,7 +37,7 @@ import ( "sigs.k8s.io/gateway-api/gwctl/pkg/utils" ) -func TestNamespacePrinter_Print(t *testing.T) { +func TestNamespacePrinter_PrintTable(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) objects := []runtime.Object{ &corev1.Namespace{ @@ -259,8 +259,8 @@ DirectlyAttachedPolicies: } } -// TestNamespacesPrinter_PrintJson tests the correctness of JSON output associated with -o json of `get` subcommand -func TestNamespacesPrinter_PrintJson(t *testing.T) { +// TestNamespacesPrinter_PrintJsonYaml tests the correctness of JSON/YAML output associated with -o json/yaml of `get` subcommand +func TestNamespacesPrinter_PrintJsonYaml(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) creationTime := fakeClock.Now().Add(-46 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON @@ -300,8 +300,8 @@ func TestNamespacesPrinter_PrintJson(t *testing.T) { } Print(nsp, resourceModel, utils.OutputFormatJSON) - got := common.JSONString(params.Out.(*bytes.Buffer).String()) - want := common.JSONString(fmt.Sprintf(` + gotJson := common.JSONString(params.Out.(*bytes.Buffer).String()) + wantJson := common.JSONString(fmt.Sprintf(` { "apiVersion": "v1", "items": [ @@ -329,58 +329,19 @@ func TestNamespacesPrinter_PrintJson(t *testing.T) { ], "kind": "List" }`, creationTime.Format(time.RFC3339))) - diff, err := want.CmpDiff(got) + diff, err := wantJson.CmpDiff(gotJson) if err != nil { t.Fatalf("Failed to compare the json diffs: %v", diff) } if diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) - } -} - -// TestNamespacesPrinter_PrintYaml tests the correctness of YAML output associated with -o yaml of `get` subcommand -func TestNamespacesPrinter_PrintYaml(t *testing.T) { - fakeClock := testingclock.NewFakeClock(time.Now()) - creationTime := fakeClock.Now().Add(-46 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON - - nsObject := &corev1.Namespace{ - TypeMeta: metav1.TypeMeta{ - Kind: "v1", - APIVersion: "Namespace", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "namespace-1", - CreationTimestamp: metav1.Time{ - Time: creationTime, - }, - Labels: map[string]string{"app": "foo", "env": "internal"}, - }, - Spec: corev1.NamespaceSpec{ - Finalizers: []corev1.FinalizerName{"kubernetes"}, - }, - Status: corev1.NamespaceStatus{ - Phase: corev1.NamespaceActive, - }, - } - - params := utils.MustParamsForTest(t, common.MustClientsForTest(t, nsObject)) - discoverer := resourcediscovery.Discoverer{ - K8sClients: params.K8sClients, - PolicyManager: params.PolicyManager, - } - resourceModel, err := discoverer.DiscoverResourcesForNamespace(resourcediscovery.Filter{}) - if err != nil { - t.Fatalf("Failed to construct resourceModel: %v", resourceModel) + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotJson, wantJson, diff) } - nsp := &NamespacesPrinter{ - Writer: params.Out, - Clock: fakeClock, - } + nsp.Writer = &bytes.Buffer{} Print(nsp, resourceModel, utils.OutputFormatYAML) - got := common.YamlString(params.Out.(*bytes.Buffer).String()) - want := common.YamlString(fmt.Sprintf(` + gotYaml := common.YamlString(nsp.Writer.(*bytes.Buffer).String()) + wantYaml := common.YamlString(fmt.Sprintf(` apiVersion: v1 items: - apiVersion: Namespace @@ -399,7 +360,7 @@ items: phase: Active kind: List `, creationTime.Format(time.RFC3339))) - if diff := cmp.Diff(want, got, common.YamlStringTransformer); diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) + if diff := cmp.Diff(wantYaml, gotYaml, common.YamlStringTransformer); diff != "" { + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotYaml, wantYaml, diff) } } diff --git a/gwctl/pkg/printer/policies_test.go b/gwctl/pkg/printer/policies_test.go index 255801b793..7279323a51 100644 --- a/gwctl/pkg/printer/policies_test.go +++ b/gwctl/pkg/printer/policies_test.go @@ -356,8 +356,8 @@ timeoutpolicies.bar.com Direct Namespaced 5m } } -// TestPoliciesPrinter_PrintCRDs_Json tests the correctness of JSON output associated with -o json of `get` subcommand -func TestPoliciesPrinter_PrintCRDs_Json(t *testing.T) { +// TestPoliciesPrinter_PrintCRDs_JsonYaml tests the correctness of JSON/YAML output associated with -o json/yaml of `get` subcommand +func TestPoliciesPrinter_PrintCRDs_JsonYaml(t *testing.T) { fakeClock := testingclock.NewFakeClock(time.Now()) creationTime1 := fakeClock.Now().Add(-24 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON creationTime2 := fakeClock.Now().Add(-5 * time.Minute).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON @@ -454,8 +454,8 @@ func TestPoliciesPrinter_PrintCRDs_Json(t *testing.T) { } pp.PrintCRDs(params.PolicyManager.GetCRDs(), utils.OutputFormatJSON) - got := common.JSONString(pp.Writer.(*bytes.Buffer).String()) - want := common.JSONString(fmt.Sprintf(`{ + gotJson := common.JSONString(pp.Writer.(*bytes.Buffer).String()) + wantJson := common.JSONString(fmt.Sprintf(`{ "apiVersion": "v1", "items": [ { @@ -531,115 +531,19 @@ func TestPoliciesPrinter_PrintCRDs_Json(t *testing.T) { ], "kind": "List" }`, creationTime1.Format(time.RFC3339), creationTime2.Format(time.RFC3339))) - diff, err := want.CmpDiff(got) + diff, err := wantJson.CmpDiff(gotJson) if err != nil { t.Fatalf("Failed to compare the json diffs: %v", diff) } if diff != "" { - t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) - } -} - -// TestPoliciesPrinter_PrintCRDs_Yaml tests the correctness of YAML output associated with -o yaml of `get` subcommand -func TestPoliciesPrinter_PrintCRDs_Yaml(t *testing.T) { - fakeClock := testingclock.NewFakeClock(time.Now()) - creationTime1 := fakeClock.Now().Add(-24 * 24 * time.Hour).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON - creationTime2 := fakeClock.Now().Add(-5 * time.Minute).UTC() // UTC being necessary for consistently handling the time while marshaling/unmarshaling its JSON - - objects := []runtime.Object{ - &apiextensionsv1.CustomResourceDefinition{ - ObjectMeta: metav1.ObjectMeta{ - Name: "healthcheckpolicies.foo.com", - Labels: map[string]string{ - gatewayv1alpha2.PolicyLabelKey: "inherited", - }, - CreationTimestamp: metav1.Time{ - Time: creationTime1, - }, - }, - Spec: apiextensionsv1.CustomResourceDefinitionSpec{ - Scope: apiextensionsv1.ClusterScoped, - Group: "foo.com", - Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}}, - Names: apiextensionsv1.CustomResourceDefinitionNames{ - Plural: "healthcheckpolicies", - Kind: "HealthCheckPolicy", - }, - }, - }, - &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "foo.com/v1", - "kind": "HealthCheckPolicy", - "metadata": map[string]interface{}{ - "name": "health-check-gateway", - }, - "spec": map[string]interface{}{ - "override": map[string]interface{}{ - "key1": "value-child-1", - }, - "default": map[string]interface{}{ - "key2": "value-child-2", - "key5": "value-child-5", - }, - "targetRef": map[string]interface{}{ - "group": "gateway.networking.k8s.io", - "kind": "Gateway", - "name": "foo-gateway", - "namespace": "default", - }, - }, - }, - }, - - &apiextensionsv1.CustomResourceDefinition{ - ObjectMeta: metav1.ObjectMeta{ - Name: "timeoutpolicies.bar.com", - Labels: map[string]string{ - gatewayv1alpha2.PolicyLabelKey: "direct", - }, - CreationTimestamp: metav1.Time{ - Time: creationTime2, - }, - }, - Spec: apiextensionsv1.CustomResourceDefinitionSpec{ - Scope: apiextensionsv1.NamespaceScoped, - Group: "bar.com", - Versions: []apiextensionsv1.CustomResourceDefinitionVersion{{Name: "v1"}}, - Names: apiextensionsv1.CustomResourceDefinitionNames{ - Plural: "timeoutpolicies", - Kind: "TimeoutPolicy", - }, - }, - }, - &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "bar.com/v1", - "kind": "TimeoutPolicy", - "metadata": map[string]interface{}{ - "name": "timeout-policy-namespace", - }, - "spec": map[string]interface{}{ - "condition": "path=/abc", - "seconds": int64(30), - "targetRef": map[string]interface{}{ - "kind": "Namespace", - "name": "default", - }, - }, - }, - }, + t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotJson, wantJson, diff) } - params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...)) - pp := &PoliciesPrinter{ - Writer: &bytes.Buffer{}, - Clock: fakeClock, - } + pp.Writer = &bytes.Buffer{} pp.PrintCRDs(params.PolicyManager.GetCRDs(), utils.OutputFormatYAML) - got := common.YamlString(pp.Writer.(*bytes.Buffer).String()) - want := common.YamlString(fmt.Sprintf(` + gotYaml := common.YamlString(pp.Writer.(*bytes.Buffer).String()) + wantYaml := common.YamlString(fmt.Sprintf(` apiVersion: v1 items: - apiVersion: apiextensions.k8s.io/v1 @@ -691,8 +595,8 @@ items: conditions: null storedVersions: null kind: List`, creationTime1.Format(time.RFC3339), creationTime2.Format(time.RFC3339))) - if diff := cmp.Diff(want, got, common.YamlStringTransformer); diff != "" { - t.Errorf("PrintDescribeView: Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff) + if diff := cmp.Diff(wantYaml, gotYaml, common.YamlStringTransformer); diff != "" { + t.Errorf("PrintDescribeView: Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", gotYaml, wantYaml, diff) } }