Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Tasks v1 readiness - SDK part #3086

Merged
merged 13 commits into from
Sep 23, 2024
7 changes: 7 additions & 0 deletions pkg/acceptance/bettertestspoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,10 @@ func (w *WarehouseDatasourceShowOutputAssert) IsEmpty() {
- support the rest of attribute types in config model builders (TODO left in `config/model/gen/model.go`)
- parametrize test client helper used - integration versus acceptance tests - this has to be changed in the generator too (TODO left in `assert/objectassert/user_snowflake_ext.go`)
- Omit computed fields in the model (like FullyQualifiedName), because it doesn't make sense to set them
- There's an error when generating models, steps to reproduce:
- Go to view resource code and change `data_metric_function` field to `testing` and make it required
- During the generation, the following error appears: mixed named and unnamed parameters.
It's a golang error indicating that the parameter has both unnamed and named parameters in function (e.g. `func(abc string, int)`).
The error is a result of both things:
1. Lists of objects are partially generated, and only parameter name is generated in some functions (the type has to be added manually).
2. `testing` is a package name that makes Go think that we want to have unnamed parameter there, but we just didn't generate the type for that field in the function argument.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ var allStructs = []SdkObjectDef{
ObjectType: sdk.ObjectTypeAuthenticationPolicy,
ObjectStruct: sdk.AuthenticationPolicy{},
},
{
IdType: "sdk.SchemaObjectIdentifier",
ObjectType: sdk.ObjectTypeTask,
ObjectStruct: sdk.Task{},
},
}

func GetSdkObjectDetails() []genhelpers.SdkObjectDetails {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package objectassert

import (
"errors"
"fmt"
"reflect"
"slices"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

func (w *TaskAssert) HasNotEmptyCreatedOn() *TaskAssert {
w.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if o.CreatedOn == "" {
return fmt.Errorf("expected created on not empty; got: %v", o.CreatedOn)
}
return nil
})
return w
}

func (w *TaskAssert) HasNotEmptyId() *TaskAssert {
w.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if o.Id == "" {
return fmt.Errorf("expected id not empty; got: %v", o.CreatedOn)
}
return nil
})
return w
}

func (w *TaskAssert) HasPredecessors(ids ...sdk.SchemaObjectIdentifier) *TaskAssert {
w.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if len(o.Predecessors) != len(ids) {
return fmt.Errorf("expected %d (%v) predecessors, got %d (%v)", len(ids), ids, len(o.Predecessors), o.Predecessors)
}
var errs []error
for _, id := range ids {
if !slices.ContainsFunc(o.Predecessors, func(predecessorId sdk.SchemaObjectIdentifier) bool {
return predecessorId.FullyQualifiedName() == id.FullyQualifiedName()
}) {
errs = append(errs, fmt.Errorf("expected id: %s, to be in the list of predecessors: %v", id.FullyQualifiedName(), o.Predecessors))
}
}
return errors.Join(errs...)
})
return w
}

func (t *TaskAssert) HasTaskRelations(expected sdk.TaskRelations) *TaskAssert {
t.AddAssertion(func(t *testing.T, o *sdk.Task) error {
t.Helper()
if slices.EqualFunc(o.TaskRelations.Predecessors, expected.Predecessors, func(id sdk.SchemaObjectIdentifier, id2 sdk.SchemaObjectIdentifier) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about a different order?

Copy link
Collaborator Author

@sfc-gh-jcieslak sfc-gh-jcieslak Sep 20, 2024

Choose a reason for hiding this comment

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

Doesn't really matter, we only care are about expected ids being there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, but they can be returned in a different order. Please use ElementsMatch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure how testify package works with our package, so I rewrote checks to be similar to the assert above (it asserts predecessors, but in the Predecessors field).

return id.FullyQualifiedName() == id2.FullyQualifiedName()
}) {
return fmt.Errorf("expected task predecessors: %v; got: %v", expected.Predecessors, o.TaskRelations.Predecessors)
}

if !reflect.DeepEqual(expected.FinalizerTask, o.TaskRelations.FinalizerTask) {
return fmt.Errorf("expected finalizer task: %v; got: %v", expected.FinalizerTask, o.TaskRelations.FinalizerTask)
}
return nil
})
return t
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions pkg/acceptance/helpers/task_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ func (c *TaskClient) client() sdk.Tasks {
func (c *TaskClient) defaultCreateTaskRequest(t *testing.T) *sdk.CreateTaskRequest {
t.Helper()
id := c.ids.RandomSchemaObjectIdentifier()
warehouseReq := sdk.NewCreateTaskWarehouseRequest().WithWarehouse(sdk.Pointer(c.ids.WarehouseId()))
return sdk.NewCreateTaskRequest(id, "SELECT CURRENT_TIMESTAMP").WithWarehouse(warehouseReq)
warehouseReq := sdk.NewCreateTaskWarehouseRequest().WithWarehouse(c.ids.WarehouseId())
return sdk.NewCreateTaskRequest(id, "SELECT CURRENT_TIMESTAMP").WithWarehouse(*warehouseReq)
}

func (c *TaskClient) CreateTask(t *testing.T) (*sdk.Task, func()) {
func (c *TaskClient) Create(t *testing.T) (*sdk.Task, func()) {
t.Helper()
return c.CreateTaskWithRequest(t, c.defaultCreateTaskRequest(t).WithSchedule(sdk.String("60 minutes")))
return c.CreateWithRequest(t, c.defaultCreateTaskRequest(t))
}

func (c *TaskClient) CreateTaskWithAfter(t *testing.T, taskId sdk.SchemaObjectIdentifier) (*sdk.Task, func()) {
func (c *TaskClient) CreateWithAfter(t *testing.T, after ...sdk.SchemaObjectIdentifier) (*sdk.Task, func()) {
t.Helper()
return c.CreateTaskWithRequest(t, c.defaultCreateTaskRequest(t).WithAfter([]sdk.SchemaObjectIdentifier{taskId}))
return c.CreateWithRequest(t, c.defaultCreateTaskRequest(t).WithAfter(after))
}

func (c *TaskClient) CreateTaskWithRequest(t *testing.T, request *sdk.CreateTaskRequest) (*sdk.Task, func()) {
func (c *TaskClient) CreateWithRequest(t *testing.T, request *sdk.CreateTaskRequest) (*sdk.Task, func()) {
t.Helper()
ctx := context.Background()

Expand All @@ -61,7 +61,14 @@ func (c *TaskClient) DropTaskFunc(t *testing.T, id sdk.SchemaObjectIdentifier) f
ctx := context.Background()

return func() {
err := c.client().Drop(ctx, sdk.NewDropTaskRequest(id).WithIfExists(sdk.Bool(true)))
err := c.client().Drop(ctx, sdk.NewDropTaskRequest(id).WithIfExists(true))
require.NoError(t, err)
}
}

func (c *TaskClient) Show(t *testing.T, id sdk.SchemaObjectIdentifier) (*sdk.Task, error) {
t.Helper()
ctx := context.Background()

return c.client().ShowByID(ctx, id)
}
2 changes: 1 addition & 1 deletion pkg/datasources/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ReadTasks(d *schema.ResourceData, meta interface{}) error {
databaseName := d.Get("database").(string)
schemaName := d.Get("schema").(string)

extractedTasks, err := client.Tasks.Show(ctx, sdk.NewShowTaskRequest().WithIn(&sdk.In{Schema: sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)}))
extractedTasks, err := client.Tasks.Show(ctx, sdk.NewShowTaskRequest().WithIn(sdk.In{Schema: sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)}))
if err != nil {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] tasks in schema (%s) not found", d.Id())
Expand Down
Loading
Loading