Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions test_suites/networkconfig/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package networkconfig

import (
"context"
_ "embed"
"fmt"
"path"
"regexp"
"strings"

"github.com/GoogleCloudPlatform/cloud-image-tests/utils"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"

pb "github.com/GoogleCloudPlatform/cloud-image-tests/test_suites/networkconfig/config_expectations_proto"
)

//go:embed config_expectations/config_expectations.textproto
var configExpectationsBytes []byte

func parseConfigExpectations() (*pb.ConfigExpectations, error) {
var configExpectations pb.ConfigExpectations
if err := prototext.Unmarshal(configExpectationsBytes, &configExpectations); err != nil {
return nil, fmt.Errorf("failed to unmarshal embedded config expectations: %w", err)
}
return &configExpectations, nil
}

// guestOSConstraints fetches the guest image from MDS and merges all matching constraints.
func guestOSConstraints(ctx context.Context, constraints []*pb.GuestOsConstraint) (*pb.GuestOsConstraint, error) {
image, err := utils.GetMetadata(ctx, "instance", "image")
if err != nil {
return nil, fmt.Errorf("getting instance image from metadata: %w", err)
}
imageName := path.Base(strings.TrimSpace(image))

merged := &pb.GuestOsConstraint{}
for _, c := range constraints {
matched, err := regexp.MatchString(c.GetOsPattern(), imageName)
if err != nil {
return nil, fmt.Errorf("matching os_pattern %q: %w", c.GetOsPattern(), err)
}
if matched {
proto.Merge(merged, c)
}
}
return merged, nil
}
41 changes: 20 additions & 21 deletions test_suites/networkconfig/deviceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package networkconfig

