Skip to content

Commit fc50fdc

Browse files
committed
clean up
1 parent 92821c1 commit fc50fdc

File tree

11 files changed

+649
-39
lines changed

11 files changed

+649
-39
lines changed

.github/workflows/acceptance-test.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ permissions:
4343
jobs:
4444
detect-changes:
4545
name: Detect Changes
46-
runs-on: ubuntu-latest
46+
runs-on: ubuntu-slim
4747
outputs:
4848
source-changed: ${{ steps.filter.outputs.source }}
4949
steps:
@@ -64,7 +64,7 @@ jobs:
6464
name: Build & Acceptance Test
6565
needs: detect-changes
6666
if: ${{ needs.detect-changes.outputs.source-changed == 'true' || github.event_name == 'workflow_dispatch' }}
67-
runs-on: ubuntu-latest
67+
runs-on: ubuntu-slim
6868
timeout-minutes: 30
6969

7070
env:
@@ -88,8 +88,6 @@ jobs:
8888

8989
- name: Run Acceptance Tests
9090
env:
91-
# For push to main: run all tests
92-
# For manual dispatch: run_all=true runs all, otherwise use individual toggles
9391
ORY_KETO_TESTS_ENABLED: ${{ github.event_name == 'push' || github.event.inputs.run_all == 'true' || github.event.inputs.enable_keto_tests == 'true' }}
9492
ORY_B2B_ENABLED: ${{ github.event_name == 'push' || github.event.inputs.run_all == 'true' || github.event.inputs.enable_b2b_tests == 'true' }}
9593
ORY_SOCIAL_PROVIDER_TESTS_ENABLED: ${{ github.event_name == 'push' || github.event.inputs.run_all == 'true' || github.event.inputs.enable_social_provider_tests == 'true' }}

.github/workflows/unit-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ permissions:
1313
jobs:
1414
detect-changes:
1515
name: Detect Changes
16-
runs-on: ubuntu-latest
16+
runs-on: ubuntu-slim
1717
outputs:
1818
source-changed: ${{ steps.filter.outputs.source }}
1919
steps:
@@ -34,7 +34,7 @@ jobs:
3434
name: Build & Unit Test
3535
needs: detect-changes
3636
if: ${{ needs.detect-changes.outputs.source-changed == 'true' }}
37-
runs-on: ubuntu-latest
37+
runs-on: ubuntu-slim
3838

3939
steps:
4040
- name: Checkout

CONTRIBUTING.md

Lines changed: 177 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,190 @@ make build
2828
make install
2929
```
3030

31-
### Running Tests
31+
## Testing
32+
33+
### Test Types
34+
35+
The provider has two types of tests:
36+
37+
1. **Unit Tests** - Fast, isolated tests that don't require API access
38+
2. **Acceptance Tests** - Integration tests that create real resources in Ory Network
39+
40+
### Unit Tests
41+
42+
Unit tests can be run without any credentials:
43+
44+
```bash
45+
make test # Run all unit tests
46+
make test-short # Run unit tests in short mode
47+
```
48+
49+
### Acceptance Tests
3250

3351
Acceptance tests are **self-contained** - they automatically create a temporary Ory project, run tests against it, and clean up when done.
3452

53+
#### Required Environment Variables
54+
3555
```bash
36-
# Unit tests (no credentials needed)
37-
make test
56+
export ORY_WORKSPACE_API_KEY="ory_wak_..." # Workspace API key
57+
export ORY_WORKSPACE_ID="..." # Workspace ID
58+
```
3859

39-
# Acceptance tests (requires Ory credentials)
40-
export ORY_WORKSPACE_API_KEY="ory_wak_..."
41-
export ORY_WORKSPACE_ID="..."
60+
#### Running Acceptance Tests
61+
62+
```bash
63+
# Standard acceptance tests
4264
make test-acc
4365

44-
# Acceptance tests with debug logging
66+
# With debug logging
4567
make test-acc-verbose
4668

