Skip to content

Commit

Permalink
fixed test to actually run against v2 resources, found issue where lo…
Browse files Browse the repository at this point in the history
…op was failing out early
  • Loading branch information
sarahalsmiller committed Dec 7, 2023
1 parent 587fcd3 commit d03de67
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 33 deletions.
59 changes: 34 additions & 25 deletions control-plane/subcommand/gateway-cleanup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,42 @@ func (c *Command) Run(args []string) int {
// do the cleanup

//V1 Cleanup
err = c.deleteV1GatewayClassAndGatewayClasConfig()
if err != nil {
c.UI.Error(err.Error())
return 1
}

//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
}

return 0
}

func (c *Command) deleteV1GatewayClassAndGatewayClasConfig() error {
// find the class config and mark it for deletion first so that we
// can do an early return if the gateway class isn't found
config := &v1alpha1.GatewayClassConfig{}
err = c.k8sClient.Get(context.Background(), types.NamespacedName{Name: c.flagGatewayClassConfigName}, config)
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
return nil
}
c.UI.Error(err.Error())
return 1
return err
}

// ignore any returned errors
Expand All @@ -157,10 +180,10 @@ func (c *Command) Run(args []string) int {
if err != nil {
if k8serrors.IsNotFound(err) {
// no gateway class, just ignore and return
return 0
return nil
}
c.UI.Error(err.Error())
return 1
return err
}

// ignore any returned errors
Expand All @@ -184,22 +207,7 @@ func (c *Command) Run(args []string) int {
// if we failed, return 0 anyway after logging the error
// since we don't want to block someone from uninstallation
}

//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
}

return 0
return nil
}

func (c *Command) validateFlags() error {
Expand Down Expand Up @@ -245,6 +253,7 @@ func (c *Command) loadGatewayConfigs() error {
file, err := os.Open(c.flagGatewayConfigLocation)
if err != nil {
if os.IsNotExist(err) {
panic(err)
c.UI.Warn(fmt.Sprintf("gateway configuration file not found, skipping gateway configuration, filename: %s", c.flagGatewayConfigLocation))
return nil
}
Expand Down Expand Up @@ -279,8 +288,8 @@ func (c *Command) deleteV2GatewayClassAndClassConfigs() error {
err := c.k8sClient.Get(context.Background(), types.NamespacedName{Name: gcc.Name, Namespace: gcc.Namespace}, config)
if err != nil {
if k8serrors.IsNotFound(err) {
// no gateway class config, just ignore and return
return nil
// no gateway class config, just ignore and continue
continue
}
return err
}
Expand All @@ -294,8 +303,8 @@ func (c *Command) deleteV2GatewayClassAndClassConfigs() error {
err = c.k8sClient.Get(context.Background(), types.NamespacedName{Name: gcc.Name, Namespace: gcc.Namespace}, gatewayClass)
if err != nil {
if k8serrors.IsNotFound(err) {
// no gateway class, just ignore and return
return nil
// no gateway class, just ignore and continue
continue
}
return err
}
Expand Down
67 changes: 59 additions & 8 deletions control-plane/subcommand/gateway-cleanup/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,62 @@ func TestRunV2Resources(t *testing.T) {
limits:
cpu: 200m
memory: 200Mi
`,
},
"multiple v2 resources exists": {
gatewayClassConfig: []*v2beta1.GatewayClassConfig{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway2",
},
},
},
gatewayClass: []*v2beta1.GatewayClass{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gateway2",
},
},
},
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
- apiVersion: mesh.consul.hashicorp.com/v2beta1
kind: GatewayClassConfig
metadata:
name: test-gateway2
spec:
deployment:
container:
resources:
requests:
cpu: 200m
memory: 200Mi
limits:
cpu: 200m
memory: 200Mi
`,
},
"v2 emptyconfigmap": {
Expand All @@ -151,13 +207,7 @@ func TestRunV2Resources(t *testing.T) {
objs = append(objs, gatewayClassConfig)
}

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

objs = append(objs, configMap)
path := createGatewayConfigFile(t, tt.configMapData, "config.yaml")

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

Expand All @@ -167,11 +217,13 @@ func TestRunV2Resources(t *testing.T) {
k8sClient: client,
flagGatewayClassName: "gateway-class",
flagGatewayClassConfigName: "gateway-class-config",
flagGatewayConfigLocation: path,
}

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

require.Equal(t, 0, code)
Expand All @@ -194,6 +246,5 @@ func createGatewayConfigFile(t *testing.T, fileContent, filename string) string
if err != nil {
t.Fatal(err)
}

return file.Name()
}

0 comments on commit d03de67

Please sign in to comment.