import (
_ "embed"
"fmt"
fs "io/fs"
"os"
Expand All @@ -32,7 +31,6 @@ import (
"github.com/GoogleCloudPlatform/cloud-image-tests/utils"
"github.com/GoogleCloudPlatform/cloud-image-tests/utils/networkutils"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"

Expand All @@ -55,9 +53,6 @@ var (
idpfTxRxIRQRe = regexp.MustCompile(`.*TxRx-(\d+)`)
)

//go:embed config_expectations/config_expectations.textproto
var configExpectationsBytes []byte

type systemKey struct {
machineType string
nicTypes []string
Expand Down Expand Up @@ -497,19 +492,13 @@ func buildQueuesXPSOnly(txQueueToXPS map[int]string) ([]*pb.TxQueue, error) {
return txQueues, nil
}

func buildQueues(nicType string, irqCPULists *irqCPULists, txQueueToXPS map[int]string) ([]*pb.TxQueue, []*pb.RxQueue, error) {
// The guest environment don't configure IRQs for IRDMA and MRDMA.
// We may wish to assert on this in the future (i.e. values from the driver), but for now we
// ignore it.
switch nicType {
case networkutils.NICTypeIRDMA, networkutils.NICTypeMRDMA:
func buildQueues(irqCPULists *irqCPULists, txQueueToXPS map[int]string) ([]*pb.TxQueue, []*pb.RxQueue, error) {
if irqCPULists == nil {
txQueues, err := buildQueuesXPSOnly(txQueueToXPS)
if err != nil {
return nil, nil, err
}
return txQueues, nil, nil
default:
// continue
}

if len(irqCPULists.txIRQAffinity) != len(txQueueToXPS) {
Expand Down Expand Up @@ -554,8 +543,11 @@ func thisSystemConfig(mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConf
return nil, fmt.Errorf("parsing ethtool -l output for %q: %w", nicName, err)
}

// The guest environment doesn't configure IRQs for IRDMA and MRDMA.
// Check mdsIface.NICType rather than nicType because images without an IRDMA driver
// bind IDPF, while config_expectations only specifies XPS for IRDMA NICs.
var queueToIRQs *irqCPULists
if nicType != networkutils.NICTypeIRDMA && nicType != networkutils.NICTypeMRDMA {
if mdsIface.NICType != networkutils.NICTypeIRDMA && mdsIface.NICType != networkutils.NICTypeMRDMA {
irqs, err := deviceIRQs(nicName)
if err != nil {
return nil, fmt.Errorf("getting device IRQs for %q: %w", nicName, err)
Expand All @@ -571,7 +563,7 @@ func thisSystemConfig(mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConf
return nil, fmt.Errorf("getting XPS for %q: %w", nicName, err)
}

txQueues, rxQueues, err := buildQueues(nicType, queueToIRQs, txQueueToXPS)
txQueues, rxQueues, err := buildQueues(queueToIRQs, txQueueToXPS)
if err != nil {
return nil, fmt.Errorf("building queues for %q: %w", nicName, err)
}
Expand Down Expand Up @@ -610,9 +602,13 @@ func thisSystemConfig(mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConf
func TestDeviceConfig(t *testing.T) {
ctx := utils.Context(t)

var configExpectations pb.ConfigExpectations
if err := prototext.Unmarshal(configExpectationsBytes, &configExpectations); err != nil {
t.Fatalf("failed to unmarshal embedded config expectations: %v", err)
configExpectations, err := parseConfigExpectations()
if err != nil {
t.Fatalf("parseConfigExpectations() = err %v, want nil", err)
}
constraints, err := guestOSConstraints(ctx, configExpectations.GetGuestOsConstraints())
if err != nil {
t.Fatalf("guestOSConstraints(ctx, ...) = err %v, want nil", err)
}

mdsIfaces, err := networkutils.ListMDSIfaces(ctx)
Expand Down Expand Up @@ -641,18 +637,21 @@ func TestDeviceConfig(t *testing.T) {
}
kernelVersion := strings.TrimSpace(string(kernelVersionOut))

wantSystemConfig, err := expectedConfigForMachine(&configExpectations, machineType, nicTypes, kernelVersion)
wantSystemConfig, err := expectedConfigForMachine(configExpectations, machineType, nicTypes, kernelVersion)
if err != nil {
t.Fatalf("expectedConfigForMachine(&configExpectations, %q, %v, %q) = err: %v, want nil", machineType, nicTypes, kernelVersion, err)
t.Fatalf("expectedConfigForMachine(configExpectations, %q, %v, %q) = err: %v, want nil", machineType, nicTypes, kernelVersion, err)
}

gotSystemConfig, err := thisSystemConfig(mdsIfaces)
if err != nil {
t.Fatalf("thisSystemConfig(mdsIfaces) = err: %v, want nil", err)
}

// Only assert the ring size if they're set in the expected config.
// Adjust expectations for guest OS constraints and unset optional fields.
for i, wantNic := range wantSystemConfig.GetNics() {
if wantNic.GetType() == networkutils.NICTypeIRDMA && !constraints.GetIncludesIrdmaDriver() {
wantNic.SetType(networkutils.NICTypeIDPF)
}
if i >= len(gotSystemConfig.GetNics()) {
break
}
Expand Down
21 changes: 17 additions & 4 deletions test_suites/networkconfig/nicname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ import (

func TestNICNames(t *testing.T) {
ctx := utils.Context(t)
configExpectations, err := parseConfigExpectations()
if err != nil {
t.Fatalf("parseConfigExpectations() = err %v want nil", err)
}
constraints, err := guestOSConstraints(ctx, configExpectations.GetGuestOsConstraints())
if err != nil {
t.Fatalf("guestOSConstraints(ctx, ...) = err %v want nil", err)
}

mdsIfaces, err := networkutils.ListMDSIfaces(ctx)
if err != nil {
t.Fatalf("ListMDSIfaces(ctx) = err %v want nil", err)
Expand All @@ -46,16 +55,20 @@ func TestNICNames(t *testing.T) {
seenIfaceNames[systemIface.Name] = true

t.Run(systemIface.Name, func(t *testing.T) {
regex, err := networkutils.ExpectedNICRegex(mdsIface.NICType)
nicType := mdsIface.NICType
if nicType == networkutils.NICTypeIRDMA && !constraints.GetIncludesIrdmaDriver() {
nicType = networkutils.NICTypeIDPF
}
regex, err := networkutils.ExpectedNICRegex(nicType)
if err != nil {
t.Errorf("ExpectedNICRegex(%q) = err %v want nil", mdsIface.NICType, err)
t.Errorf("ExpectedNICRegex(%q) = err %v want nil", nicType, err)
return
}

if regex.MatchString(systemIface.Name) {
t.Logf("NIC name %q matches naming expectations for NIC type %q", systemIface.Name, mdsIface.NICType)
t.Logf("NIC name %q matches naming expectations for NIC type %q", systemIface.Name, nicType)
} else {
t.Errorf("NIC name %q does not match expected regex %q for NIC type %q", systemIface.Name, regex.String(), mdsIface.NICType)
t.Errorf("NIC name %q does not match expected regex %q for NIC type %q", systemIface.Name, regex.String(), nicType)
}
})
}
Expand Down