Skip to content

Commit

Permalink
add tests for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahalsmiller committed Dec 6, 2023
1 parent f6c1de9 commit 0f65722
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions control-plane/subcommand/gateway-cleanup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (c *Command) Run(args []string) int {
config := &v1alpha1.GatewayClassConfig{}
err = c.k8sClient.Get(context.Background(), types.NamespacedName{Name: c.flagGatewayClassConfigName}, config)
if err != nil {

if k8serrors.IsNotFound(err) {
// no gateway class config, just ignore and return
return 0
Expand All @@ -150,6 +151,7 @@ func (c *Command) Run(args []string) int {
_ = c.k8sClient.Delete(context.Background(), config)

// find the gateway class

gatewayClass := &gwv1beta1.GatewayClass{}
err = c.k8sClient.Get(context.Background(), types.NamespacedName{Name: c.flagGatewayClassName}, gatewayClass)
if err != nil {
Expand Down Expand Up @@ -186,12 +188,14 @@ func (c *Command) Run(args []string) int {
//V2 Cleanup
err = c.loadGatewayConfigs()
if err != nil {

c.UI.Error(err.Error())
return 1
}
err = c.deleteV2GatewayClassAndClassConfigs()
if err != nil {
c.UI.Error(err.Error())

return 1
}

Expand Down
115 changes: 115 additions & 0 deletions control-plane/subcommand/gateway-cleanup/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package gatewaycleanup

import (
"github.com/hashicorp/consul-k8s/control-plane/api/mesh/v2beta1"
corev1 "k8s.io/api/core/v1"
"os"
"testing"

"github.com/mitchellh/cli"
Expand Down Expand Up @@ -52,6 +55,7 @@ func TestRun(t *testing.T) {
s := runtime.NewScheme()
require.NoError(t, gwv1beta1.Install(s))
require.NoError(t, v1alpha1.AddToScheme(s))
require.NoError(t, v2beta1.AddMeshToScheme(s))

objs := []client.Object{}
if tt.gatewayClass != nil {
Expand Down Expand Up @@ -82,3 +86,114 @@ func TestRun(t *testing.T) {
})
}
}

func TestRunV2Resources(t *testing.T) {
t.Parallel()

for name, tt := range map[string]struct {
gatewayClassConfig []*v2beta1.GatewayClassConfig
gatewayClass []*v2beta1.GatewayClass
configMapData string
}{

"v2 resources exists": {
gatewayClassConfig: []*v2beta1.GatewayClassConfig{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway",
},
},
},
gatewayClass: []*v2beta1.GatewayClass{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway",
},
},
},
configMapData: `gatewayClassConfigs:
- apiVersion: mesh.consul.hashicorp.com/v2beta1
kind: GatewayClassConfig
metadata:
name: test-gateway
spec:
deployment:
container:
resources:
requests:
cpu: 200m
memory: 200Mi
limits:
cpu: 200m
memory: 200Mi
`,
},
"v2 emptyconfigmap": {
configMapData: "",
},
} {
t.Run(name, func(t *testing.T) {
tt := tt

t.Parallel()

s := runtime.NewScheme()
require.NoError(t, gwv1beta1.Install(s))
require.NoError(t, v2beta1.AddMeshToScheme(s))
require.NoError(t, corev1.AddToScheme(s))
require.NoError(t, v1alpha1.AddToScheme(s))

objs := []client.Object{}
for _, gatewayClass := range tt.gatewayClass {
objs = append(objs, gatewayClass)
}
for _, gatewayClassConfig := range tt.gatewayClassConfig {
objs = append(objs, gatewayClassConfig)
}

configMap := &corev1.ConfigMap{
Data: map[string]string{
"config.yaml": tt.configMapData,
},
}

objs = append(objs, configMap)

client := fake.NewClientBuilder().WithScheme(s).WithObjects(objs...).Build()

ui := cli.NewMockUi()
cmd := Command{
UI: ui,
k8sClient: client,
flagGatewayClassName: "gateway-class",
flagGatewayClassConfigName: "gateway-class-config",
}

code := cmd.Run([]string{
"-gateway-class-config-name", "gateway-class-config",
"-gateway-class-name", "gateway-class",
})

require.Equal(t, 0, code)
})
}
}

func createGatewayConfigFile(t *testing.T, fileContent, filename string) string {
t.Helper()

// create a temp file to store configuration yaml
tmpdir := t.TempDir()
file, err := os.CreateTemp(tmpdir, filename)
if err != nil {
t.Fatal(err)
}
defer file.Close()

_, err = file.WriteString(fileContent)
if err != nil {
t.Fatal(err)
}

return file.Name()
}

0 comments on commit 0f65722

Please sign in to comment.