Skip to content

Commit 928bb55

Browse files
Leandro (Leo) Dorileocopybara-github
authored andcommitted
Add test for alias IP handling with ip_forwarding config.
The new test verifies that alias IP routes are correctly removed when the guest agent is restarted after `ip_forwarding` is set to `false` in `/etc/default/instance_configs.cfg`. PiperOrigin-RevId: 813023486
1 parent 449eadc commit 928bb55

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

test_suites/network/alias_ip_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,68 @@ func TestAliasAgentRestart(t *testing.T) {
241241
}
242242
}
243243

244+
func TestAliasAgentRestartWithIPForwardingConfigFalse(t *testing.T) {
245+
utils.LinuxOnly(t)
246+
247+
ctx := utils.Context(t)
248+
iface := readNic(ctx, t, 0)
249+
250+
t.Cleanup(func() {
251+
// Swap the IP forwarding configuration from false to true.
252+
swapIPForwardingConfiguration(t, "ip_forwarding = false")
253+
254+
if err := utils.RestartAgent(ctx); err != nil {
255+
t.Fatal(err)
256+
}
257+
})
258+
259+
beforeRestart, err := getGoogleRoutes(iface.Name)
260+
if err != nil {
261+
t.Fatal(err)
262+
}
263+
264+
// Swap the IP forwarding configuration from true to false.
265+
swapIPForwardingConfiguration(t, "ip_forwarding = true")
266+
267+
if err := utils.RestartAgent(ctx); err != nil {
268+
t.Fatal(err)
269+
}
270+
271+
afterRestart, err := getGoogleRoutes(iface.Name)
272+
if err == nil {
273+
t.Fatal("Routes exists after restart, but should not exist")
274+
}
275+
276+
if compare(beforeRestart, afterRestart) {
277+
t.Fatalf("Routes are consistent after restart, but should not be")
278+
}
279+
}
280+
281+
func swapIPForwardingConfiguration(t *testing.T, currentConfig string) {
282+
t.Helper()
283+
284+
configFile := "/etc/default/instance_configs.cfg"
285+
286+
toggle := map[string]string{
287+
"ip_forwarding = true": "ip_forwarding = false",
288+
"ip_forwarding = false": "ip_forwarding = true",
289+
}
290+
291+
data, err := os.ReadFile(configFile)
292+
if err != nil {
293+
t.Fatal(err)
294+
}
295+
296+
from := currentConfig
297+
to := toggle[currentConfig]
298+
299+
changedConfig := strings.ReplaceAll(string(data), from, to)
300+
301+
if err := os.WriteFile(configFile, []byte(changedConfig), 0644); err != nil {
302+
t.Fatal(err)
303+
}
304+
}
305+
244306
func verifyIPExist(ctx context.Context, routes []string) error {
245307
expected, err := utils.GetMetadata(ctx, "instance", "network-interfaces", "0", "ip-aliases", "0")
246308
if err != nil {

0 commit comments

Comments
 (0)