Fix tests

pull/20401/head
Nick Cellino 2024-02-05 21:29:50 -05:00
parent bd7724aa9d
commit 1d91dffbd1
2 changed files with 112 additions and 18 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/hashicorp/consul/agent/hcp/bootstrap/constants"
hcpclient "github.com/hashicorp/consul/agent/hcp/client"
"github.com/hashicorp/consul/agent/hcp/config"
hcpctl "github.com/hashicorp/consul/internal/hcp"
pbhcp "github.com/hashicorp/consul/proto-public/pbhcp/v2"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/sdk/testutil"
@ -41,6 +42,97 @@ func TestMonitorHCPLink_Ok(t *testing.T) {
return "test-mgmt-token", nil
}
dataDir := testutil.TempDir(t, "test-link-controller")
os.Mkdir(filepath.Join(dataDir, constants.SubDir), os.ModeDir)
existingCfg := config.CloudConfig{
AuthURL: "test.com",
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
MonitorHCPLink(
ctx, hclog.New(&hclog.LoggerOptions{Output: io.Discard}), mgr, linkWatchCh, mockHcpClientFn,
loadMgmtTokenFn, existingCfg, dataDir,
)
}()
// Set up a link
link := pbhcp.Link{
ResourceId: "abc",
ClientId: "def",
ClientSecret: "ghi",
AccessLevel: pbhcp.AccessLevel_ACCESS_LEVEL_GLOBAL_READ_WRITE,
}
expectedCfg := config.CloudConfig{
ResourceID: link.ResourceId,
ClientID: link.ClientId,
ClientSecret: link.ClientSecret,
AuthURL: "test.com",
ManagementToken: "test-mgmt-token",
}
linkResource, err := anypb.New(&link)
require.NoError(t, err)
// Create link, expect HCP manager to be updated and started
mgr.EXPECT().Start(mock.Anything).Return(nil).Once()
mgr.EXPECT().UpdateConfig(mockHCPClient, expectedCfg)
linkWatchCh <- &pbresource.WatchEvent{
Event: &pbresource.WatchEvent_Upsert_{
Upsert: &pbresource.WatchEvent_Upsert{
Resource: &pbresource.Resource{
Id: &pbresource.ID{
Name: "global",
Type: pbhcp.LinkType,
},
Status: map[string]*pbresource.Status{
hcpctl.StatusKey: {
Conditions: []*pbresource.Condition{hcpctl.ConditionValidatedSuccess},
},
},
Data: linkResource,
},
},
},
}
// Delete link, expect HCP manager to be stopped
mgr.EXPECT().Stop().Return(nil).Once()
linkWatchCh <- &pbresource.WatchEvent{
Event: &pbresource.WatchEvent_Delete_{
Delete: &pbresource.WatchEvent_Delete{},
},
}
// Wait for MonitorHCPLink to return before assertions run
close(linkWatchCh)
wg.Wait()
// Ensure hcp-config directory is removed
file := filepath.Join(dataDir, constants.SubDir)
if _, err := os.Stat(file); err == nil || !os.IsNotExist(err) {
require.Fail(t, "should have removed hcp-config directory")
}
}
func TestMonitorHCPLink_ValidationError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
linkWatchCh := make(chan *pbresource.WatchEvent)
mgr := NewMockManager(t)
mockHCPClient := hcpclient.NewMockClient(t)
mockHcpClientFn := func(_ config.CloudConfig) (hcpclient.Client, error) {
return mockHCPClient, nil
}
loadMgmtTokenFn := func(ctx context.Context, logger hclog.Logger, hcpClient hcpclient.Client, dataDir string) (string, error) {
return "test-mgmt-token", nil
}
dataDir := testutil.TempDir(t, "test-link-controller")
os.Mkdir(filepath.Join(dataDir, constants.SubDir), os.ModeDir)
@ -62,23 +154,10 @@ func TestMonitorHCPLink_Ok(t *testing.T) {
ClientSecret: "ghi",
AccessLevel: pbhcp.AccessLevel_ACCESS_LEVEL_GLOBAL_READ_WRITE,
}
existingManagerCfg := config.CloudConfig{
AuthURL: "test.com",
}
expectedCfg := config.CloudConfig{
ResourceID: link.ResourceId,
ClientID: link.ClientId,
ClientSecret: link.ClientSecret,
AuthURL: "test.com",
ManagementToken: "test-mgmt-token",
}
linkResource, err := anypb.New(&link)
require.NoError(t, err)
// Create link, expect HCP manager to be updated and started
mgr.EXPECT().Start(mock.Anything).Return(nil).Once()
mgr.EXPECT().GetCloudConfig().Return(existingManagerCfg)
mgr.EXPECT().UpdateConfig(mockHCPClient, expectedCfg)
linkWatchCh <- &pbresource.WatchEvent{
Event: &pbresource.WatchEvent_Upsert_{
Upsert: &pbresource.WatchEvent_Upsert{
@ -87,6 +166,11 @@ func TestMonitorHCPLink_Ok(t *testing.T) {
Name: "global",
Type: pbhcp.LinkType,
},
Status: map[string]*pbresource.Status{
hcpctl.StatusKey: {
Conditions: []*pbresource.Condition{hcpctl.ConditionValidatedFailed},
},
},
Data: linkResource,
},
},
@ -127,6 +211,9 @@ func TestMonitorHCPLink_Ok_ReadOnly(t *testing.T) {
loadMgmtTokenFn := func(ctx context.Context, logger hclog.Logger, hcpClient hcpclient.Client, dataDir string) (string, error) {
return "test-mgmt-token", nil
}
existingManagerCfg := config.CloudConfig{
AuthURL: "test.com",
}
var wg sync.WaitGroup
@ -135,7 +222,7 @@ func TestMonitorHCPLink_Ok_ReadOnly(t *testing.T) {
defer wg.Done()
MonitorHCPLink(
ctx, hclog.New(&hclog.LoggerOptions{Output: io.Discard}), mgr, linkWatchCh, mockHcpClientFn,
loadMgmtTokenFn, config.CloudConfig{}, "",
loadMgmtTokenFn, existingManagerCfg, "",
)
}()
@ -147,9 +234,6 @@ func TestMonitorHCPLink_Ok_ReadOnly(t *testing.T) {
ClientSecret: "ghi",
AccessLevel: pbhcp.AccessLevel_ACCESS_LEVEL_GLOBAL_READ_ONLY,
}
existingManagerCfg := config.CloudConfig{
AuthURL: "test.com",
}
expectedCfg := config.CloudConfig{
ResourceID: link.ResourceId,
ClientID: link.ClientId,
@ -162,7 +246,6 @@ func TestMonitorHCPLink_Ok_ReadOnly(t *testing.T) {
// Create link, expect HCP manager to be updated and started
mgr.EXPECT().Start(mock.Anything).Return(nil).Once()
mgr.EXPECT().GetCloudConfig().Return(existingManagerCfg)
mgr.EXPECT().UpdateConfig(mockHCPClient, expectedCfg)
linkWatchCh <- &pbresource.WatchEvent{
Event: &pbresource.WatchEvent_Upsert_{
@ -172,6 +255,11 @@ func TestMonitorHCPLink_Ok_ReadOnly(t *testing.T) {
Name: "global",
Type: pbhcp.LinkType,
},
Status: map[string]*pbresource.Status{
hcpctl.StatusKey: {
Conditions: []*pbresource.Condition{hcpctl.ConditionValidatedSuccess},
},
},
Data: linkResource,
},
},

View File

@ -26,3 +26,9 @@ var IsValidated = link.IsValidated
func RegisterControllers(mgr *controller.Manager, deps ControllerDependencies) {
controllers.Register(mgr, deps)
}
// Needed for testing
var StatusKey = link.StatusKey
var StatusValidated = link.StatusValidated
var ConditionValidatedSuccess = link.ConditionValidatedSuccess
var ConditionValidatedFailed = link.ConditionValidatedFailed