<commit_before>// +build cit

package network

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/utils"
)

const (
	markerFile = "/boot-marker"
)

func TestAliases(t *testing.T) {
	if err := verifyIPAliases(); err != nil {
		t.Fatal(err)
	}
}

func TestAliasAfterReboot(t *testing.T) {
	_, err := os.Stat(markerFile)
	if os.IsNotExist(err) {
		// first boot
		if _, err := os.Create(markerFile); err != nil {
			t.Fatalf("failed creating marker file: %v", err)
		}
		t.Fatal("first boot")
	}
	// second boot
	if err := verifyIPAliases(); err != nil {
		t.Fatal(err)
	}
}

func verifyIPAliases() error {
	var networkInterface string

	image, err := utils.GetMetadata("image")
	if err != nil {
		return fmt.Errorf("couldn't get image from metadata: %v", err)
	}
	switch {
	case strings.Contains(image, "debian-10"), strings.Contains(image, "ubuntu"):
		networkInterface = defaultPredictableInterface
	default:
		networkInterface = defaultInterface
	}

	actualIPs, err := getGoogleRoutes(networkInterface)
	if err != nil {
		return err
	}
	if err := verifyIPExist(actualIPs); err != nil {
		return err
	}
	return nil
}

func getGoogleRoutes(networkInterface string) ([]string, error) {
	// First, we probably need to wait so the guest agent can add the
	// routes. If this is insufficient, we might need to add retries.
	time.Sleep(30 * time.Second)

	arguments := strings.Split(fmt.Sprintf("route list table local type local scope host dev %s proto 66", networkInterface), " ")
	cmd := exec.Command("ip", arguments...)
	b, err := cmd.Output()
	if err != nil {
		return nil, err
	}
	if len(b) == 0 {
		return nil, fmt.Errorf("alias IPs not configured")
	}
	var res []string
	for _, line := range strings.Split(string(b), "\n") {
		ip := strings.Split(line, " ")
		if len(ip) >= 2 {
			res = append(res, ip[1])
		}
	}
	return res, nil
}

func TestAliasAgentRestart(t *testing.T) {
	var networkInterface string

	image, err := utils.GetMetadata("image")
	if err != nil {
		t.Fatalf("couldn't get image from metadata")
	}

	switch {
	case strings.Contains(image, "debian-10"), strings.Contains(image, "ubuntu"):
		networkInterface = defaultPredictableInterface
	default:
		networkInterface = defaultInterface
	}

	beforeRestart, err := getGoogleRoutes(networkInterface)
	if err != nil {
		t.Fatal(err)
	}
	cmd := exec.Command("systemctl", "restart", "google-guest-agent")
	err = cmd.Run()
	if err != nil {
		t.Fatal(err)
	}
	afterRestart, err := getGoogleRoutes(networkInterface)<commit_msg>Support d11 in Alias IP test (#276)<commit_after>// +build cit

package network

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	"github.com/GoogleCloudPlatform/guest-test-infra/imagetest/utils"
)

const (
	markerFile = "/boot-marker"
)

func TestAliases(t *testing.T) {
	if err := verifyIPAliases(); err != nil {
		t.Fatal(err)
	}
}

func TestAliasAfterReboot(t *testing.T) {
	_, err := os.Stat(markerFile)
	if os.IsNotExist(err) {
		// first boot
		if _, err := os.Create(markerFile); err != nil {
			t.Fatalf("failed creating marker file: %v", err)
		}
		t.Fatal("first boot")
	}
	// second boot
	if err := verifyIPAliases(); err != nil {
		t.Fatal(err)
	}
}

func verifyIPAliases() error {
	var networkInterface string

	image, err := utils.GetMetadata("image")
	if err != nil {
		return fmt.Errorf("couldn't get image from metadata: %v", err)
	}
	switch {
	case strings.Contains(image, "debian-10"), strings.Contains(image, "debian-11"), strings.Contains(image, "ubuntu"):
		networkInterface = defaultPredictableInterface
	default:
		networkInterface = defaultInterface
	}

	actualIPs, err := getGoogleRoutes(networkInterface)
	if err != nil {
		return err
	}
	if err := verifyIPExist(actualIPs); err != nil {
		return err
	}
	return nil
}

func getGoogleRoutes(networkInterface string) ([]string, error) {
	// First, we probably need to wait so the guest agent can add the
	// routes. If this is insufficient, we might need to add retries.
	time.Sleep(30 * time.Second)

	arguments := strings.Split(fmt.Sprintf("route list table local type local scope host dev %s proto 66", networkInterface), " ")
	cmd := exec.Command("ip", arguments...)
	b, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("error listing Google routes: %s", b)
	}
	if len(b) == 0 {
		return nil, fmt.Errorf("No Google routes found")
	}
	var res []string
	for _, line := range strings.Split(string(b), "\n") {
		ip := strings.Split(line, " ")
		if len(ip) >= 2 {
			res = append(res, ip[1])
		}
	}
	return res, nil
}

func TestAliasAgentRestart(t *testing.T) {
	var networkInterface string

	image, err := utils.GetMetadata("image")
	if err != nil {
		t.Fatalf("couldn't get image from metadata")
	}

	switch {
	case strings.Contains(image, "debian-10"), strings.Contains(image, "debian-11"), strings.Contains(image, "ubuntu"):
		networkInterface = defaultPredictableInterface
	default:
		networkInterface = defaultInterface
	}

	beforeRestart, err := getGoogleRoutes(networkInterface)
	if err != nil {
		t.Fatal(err)
	}
	cmd := exec.Command("systemctl", "restart", "google-guest-agent")
	err = cmd.Run()
	if err != nil {
		t.Fatal(err)
	}
	afterRestart, err := getGoogleRoutes(networkInterface)
{'fraction_non_alphanumeric': 0.14524397190051264, 'fraction_numerical': 0.005885703436491361, 'mean_word_length': 4.050814956855225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '405854', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1810, 'n_words': 486}