Skip to content

Commit f66e7c5

Browse files
authored
chore: add tests for policy (#46)
* feat: support policy data source * chore: support CRUD policy resource * chore: support config sql review rules * chore: update docs * chore: update version * chore: update docs * chore: update docs * chore: update docs * chore: optimize * chore: update * chore: update version * chore: add tests
1 parent e044506 commit f66e7c5

File tree

7 files changed

+385
-7
lines changed

7 files changed

+385
-7
lines changed

provider/data_source_policy.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ var policyParentIdentificationMap = map[string]*schema.Schema{
4646
},
4747
}
4848

49-
// TODO(ed): add test and doc.
5049
func dataSourcePolicy() *schema.Resource {
5150
return &schema.Resource{
5251
Description: "The policy data source.",

provider/data_source_policy_list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/bytebase/terraform-provider-bytebase/api"
1212
)
1313

14-
// TODO(ed): add test and doc.
1514
func dataSourcePolicyList() *schema.Resource {
1615
return &schema.Resource{
1716
Description: "The policy data source list.",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/bytebase/terraform-provider-bytebase/api"
9+
"github.com/bytebase/terraform-provider-bytebase/provider/internal"
10+
)
11+
12+
func TestAccPolicyListDataSource(t *testing.T) {
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
testAccPreCheck(t)
16+
},
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccCheckPolicyDestroy,
19+
Steps: []resource.TestStep{
20+
internal.GetTestStepForDataSourceList(
21+
"",
22+
"",
23+
"bytebase_policy_list",
24+
"before",
25+
"policies",
26+
0,
27+
),
28+
internal.GetTestStepForDataSourceList(
29+
testAccCheckPolicyResource(
30+
"backup_plan",
31+
"dev",
32+
getBackupPlanPolicy(string(api.BackupPlanScheduleDaily), 999),
33+
api.PolicyTypeBackupPlan,
34+
),
35+
"bytebase_policy.backup_plan",
36+
"bytebase_policy_list",
37+
"after",
38+
"policies",
39+
1,
40+
),
41+
},
42+
})
43+
}

