Skip to content

Commit

Permalink
adding more tests
Browse files Browse the repository at this point in the history
Signed-off-by: lakshmimsft <[email protected]>
  • Loading branch information
lakshmimsft committed Feb 10, 2025
1 parent ded457d commit 95b4bb4
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func CreateAppScopedNamespace(ctx context.Context, newResource, oldResource *dat

if oldResource != nil {
c := oldResource.Properties.Status.Compute
if c.Kind == rpv1.KubernetesComputeKind && c.KubernetesCompute.Namespace != kubeNamespace {
if c != nil && c.Kind == rpv1.KubernetesComputeKind && c.KubernetesCompute.Namespace != kubeNamespace {
return rest.NewBadRequestResponse(fmt.Sprintf("Updating an application's Kubernetes namespace from '%s' to '%s' requires the application to be deleted and redeployed. Please delete your application and try again.", c.KubernetesCompute.Namespace, kubeNamespace)), nil
}
}
Expand Down
110 changes: 110 additions & 0 deletions pkg/dynamicrp/backend/dynamicresource_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,113 @@ func testUCPClientFactory() (*v20231001preview.ClientFactory, error) {
},
})
}

func Test_hasCapability(t *testing.T) {
tests := []struct {
name string
resourceType *v20231001preview.ResourceTypeResource
capability string
want bool
}{
{
name: "has capability",
resourceType: &v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{
Capabilities: []*string{to.Ptr("capability1"), to.Ptr("capability2")},
},
},
capability: "capability1",
want: true,
},
{
name: "does not have capability",
resourceType: &v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{
Capabilities: []*string{to.Ptr("capability1"), to.Ptr("capability2")},
},
},
capability: "capability3",
want: false,
},
{
name: "nil capabilities",
resourceType: &v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{
Capabilities: nil,
},
},
capability: "capability1",
want: false,
},
{
name: "empty capabilities",
resourceType: &v20231001preview.ResourceTypeResource{
Properties: &v20231001preview.ResourceTypeProperties{
Capabilities: []*string{},
},
},
capability: "capability1",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := hasCapability(tt.resourceType, tt.capability)
require.Equal(t, tt.want, got)
})
}
}

func Test_DynamicResourceController_fetchResourceType(t *testing.T) {
setup := func() *DynamicResourceController {
ucp, err := testUCPClientFactory()
require.NoError(t, err)
controller, err := NewDynamicResourceController(ctrl.Options{}, ucp, nil, nil)
require.NoError(t, err)
return controller.(*DynamicResourceController)
}

tests := []struct {
name string
resourceID string
wantErr bool
errMessage string
}{
{
name: "inert resource type found",
resourceID: "/planes/radius/local/resourceGroups/test-group/providers/" + inertResourceType + "/test-resource",
wantErr: false,
},
{
name: "recipe resource type found",
resourceID: "/planes/radius/local/resourceGroups/test-group/providers/" + recipeResourceType + "/test-resource",
wantErr: false,
},
{
name: "unknown resource type",
resourceID: "/planes/radius/local/resourceGroups/test-group/providers/Applications.Test/unknownType/test-resource",
wantErr: true,
errMessage: "resource type Applications.Test/unknownType not recognized",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
controller := setup()
id, err := resources.ParseResource(tt.resourceID)
require.NoError(t, err)

resourceType, err := controller.fetchResourceType(context.Background(), id)
if tt.wantErr {
require.Error(t, err)
require.Contains(t, err.Error(), tt.errMessage)
return
}

require.NoError(t, err)
require.NotNil(t, resourceType)
require.NotNil(t, resourceType.Properties)
})
}
}
13 changes: 9 additions & 4 deletions pkg/dynamicrp/datamodel/dynamicresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ func (d *DynamicResource) GetRecipe() *portableresources.ResourceRecipe {

// SetRecipe implements datamodel.RecipeDataModel.
func (d *DynamicResource) SetRecipe(recipe *portableresources.ResourceRecipe) {
if d.Properties == nil {
d.Properties = map[string]any{}
}

if recipe == nil {
d.Properties["recipe"] = map[string]any{}
return
}

// This is the best we can do. We designed the ResourceRecipe type to be JSON-marshallable.
bs, err := json.Marshal(recipe)
if err != nil {
Expand All @@ -112,10 +121,6 @@ func (d *DynamicResource) SetRecipe(recipe *portableresources.ResourceRecipe) {
panic("failed to unmarshal recipe: " + err.Error())
}

if d.Properties == nil {
d.Properties = map[string]any{}
}

d.Properties["recipe"] = store
}

Expand Down
Loading

0 comments on commit 95b4bb4

Please sign in to comment.