-
Notifications
You must be signed in to change notification settings - Fork 29
Add Longhorn UI/API access validation test suite #520
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
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/validate-longhorn-ui-access-again
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
689b520
Initial plan
Copilot f31878b
Add Longhorn UI Access validation test with review feedback applied
Copilot 87378ae
Refactor Longhorn API client based on review feedback
Copilot 800d490
Fix compilation error in ValidateSettings
Copilot 999c820
Accept both detached and attached states as valid volume states
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/rancher/shepherd/clients/rancher" | ||
| steveV1 "github.com/rancher/shepherd/clients/rancher/v1" | ||
| "github.com/rancher/shepherd/extensions/defaults" | ||
| "github.com/rancher/shepherd/pkg/namegenerator" | ||
| "github.com/sirupsen/logrus" | ||
| kwait "k8s.io/apimachinery/pkg/util/wait" | ||
| ) | ||
|
|
||
| const ( | ||
| LonghornNodeType = "longhorn.io.node" | ||
| LonghornSettingType = "longhorn.io.setting" | ||
| LonghornVolumeType = "longhorn.io.volume" | ||
| ) | ||
|
|
||
| // getReplicaCount determines an appropriate replica count for a Longhorn volume | ||
| // based on the number of available Longhorn nodes in the given namespace. | ||
| func getReplicaCount(client *rancher.Client, clusterID, namespace string) (int, error) { | ||
| steveClient, err := client.Steve.ProxyDownstream(clusterID) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("failed to get downstream client for replica count: %w", err) | ||
| } | ||
|
|
||
| longhornNodes, err := steveClient.SteveType(LonghornNodeType).NamespacedSteveClient(namespace).List(nil) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("failed to list Longhorn nodes: %w", err) | ||
| } | ||
|
|
||
| nodeCount := len(longhornNodes.Data) | ||
| if nodeCount <= 0 { | ||
| return 0, fmt.Errorf("no Longhorn nodes found in namespace %s", namespace) | ||
| } | ||
|
|
||
| return nodeCount, nil | ||
| } | ||
|
|
||
| // CreateVolume creates a new Longhorn volume via the Rancher Steve API and returns a pointer to it | ||
| func CreateVolume(client *rancher.Client, clusterID, namespace string) (*steveV1.SteveAPIObject, error) { | ||
| volumeName := namegenerator.AppendRandomString("test-lh-vol") | ||
|
|
||
| replicaCount, err := getReplicaCount(client, clusterID, namespace) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| steveClient, err := client.Steve.ProxyDownstream(clusterID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get downstream client: %w", err) | ||
| } | ||
|
|
||
| // Create volume spec | ||
| volumeSpec := map[string]interface{}{ | ||
| "type": LonghornVolumeType, | ||
| "metadata": map[string]interface{}{ | ||
| "name": volumeName, | ||
| "namespace": namespace, | ||
| }, | ||
| "spec": map[string]interface{}{ | ||
| "numberOfReplicas": replicaCount, | ||
| "size": "1073741824", // 1Gi in bytes | ||
| // blockdev frontend is required for Longhorn data engine v1, which is the default storage engine | ||
| // that uses Linux kernel block devices to manage volumes | ||
| "frontend": "blockdev", | ||
| }, | ||
| } | ||
|
|
||
| logrus.Infof("Creating Longhorn volume: %s with %d replicas", volumeName, replicaCount) | ||
| volume, err := steveClient.SteveType(LonghornVolumeType).Create(volumeSpec) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create volume: %w", err) | ||
| } | ||
|
|
||
| logrus.Infof("Successfully created volume: %s", volumeName) | ||
|
|
||
| // Register cleanup function for the volume | ||
| client.Session.RegisterCleanupFunc(func() error { | ||
| logrus.Infof("Cleaning up test volume: %s", volumeName) | ||
| return DeleteVolume(client, clusterID, namespace, volumeName) | ||
| }) | ||
|
|
||
| return volume, nil | ||
| } | ||
|
|
||
| // ValidateVolumeActive validates that a volume is in an active/detached state and ready to use | ||
| func ValidateVolumeActive(client *rancher.Client, clusterID, namespace, volumeName string) error { | ||
| logrus.Infof("Validating volume %s is active", volumeName) | ||
|
|
||
| steveClient, err := client.Steve.ProxyDownstream(clusterID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get downstream client: %w", err) | ||
| } | ||
|
|
||
| err = kwait.PollUntilContextTimeout(context.TODO(), 5*time.Second, defaults.FiveMinuteTimeout, true, func(ctx context.Context) (done bool, err error) { | ||
| volumeID := fmt.Sprintf("%s/%s", namespace, volumeName) | ||
| volume, err := steveClient.SteveType(LonghornVolumeType).ByID(volumeID) | ||
| if err != nil { | ||
| // Ignore error and continue polling as volume may not be available immediately | ||
| return false, nil | ||
| } | ||
slickwarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Extract status from the volume | ||
| if volume.Status == nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| statusMap, ok := volume.Status.(map[string]interface{}) | ||
| if !ok { | ||
| return false, nil | ||
| } | ||
|
|
||
| state, _ := statusMap["state"].(string) | ||
| robustness, _ := statusMap["robustness"].(string) | ||
|
|
||
| logrus.Infof("Volume %s state: %s, robustness: %s", volumeName, state, robustness) | ||
|
|
||
| // Volume is ready when it's in a valid state (detached or attached) with valid robustness | ||
| // Valid states: detached (ready to attach), attached (in use) | ||
| // "unknown" robustness is expected for detached volumes with no replicas scheduled | ||
| if (state == "detached" || state == "attached") && (robustness == "healthy" || robustness == "unknown") { | ||
| return true, nil | ||
| } | ||
|
|
||
| return false, nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("volume %s did not become active: %w", volumeName, err) | ||
| } | ||
|
|
||
| logrus.Infof("Volume %s is active and ready to use", volumeName) | ||
| return nil | ||
| } | ||
|
|
||
| // DeleteVolume deletes a Longhorn volume | ||
| func DeleteVolume(client *rancher.Client, clusterID, namespace, volumeName string) error { | ||
| steveClient, err := client.Steve.ProxyDownstream(clusterID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get downstream client: %w", err) | ||
| } | ||
|
|
||
| volumeID := fmt.Sprintf("%s/%s", namespace, volumeName) | ||
| volume, err := steveClient.SteveType(LonghornVolumeType).ByID(volumeID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get volume %s: %w", volumeName, err) | ||
| } | ||
|
|
||
| logrus.Infof("Deleting volume: %s", volumeName) | ||
| err = steveClient.SteveType(LonghornVolumeType).Delete(volume) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to delete volume %s: %w", volumeName, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // ValidateNodes validates that all Longhorn nodes are in a valid state | ||
| // This check is performed immediately without polling because nodes should already be | ||
| // in a ready state before Longhorn installation completes | ||
| func ValidateNodes(client *rancher.Client, clusterID, namespace string) error { | ||
| steveClient, err := client.Steve.ProxyDownstream(clusterID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get downstream client: %w", err) | ||
| } | ||
|
|
||
| nodes, err := steveClient.SteveType(LonghornNodeType).NamespacedSteveClient(namespace).List(nil) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list nodes: %w", err) | ||
| } | ||
|
|
||
| if len(nodes.Data) == 0 { | ||
| return fmt.Errorf("no Longhorn nodes found") | ||
| } | ||
|
|
||
| // Validate each node has valid conditions | ||
| for _, node := range nodes.Data { | ||
| if node.Status == nil { | ||
| return fmt.Errorf("node %s has no status", node.Name) | ||
| } | ||
| } | ||
slickwarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // ValidateSettings validates that Longhorn settings are properly configured | ||
| // Checks that at least one setting has a non-nil value to ensure settings are accessible | ||
| func ValidateSettings(client *rancher.Client, clusterID, namespace string) error { | ||
| steveClient, err := client.Steve.ProxyDownstream(clusterID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get downstream client: %w", err) | ||
| } | ||
|
|
||
| settings, err := steveClient.SteveType(LonghornSettingType).NamespacedSteveClient(namespace).List(nil) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list settings: %w", err) | ||
| } | ||
|
|
||
| if len(settings.Data) == 0 { | ||
| return fmt.Errorf("no Longhorn settings found") | ||
| } | ||
|
|
||
| // Validate that at least one setting has a value field | ||
| hasValidSetting := false | ||
| for _, setting := range settings.Data { | ||
| if setting.JSONResp != nil { | ||
| if _, exists := setting.JSONResp["value"]; exists { | ||
| hasValidSetting = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if !hasValidSetting { | ||
| return fmt.Errorf("no Longhorn settings have valid value fields") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideia: We could adopt
fmt.Errorfas a code standart for the next automations