From 60c565169707ec6c1fc20467e862a421a29ad5f7 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Fri, 27 Dec 2024 15:02:43 -0500 Subject: [PATCH] update `TestClient_UpdateAgentProfile()` --- apstra/api_system_agent_profiles_test.go | 89 +++++++++++++++--------- 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/apstra/api_system_agent_profiles_test.go b/apstra/api_system_agent_profiles_test.go index 09aec28a..5bf149e1 100644 --- a/apstra/api_system_agent_profiles_test.go +++ b/apstra/api_system_agent_profiles_test.go @@ -151,44 +151,71 @@ func TestClient_UpdateAgentProfile(t *testing.T) { } for clientName, client := range clients { - log.Printf("testing GetAllSystemAgents() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion()) - agents, err := client.client.GetAllSystemAgents(ctx) - require.NoError(t, err) - - var profileWithAckedSystem ObjectId - for _, agent := range agents { - if agent.Config.Profile == "" { - continue - } + clientName, client := clientName, client + t.Run(client.name(), func(t *testing.T) { + t.Parallel() - systemInfo, err := client.client.GetSystemInfo(ctx, agent.Status.SystemId) + t.Logf("testing GetAllSystemAgents() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion()) + agents, err := client.client.GetAllSystemAgents(ctx) require.NoError(t, err) - if systemInfo.Status.IsAcknowledged { - profileWithAckedSystem = agent.Config.Profile - break + profiles, err := client.client.GetAllAgentProfiles(ctx) + require.NoError(t, err) + profileMap := make(map[ObjectId]AgentProfile, len(profiles)) + for _, profile := range profiles { + profileMap[profile.Id] = profile } - } - - if profileWithAckedSystem == "" { - t.Skipf("no agent profile") - } - profile, err := client.client.GetAgentProfile(ctx, profileWithAckedSystem) - require.NoError(t, err) + for i := len(agents) - 1; i >= 0; i-- { // loop backward through agents + // remove agents without profile + if agents[i].Config.Profile == "" { + agents[i] = agents[len(agents)-1] // copy last to index i + agents = agents[:len(agents)-1] // delete last item + continue + } + + // remove agents with platform configured + if agents[i].Config.Platform != "" { + agents[i] = agents[len(agents)-1] // copy last to index i + agents = agents[:len(agents)-1] // delete last item + continue + } + + _, ok := profileMap[agents[i].Config.Profile] + if !ok { + t.Fatal(fmt.Errorf("agent %s claims profile %s, which is not a valid profile", agents[i].Id, agents[i].Config.Profile)) + } + + // remove agents with un-acked systems + systemInfo, err := client.client.GetSystemInfo(ctx, agents[i].Status.SystemId) + require.NoError(t, err) + if !systemInfo.Status.IsAcknowledged { + agents[i] = agents[len(agents)-1] // copy last to index i + agents = agents[:len(agents)-1] // delete last item + continue + } + } - origPlatform := profile.Platform - _ = origPlatform - profile.Platform = "" + if len(agents) == 0 { + t.Skip("skipping because no system agents rely on agent profile to determine platform type") + } - err = client.client.UpdateAgentProfile(ctx, profile.Id, &AgentProfileConfig{ - Label: profile.Label, - Platform: nil, + // At this point, agents is full of system agents which rely on their associated agent + // profile for platform info. We'll attempt to modify the agent profile to elicit an error. + + for _, agent := range agents { + profile := profileMap[agent.Config.Profile] + t.Logf("trying to provoke an error with UpdateAgentProfile() against %s %s (%s)", client.clientType, clientName, client.client.ApiVersion()) + err = client.client.UpdateAgentProfile(ctx, profile.Id, &AgentProfileConfig{ + Label: profile.Label, + Platform: toPtr(""), + }) + require.Error(t, err) + var ace ClientErr + if !(errors.As(err, &ace) && ace.errType == ErrAgentProfilePlatformRequired) { + t.Fatalf("error should have been type %d, err is %q", ErrAgentProfilePlatformRequired, err.Error()) + } + } }) - require.Error(t, err) - var ace ClientErr - if !(errors.As(err, &ace) && ace.errType == ErrAgentProfilePlatformRequired) { - t.Fatalf("error should have been type %d, err is %q", ErrAgentProfilePlatformRequired, err.Error()) - } } }