Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
304 changes: 304 additions & 0 deletions interoperability/longhorn/api/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
package api

import (
"context"
"fmt"
"testing"
"time"

"github.com/rancher/shepherd/clients/rancher"
"github.com/rancher/shepherd/extensions/defaults"
"github.com/rancher/shepherd/pkg/namegenerator"
"github.com/rancher/tests/actions/charts"
"github.com/rancher/tests/interoperability/longhorn"
kwait "k8s.io/apimachinery/pkg/util/wait"
)

const (
longhornNodeType = "longhorn.io.node"
longhornSettingType = "longhorn.io.setting"
longhornVolumeType = "longhorn.io.volume"
)

// LonghornClient represents a client for interacting with Longhorn resources via Rancher API
type LonghornClient struct {
Client *rancher.Client
ClusterID string
ServiceURL string
}

// NewLonghornClient creates a new Longhorn client that uses Rancher Steve API
func NewLonghornClient(client *rancher.Client, clusterID, serviceURL string) (*LonghornClient, error) {
longhornClient := &LonghornClient{
Client: client,
ClusterID: clusterID,
ServiceURL: serviceURL,
}

return longhornClient, nil
}

// getReplicaCount determines an appropriate replica count for a Longhorn volume
// based on the number of available Longhorn nodes. It caps the replica count
// at 3 to preserve the previous default behavior on larger clusters, while
// ensuring it does not exceed the number of nodes on smaller clusters.
func getReplicaCount(t *testing.T, lc *LonghornClient) (int, error) {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return 0, fmt.Errorf("failed to get downstream client for replica count: %w", err)
Copy link
Contributor

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.Errorf as a code standart for the next automations

}

longhornNodes, err := steveClient.SteveType(longhornNodeType).NamespacedSteveClient(charts.LonghornNamespace).List(nil)
if err != nil {
return 0, fmt.Errorf("failed to list Longhorn nodes: %w", err)
}

nodeCount := len(longhornNodes.Data)
if nodeCount <= 0 {
t.Logf("No Longhorn nodes found; defaulting replica count to 1")
return 1, nil
}

// Do not exceed the number of nodes, and cap at 3 to match previous behavior.
if nodeCount >= 3 {
return 3, nil
}

return nodeCount, nil
}

// CreateVolume creates a new Longhorn volume via the Rancher Steve API
func CreateVolume(t *testing.T, lc *LonghornClient) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

do not use testing.T as a parameter only for logging. Use logrus for logging

Copy link
Author

Choose a reason for hiding this comment

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

Replaced all testing.T logging with logrus throughout the file. (800d490)

volumeName := namegenerator.AppendRandomString("test-lh-vol")

replicaCount, err := getReplicaCount(t, lc)
if err != nil {
return "", err
}

steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return "", 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": charts.LonghornNamespace,
},
"spec": map[string]interface{}{
"numberOfReplicas": replicaCount,
"size": "1073741824", // 1Gi in bytes
"frontend": "blockdev", // Required for data engine v1
},
}

t.Logf("Creating Longhorn volume: %s with %d replicas", volumeName, replicaCount)
_, err = steveClient.SteveType(longhornVolumeType).Create(volumeSpec)
if err != nil {
return "", fmt.Errorf("failed to create volume: %w", err)
}

t.Logf("Successfully created volume: %s", volumeName)
return volumeName, nil
}

// ValidateVolumeActive validates that a volume is in an active/detached state and ready to use
func ValidateVolumeActive(t *testing.T, lc *LonghornClient, volumeName string) error {
t.Logf("Validating volume %s is active", volumeName)

steveClient, err := lc.Client.Steve.ProxyDownstream(lc.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", charts.LonghornNamespace, volumeName)
volume, err := steveClient.SteveType(longhornVolumeType).ByID(volumeID)
if err != nil {
return false, nil
}

// 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)

t.Logf("Volume %s state: %s, robustness: %s", volumeName, state, robustness)

// Volume is ready when it's in detached state with valid robustness
// "unknown" robustness is expected for detached volumes with no replicas scheduled
if state == "detached" && (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)
}

t.Logf("Volume %s is active and ready to use", volumeName)
return nil
}

// DeleteVolume deletes a Longhorn volume
func DeleteVolume(t *testing.T, lc *LonghornClient, volumeName string) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}

volumeID := fmt.Sprintf("%s/%s", charts.LonghornNamespace, volumeName)
volume, err := steveClient.SteveType(longhornVolumeType).ByID(volumeID)
if err != nil {
return fmt.Errorf("failed to get volume %s: %w", volumeName, err)
}

t.Logf("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
func ValidateNodes(lc *LonghornClient) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}

nodes, err := steveClient.SteveType(longhornNodeType).NamespacedSteveClient(charts.LonghornNamespace).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)
}
}

return nil
}

// ValidateSettings validates that Longhorn settings are properly configured
func ValidateSettings(lc *LonghornClient) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}

settings, err := steveClient.SteveType(longhornSettingType).NamespacedSteveClient(charts.LonghornNamespace).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")
}

return nil
}

// ValidateVolumeInRancherAPI validates that the volume is accessible and in a ready state through Rancher API
func ValidateVolumeInRancherAPI(t *testing.T, lc *LonghornClient, volumeName string) error {
t.Logf("Validating volume %s is accessible through Rancher API", volumeName)

steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client: %w", err)
}

