azure-container-networking/cns/restserver/homeazmonitor_test.go

103 строки
2.9 KiB
Go

package restserver
import (
"context"
"testing"
"time"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/fakes"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Azure/azure-container-networking/nmagent"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
)
// TestHomeAzMonitor makes sure the HomeAzMonitor works properly in caching home az
func TestHomeAzMonitor(t *testing.T) {
tests := []struct {
name string
client *fakes.NMAgentClientFake
homeAzExp cns.HomeAzResponse
shouldErr bool
}{
{
"happy path",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
return []string{"GetHomeAz"}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{HomeAz: uint(1)}, nil
},
},
cns.HomeAzResponse{IsSupported: true, HomeAz: uint(1)},
false,
},
{
"getHomeAz is not supported in nmagent",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
return []string{"dummy"}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{}, nil
},
},
cns.HomeAzResponse{},
false,
},
{
"api supported but home az value is not valid",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
return []string{GetHomeAzAPIName}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{HomeAz: 0}, nil
},
},
cns.HomeAzResponse{IsSupported: true},
true,
},
{
"api supported but got unexpected errors",
&fakes.NMAgentClientFake{
SupportedAPIsF: func(ctx context.Context) ([]string, error) {
return []string{GetHomeAzAPIName}, nil
},
GetHomeAzF: func(ctx context.Context) (nmagent.AzResponse, error) {
return nmagent.AzResponse{}, errors.New("unexpected error")
},
},
cns.HomeAzResponse{IsSupported: true},
true,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
homeAzMonitor := NewHomeAzMonitor(test.client, time.Second)
homeAzMonitor.Populate(context.TODO())
getHomeAzResponse := homeAzMonitor.GetHomeAz(context.TODO())
// check the homeAz cache value
if !cmp.Equal(getHomeAzResponse.HomeAzResponse, test.homeAzExp) {
t.Error("homeAz cache differs from expectation: diff:", cmp.Diff(getHomeAzResponse.HomeAzResponse, test.homeAzExp))
}
// check returnCode for error
if getHomeAzResponse.Response.ReturnCode != types.Success && !test.shouldErr {
t.Fatal("unexpected error: ", getHomeAzResponse.Response.Message)
}
if getHomeAzResponse.Response.ReturnCode == types.Success && test.shouldErr {
t.Fatal("expected error but received none")
}
t.Cleanup(func() {
homeAzMonitor.Stop()
})
})
}
}