Skip to content

Commit

Permalink
Moves plan generator from utils package to testing.
Browse files Browse the repository at this point in the history
Cherry picked commit, fixed typo in directory name and removed config
change from test as it won't work here. Will be added later.
  • Loading branch information
anvial authored and hmlanigan committed Feb 27, 2024
1 parent ef44853 commit 3d3d4a3
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
67 changes: 67 additions & 0 deletions internal/provider/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

internaltesting "github.com/juju/terraform-provider-juju/internal/testing"
)

func TestAcc_ResourceApplication(t *testing.T) {
Expand Down Expand Up @@ -137,6 +139,37 @@ func TestAcc_ResourceApplication_Updates(t *testing.T) {
})
}

// TestAcc_ResourceApplication_UpdatesRevisionConfig will test the revision update that have new config parameters on
// the charm. The test will check that the config is updated and the revision is updated as well.
func TestAcc_ResourceApplication_UpdatesRevisionConfig(t *testing.T) {
if testingCloud != LXDCloudTesting {
t.Skip(t.Name() + " only runs with LXD")
}
modelName := acctest.RandomWithPrefix("tf-test-application")
appName := "github-runner"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: frameworkProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccResourceApplicationWithRevisionAndConfig(modelName, appName, 88, ""),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application."+appName, "model", modelName),
resource.TestCheckResourceAttr("juju_application."+appName, "charm.#", "1"),
resource.TestCheckResourceAttr("juju_application."+appName, "charm.0.name", appName),
resource.TestCheckResourceAttr("juju_application."+appName, "charm.0.revision", "88"),
),
},
{
Config: testAccResourceApplicationWithRevisionAndConfig(modelName, appName, 95, ""),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_application."+appName, "charm.0.revision", "95"),
),
},
},
})
}

func TestAcc_CharmUpdates(t *testing.T) {
modelName := acctest.RandomWithPrefix("tf-test-charmupdates")

Expand Down Expand Up @@ -290,6 +323,40 @@ func testAccResourceApplicationBasic(modelName, appName string) string {
}
}

func testAccResourceApplicationWithRevisionAndConfig(modelName, appName string, revision int, configParamName string) string {
return internaltesting.GetStringFromTemplateWithData(
"testAccResourceApplicationWithRevisionAndConfig",
`
resource "juju_model" "{{.ModelName}}" {
name = "{{.ModelName}}"
}
resource "juju_application" "{{.AppName}}" {
name = "{{.AppName}}"
model = juju_model.{{.ModelName}}.name
charm {
name = "{{.AppName}}"
revision = {{.Revision}}
channel = "latest/edge"
}
{{ if ne .ConfigParamName "" }}
config = {
{{.ConfigParamName}} = "{{.ConfigParamName}}-value"
}
{{ end }}
units = 1
}
`, internaltesting.TemplateData{
"ModelName": modelName,
"AppName": appName,
"Revision": fmt.Sprintf("%d", revision),
"ConfigParamName": configParamName,
})
}

func testAccResourceApplicationUpdates(modelName string, units int, expose bool, hostname string) string {
exposeStr := "expose{}"
if !expose {
Expand Down
84 changes: 84 additions & 0 deletions internal/testing/plangenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the Apache License, Version 2.0, see LICENCE file for details.

package testsing

import (
"bytes"
"text/template"
)

type TemplateData map[string]string

// GetStringFromTemplateWithData returns a string from a template with data.
//
// Here is the example usage:
// templateStr := GetStringFromTemplateWithData(
//
// "testAccResourceApplicationWithRevisionAndConfig",
// `
//
// resource "juju_model" "this" {
// name = "{{.ModelName}}"
// }
//
// resource "juju_application" "{{.AppName}}" {
// name = "{{.AppName}}"
// model = juju_model.this.name
//
// charm {
// name = "{{.AppName}}"
// revision = {{.Revision}}
// channel = "latest/edge"
// }
//
// {{ if ne .ConfigParamName "" }}
// config = {
// {{.ConfigParamName}} = "{{.ConfigParamName}}-value"
// }
// {{ end }}
//
// units = 1
// }
//
// `, utils.TemplateData{
// "ModelName": "test-model",
// "AppName": "test-app"
// "Revision": fmt.Sprintf("%d", 7),
// "ConfigParamName": "test-config-param-name",
// })
//
// The templateStr will be:
//
// resource "juju_model" "this" {
// name = "test-model"
// }
//
// resource "juju_application" "test-app" {
// name = "test-app"
// model = juju_model.this.name
//
// charm {
// name = "test-app"
// revision = 7
// channel = "latest/edge"
// }
//
// config = {
// test-config-param-name = "test-config-param-name-value"
// }
//
// units = 1
// }
func GetStringFromTemplateWithData(templateName string, templateStr string, templateData TemplateData) string {
tmpl, err := template.New(templateName).Parse(templateStr)
if err != nil {
panic(err)
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, templateData)
if err != nil {
panic(err)
}
return tpl.String()
}

0 comments on commit 3d3d4a3

Please sign in to comment.