Skip to content

Add unit tests for createHostNCApipaNetwork() function and apply changes from PR #3693 #3695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 30, 2025

This PR addresses the follow-up work for PR #3693 by adding comprehensive unit tests and applying the necessary changes to the createHostNCApipaNetwork() function.

Changes Made

1. Applied Changes from PR #3693

  • Added vEthernethostNCLoopbackAdapterName constant for the vEthernet interface name: "vEthernet (LoopbackAdapterHostNCConnectivity)"
  • Updated createHostNCApipaNetwork() logic to check for both loopback adapter interfaces before creating a new one
  • Fixed typo in comment: "fitst" → "first"

2. Code Refactoring for Testability

  • Extracted interface existence logic into a pure function shouldCreateLoopbackAdapter() that can be easily unit tested
  • This function takes an interface existence checker as a parameter, enabling dependency injection for testing

3. Comprehensive Unit Tests

Added TestShouldCreateLoopbackAdapter() that covers all scenarios:

  • Neither interface exists: Should create loopback adapter
  • hostNCLoopbackAdapterName exists: Should skip creation
  • vEthernethostNCLoopbackAdapterName exists: Should skip creation
  • Both interfaces exist: Should skip creation (prioritizes hostNCLoopbackAdapterName)

4. Additional Tests

  • Added TestConstants() to validate the vEthernet constant is constructed correctly
  • Added Windows build constraints to ensure Windows-specific code only runs on Windows

Code Example

The new logic checks for both interface types:

// Before (PR #3693)
if interfaceExists, _ := networkcontainers.InterfaceExists(hostNCLoopbackAdapterName); !interfaceExists {
    // create adapter
}

// After (this PR) 
shouldCreate, logMessage := shouldCreateLoopbackAdapter(networkcontainers.InterfaceExists)
logger.Printf(logMessage)
if shouldCreate {
    // create adapter
}

The extracted function enables comprehensive testing:

func shouldCreateLoopbackAdapter(interfaceExistsFunc func(string) (bool, error)) (bool, string) {
    loopbackInterfaceExists, _ := interfaceExistsFunc(hostNCLoopbackAdapterName)
    vethernetLoopbackInterfaceExists, _ := interfaceExistsFunc(vEthernethostNCLoopbackAdapterName)
    
    if loopbackInterfaceExists {
        return false, hostNCLoopbackAdapterName + " already created, skipping loopback interface creation"
    }
    if vethernetLoopbackInterfaceExists {
        return false, vEthernethostNCLoopbackAdapterName + " already created, skipping loopback interface creation"
    }
    
    return true, "Creating loopback adapter"
}

Fixes #3694.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Adding UT for createHostNCApipaNetwork() function and cover the case for hostNCLoopbackAdapterName existence Add unit tests for createHostNCApipaNetwork() function and apply changes from PR #3693 May 30, 2025
@Copilot Copilot AI requested a review from behzad-mir May 30, 2025 22:46
Copilot finished work on behalf of behzad-mir May 30, 2025 22:46
Copy link

This pull request is stale because it has been open for 2 weeks with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the stale Stale due to inactivity. label Jun 14, 2025
Copy link

Pull request closed due to inactivity.

@github-actions github-actions bot closed this Jun 22, 2025
@github-actions github-actions bot deleted the copilot/fix-3694 branch June 22, 2025 00:01
@rbtr rbtr restored the copilot/fix-3694 branch June 23, 2025 17:29
@rbtr rbtr removed the stale Stale due to inactivity. label Jun 23, 2025
@rbtr rbtr reopened this Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding UT for createHostNCApipaNetwork() function and cover the case for hostNCLoopbackAdapterName existence
3 participants