provider/data_source_policy_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
10+
"github.com/bytebase/terraform-provider-bytebase/api"
11+
"github.com/bytebase/terraform-provider-bytebase/provider/internal"
12+
)
13+
14+
func TestAccPolicyDataSource(t *testing.T) {
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() {
17+
testAccPreCheck(t)
18+
},
19+
Providers: testAccProviders,
20+
CheckDestroy: testAccCheckPolicyDestroy,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccCheckPolicyDataSource(
24+
testAccCheckPolicyResource(
25+
"backup_plan",
26+
"dev",
27+
getBackupPlanPolicy(string(api.BackupPlanScheduleDaily), 999),
28+
api.PolicyTypeBackupPlan,
29+
),
30+
"backup_plan",
31+
"dev",
32+
"bytebase_policy.backup_plan",
33+
api.PolicyTypeBackupPlan,
34+
),
35+
Check: resource.ComposeTestCheckFunc(
36+
internal.TestCheckResourceExists("data.bytebase_policy.backup_plan"),
37+
resource.TestCheckResourceAttr("data.bytebase_policy.backup_plan", "type", string(api.PolicyTypeBackupPlan)),
38+
resource.TestCheckResourceAttr("data.bytebase_policy.backup_plan", "backup_plan_policy.#", "1"),
39+
resource.TestCheckResourceAttr("data.bytebase_policy.backup_plan", "backup_plan_policy.0.schedule", string(api.BackupPlanScheduleDaily)),
40+
resource.TestCheckResourceAttr("data.bytebase_policy.backup_plan", "backup_plan_policy.0.retention_duration", "999"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func TestAccPolicyDataSource_NotFound(t *testing.T) {
48+
resource.Test(t, resource.TestCase{
49+
PreCheck: func() {
50+
testAccPreCheck(t)
51+
},
52+
Providers: testAccProviders,
53+
CheckDestroy: testAccCheckEnvironmentDestroy,
54+
Steps: []resource.TestStep{
55+
{
56+
Config: testAccCheckPolicyDataSource(
57+
"",
58+
"policy",
59+
"dev",
60+
"",
61+
api.PolicyTypeDeploymentApproval,
62+
),
63+
ExpectError: regexp.MustCompile(fmt.Sprintf("Cannot found policy environments/dev/policies/%s", api.PolicyTypeDeploymentApproval)),
64+
},
65+
},
66+
})
67+
}
68+
69+
func testAccCheckPolicyDataSource(
70+
resource,
71+
identifier,
72+
environment,
73+
dependsOn string,
74+
pType api.PolicyType) string {
75+
return fmt.Sprintf(`
76+
%s
77+
78+
data "bytebase_policy" "%s" {
79+
environment = "%s"
80+
type = "%s"
81+
depends_on = [
82+
%s
83+
]
84+
}
85+
`, resource, identifier, environment, pType, dependsOn)
86+
}

provider/internal/mock_client.go

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func (c *mockClient) ListPolicies(_ context.Context, find *api.PolicyFindMessage
344344
if policy.State == api.Deleted && !find.ShowDeleted {
345345
continue
346346
}
347-
if policy.Name == name {
347+
if name == "policies" || policy.Name == name {
348348
policies = append(policies, policy)
349349
}
350350
}
@@ -365,10 +365,75 @@ func (c *mockClient) GetPolicy(_ context.Context, find *api.PolicyFindMessage) (
365365
return policy, nil
366366
}
367367

368-
// TODO(ed): update the mock and tests
369368
// UpsertPolicy creates or updates the policy.
370-
func (*mockClient) UpsertPolicy(_ context.Context, _ *api.PolicyFindMessage, _ *api.PolicyPatchMessage) (*api.PolicyMessage, error) {
371-
return nil, nil
369+
func (c *mockClient) UpsertPolicy(_ context.Context, find *api.PolicyFindMessage, patch *api.PolicyPatchMessage) (*api.PolicyMessage, error) {
370+
name := getPolicyRequestName(find)
371+
policy, existed := c.policyMap[name]
372+
373+
if !existed {
374+
policy = &api.PolicyMessage{
375+
Name: name,
376+
State: api.Active,
377+
}
378+
}
379+
380+
switch patch.Type {
381+
case api.PolicyTypeAccessControl:
382+
if !existed {
383+
if patch.AccessControlPolicy == nil {
384+
return nil, errors.Errorf("payload is required to create the policy")
385+
}
386+
}
387+
if v := patch.AccessControlPolicy; v != nil {
388+
policy.AccessControlPolicy = v
389+
}
390+
case api.PolicyTypeBackupPlan:
391+
if !existed {
392+
if patch.BackupPlanPolicy == nil {
393+
return nil, errors.Errorf("payload is required to create the policy")
394+
}
395+
}
396+
if v := patch.BackupPlanPolicy; v != nil {
397+
policy.BackupPlanPolicy = v
398+
}
399+
case api.PolicyTypeDeploymentApproval:
400+
if !existed {
401+
if patch.DeploymentApprovalPolicy == nil {
402+
return nil, errors.Errorf("payload is required to create the policy")
403+
}
404+
}
405+
if v := patch.DeploymentApprovalPolicy; v != nil {
406+
policy.DeploymentApprovalPolicy = v
407+
}
408+
case api.PolicyTypeSQLReview:
409+
if !existed {
410+
if patch.SQLReviewPolicy == nil {
411+
return nil, errors.Errorf("payload is required to create the policy")
412+
}
413+
}
414+
if v := patch.SQLReviewPolicy; v != nil {
415+
policy.SQLReviewPolicy = v
416+
}
417+
case api.PolicyTypeSensitiveData:
418+
if !existed {
419+
if patch.SensitiveDataPolicy == nil {
420+
return nil, errors.Errorf("payload is required to create the policy")
421+
}
422+
}
423+
if v := patch.SensitiveDataPolicy; v != nil {
424+
policy.SensitiveDataPolicy = v
425+
}
426+
default:
427+
return nil, errors.Errorf("invalid policy type %v", patch.Type)
428+
}
429+
430+
if v := patch.InheritFromParent; v != nil {
431+
policy.InheritFromParent = *v
432+
}
433+
434+
c.policyMap[name] = policy
435+
436+
return policy, nil
372437
}
373438

374439
// DeletePolicy deletes the policy.

provider/resource_policy.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/bytebase/terraform-provider-bytebase/provider/internal"
1515
)
1616

17-
// TODO(ed): add test and doc.
1817
func resourcePolicy() *schema.Resource {
1918
return &schema.Resource{
2019
Description: "The policy resource.",

0 commit comments

Comments
 (0)