|
| 1 | +# Gitea Integration for E2E Tests |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The e2e test suite uses an in-cluster Gitea instance to provide git repository functionality. This eliminates external dependencies on GitHub and provides complete isolation for testing. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +- **Gitea Installation**: Deployed via Helm during cluster setup |
| 10 | +- **Network Access**: NodePort on port 30000 (HTTP) and 30022 (SSH) |
| 11 | +- **Service Discovery**: ConfigMap in kube-public namespace contains endpoint |
| 12 | +- **Authentication**: Admin user (giteaadmin/giteapass) for test operations |
| 13 | +- **Provider Abstraction**: Tests use `RepositoryProvider` interface, allowing easy switching between Gitea/GitHub/GitLab |
| 14 | + |
| 15 | +## Using Repository Provider in Tests |
| 16 | + |
| 17 | +### Basic Pattern |
| 18 | + |
| 19 | +```go |
| 20 | +var ( |
| 21 | + repoURL string |
| 22 | + repoDir string |
| 23 | +) |
| 24 | + |
| 25 | +BeforeEach(func() { |
| 26 | + var err error |
| 27 | + |
| 28 | + // Create repository provider resources with automatic cleanup |
| 29 | + username, password, _, cleanup, err := repoProvider.CreateRandomUser() |
| 30 | + Expect(err).NotTo(HaveOccurred()) |
| 31 | + DeferCleanup(cleanup) |
| 32 | + |
| 33 | + _, repoURL, cleanup, err = repoProvider.CreateRandomRepo(username, false) |
| 34 | + Expect(err).NotTo(HaveOccurred()) |
| 35 | + DeferCleanup(cleanup) |
| 36 | + |
| 37 | + // Initialize with function code |
| 38 | + repoDir, err = InitializeRepoWithFunction(repoURL, username, password, "go") |
| 39 | + Expect(err).NotTo(HaveOccurred()) |
| 40 | + DeferCleanup(os.RemoveAll, repoDir) |
| 41 | +}) |
| 42 | +``` |
| 43 | + |
| 44 | +### Available Helper Methods |
| 45 | + |
| 46 | +**RepositoryProvider Interface:** |
| 47 | +- `CreateUser(username, password, email string) (cleanup func(), err error)` - Create user with cleanup function |
| 48 | +- `CreateRandomUser() (username, password, email string, cleanup func(), err error)` - Create user with random credentials |
| 49 | +- `CreateRepo(owner, name string, private bool) (url string, cleanup func(), err error)` - Create repository |
| 50 | +- `CreateRandomRepo(owner string, private bool) (name, url string, cleanup func(), err error)` - Create repo with random name |
| 51 | +- `CreateAccessToken(username, password, tokenName string) (string, error)` - Generate access token |
| 52 | + |
| 53 | +**E2E Helper Functions:** |
| 54 | +- `InitializeRepoWithFunction(url, user, pass, lang)` - Clone, init function, push |
| 55 | +- `CommitAndPush(repoDir, msg, file, ...otherFiles)` - Commit and push files |
| 56 | + |
| 57 | +### DeferCleanup Pattern |
| 58 | + |
| 59 | +All Create methods return cleanup functions that can be used with Ginkgo's `DeferCleanup`: |
| 60 | + |
| 61 | +```go |
| 62 | +username, password, _, cleanup, err := repoProvider.CreateRandomUser() |
| 63 | +Expect(err).NotTo(HaveOccurred()) |
| 64 | +DeferCleanup(cleanup) // Automatically deletes user after test |
| 65 | +``` |
| 66 | + |
| 67 | +This ensures resources are cleaned up even if tests fail. |
| 68 | + |
| 69 | +## Accessing Gitea UI |
| 70 | + |
| 71 | +During development, you can access the Gitea web UI: |
| 72 | + |
| 73 | +1. Get the Gitea endpoint: |
| 74 | + ```bash |
| 75 | + kubectl get configmap gitea-endpoint -n kube-public -o jsonpath='{.data.http}' |
| 76 | + ``` |
| 77 | + |
| 78 | +2. Open in browser and login: |
| 79 | + - Username: `giteaadmin` |
| 80 | + - Password: `giteapass` |
| 81 | + |
| 82 | +## Testing Private Repositories |
| 83 | + |
| 84 | +```go |
| 85 | +// Create private repo |
| 86 | +_, repoURL, cleanup, err := repoProvider.CreateRandomRepo(username, true) |
| 87 | +Expect(err).NotTo(HaveOccurred()) |
| 88 | +DeferCleanup(cleanup) |
| 89 | + |
| 90 | +// Create access token |
| 91 | +token, err := repoProvider.CreateAccessToken(username, password, "test-token") |
| 92 | + |
| 93 | +// Use token in Function spec |
| 94 | +Spec: functionsdevv1alpha1.FunctionSpec{ |
| 95 | + Source: functionsdevv1alpha1.FunctionSpecSource{ |
| 96 | + RepositoryURL: repoURL, |
| 97 | + Credentials: &functionsdevv1alpha1.Credentials{ |
| 98 | + Token: token, |
| 99 | + }, |
| 100 | + }, |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Bundle Test Pattern |
| 105 | + |
| 106 | +Bundle tests use the `TestNamespace` struct for better organization: |
| 107 | + |
| 108 | +```go |
| 109 | +type TestNamespace struct { |
| 110 | + Name string |
| 111 | + RepoURL string |
| 112 | +} |
| 113 | + |
| 114 | +func createNamespaceAndDeployFunction() TestNamespace { |
| 115 | + // Creates namespace and deploys function |
| 116 | + // Returns TestNamespace with both namespace and repo URL |
| 117 | +} |
| 118 | + |
| 119 | +// Usage |
| 120 | +testNs := createNamespaceAndDeployFunction() |
| 121 | +CreateFunctionAndWaitForReady(testNs) |
| 122 | +``` |
| 123 | + |
| 124 | +## Troubleshooting |
| 125 | + |
| 126 | +### Gitea not accessible |
| 127 | + |
| 128 | +```bash |
| 129 | +kubectl get pods -n gitea |
| 130 | +kubectl logs -n gitea deployment/gitea |
| 131 | +``` |
| 132 | + |
| 133 | +### ConfigMap missing |
| 134 | + |
| 135 | +```bash |
| 136 | +kubectl get configmap -n kube-public |
| 137 | +./hack/create-kind-cluster.sh # Recreate cluster |
| 138 | +``` |
| 139 | + |
| 140 | +### Test failures with git operations |
| 141 | + |
| 142 | +- Check Gitea pod is running |
| 143 | +- Verify network connectivity to NodePort: |
| 144 | + ```bash |
| 145 | + GITEA_IP=$(kubectl get configmap gitea-endpoint -n kube-public -o jsonpath='{.data.http}') |
| 146 | + curl $GITEA_IP |
| 147 | + ``` |
| 148 | +- Check git credentials in test |
| 149 | + |
| 150 | +### Repository cleanup issues |
| 151 | + |
| 152 | +If tests leave behind repositories, you can clean them manually: |
| 153 | + |
| 154 | +```bash |
| 155 | +# List all users |
| 156 | +curl -u giteaadmin:giteapass http://<gitea-endpoint>/api/v1/admin/users |
| 157 | + |
| 158 | +# Delete user (includes their repos) |
| 159 | +curl -X DELETE -u giteaadmin:giteapass http://<gitea-endpoint>/api/v1/admin/users/<username> |
| 160 | +``` |
| 161 | + |
| 162 | +## Implementation Details |
| 163 | + |
| 164 | +### Cluster Setup |
| 165 | + |
| 166 | +Gitea is installed during cluster creation in `hack/create-kind-cluster.sh`: |
| 167 | + |
| 168 | +```bash |
| 169 | +helm install gitea gitea-charts/gitea --namespace gitea --create-namespace \ |
| 170 | + --set service.http.type=NodePort \ |
| 171 | + --set service.http.nodePort=30000 \ |
| 172 | + --set gitea.admin.username=giteaadmin \ |
| 173 | + --set gitea.admin.password=giteapass \ |
| 174 | + --set persistence.enabled=false |
| 175 | +``` |
| 176 | + |
| 177 | +### Service Discovery |
| 178 | + |
| 179 | +The cluster setup creates a ConfigMap with the Gitea endpoint: |
| 180 | + |
| 181 | +```yaml |
| 182 | +apiVersion: v1 |
| 183 | +kind: ConfigMap |
| 184 | +metadata: |
| 185 | + name: gitea-endpoint |
| 186 | + namespace: kube-public |
| 187 | +data: |
| 188 | + http: "http://<node-ip>:30000" |
| 189 | + ssh: "<node-ip>:30022" |
| 190 | +``` |
| 191 | +
|
| 192 | +Tests read this ConfigMap to discover where Gitea is running. |
| 193 | +
|
| 194 | +### Networking |
| 195 | +
|
| 196 | +- **Kind Node IP**: Retrieved via Docker inspect of control-plane node |
| 197 | +- **NodePort**: Fixed ports 30000 (HTTP) and 30022 (SSH) |
| 198 | +- **Accessibility**: Node IP is reachable from both host machine and cluster pods |
| 199 | +
|
| 200 | +This allows the same repository URL to work in both test code (running on host) and Function resources (running in cluster). |
0 commit comments