Skip to content

Commit

Permalink
test(app): 2645 Added test for label selection
Browse files Browse the repository at this point in the history
Based on the issue description, the name in the metadata nested resource was mandatory, which is not the intended behaviour. Added test to verify if the label selection is working as intended and that the name attribute is not required every time.
  • Loading branch information
Klopklopi committed Dec 31, 2024
1 parent 022f6b4 commit 5cac0da
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions kubernetes/data_source_kubernetes_pod_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,37 @@ func TestAccKubernetesDataSourcePodV1_not_found(t *testing.T) {
})
}

func TestAccKubernetesDataSourcePodV1_readWithLabel(t *testing.T) {
resourceName := "kubernetes_pod_v1.test"
dataSourceName := "data.kubernetes_pod_v1.test"
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
labelKey := "app"
labelValue := "test"
imageName := busyboxImage

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(resourceName, "metadata.0.labels."+labelKey, labelValue),
),
},
{
Config: testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue) +
testAccKubernetesDataSourcePodV1_readWithLabel(labelKey, labelValue),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(resourceName, "metadata.0.labels."+labelKey, labelValue),
),
},
},
})
}

func testAccKubernetesDataSourcePodV1_basic(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" {
metadata {
Expand All @@ -74,6 +105,24 @@ func testAccKubernetesDataSourcePodV1_basic(name, imageName string) string {
`, name, imageName)
}

func testAccKubernetesDataSourcePodV1_basicWithLabel(name, imageName, labelKey, labelValue string) string {
return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" {
metadata {
name = "%s"
labels = {
%s = "%s"
}
}
spec {
container {
image = "%s"
name = "containername"
}
}
}
`, name, labelKey, labelValue, imageName)
}

func testAccKubernetesDataSourcePodV1_read() string {
return `data "kubernetes_pod_v1" "test" {
metadata {
Expand All @@ -91,3 +140,14 @@ func testAccKubernetesDataSourcePodV1_nonexistent(name string) string {
}
`, name)
}

func testAccKubernetesDataSourcePodV1_readWithLabel(labelKey, labelValue string) string {
return fmt.Sprintf(`data "kubernetes_pod_v1" "test" {
metadata {
labels = {
%s = "%s"
}
}
}
`, labelKey, labelValue)
}

0 comments on commit 5cac0da

Please sign in to comment.