Skip to content

Commit

Permalink
chore: squash json and yaml printer tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yashvardhan Kukreja <yash.kukreja.98@gmail.com>
  • Loading branch information
yashvardhan-kukreja committed May 1, 2024
1 parent 90f456a commit 7138fc5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 304 deletions.
2 changes: 1 addition & 1 deletion gwctl/pkg/printer/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"

Expand All @@ -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.
Expand Down
70 changes: 12 additions & 58 deletions gwctl/pkg/printer/gatewayclasses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
51 changes: 44 additions & 7 deletions gwctl/pkg/printer/gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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)
}
}

Expand Down
92 changes: 12 additions & 80 deletions gwctl/pkg/printer/httproutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
Loading

0 comments on commit 7138fc5

Please sign in to comment.