forked from hashicorp/terraform-provider-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix manifest object type mutation (hashicorp#2372)
* Adapt Object branch of `morph.DeepUnknown` to use type of Value instead of mutating object type * Move the helper function PointerOf to the folder internal to make it available for all providers * Add a new manifest test helper function `CreatePod` * Add a new acceptance test for `kubernetes_resources` data source * Remove internal pointer package
- Loading branch information
1 parent
f53f684
commit f83d63a
Showing
6 changed files
with
174 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
`data_source/kubernetes_resources`: fix an issue where the provider exit with an error when the data source `kubernetes_resources` receives multiple Kubernetes objects containing tuples with different numbers of elements. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
//go:build acceptance | ||
// +build acceptance | ||
|
||
package acceptance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/provider" | ||
"github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/kubernetes" | ||
tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/state" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/utils/ptr" | ||
) | ||
|
||
func TestDataSourceKubernetesResources_DiffTuples(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t) | ||
if err != nil { | ||
t.Errorf("Failed to create provider instance: %q", err) | ||
} | ||
|
||
name := randName() | ||
namespace := randName() | ||
|
||
tf := tfhelper.RequireNewWorkingDir(ctx, t) | ||
tf.SetReattachInfo(ctx, reattachInfo) | ||
defer func() { | ||
tf.Destroy(ctx) | ||
tf.Close() | ||
k8shelper.AssertResourceDoesNotExist(t, "v1", "namespaces", name) | ||
}() | ||
|
||
// Create a Namespace to provision the rest of the resources in it. | ||
k8shelper.CreateNamespace(t, namespace) | ||
defer k8shelper.DeleteResource(t, namespace, kubernetes.NewGroupVersionResource("v1", "namespaces")) | ||
|
||
// Create Pods to use a data source. | ||
// The only difference between the two pods is the number of VolumeMounts(tuples) that will be created. | ||
// This is necessary to ensure that DeepUnknown doesn't mutate object type when iterates over items. | ||
// kubernetes_manifest failed to create pods with volumes, thus create them manually. | ||
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "pods", namespace, name) | ||
|
||
volumes := []corev1.Volume{ | ||
{ | ||
Name: "config", | ||
VolumeSource: corev1.VolumeSource{ | ||
EmptyDir: &corev1.EmptyDirVolumeSource{}, | ||
}, | ||
}, | ||
} | ||
podSpecs := []corev1.PodSpec{ | ||
{ | ||
TerminationGracePeriodSeconds: ptr.To(int64(1)), | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "this", | ||
Image: "busybox", | ||
Command: []string{"sleep", "infinity"}, | ||
VolumeMounts: []corev1.VolumeMount{ | ||
{ | ||
Name: "config", | ||
MountPath: "/config-a", | ||
}, | ||
{ | ||
Name: "config", | ||
MountPath: "/config-b", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Volumes: volumes, | ||
}, | ||
{ | ||
TerminationGracePeriodSeconds: ptr.To(int64(1)), | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "this", | ||
Image: "busybox", | ||
Command: []string{"sleep", "infinity"}, | ||
VolumeMounts: []corev1.VolumeMount{ | ||
{ | ||
Name: "config", | ||
MountPath: "/config-a", | ||
}, | ||
{ | ||
Name: "config", | ||
MountPath: "/config-b", | ||
}, | ||
{ | ||
Name: "config", | ||
MountPath: "/config-c", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Volumes: volumes, | ||
}, | ||
} | ||
|
||
for i, ps := range podSpecs { | ||
k8shelper.CreatePod(t, fmt.Sprintf("%s-%d", name, i), namespace, ps) | ||
} | ||
|
||
// Get pods | ||
tfvars := TFVARS{ | ||
"namespace": namespace, | ||
} | ||
tfconfig := loadTerraformConfig(t, "DataSourceResources/pods_data_source.tf", tfvars) | ||
tf.SetConfig(ctx, tfconfig) | ||
tf.Init(ctx) | ||
err = tf.Apply(ctx) | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
st, err := tf.State(ctx) | ||
if err != nil { | ||
t.Fatalf("Failed to retrieve terraform state: %q", err) | ||
} | ||
tfstate := tfstatehelper.NewHelper(st) | ||
|
||
// check the data source | ||
tfstate.AssertAttributeLen(t, "data.kubernetes_resources.pods.objects", len(podSpecs)) | ||
} |
8 changes: 8 additions & 0 deletions
8
manifest/test/acceptance/testdata/DataSourceResources/pods_data_source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright (c) HashiCorp, Inc. | ||
# SPDX-License-Identifier: MPL-2.0 | ||
|
||
data "kubernetes_resources" "pods" { | ||
kind = "Pod" | ||
api_version = "v1" | ||
namespace = var.namespace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters