Skip to content

Commit fbd1ccc

Browse files
authored
Merge pull request juju#641 from alesstimec/jaas-integration-fix
fix: jaas integration test
2 parents d8077ec + d344dfa commit fbd1ccc

File tree

5 files changed

+120
-19
lines changed

5 files changed

+120
-19
lines changed

.github/workflows/test_integration.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
TF_ACC: "1"
104104
TEST_CLOUD: ${{ matrix.action-operator.cloud }}
105105
run: go test -parallel 1 -timeout 60m -v -cover ./internal/provider/
106-
timeout-minutes: 40
106+
timeout-minutes: 60
107107

108108
# Run acceptance tests in a matrix with Terraform CLI versions
109109
add-machine-test:
@@ -189,5 +189,5 @@ jobs:
189189
190190
echo "Running the test"
191191
cd ./internal/provider/
192-
go test ./... -timeout 30m -v -test.run TestAcc_ResourceMachine_AddMachine
193-
timeout-minutes: 40
192+
go test ./... -timeout 60m -v -test.run TestAcc_ResourceMachine_AddMachine
193+
timeout-minutes: 60

.github/workflows/test_integration_jaas.yaml

+13-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
test:
4242
name: Integration-JAAS
4343
needs: build
44-
runs-on: ubuntu-latest
44+
runs-on: [self-hosted, jammy, x64]
4545
strategy:
4646
fail-fast: false
4747
timeout-minutes: 60
@@ -55,6 +55,10 @@ jobs:
5555
with:
5656
terraform_version: "1.9.*"
5757
terraform_wrapper: false
58+
- name: Install docker compose plugin
59+
run: |
60+
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove -y $pkg; done
61+
sudo snap install docker --channel latest/stable
5862
# Starting JAAS will start the JIMM controller and dependencies and create a Juju controller on LXD and connect it to JIMM.
5963
- name: Setup JAAS
6064
uses: canonical/jimm/.github/actions/test-server@v3
@@ -68,11 +72,13 @@ jobs:
6872
sudo snap install microk8s --channel=1.28-strict/stable
6973
sudo usermod -a -G snap_microk8s $USER
7074
sudo chown -R $USER ~/.kube
71-
sudo microk8s.enable dns storage
72-
sudo microk8s.enable dns local-storage
75+
sudo microk8s.enable dns
76+
sudo microk8s.enable storage
77+
sudo microk8s.enable hostpath-storage
7378
sudo -g snap_microk8s -E microk8s status --wait-ready --timeout=600
79+
sudo microk8s.config view | tee /home/$USER/microk8s-config.yaml
7480
echo "MICROK8S_CONFIG<<EOF" >> $GITHUB_ENV
75-
sudo microk8s.config view >> $GITHUB_ENV
81+
echo "$(cat /home/${USER}/microk8s-config.yaml)" >> $GITHUB_ENV
7682
echo "EOF" >> $GITHUB_ENV
7783
- name: Create additional networks when testing with LXD
7884
run: |
@@ -97,5 +103,6 @@ jobs:
97103
- env:
98104
TF_ACC: "1"
99105
TEST_CLOUD: "lxd"
100-
run: go test -parallel 10 -timeout 40m -v -cover ./internal/provider/
101-
timeout-minutes: 40
106+
run: go test -parallel 1 -timeout 60m -v -cover ./internal/provider/
107+
timeout-minutes: 60
108+

