Skip to content

Commit d154c23

Browse files
committed
fix: handle optional flags correctly in VRF route update
1 parent 36f4f5e commit d154c23

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

internal/vrf/updateroute.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ func (c *Client) UpdateRoute() *cobra.Command {
2929
inc := []string{}
3030
exc := []string{}
3131

32-
vrfRouteUpdateInput := metalv1.VrfRouteUpdateInput{
33-
Prefix: &prefix,
34-
NextHop: &nextHop,
35-
Tags: tags,
32+
vrfRouteUpdateInput := metalv1.VrfRouteUpdateInput{}
33+
34+
if prefix != "" {
35+
vrfRouteUpdateInput.Prefix = &prefix
36+
}
37+
if nextHop != "" {
38+
vrfRouteUpdateInput.NextHop = &nextHop
39+
}
40+
if cmd.Flag("tags").Changed {
41+
vrfRouteUpdateInput.Tags = tags
3642
}
3743

3844
vrfRoute, _, err := c.Service.UpdateVrfRouteById(context.Background(), vrfID).VrfRouteUpdateInput(vrfRouteUpdateInput).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute()

test/e2e/vrfstest/vrf_route_test.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/equinix/equinix-sdk-go/services/metalv1"
89
root "github.com/equinix/metal-cli/internal/cli"
910
outputPkg "github.com/equinix/metal-cli/internal/outputs"
1011
"github.com/equinix/metal-cli/internal/vrf"
@@ -113,10 +114,6 @@ func TestCli_Vrf_Route(t *testing.T) {
113114
},
114115
want: &cobra.Command{},
115116
cmdFunc: func(t *testing.T, c *cobra.Command) {
116-
// Actually user have to wait for 5 min to updae the VRF-routes. This test case is skipped intentionally
117-
if true {
118-
t.Skip("Skipping this test because someCondition is true")
119-
}
120117
root := c.Root()
121118

122119
projName := "metal-cli-" + randName + "-vrf-list-test"
@@ -128,19 +125,19 @@ func TestCli_Vrf_Route(t *testing.T) {
128125

129126
ipReservation := helper.CreateTestVrfIpRequest(t, projectId.GetId(), vrf.GetId())
130127
_ = helper.CreateTestVrfGateway(t, projectId.GetId(), ipReservation.VrfIpReservation.GetId(), vlan.GetId())
131-
_ = helper.CreateTestVrfRoute(t, vrf.GetId())
128+
route := helper.CreateTestVrfRoute(t, vrf.GetId())
132129

133-
if vlan.GetId() != "" && vrf.GetId() != "" {
134-
root.SetArgs([]string{subCommand, "update-route", "-i", vrf.GetId(), "-t", "foobar"})
130+
_ := helper.WaitForVrfRouteState(t, route.GetId(), metalv1.VRFROUTESTATUS_ACTIVE)
135131

136-
out := helper.ExecuteAndCaptureOutput(t, root)
132+
root.SetArgs([]string{subCommand, "update-route", "-i", route.GetId(), "-t", "foobar"})
137133

138-
if !strings.Contains(string(out[:]), "TYPE") &&
139-
!strings.Contains(string(out[:]), "static") &&
140-
!strings.Contains(string(out[:]), "PREFIX") &&
141-
!strings.Contains(string(out[:]), "0.0.0.0/0") {
142-
t.Error("expected output should include TYPE static PREFIX and 0.0.0.0/0, in the out string ")
143-
}
134+
out := helper.ExecuteAndCaptureOutput(t, root)
135+
136+
if !strings.Contains(string(out[:]), "TYPE") &&
137+
!strings.Contains(string(out[:]), "static") &&
138+
!strings.Contains(string(out[:]), "PREFIX") &&
139+
!strings.Contains(string(out[:]), "0.0.0.0/0") {
140+
t.Error("expected output should include TYPE static PREFIX and 0.0.0.0/0, in the out string ")
144141
}
145142
}
146143
},

test/helper/helper.go

+27
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,33 @@ func WaitForDeviceState(t *testing.T, deviceId string, states ...metalv1.DeviceS
188188
return false, fmt.Errorf("timed out waiting for device %v state %v to become one of %v", deviceId, device.GetState(), states)
189189
}
190190

191+
func WaitForVrfRouteState(t *testing.T, routeId string, statuses ...metalv1.VrfRouteStatus) bool {
192+
var route *metalv1.VrfRoute
193+
var err error
194+
t.Helper()
195+
predefinedTime := 900 * time.Second // Adjust this as needed
196+
retryInterval := 10 * time.Second // Adjust this as needed
197+
startTime := time.Now()
198+
client := TestClient()
199+
for time.Since(startTime) < predefinedTime {
200+
route, _, err = client.VRFsApi.FindVrfRouteById(context.Background(), routeId).Execute()
201+
if err != nil {
202+
t.Fatal(err)
203+
return false
204+
}
205+
for _, status := range statuses {
206+
if route.GetStatus() == status {
207+
return true
208+
}
209+
}
210+
211+
// Sleep for the specified interval
212+
time.Sleep(retryInterval)
213+
}
214+
t.Fatalf("timed out waiting for VRF route %v status %v to become one of %v", routeId, route.GetStatus(), statuses)
215+
return false
216+
}
217+
191218
func WaitForAttachVlanToPort(t *testing.T, portId string, attach bool) error {
192219
t.Helper()
193220
ticker := time.NewTicker(5 * time.Second)

0 commit comments

Comments
 (0)