Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Lint

on:
pull_request:
branches: [ main, master ]

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.24'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=5m

- name: Run go fmt
run: |
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
echo "The following files are not formatted:"
gofmt -s -l .
echo "Please run 'go fmt ./...' to format your code"
exit 1
fi

- name: Run go vet
run: go vet ./...

- name: Run go mod tidy check
run: |
go mod tidy
if [ -n "$(git status --porcelain go.mod go.sum)" ]; then
echo "go.mod or go.sum is not tidy"
git diff go.mod go.sum
exit 1
fi

- name: Check for unused dependencies
run: |
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run --enable unused
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Tests

on:
pull_request:
branches: [ main, master ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.24'

- name: Run tests
run: go test ./...
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ kubectl oadp version
- **Permission-aware**: Works within the permissions of the current user/service account
- **Integration with OADP**: Leverages the underlying Velero infrastructure managed by OADP operator

## Testing

This project includes comprehensive CLI integration tests organized by functionality.

### Quick Test Commands

```bash
# Run all tests (standard Go pattern)
go test ./...
```

📖 **For detailed test documentation, see [tests/README.md](tests/README.md)**

## Development

This CLI is built using:
Expand Down
10 changes: 1 addition & 9 deletions cmd/non-admin/backup.go → cmd/non-admin/backup/backup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nonadmin
package backup

/*
Copyright 2017 the Velero contributors.
Expand All @@ -22,14 +22,6 @@ import (
"github.com/vmware-tanzu/velero/pkg/client"
)

// NewNonAdminFactory creates a client factory for NonAdminBackup operations
// that uses the current kubeconfig context namespace instead of hardcoded openshift-adp
func NewNonAdminFactory() client.Factory {
// Don't set a default namespace, let it use the kubeconfig context
cfg := client.VeleroConfig{}
return client.NewFactory("oadp-nonadmin-cli", cfg)
}

// NewBackupCommand creates the "backup" subcommand under nonadmin
func NewBackupCommand(f client.Factory) *cobra.Command {
c := &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/non-admin/create.go → cmd/non-admin/backup/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nonadmin
package backup

/*
Copyright The Velero Contributors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package nonadmin
package backup

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
11 changes: 10 additions & 1 deletion cmd/non-admin/nonadmin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nonadmin

import (
"github.com/migtools/oadp-cli/cmd/non-admin/backup"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/velero/pkg/client"
)
Expand All @@ -14,7 +15,15 @@ func NewNonAdminCommand(f client.Factory) *cobra.Command {
}

// Add backup subcommand
c.AddCommand(NewBackupCommand(f))
c.AddCommand(backup.NewBackupCommand(f))

return c
}

// NewNonAdminFactory creates a client factory for NonAdminBackup operations
// that uses the current kubeconfig context namespace instead of hardcoded openshift-adp
func NewNonAdminFactory() client.Factory {
// Don't set a default namespace, let it use the kubeconfig context
cfg := client.VeleroConfig{}
return client.NewFactory("oadp-nonadmin-cli", cfg)
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

nonadmin "github.com/joeavaikath/sample-plugin/cmd/non-admin"
nonadmin "github.com/migtools/oadp-cli/cmd/non-admin"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/velero/pkg/cmd/cli/backup"
"github.com/vmware-tanzu/velero/pkg/cmd/cli/restore"
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/joeavaikath/sample-plugin
module github.com/migtools/oadp-cli

go 1.24.3
go 1.24.0

toolchain go1.24.3

require (
github.com/migtools/oadp-non-admin v0.0.0-20250505165924-a9be4321819c
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/joeavaikath/sample-plugin/cmd"
"github.com/migtools/oadp-cli/cmd"
)

func main() {
Expand Down
143 changes: 143 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# OADP CLI Tests

This directory contains organized integration tests for the OADP CLI.

## Test Structure

### Core Test Files

- **`help_test.go`** - 🏆 **Help command tests** (baseline functionality)
- Tests all `--help` and `-h` commands across all paths
- Verifies expected help text appears
- Core functionality that must always work

- **`build_test.go`** - Binary building and execution tests
- Tests that binary can be built successfully
- Smoke tests for basic command execution
- Version and basic functionality tests

### Supporting Files

- **`common.go`** - Shared test utilities and helper functions
- **`main_test.go`** - Test setup and teardown
- **`go.mod`** - Module configuration

## Running Tests

### Run All Tests
```bash
# Standard Go command - runs all tests in project
go test ./...

# Run just the CLI tests
go test -v -timeout 60s ./tests

# From tests directory
cd tests && go test -v -timeout 60s
```

### Run Specific Test Categories
```bash
# From project root
go test -v ./tests -run TestCLIHelp # Help command tests
go test -v ./tests -run TestCLIBinary # Build and smoke tests

# From tests directory
go test -v -run TestCLIHelp # Help command tests
go test -v -run TestCLIBinary # Build and smoke tests
```

### Quick Test Run
```bash
# Standard Go pattern - finds all tests
go test ./...

# Just CLI tests
go test ./tests

# With output
go test -v ./tests
```

## Test Categories

### 🏆 Help Tests (`help_test.go`)
These are the **core functionality tests** - verify all help commands work:
- `TestCLIHelpCommands` - All help commands work across all paths
- `TestCLIHelpFlags` - Both `--help` and `-h` work consistently

### 🔧 Build Tests (`build_test.go`)
Tests that verify the binary itself works:
- `TestCLIBinaryBuild` - Binary builds and executes
- `TestCLIBinaryVersion` - Version command works
- `TestCLIBinarySmoke` - Basic commands don't crash

## Test Configuration

- **Build timeout**: 30 seconds
- **Test timeout**: 10 seconds per test
- **Binary name**: `oadp-test` (temporary)
- **Tests local code**: Whatever is currently on disk (including uncommitted changes)

## Adding New Tests

### Adding to Existing Categories
Add new test cases to the appropriate file:

```go
// In help_test.go for new help commands
{
name: "new command help",
args: []string{"new", "command", "--help"},
expectContains: []string{
"Description of new command",
},
},
```

### Creating New Test Categories
1. Create a new `*_test.go` file
2. Import the `tests` package
3. Use helper functions from `common.go`
4. Follow existing patterns

Example:
```go
package tests

import "testing"

func TestCLINewFeature(t *testing.T) {
binaryPath := buildCLIBinary(t)
defer cleanup(t, binaryPath)

// Your test logic here
}
```

## Troubleshooting

### Build Failures
- Check that all dependencies in `../go.mod` are available
- Verify you're running from the correct directory
- Check that parent directory has valid Go module

### Test Failures
- Check expected strings match actual CLI output
- Use `-v` flag to see detailed test output
- Look at the full command output in logs

### Common Commands
```bash
# Run with verbose output
go test -v ./tests

# Run with race detection
go test -race ./tests

# Run specific test function
go test -v ./tests -run TestCLIHelpCommands

# Run tests with coverage
go test -cover ./tests
```
Loading
Loading