4769
# Run all tests with all features enabled
4870
make test-acc-all
71+
72+
# Run specific resource tests
73+
ORY_KETO_TESTS_ENABLED=true make test-acc-keto
4974
```
5075

5176
#### Optional Feature Flags
5277

5378
Some tests require specific Ory plan features. Enable them with environment variables:
5479

80+
| Environment Variable | Description |
81+
|---------------------|-------------|
82+
| `ORY_KETO_TESTS_ENABLED=true` | Run relationship/Keto tests |
83+
| `ORY_B2B_ENABLED=true` | Run B2B/organization tests (requires B2B plan) |
84+
| `ORY_SOCIAL_PROVIDER_TESTS_ENABLED=true` | Run social provider tests |
85+
| `ORY_SCHEMA_TESTS_ENABLED=true` | Run identity schema tests |
86+
| `ORY_PROJECT_TESTS_ENABLED=true` | Run project creation/deletion tests |
87+
88+
#### API URL Overrides (for local development)
89+
5590
```bash
56-
# Run relationship/Keto tests
57-
ORY_KETO_TESTS_ENABLED=true make test-acc
91+
export ORY_CONSOLE_API_URL="https://api.console.ory.sh" # Console API
92+
export ORY_PROJECT_API_URL="https://%s.projects.oryapis.com" # Project API template
93+
```
5894

59-
# Run B2B/organization tests (requires B2B plan)
60-
ORY_B2B_ENABLED=true make test-acc
95+
### Cleaning Up Dangling Resources
6196

62-
# Run social provider tests
63-
ORY_SOCIAL_PROVIDER_TESTS_ENABLED=true make test-acc
97+
If tests fail and leave resources behind, use the sweepers to clean up:
98+
99+
```bash
100+
# Sweep all test resources
101+
go test -v ./internal/acctest -sweep=all -timeout 30m
102+
103+
# Sweep specific resource types
104+
go test -v ./internal/acctest -sweep=ory_oauth2_client -timeout 30m
105+
106+
# Enable project deletion (use with caution!)
107+
ORY_SWEEP_PROJECTS=true go test -v ./internal/acctest -sweep=ory_project -timeout 30m
108+
```
109+
110+
Sweepers only delete resources with names starting with `tf-acc-test` or `Test`.
111+
112+
### Writing Acceptance Tests
113+
114+
Follow these guidelines when writing acceptance tests:
115+
116+
#### 1. Use the Test Utilities
117+
118+
```go
119+
//go:build acceptance
120+
121+
package myresource_test
122+
123+
import (
124+
"testing"
125+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
126+
"github.com/ory/terraform-provider-orynetwork/internal/acctest"
127+
)
128+
129+
func TestAccMyResource_basic(t *testing.T) {
130+
// Use acctest.RunTest for consistent test execution
131+
acctest.RunTest(t, resource.TestCase{
132+
PreCheck: func() { acctest.AccPreCheck(t) },
133+
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories(),
134+
CheckDestroy: acctest.CheckDestroy("ory_myresource", myResourceExists),
135+
Steps: []resource.TestStep{
136+
// Create and Read
137+
{
138+
Config: testAccMyResourceConfig(),
139+
Check: resource.ComposeAggregateTestCheckFunc(
140+
resource.TestCheckResourceAttrSet("ory_myresource.test", "id"),
141+
),
142+
},
143+
// ImportState
144+
{
145+
ResourceName: "ory_myresource.test",
146+
ImportState: true,
147+
ImportStateVerify: true,
148+
},
149+
},
150+
})
151+
}
152+
```
153+
154+
#### 2. Implement CheckDestroy
155+
156+
Every acceptance test should verify resources are properly deleted:
157+
158+
```go
159+
// Use existing check functions
160+
CheckDestroy: acctest.CheckDestroy("ory_identity", acctest.IdentityExists),
161+
CheckDestroy: acctest.CheckDestroy("ory_oauth2_client", acctest.OAuth2ClientExists),
162+
CheckDestroy: acctest.CheckDestroy("ory_organization", acctest.OrganizationExists),
163+
CheckDestroy: acctest.CheckDestroy("ory_project", acctest.ProjectExists),
164+
165+
// Or implement a custom existence check
166+
func myResourceExists(ctx context.Context, id string) (bool, error) {
167+
c, err := acctest.GetOryClient()
168+
if err != nil {
169+
return false, err
170+
}
171+
_, err = c.GetMyResource(ctx, id)
172+
if err != nil {
173+
if strings.Contains(err.Error(), "404") {
174+
return false, nil
175+
}
176+
return false, err
177+
}
178+
return true, nil
179+
}
180+
```
181+
182+
#### 3. Test Configuration Best Practices
183+
184+
- Use `fmt.Sprintf()` for variable injection in HCL configs
185+
- Prefix test resource names with `tf-acc-test` or `Test` for sweeper compatibility
186+
- Include the `provider "ory" {}` declaration in each config
187+
- Test create, read, update, import, and delete operations
188+
189+
```go
190+
func testAccMyResourceConfig(name string) string {
191+
return fmt.Sprintf(`
192+
provider "ory" {}
193+
194+
resource "ory_myresource" "test" {
195+
name = %[1]q
196+
}
197+
`, name)
198+
}
199+
```
200+
201+
#### 4. Feature-Gated Tests
202+
203+
For tests requiring specific Ory plan features:
204+
205+
```go
206+
func TestAccOrganizationResource_basic(t *testing.T) {
207+
acctest.RequireB2BTests(t) // Skips if ORY_B2B_ENABLED != "true"
208+
// ... test implementation
209+
}
210+
211+
func TestAccRelationshipResource_basic(t *testing.T) {
212+
acctest.RequireKetoTests(t) // Skips if ORY_KETO_TESTS_ENABLED != "true"
213+
// ... test implementation
214+
}
64215
```
65216

66217
### Using Local Provider
@@ -93,7 +244,18 @@ provider_installation {
93244
3. Register the resource in `internal/provider/provider.go`
94245
4. Add documentation in `docs/resources/`
95246
5. Add examples in `examples/resources/`
96-
6. Write acceptance tests
247+
6. Write acceptance tests with CheckDestroy
248+
249+
### Resource Contribution Checklist
250+
251+
- [ ] Resource implements all CRUD operations
252+
- [ ] Resource supports import via `ImportState()`
253+
- [ ] Acceptance tests cover create, read, update, import, delete
254+
- [ ] Tests use `acctest.CheckDestroy()` to verify cleanup
255+
- [ ] Tests use `acctest.RunTest()` for consistent test execution
256+
- [ ] Documentation added to `docs/resources/`
257+
- [ ] Examples added to `examples/resources/`
258+
- [ ] Code passes `make lint` and `make fmt`
97259

98260
### Code Style
99261

@@ -110,7 +272,7 @@ Use clear, descriptive commit messages:
110272
Add ory_foo resource for managing foos
111273
112274
- Implement CRUD operations
113-
- Add acceptance tests
275+
- Add acceptance tests with CheckDestroy
114276
- Add documentation
115277
```
116278

