Skip to content

Commit

Permalink
Fix(tests): fixing the integration tests
Browse files Browse the repository at this point in the history
fix(resources): Fix the helper methods to get the base or series if none of them explicitly provided.

Signed-off-by: gatici <[email protected]>
  • Loading branch information
gatici committed Aug 7, 2024
1 parent b08f919 commit bab151a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 66 deletions.
29 changes: 23 additions & 6 deletions internal/juju/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,27 @@ func (c applicationsClient) deployFromRepository(applicationAPIClient *apiapplic
}

charmsAPIClient := apicharms.NewClient(conn)
resolvedURL, resolvedOrigin, _, err := getCharmResolvedUrlAndOrigin(conn, transformedInput)
modelconfigAPIClient := apimodelconfig.NewClient(conn)
resolvedURL, resolvedOrigin, supportedBases, err := getCharmResolvedUrlAndOrigin(conn, transformedInput)
if err != nil {
return resourceIDs, apiCharmID, err
}
userSuppliedBase := transformedInput.charmBase
baseToUse, err := c.baseToUse(modelconfigAPIClient, userSuppliedBase, resolvedOrigin.Base, supportedBases)
if err != nil {
return resourceIDs, apiCharmID, err
}
if !userSuppliedBase.Empty() && !userSuppliedBase.IsCompatible(baseToUse) {
return resourceIDs, apiCharmID, err
}
resolvedOrigin.Base = baseToUse
series, err := corebase.GetSeriesFromBase(baseToUse)
if err != nil {
return resourceIDs, apiCharmID, err
}
resolvedURL = resolvedURL.WithSeries(series)

