Skip to content

Commit

Permalink
mock test for workspace (#512)
Browse files Browse the repository at this point in the history
* mock test for workspace

* schedule time issue fixed

* removed commented codes

---------

Co-authored-by: ”Srinivas <“[email protected]”>
Co-authored-by: Sivaanand Murugesan <[email protected]>
  • Loading branch information
3 people authored Sep 5, 2024
1 parent 3f89265 commit f67d5c0
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 150 deletions.
231 changes: 180 additions & 51 deletions spectrocloud/resource_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,61 +105,190 @@ func TestToWorkspace(t *testing.T) {
}
}

func prepareResourceWorkspace() *schema.ResourceData {
func prepareBaseWorkspaceSchema() *schema.ResourceData {
// Get an initialized ResourceData from resourceWorkspace
d := resourceWorkspace().TestResourceData()
d.SetId("test-ws-id")
_ = d.Set("name", "test-ws")
_ = d.Set("tags", []string{"dev:test"})
_ = d.Set("description", "test description")
var c []interface{}
c = append(c, map[string]interface{}{
"uid": "test-cluster-id",
})
var bp []interface{}
bp = append(bp, map[string]interface{}{
"prefix": "test-prefix",
"backup_location_id": "test-location-id",
"schedule": "0 1 * * *",
"expiry_in_hour": 1,
"include_disks": false,
"include_cluster_resources": true,
"namespaces": []string{"ns1", "ns2"},
"cluster_uids": []string{"cluster1", "cluster2"},
"include_all_clusters": false,
})
_ = d.Set("backup_policy", bp)
var subjects []interface{}
subjects = append(subjects, map[string]interface{}{
"type": "User",
"name": "test-name-user",
"namespace": "ns1",
})
var rbacs []interface{}
rbacs = append(rbacs, map[string]interface{}{
"type": "RoleBinding",
"namespace": "ns1",
"role": map[string]string{
"test": "admin",
},
"subjects": subjects,
})
_ = d.Set("cluster_rbac_binding", rbacs)
var ns []interface{}
ns = append(ns, map[string]interface{}{
"name": "test-ns-name",
"resource_allocation": map[string]string{
"test": "test",
},
"images_blacklist": []string{"test-list"},
})
_ = d.Set("namespaces", ns)
// Set values for the required and optional fields
if err := d.Set("name", "Default"); err != nil {
panic(err) // Handle the error as appropriate in your test setup
}
if err := d.Set("description", "A workspace for testing"); err != nil {
panic(err)
}

//Set the clusters field with ClusterRefs
clusters := []interface{}{
map[string]interface{}{
"uid": "Default",
},
}
if err := d.Set("clusters", clusters); err != nil {
panic(err)
}
return d
}

func TestResourceWorkspaceCreate(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()

// Call the function
diags := resourceWorkspaceCreate(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceWorkspaceRead(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()

// Call the function
diags := resourceWorkspaceRead(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceWorkspaceUpdate(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()

// Call the function
diags := resourceWorkspaceUpdate(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceWorkspaceDelete(t *testing.T) {
d := prepareResourceWorkspace()
var ctx context.Context
diags := resourceWorkspaceDelete(ctx, d, unitTestMockAPIClient)
assert.Empty(t, diags)
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()
resourceData.SetId("12763471256725")

// Call the function
diags := resourceWorkspaceDelete(ctx, resourceData, unitTestMockAPIClient)

// Assertions
assert.Equal(t, 0, len(diags))

}

func TestResourceWorkspaceCreateNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()

// Call the function
diags := resourceWorkspaceCreate(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "workspaces already exist") // Verify the error message
}

}

func TestResourceWorkspaceReadNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()
resourceData.SetId("12763471256725")

// Call the function
diags := resourceWorkspaceRead(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "workspaces not found") // Verify the error message
}

}

func TestResourceWorkspaceUpdateNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()
resourceData.SetId("12763471256725")

// Call the function
diags := resourceWorkspaceUpdate(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "workspaces not found") // Verify the error message
}
}

func TestResourceWorkspaceDeleteNegative(t *testing.T) {
ctx := context.Background()
resourceData := prepareBaseWorkspaceSchema()
resourceData.SetId("12763471256725")

// Call the function
diags := resourceWorkspaceDelete(ctx, resourceData, unitTestMockAPINegativeClient)

// Assertions
if assert.NotEmpty(t, diags) { // Check that diags is not empty
assert.Contains(t, diags[0].Summary, "workspaces not found") // Verify the error message
}
}

//func prepareResourceWorkspace() *schema.ResourceData {
// d := resourceWorkspace().TestResourceData()
// d.SetId("test-ws-id")
// _ = d.Set("name", "test-ws")
// _ = d.Set("tags", []string{"dev:test"})
// _ = d.Set("description", "test description")
// var c []interface{}
// c = append(c, map[string]interface{}{
// "uid": "test-cluster-id",
// })
// var bp []interface{}
// bp = append(bp, map[string]interface{}{
// "prefix": "test-prefix",
// "backup_location_id": "test-location-id",
// "schedule": "0 1 * * *",
// "expiry_in_hour": 1,
// "include_disks": false,
// "include_cluster_resources": true,
// "namespaces": []string{"ns1", "ns2"},
// "cluster_uids": []string{"cluster1", "cluster2"},
// "include_all_clusters": false,
// })
// _ = d.Set("backup_policy", bp)
// var subjects []interface{}
// subjects = append(subjects, map[string]interface{}{
// "type": "User",
// "name": "test-name-user",
// "namespace": "ns1",
// })
// var rbacs []interface{}
// rbacs = append(rbacs, map[string]interface{}{
// "type": "RoleBinding",
// "namespace": "ns1",
// "role": map[string]string{
// "test": "admin",
// },
// "subjects": subjects,
// })
// _ = d.Set("cluster_rbac_binding", rbacs)
// var ns []interface{}
// ns = append(ns, map[string]interface{}{
// "name": "test-ns-name",
// "resource_allocation": map[string]string{
// "test": "test",
// },
// "images_blacklist": []string{"test-list"},
// })
// _ = d.Set("namespaces", ns)
//
// return d
//}
//
//func TestResourceWorkspaceDelete(t *testing.T) {
// d := prepareResourceWorkspace()
// var ctx context.Context
// diags := resourceWorkspaceDelete(ctx, d, unitTestMockAPIClient)
// assert.Empty(t, diags)
//}
3 changes: 2 additions & 1 deletion tests/mockApiServer/apiServerMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func init() {
routes.IPPoolRoutes,
routes.MacrosRoutes,
routes.TenantRoutes,
routes.WorkSpaceRoutes,
routes.WorkspaceRoutes,
routes.AlertRoutes,
routes.ClusterGroupRoutes,
)
Expand All @@ -127,5 +127,6 @@ func init() {
routes.ClusterCommonNegativeRoutes,
routes.MacrosNegativeRoutes,
routes.TenantNegativeRoutes,
routes.WorkspaceNegativeRoutes,
)
}
Loading

0 comments on commit f67d5c0

Please sign in to comment.