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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cns/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestMain(m *testing.M) {
logger.InitLogger(logName, 0, 0, tmpLogDir+"/")
config := common.ServiceConfig{}

httpRestService, err := restserver.NewHTTPRestService(&config, fakes.NewFakeImdsClient(), fakes.NewFakeNMAgentClient())
httpRestService, err := restserver.NewHTTPRestService(&config, &fakes.WireserverClientFake{}, fakes.NewFakeNMAgentClient())
svc = httpRestService.(*restserver.HTTPRestService)
svc.Name = "cns-test-server"
fakeNNC := v1alpha.NodeNetworkConfig{
Expand Down
48 changes: 25 additions & 23 deletions cns/dockerclient/dockerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ package dockerclient

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/Azure/azure-container-networking/cns/imdsclient"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/cns/wireserver"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/platform"
"github.com/pkg/errors"
)

const (
Expand All @@ -22,34 +24,30 @@ const (
bridgeMode = "bridge"
)

// DockerClient specifies a client to connect to docker.
type DockerClient struct {
connectionURL string
imdsClient imdsclient.ImdsClientInterface
type interfaceGetter interface {
GetInterfaces(ctx context.Context) (*wireserver.GetInterfacesResult, error)
}

// NewDockerClient create a new docker client.
func NewDockerClient(url string) (*DockerClient, error) {
return &DockerClient{
connectionURL: url,
imdsClient: new(imdsclient.ImdsClient),
}, nil
// Client specifies a client to connect to docker.
type Client struct {
connectionURL string
wscli interfaceGetter
}

// NewDefaultDockerClient create a new docker client.
func NewDefaultDockerClient(imdsClient imdsclient.ImdsClientInterface) (*DockerClient, error) {
return &DockerClient{
// NewDefaultClient create a new docker client.
func NewDefaultClient(wscli interfaceGetter) (*Client, error) {
return &Client{
connectionURL: defaultDockerConnectionURL,
imdsClient: imdsClient,
wscli: wscli,
}, nil
}

// NetworkExists tries to retrieve a network from docker (if it exists).
func (dockerClient *DockerClient) NetworkExists(networkName string) error {
func (c *Client) NetworkExists(networkName string) error {
logger.Printf("[Azure CNS] NetworkExists")

res, err := http.Get(
dockerClient.connectionURL + inspectNetworkPath + networkName)
c.connectionURL + inspectNetworkPath + networkName)
if err != nil {
logger.Errorf("[Azure CNS] Error received from http Post for docker network inspect %v %v", networkName, err.Error())
return err
Expand All @@ -73,7 +71,7 @@ func (dockerClient *DockerClient) NetworkExists(networkName string) error {
}

// CreateNetwork creates a network using docker network create.
func (dockerClient *DockerClient) CreateNetwork(networkName string, nicInfo *imdsclient.InterfaceInfo, options map[string]interface{}) error {
func (c *Client) CreateNetwork(networkName string, nicInfo *wireserver.InterfaceInfo, options map[string]interface{}) error {
logger.Printf("[Azure CNS] CreateNetwork")

enableSnat := true
Expand Down Expand Up @@ -116,7 +114,7 @@ func (dockerClient *DockerClient) CreateNetwork(networkName string, nicInfo *imd
}

res, err := http.Post(
dockerClient.connectionURL+createNetworkPath,
c.connectionURL+createNetworkPath,
common.JsonContent,
netConfigJSON)
if err != nil {
Expand Down Expand Up @@ -149,11 +147,11 @@ func (dockerClient *DockerClient) CreateNetwork(networkName string, nicInfo *imd
}

// DeleteNetwork creates a network using docker network create.
func (dockerClient *DockerClient) DeleteNetwork(networkName string) error {
func (c *Client) DeleteNetwork(networkName string) error {
p := platform.NewExecClient()
logger.Printf("[Azure CNS] DeleteNetwork")

url := dockerClient.connectionURL + inspectNetworkPath + networkName
url := c.connectionURL + inspectNetworkPath + networkName
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
logger.Printf("[Azure CNS] Error received while creating http DELETE request for network delete %v %v", networkName, err.Error())
Expand All @@ -172,9 +170,13 @@ func (dockerClient *DockerClient) DeleteNetwork(networkName string) error {

// network successfully deleted.
if res.StatusCode == 204 {
primaryNic, err := dockerClient.imdsClient.GetPrimaryInterfaceInfoFromHost()
res, err := c.wscli.GetInterfaces(context.TODO()) // TODO(rbtr): thread context through this client
if err != nil {
return errors.Wrap(err, "failed to get interfaces from IMDS")
}
primaryNic, err := wireserver.GetPrimaryInterfaceFromResult(res)
if err != nil {
return err
return errors.Wrap(err, "failed to get primary interface from IMDS response")
}

cmd := fmt.Sprintf("iptables -t nat -D POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d %v -j MASQUERADE",
Expand Down
71 changes: 28 additions & 43 deletions cns/fakes/imdsclientfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,37 @@
package fakes

import (
"github.com/Azure/azure-container-networking/cns/imdsclient"
"github.com/Azure/azure-container-networking/cns/logger"
"context"

"github.com/Azure/azure-container-networking/cns/wireserver"
)

var (
HostPrimaryIpTest = "10.0.0.4"
HostSubnetTest = "10.0.0.0/24"
// HostPrimaryIP 10.0.0.4
HostPrimaryIP = "10.0.0.4"
// HostSubnet 10.0.0.0/24
HostSubnet = "10.0.0.0/24"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consts? otherwise, if they need to be mutated for tests, make them part of the fake?

)

// ImdsClient can be used to connect to VM Host agent in Azure.
type ImdsClientTest struct{}

func NewFakeImdsClient() *ImdsClientTest {
return &ImdsClientTest{}
}

// GetNetworkContainerInfoFromHost - Mock implementation to return Container version info.
func (imdsClient *ImdsClientTest) GetNetworkContainerInfoFromHost(networkContainerID string, primaryAddress string, authToken string, apiVersion string) (*imdsclient.ContainerVersion, error) {
ret := &imdsclient.ContainerVersion{}

return ret, nil
}

// GetPrimaryInterfaceInfoFromHost - Mock implementation to return Host interface info
func (imdsClient *ImdsClientTest) GetPrimaryInterfaceInfoFromHost() (*imdsclient.InterfaceInfo, error) {
logger.Printf("[Azure CNS] GetPrimaryInterfaceInfoFromHost")

interfaceInfo := &imdsclient.InterfaceInfo{
Subnet: HostSubnetTest,
PrimaryIP: HostPrimaryIpTest,
}

return interfaceInfo, nil
}

// GetPrimaryInterfaceInfoFromMemory - Mock implementation to return host interface info
func (imdsClient *ImdsClientTest) GetPrimaryInterfaceInfoFromMemory() (*imdsclient.InterfaceInfo, error) {
logger.Printf("[Azure CNS] GetPrimaryInterfaceInfoFromMemory")

return imdsClient.GetPrimaryInterfaceInfoFromHost()
}

// GetNetworkContainerInfoFromHostWithoutToken - Mock implementation to return host NMAgent NC version
// Set it as 0 which is the same as default initial NC version for testing purpose
func (imdsClient *ImdsClientTest) GetNetworkContainerInfoFromHostWithoutToken() int {
logger.Printf("[Azure CNS] get the NC version from NMAgent")

return 0
type WireserverClientFake struct{}

func (c *WireserverClientFake) GetInterfaces(ctx context.Context) (*wireserver.GetInterfacesResult, error) {
return &wireserver.GetInterfacesResult{
Interface: []wireserver.Interface{
{
IsPrimary: true,
IPSubnet: []wireserver.Subnet{
{
Prefix: HostSubnet,
IPAddress: []wireserver.Address{
{
Address: HostPrimaryIP,
IsPrimary: true,
},
},
},
},
},
},
}, nil
}
68 changes: 0 additions & 68 deletions cns/imdsclient/api.go

This file was deleted.

Loading