Skip to content

Commit

Permalink
chore: add 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 Apr 9, 2024
1 parent e5b4fe9 commit e18bf13
Show file tree
Hide file tree
Showing 6 changed files with 1,105 additions and 1 deletion.
20 changes: 20 additions & 0 deletions gwctl/pkg/common/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package common

import (
"encoding/json"
"fmt"
"strings"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -51,3 +53,21 @@ var YamlStringTransformer = cmp.Transformer("YamlLines", func(s YamlString) []st
}
return lines[start : end+1]
})

type JsonString string

func (src JsonString) CmpDiff(tgt JsonString) (diff string, err error) {
var srcMap, targetMap map[string]interface{}
err = json.Unmarshal([]byte(src), &srcMap)
if err != nil {
err = fmt.Errorf("failed to unmarshal the source json: %w", err)
return
}
err = json.Unmarshal([]byte(tgt), &targetMap)
if err != nil {
err = fmt.Errorf("failed to unmarshal the target json: %w", err)
return
}

return cmp.Diff(srcMap, targetMap), nil
}
166 changes: 166 additions & 0 deletions gwctl/pkg/printer/gatewayclasses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package printer

import (
"bytes"
"fmt"
"testing"
"time"

Expand All @@ -29,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
testingclock "k8s.io/utils/clock/testing"
apisv1alpha2 "sigs.k8s.io/gateway-api/apis/applyconfiguration/apis/v1alpha2"

gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
Expand Down Expand Up @@ -296,3 +298,167 @@ foo-com-internal-gateway-class foo-com-internal-gateway-class/controller True
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
}
}

// TestGatewayClassesPrinter_PrintJson tests the -o json output of the `get` subcommand
func TestGatewayClassesPrinter_PrintJson(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 marshalling/unmarshalling its JSON

gtwName := "foo-com-internal-gateway-class"
gtwApplyConfig := apisv1alpha2.GatewayClass(gtwName)

gtwObject := &gatewayv1.GatewayClass{
TypeMeta: metav1.TypeMeta{
APIVersion: *gtwApplyConfig.APIVersion,
Kind: *gtwApplyConfig.Kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo-com-internal-gateway-class",
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,
},
},
},
}
gtwObject.APIVersion = *gtwApplyConfig.APIVersion
gtwObject.Kind = *gtwApplyConfig.Kind

params := utils.MustParamsForTest(t, common.MustClientsForTest(t, gtwObject))
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)
}

gcp := &GatewayClassesPrinter{
Out: params.Out,
Clock: fakeClock,
}
gcp.PrintJson(resourceModel)

got := common.JsonString(params.Out.(*bytes.Buffer).String())
want := common.JsonString(fmt.Sprintf(`
{
"kind": "GatewayClass",
"apiVersion": "gateway.networking.k8s.io/v1alpha2",
"metadata": {
"name": "foo-com-internal-gateway-class",
"resourceVersion": "999",
"creationTimestamp": "%s",
"labels": {
"app": "foo",
"env": "internal"
}
},
"spec": {
"controllerName": "foo-com-internal-gateway-class/controller"
},
"status": {
"conditions": [
{
"type": "Accepted",
"status": "True",
"lastTransitionTime": null,
"reason": "",
"message": ""
}
]
}
}`, creationTime.Format(time.RFC3339)))
diff, err := want.CmpDiff(got)
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 marshalling/unmarshalling its JSON

gtwName := "foo-com-internal-gateway-class"
gtwApplyConfig := apisv1alpha2.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)
}

gcp := &GatewayClassesPrinter{
Out: params.Out,
Clock: fakeClock,
}

gcp.PrintYaml(resourceModel)

got := params.Out.(*bytes.Buffer).String()
want := fmt.Sprintf(`
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GatewayClass
metadata:
creationTimestamp: "%s"
labels:
app: foo
env: internal
name: foo-com-internal-gateway-class
resourceVersion: "999"
spec:
controllerName: foo-com-internal-gateway-class/controller
status:
conditions:
- lastTransitionTime: null
message: ""
reason: ""
status: "True"
type: Accepted
`, creationTime.Format(time.RFC3339))
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
t.Errorf("Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
}
}
Loading

0 comments on commit e18bf13

Please sign in to comment.