docs/resources/kubernetes_cloud.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ resource "juju_model" "my-model" {
3737
### Optional
3838

3939
- `kubernetes_config` (String, Sensitive) The kubernetes config file path for the cloud. Cloud credentials will be added to the Juju controller for you.
40-
- `parent_cloud_name` (String) The parent cloud name in case adding k8s cluster from existed cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.
41-
- `parent_cloud_region` (String) The parent cloud region name in case adding k8s cluster from existed cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.
40+
- `parent_cloud_name` (String) The parent cloud name, for adding a k8s cluster from an existing cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform. *Note* that this value must be set when running against a JAAS controller.
41+
- `parent_cloud_region` (String) The parent cloud region name, for adding a k8s cluster from an existing cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform. *Note* that this value must be set when running against a JAAS controller.
4242

4343
### Read-Only
4444

internal/provider/resource_kubernetes_cloud.go

+53-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
// Ensure provider defined types fully satisfy framework interfaces.
2121
var _ resource.Resource = &kubernetesCloudResource{}
2222
var _ resource.ResourceWithConfigure = &kubernetesCloudResource{}
23+
var _ resource.ResourceWithConfigValidators = &kubernetesCloudResource{}
2324

2425
func NewKubernetesCloudResource() resource.Resource {
2526
return &kubernetesCloudResource{}
@@ -62,6 +63,13 @@ func (r *kubernetesCloudResource) Configure(ctx context.Context, req resource.Co
6263
r.subCtx = tflog.NewSubsystem(ctx, LogResourceKubernetesCloud)
6364
}
6465

66+
// ConfigValidators returns a list of functions which will all be performed during validation.
67+
func (r *kubernetesCloudResource) ConfigValidators(context.Context) []resource.ConfigValidator {
68+
return []resource.ConfigValidator{
69+
&kuberenetesCloudJAASValidator{r.client},
70+
}
71+
}
72+
6573
// Metadata returns the metadata for the kubernetes cloud resource.
6674
func (r *kubernetesCloudResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
6775
resp.TypeName = req.ProviderTypeName + "_kubernetes_cloud"
@@ -92,11 +100,11 @@ func (r *kubernetesCloudResource) Schema(_ context.Context, req resource.SchemaR
92100
Sensitive: true,
93101
},
94102
"parent_cloud_name": schema.StringAttribute{
95-
Description: "The parent cloud name in case adding k8s cluster from existed cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.",
103+
Description: "The parent cloud name, for adding a k8s cluster from an existing cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform. *Note* that this value must be set when running against a JAAS controller.",
96104
Optional: true,
97105
},
98106
"parent_cloud_region": schema.StringAttribute{
99-
Description: "The parent cloud region name in case adding k8s cluster from existed cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.",
107+
Description: "The parent cloud region name, for adding a k8s cluster from an existing cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform. *Note* that this value must be set when running against a JAAS controller.",
100108
Optional: true,
101109
},
102110
"id": schema.StringAttribute{
@@ -128,8 +136,10 @@ func (r *kubernetesCloudResource) Create(ctx context.Context, req resource.Creat
128136
// Create the kubernetes cloud.
129137
cloudCredentialName, err := r.client.Clouds.CreateKubernetesCloud(
130138
&juju.CreateKubernetesCloudInput{
131-
Name: plan.CloudName.ValueString(),
132-
KubernetesConfig: plan.KubernetesConfig.ValueString(),
139+
Name: plan.CloudName.ValueString(),
140+
KubernetesConfig: plan.KubernetesConfig.ValueString(),
141+
ParentCloudName: plan.ParentCloudName.ValueString(),
142+
ParentCloudRegion: plan.ParentCloudRegion.ValueString(),
133143
},
134144
)
135145
if err != nil {
@@ -201,8 +211,10 @@ func (r *kubernetesCloudResource) Update(ctx context.Context, req resource.Updat
201211
// Update the kubernetes cloud.
202212
err := r.client.Clouds.UpdateKubernetesCloud(
203213
juju.UpdateKubernetesCloudInput{
204-
Name: plan.CloudName.ValueString(),
205-
KubernetesConfig: plan.KubernetesConfig.ValueString(),
214+
Name: plan.CloudName.ValueString(),
215+
KubernetesConfig: plan.KubernetesConfig.ValueString(),
216+
ParentCloudName: plan.ParentCloudName.ValueString(),
217+
ParentCloudRegion: plan.ParentCloudRegion.ValueString(),
206218
},
207219
)
208220
if err != nil {
@@ -250,6 +262,41 @@ func (r *kubernetesCloudResource) trace(msg string, additionalFields ...map[stri
250262
tflog.SubsystemTrace(r.subCtx, LogResourceKubernetesCloud, msg, additionalFields...)
251263
}
252264

265+
type kuberenetesCloudJAASValidator struct {
266+
client *juju.Client
267+
}
268+
269+
// Description implements the Description method of the resource.ConfigValidator interface.
270+
func (v *kuberenetesCloudJAASValidator) Description(ctx context.Context) string {
271+
return v.MarkdownDescription(ctx)
272+
}
273+
274+
// MarkdownDescription implements the MarkdownDescription method of the resource.ConfigValidator interface.
275+
func (v *kuberenetesCloudJAASValidator) MarkdownDescription(_ context.Context) string {
276+
return "Enforces that the parent_cloud_name is specified when applying to a JAAS controller."
277+
}
278+
279+
// ValidateResource implements the ValidateResource method of the resource.ConfigValidator interface.
280+
func (v *kuberenetesCloudJAASValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
281+
if v.client == nil {
282+
return
283+
}
284+
285+
if !v.client.IsJAAS() {
286+
return
287+
}
288+
289+
var data kubernetesCloudResourceModel
290+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
291+
if resp.Diagnostics.HasError() {
292+
return
293+
}
294+
295+
if data.ParentCloudName.ValueString() == "" {
296+
resp.Diagnostics.AddError("Plan Error", "Field `parent_cloud_name` must be specified when applying to a JAAS controller.")
297+
}
298+
}
299+
253300
func newKubernetesCloudID(kubernetesCloudName string, cloudCredentialName string) string {
254301
return fmt.Sprintf("%s:%s", kubernetesCloudName, cloudCredentialName)
255302
}

internal/provider/resource_kubernetes_cloud_test.go

+49-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package provider
55

66
import (
77
"os"
8+
"regexp"
89
"testing"
910

1011
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
@@ -24,12 +25,17 @@ func TestAcc_ResourceKubernetesCloud(t *testing.T) {
2425
cloudName := acctest.RandomWithPrefix("tf-test-k8scloud")
2526
cloudConfig := os.Getenv("MICROK8S_CONFIG")
2627

28+
jaasTest := false
29+
if _, ok := os.LookupEnv("IS_JAAS"); ok {
30+
jaasTest = true
31+
}
32+
2733
resource.ParallelTest(t, resource.TestCase{
2834
PreCheck: func() { testAccPreCheck(t) },
2935
ProtoV6ProviderFactories: frameworkProviderFactories,
3036
Steps: []resource.TestStep{
3137
{
32-
Config: testAccResourceKubernetesCloudWithoutModel(cloudName, cloudConfig),
38+
Config: testAccResourceKubernetesCloudWithoutModel(cloudName, cloudConfig, jaasTest),
3339
Check: resource.ComposeTestCheckFunc(
3440
resource.TestCheckResourceAttr("juju_kubernetes_cloud."+cloudName, "name", cloudName),
3541
),
@@ -38,13 +44,54 @@ func TestAcc_ResourceKubernetesCloud(t *testing.T) {
3844
})
3945
}
4046

41-
func testAccResourceKubernetesCloudWithoutModel(cloudName string, config string) string {
47+
func TestAcc_ResourceKubernetesCloudWithJAASIncompleteConfig(t *testing.T) {
48+
OnlyTestAgainstJAAS(t)
49+
// TODO: This test is not adding model as a resource, which is required.
50+
// The reason in the race that we (potentially) have in the Juju side.
51+
// Once the problem is fixed (https://bugs.launchpad.net/juju/+bug/2084448),
52+
// we should add the model as a resource.
53+
if testingCloud != LXDCloudTesting {
54+
t.Skip(t.Name() + " only runs with LXD")
55+
}
56+
cloudName := acctest.RandomWithPrefix("tf-test-k8scloud")
57+
cloudConfig := os.Getenv("MICROK8S_CONFIG")
58+
59+
resource.ParallelTest(t, resource.TestCase{
60+
PreCheck: func() { testAccPreCheck(t) },
61+
ProtoV6ProviderFactories: frameworkProviderFactories,
62+
Steps: []resource.TestStep{{
63+
Config: testAccResourceKubernetesCloudWithoutParentCloudName(cloudName, cloudConfig),
64+
ExpectError: regexp.MustCompile("Field `parent_cloud_name` must be specified when applying to a JAAS.*"),
65+
}},
66+
})
67+
}
68+
69+
func testAccResourceKubernetesCloudWithoutModel(cloudName string, config string, jaasTest bool) string {
4270
return internaltesting.GetStringFromTemplateWithData(
4371
"testAccResourceSecret",
4472
`
4573
resource "juju_kubernetes_cloud" "{{.CloudName}}" {
4674
name = "{{.CloudName}}"
4775
kubernetes_config = file("~/microk8s-config.yaml")
76+
{{ if .JAASTest }}
77+
parent_cloud_name = "lxd"
78+
parent_cloud_region = "localhost"
79+
{{ end }}
80+
}
81+
`, internaltesting.TemplateData{
82+
"CloudName": cloudName,
83+
"Config": config,
84+
"JAASTest": jaasTest,
85+
})
86+
}
87+
88+
func testAccResourceKubernetesCloudWithoutParentCloudName(cloudName string, config string) string {
89+
return internaltesting.GetStringFromTemplateWithData(
90+
"testAccResourceSecret",
91+
`
92+
resource "juju_kubernetes_cloud" "{{.CloudName}}" {
93+
name = "{{.CloudName}}"
94+
kubernetes_config = "test config"
4895
}
4996
`, internaltesting.TemplateData{
5097
"CloudName": cloudName,

0 commit comments

Comments
 (0)