Files
teleport/integrations/terraform-mwi/provider/kubernetes_data_source_test.go
T
rosstimothy 7541d6d7d0 Introduce logtest package (#56741)
Moves all test related logger initialization and creation to the
logtest package to reduce testing symbols in production code. The
existing helpers in lib/utils have been left in place until the
enterprise references can be converted.

Updates #51023.
2025-07-15 18:11:10 +00:00

97 lines
2.7 KiB
Go

/*
Copyright 2015-2025 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"fmt"
"slices"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
corev1 "k8s.io/api/core/v1"
"github.com/gravitational/teleport/lib/utils/log/logtest"
"github.com/gravitational/teleport/tool/teleport/testenv"
)
func TestAccKubernetesDataSource(t *testing.T) {
log := logtest.NewLogger()
ctx := t.Context()
process, kubeMock := setupKubernetesHarness(t, log)
rootClient := testenv.MakeDefaultAuthClient(t, process)
_, pt := setupKubernetesAccessBot(ctx, t, rootClient)
config := fmt.Sprintf(`
provider "teleportmwi" {
proxy_server = "%[1]s"
join_method = "kubernetes"
join_token = "%[2]s"
insecure = true
}
data "teleportmwi_kubernetes" "example" {
selector = {
name = "%[3]s"
}
}
provider "kubernetes" {
host = data.teleportmwi_kubernetes.example.output.host
tls_server_name = data.teleportmwi_kubernetes.example.output.tls_server_name
client_certificate = data.teleportmwi_kubernetes.example.output.client_certificate
client_key = data.teleportmwi_kubernetes.example.output.client_key
cluster_ca_certificate = data.teleportmwi_kubernetes.example.output.cluster_ca_certificate
}
resource "kubernetes_namespace" "ns" {
metadata {
name = "tf-mwi-ds-test"
}
}
`, process.Config.Proxy.WebAddr.String(), pt.GetName(), kubeClusterName)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"kubernetes": {
Source: "hashicorp/kubernetes",
},
},
Steps: []resource.TestStep{
{
Config: config,
Check: func(state *terraform.State) error {
// Check that the namespace was actually created!
containsCreatedNamespace := slices.ContainsFunc(
kubeMock.ListNamespaces().Items,
func(ns corev1.Namespace) bool {
return ns.Name == "tf-mwi-ds-test"
},
)
if !containsCreatedNamespace {
return fmt.Errorf("expected to find created namespace in TF state")
}
return nil
},
},
},
})
}