// Get the volume using the Rancher API path
volumeID := fmt.Sprintf("%s/%s", charts.LonghornNamespace, volumeName)
volume, err := steveClient.SteveType(longhornVolumeType).ByID(volumeID)
if err != nil {
return fmt.Errorf("failed to get volume %s through Rancher API: %w", volumeName, err)
}

// Validate volume has status
if volume.Status == nil {
return fmt.Errorf("volume %s has no status in Rancher API", volumeName)
}

statusMap, ok := volume.Status.(map[string]interface{})
if !ok {
return fmt.Errorf("volume %s status is not in expected format", volumeName)
}

state, _ := statusMap["state"].(string)
robustness, _ := statusMap["robustness"].(string)

t.Logf("Volume %s in Rancher API - state: %s, robustness: %s", volumeName, state, robustness)

// Verify volume is in a ready state
if state != "detached" {
return fmt.Errorf("volume %s is not in detached state through Rancher API, current state: %s", volumeName, state)
}

if robustness != "healthy" && robustness != "unknown" {
return fmt.Errorf("volume %s has invalid robustness through Rancher API: %s", volumeName, robustness)
}

t.Logf("Volume %s validated successfully through Rancher API", volumeName)
return nil
}

// ValidateDynamicConfiguration validates Longhorn configuration based on user-provided test config
func ValidateDynamicConfiguration(t *testing.T, lc *LonghornClient, config longhorn.TestConfig) error {
steveClient, err := lc.Client.Steve.ProxyDownstream(lc.ClusterID)
if err != nil {
return fmt.Errorf("failed to get downstream client for dynamic validation: %w", err)
}

// Validate that the configured storage class exists
t.Logf("Validating configured storage class: %s", config.LonghornTestStorageClass)
storageClasses, err := steveClient.SteveType("storage.k8s.io.storageclass").List(nil)
if err != nil {
return fmt.Errorf("failed to list storage classes: %w", err)
}

found := false
for _, sc := range storageClasses.Data {
if sc.Name == config.LonghornTestStorageClass {
found = true
t.Logf("Found configured storage class: %s", config.LonghornTestStorageClass)
break
}
}

if !found {
return fmt.Errorf("configured storage class %s not found", config.LonghornTestStorageClass)
}

// Validate settings exist
settings, err := steveClient.SteveType(longhornSettingType).NamespacedSteveClient(charts.LonghornNamespace).List(nil)
if err != nil {
return fmt.Errorf("failed to list settings: %w", err)
}

t.Logf("Successfully validated Longhorn configuration with %d settings", len(settings.Data))
t.Logf("Validated storage class: %s from test configuration", config.LonghornTestStorageClass)

return nil
}
46 changes: 43 additions & 3 deletions validation/longhorn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ This directory contains tests for interoperability between Rancher and Longhorn.

## Running the tests

This package contains two test suites:
1. `TestLonghornChartTestSuite`: Tests envolving installing Longhorn through Rancher Charts.
This package contains three test suites:

1. `TestLonghornChartTestSuite`: Tests involving installing Longhorn through Rancher Charts.
2. `TestLonghornTestSuite`: Tests that handle various other Longhorn use cases, can be run with a custom pre-installed Longhorn.
3. `TestLonghornUIAccessTestSuite`: Tests that validate Longhorn UI/API access and functionality on downstream clusters.

Additional configuration for both suites can be included in the Cattle Config file as follows:
Additional configuration for all suites can be included in the Cattle Config file as follows:

```yaml
longhorn:
Expand All @@ -17,3 +19,41 @@ longhorn:
```

If no additional configuration is provided, the default project name `longhorn-test` and the storage class `longhorn` are used.

## Longhorn UI Access Test

The `TestLonghornUIAccessTestSuite` validates Longhorn UI and API access on a downstream Rancher cluster. It performs the following checks:

1. **Pod Validation**: Verifies all pods in the `longhorn-system` namespace are in an active/running state
2. **Service Accessibility**: Checks that the Longhorn frontend service is accessible and returns valid HTTP responses
- Supports ClusterIP (via proxy), NodePort, and LoadBalancer service types
3. **Longhorn API Validation**:
- Validates Longhorn nodes are in a valid state
- Validates Longhorn settings are properly configured
- Creates a test volume via the Longhorn API
- Verifies the volume is active through both Longhorn and Rancher APIs
- Validates the volume uses the correct Longhorn storage class

### Test Methods

- `TestLonghornUIAccess`: Static test that validates core functionality without user-provided configuration
- `TestLonghornUIDynamic`: Dynamic test that validates configuration based on user-provided settings in the config file

### Running the UI Access Test

```bash
go test -v -tags "validation" -run TestLonghornUIAccessTestSuite ./validation/longhorn/
```

Or with specific test methods:

```bash
go test -v -tags "validation" -run TestLonghornUIAccessTestSuite/TestLonghornUIAccess ./validation/longhorn/
go test -v -tags "validation" -run TestLonghornUIAccessTestSuite/TestLonghornUIDynamic ./validation/longhorn/
```

### Prerequisites

- Longhorn must be installed on the downstream cluster (either pre-installed or installed by the test suite)
- The cluster must be accessible via Rancher
- The test requires network access to the Longhorn service in the downstream cluster
Loading