Skip to content

Commit

Permalink
Update network manager unit tests (#351)
Browse files Browse the repository at this point in the history
- Create dhclient unit tests.
- Add more test cases to manager unit tests.
  • Loading branch information
drewhli authored Feb 9, 2024
1 parent 3e94187 commit 038a4a4
Show file tree
Hide file tree
Showing 9 changed files with 888 additions and 112 deletions.
4 changes: 4 additions & 0 deletions google_guest_agent/network/manager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ package manager
import (
"fmt"
"net"
"os/exec"

"github.com/GoogleCloudPlatform/guest-agent/metadata"
"github.com/GoogleCloudPlatform/guest-logging-go/logger"
)

var (
badMAC = make(map[string]net.Interface)

// execLookPath points to the function to check if a path exists.
execLookPath = exec.LookPath
)

// interfaceNames extracts the names of the network interfaces from the provided list
Expand Down
13 changes: 10 additions & 3 deletions google_guest_agent/network/manager/dhclient_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (

const (
// The base directory for dhclient files managed by guest agent.
baseDhclientDir = "/var/google-dhclient.d"
defaultBaseDhclientDir = "/var/google-dhclient.d"

// For finer control of the execution, dhclient is invoked for
// each interface individually such that each call will have its
Expand All @@ -58,6 +58,11 @@ var (
ipv6 = ipVersion{"ipv6", "-6"}
)

var (
// baseDhclientDir points to the base directory for DHClient leases and PIDs.
baseDhclientDir = defaultBaseDhclientDir
)

// ipVersion is a wrapper containing the human-readable version string and
// the respective dhclient argument.
type ipVersion struct {
Expand Down Expand Up @@ -85,7 +90,7 @@ func (n dhclient) Name() string {
// IsManaging checks if the dhclient CLI is available.
func (n dhclient) IsManaging(ctx context.Context, iface string) (bool, error) {
// Check if the dhclient CLI exists.
if _, err := exec.LookPath("dhclient"); err != nil {
if _, err := execLookPath("dhclient"); err != nil {
if errors.Is(err, exec.ErrNotFound) {
return false, nil
}
Expand Down Expand Up @@ -260,7 +265,9 @@ func dhclientProcessExists(ctx context.Context, iface string, ipVersion ipVersio
if ipVersion == ipv6 {
return containsProtocolArg, nil
}
return true, nil
if ipVersion == ipv4 && !slices.Contains(commandLine, ipv6.dhclientArg) {
return true, nil
}
}
}
return false, nil
Expand Down
Loading

0 comments on commit 038a4a4

Please sign in to comment.