// Add charm expects base or series, one of them should exist.
resultOrigin, err := charmsAPIClient.AddCharm(resolvedURL, resolvedOrigin, false)
if err != nil {
return resourceIDs, apiCharmID, err
Expand Down Expand Up @@ -1444,7 +1461,7 @@ func (c applicationsClient) updateResources(appName string, resources map[string
func addPendingResources(appName string, charmResourcesToAdd map[string]charmresources.Meta, resourcesToUse map[string]string,
charmID apiapplication.CharmID, resourcesAPIClient ResourceAPIClient) (map[string]string, error) {
pendingResourcesforAdd := []charmresources.Resource{}
toReturn := map[string]string{}
resourceIDs := map[string]string{}

for _, resourceMeta := range charmResourcesToAdd {
if resourcesToUse == nil {
Expand Down Expand Up @@ -1496,11 +1513,11 @@ func addPendingResources(appName string, charmResourcesToAdd map[string]charmres
return nil, typedError(err)
}
// Add the resource name and the corresponding UUID to the resources map.
toReturn[resourceMeta.Name] = toRequestUpload
resourceIDs[resourceMeta.Name] = toRequestUpload
}

if len(pendingResourcesforAdd) == 0 {
return toReturn, nil
return resourceIDs, nil
}

resourcesReqforAdd := apiresources.AddPendingResourcesArgs{
Expand All @@ -1517,10 +1534,10 @@ func addPendingResources(appName string, charmResourcesToAdd map[string]charmres
}
// Add the resource name and the corresponding UUID to the resources map
for i, argsResource := range pendingResourcesforAdd {
toReturn[argsResource.Meta.Name] = toRequestAdd[i]
resourceIDs[argsResource.Meta.Name] = toRequestAdd[i]
}

return toReturn, nil
return resourceIDs, nil
}

func computeUpdatedBindings(modelDefaultSpace string, currentBindings map[string]string, inputBindings map[string]string, appName string) (params.ApplicationMergeBindingsArgs, error) {
Expand Down
18 changes: 8 additions & 10 deletions internal/juju/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package juju

import (
"fmt"
"strconv"

"github.com/juju/charm/v12"
charmresources "github.com/juju/charm/v12/resource"
Expand All @@ -19,13 +18,8 @@ import (
corebase "github.com/juju/juju/core/base"
)

// isInt checks if strings could be converted to an integer
// Used to detect resources which are given with revision number
func isInt(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}

// getResourceIDs uploads pending resources and
// returns the resource IDs of uploaded resources
func (c applicationsClient) getResourceIDs(transformedInput transformedCreateApplicationInput, conn api.Connection, deployInfo apiapplication.DeployInfo, pendingResources []apiapplication.PendingResourceUpload) (map[string]string, error) {
resourceIDs := map[string]string{}
charmsAPIClient := apicharms.NewClient(conn)
Expand All @@ -38,14 +32,13 @@ func (c applicationsClient) getResourceIDs(transformedInput transformedCreateApp
if err != nil {
return resourceIDs, err
}

userSuppliedBase := transformedInput.charmBase
baseToUse, err := c.baseToUse(modelconfigAPIClient, userSuppliedBase, resolvedOrigin.Base, supportedBases)
if err != nil {
return resourceIDs, err
}

resolvedOrigin.Base = baseToUse

// 3.3 version of ResolveCharm does not always include the series
// in the url. However, juju 2.9 requires it.
series, err := corebase.GetSeriesFromBase(baseToUse)
Expand Down Expand Up @@ -95,6 +88,8 @@ func (c applicationsClient) getResourceIDs(transformedInput transformedCreateApp
return resourceIDs, nil
}

// getResourceIDs uploads pending resources and
// returns the resource IDs of uploaded resources
func getCharmResolvedUrlAndOrigin(conn api.Connection, transformedInput transformedCreateApplicationInput) (*charm.URL, apicommoncharm.Origin, []corebase.Base, error) {
charmsAPIClient := apicharms.NewClient(conn)
modelconfigAPIClient := apimodelconfig.NewClient(conn)
Expand All @@ -119,6 +114,9 @@ func getCharmResolvedUrlAndOrigin(conn api.Connection, transformedInput transfor
}

userSuppliedBase := transformedInput.charmBase
if err != nil {
return nil, apicommoncharm.Origin{}, []corebase.Base{}, err
}
platformCons, err := modelconfigAPIClient.GetModelConstraints()
if err != nil {
return nil, apicommoncharm.Origin{}, []corebase.Base{}, err
Expand Down
50 changes: 0 additions & 50 deletions internal/provider/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,47 +425,13 @@ func TestAcc_CustomResourceUpdatesMicrok8s(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "10"),
),
ExpectNonEmptyPlan: true,
},
{
// Update charm channel and keep resource revision
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/beta", "ausf-image", "10"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "10"),
),
ExpectNonEmptyPlan: true,
},
{
// Keep charm channel and update resource which is given in a json file
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/beta", "ausf-image", "resource_files/ausf-image.json"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "resource_files/ausf-image.json"),
),
ExpectNonEmptyPlan: true,
},
{
// Update charm channel and keep resource which is given in a json file
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/edge", "ausf-image", "resource_files/ausf-image.json"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "resource_files/ausf-image.json"),
),
ExpectNonEmptyPlan: true,
},
{
// Keep charm channel and update resource which is given in a yaml file
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/edge", "ausf-image", "resource_files/ausf-image.yaml"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "resource_files/ausf-image.yaml"),
),
ExpectNonEmptyPlan: true,
},
{
// Update charm channel and keep resource which is given in a yaml file
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/beta", "ausf-image", "resource_files/ausf-image.yaml"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "resource_files/ausf-image.yaml"),
),
ExpectNonEmptyPlan: true,
},
{
// Keep charm channel and remove resource revision
Expand Down Expand Up @@ -523,29 +489,13 @@ func TestAcc_CustomResourcesRemovedFromPlanMicrok8s(t *testing.T) {
resource.TestCheckNoResourceAttr("juju_application.this", "resources"),
),
},
{
// Keep charm channel and update resource which is given in a json file
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/edge", "ausf-image", "resource_files/ausf-image.json"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "resource_files/ausf-image.json"),
),
ExpectNonEmptyPlan: true,
},
{
// Update charm channel and remove image resource which is given in a json file
Config: testAccResourceApplicationWithoutCustomResources(modelName, "1.3/beta"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("juju_application.this", "resources"),
),
},
{
// Keep charm channel and add resource which is given in a yaml file
Config: testAccResourceApplicationWithCustomResources(modelName, "1.3/beta", "ausf-image", "resource_files/ausf-image.yaml"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application.this", "resources.ausf-image", "resource_files/ausf-image.yaml"),
),
ExpectNonEmptyPlan: true,
},
{
// Update charm channel and remove resource
Config: testAccResourceApplicationWithoutCustomResources(modelName, "1.3/edge"),
Expand Down

0 comments on commit bab151a

Please sign in to comment.