From bab151a8e5b52e742c2413bfd54bbe631fb5af72 Mon Sep 17 00:00:00 2001 From: gatici Date: Wed, 7 Aug 2024 23:26:25 +0300 Subject: [PATCH] Fix(tests): fixing the integration tests fix(resources): Fix the helper methods to get the base or series if none of them explicitly provided. Signed-off-by: gatici --- internal/juju/applications.go | 29 ++++++++--- internal/juju/resources.go | 18 +++---- .../provider/resource_application_test.go | 50 ------------------- 3 files changed, 31 insertions(+), 66 deletions(-) diff --git a/internal/juju/applications.go b/internal/juju/applications.go index 10e80fe0..48b9d12d 100644 --- a/internal/juju/applications.go +++ b/internal/juju/applications.go @@ -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 @@ -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 { @@ -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{ @@ -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) { diff --git a/internal/juju/resources.go b/internal/juju/resources.go index 2b0e758d..550f58d0 100644 --- a/internal/juju/resources.go +++ b/internal/juju/resources.go @@ -5,7 +5,6 @@ package juju import ( "fmt" - "strconv" "github.com/juju/charm/v12" charmresources "github.com/juju/charm/v12/resource" @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/internal/provider/resource_application_test.go b/internal/provider/resource_application_test.go index ef0b1370..664c3619 100644 --- a/internal/provider/resource_application_test.go +++ b/internal/provider/resource_application_test.go @@ -425,7 +425,6 @@ 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 @@ -433,39 +432,6 @@ func TestAcc_CustomResourceUpdatesMicrok8s(t *testing.T) { 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 @@ -523,14 +489,6 @@ 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"), @@ -538,14 +496,6 @@ func TestAcc_CustomResourcesRemovedFromPlanMicrok8s(t *testing.T) { 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"),