examples/resources/ory_social_provider/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ resource "ory_social_provider" "microsoft" {
2323
scopes = ["openid", "profile", "email"]
2424
}
2525

26-
# Generic OIDC Provider (e.g., Okta, Auth0)
26+
# Generic OIDC Provider
2727
resource "ory_social_provider" "corporate_sso" {
2828
provider_id = "generic"
2929
provider_label = "Corporate SSO"

internal/acctest/acctest.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/hashicorp/terraform-plugin-framework/providerserver"
1313
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1415
ory "github.com/ory/client-go"
1516

1617
"github.com/ory/terraform-provider-orynetwork/internal/client"
@@ -251,11 +252,29 @@ func getOryClient() (*client.OryClient, error) {
251252
ProjectAPIURL: projectURL,
252253
}
253254

255+
// Also set project credentials if available
256+
if sharedTestProject != nil {
257+
cfg.ProjectAPIKey = sharedTestProject.APIKey
258+
cfg.ProjectSlug = sharedTestProject.Slug
259+
cfg.ProjectID = sharedTestProject.ID
260+
} else {
261+
// Fall back to environment variables
262+
cfg.ProjectAPIKey = os.Getenv("ORY_PROJECT_API_KEY")
263+
cfg.ProjectSlug = os.Getenv("ORY_PROJECT_SLUG")
264+
cfg.ProjectID = os.Getenv("ORY_PROJECT_ID")
265+
}
266+
254267
var err error
255268
oryClient, err = client.NewOryClient(cfg)
256269
return oryClient, err
257270
}
258271

272+
// GetOryClient returns a shared Ory client for use in tests.
273+
// This is an exported version for use in custom CheckDestroy functions.
274+
func GetOryClient() (*client.OryClient, error) {
275+
return getOryClient()
276+
}
277+
259278
// SkipIfFeatureDisabled skips the test if the specified feature flag is not set to "true".
260279
func SkipIfFeatureDisabled(t *testing.T, envVar, featureName string) {
261280
t.Helper()
@@ -293,3 +312,11 @@ func RequireProjectTests(t *testing.T) {
293312
t.Helper()
294313
SkipIfFeatureDisabled(t, "ORY_PROJECT_TESTS_ENABLED", "project")
295314
}
315+
316+
// RunTest runs an acceptance test.
317+
// This is a convenience wrapper around resource.Test() that follows
318+
// provider conventions and can be extended in the future.
319+
func RunTest(t *testing.T, tc resource.TestCase) {
320+
t.Helper()
321+
resource.Test(t, tc)
322+
}

0 commit comments

Comments
 (0)