Skip to content

Commit

Permalink
provider configure,construct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EronWright committed May 1, 2024
1 parent d062cf9 commit 992e77f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
102 changes: 90 additions & 12 deletions provider/pkg/provider/provider_configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ package provider
import (
"context"
_ "embed"
"encoding/json"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
kubeversion "k8s.io/apimachinery/pkg/version"
"k8s.io/cli-runtime/pkg/genericclioptions"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/utils/ptr"
)

var _ = Describe("RPC:Configure", func() {
Expand Down Expand Up @@ -89,6 +93,21 @@ var _ = Describe("RPC:Configure", func() {
})
})

Describe("Namespacing", func() {
Context("when configured to use a particular namespace", func() {
JustBeforeEach(func() {
req.Variables["kubernetes:config:namespace"] = "pulumi"
})
It("should use the configured namespace as the default namespace", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(k.defaultNamespace).To(Equal("pulumi"))
helmFlags := k.helmSettings.RESTClientGetter().(*genericclioptions.ConfigFlags)
Expect(helmFlags.Namespace).To(PointTo(Equal("pulumi")))
})
})
})

Describe("Kubeconfig Parsing", func() {
var other *clientcmdapi.Config

Expand All @@ -100,18 +119,7 @@ var _ = Describe("RPC:Configure", func() {
// Define some "shared behaviors" that will be used to test various use cases.
// pattern: https://onsi.github.io/ginkgo/#shared-behaviors

commonChecks := func() {
Context("when configured to use a particular namespace", func() {
JustBeforeEach(func() {
req.Variables["kubernetes:config:namespace"] = "pulumi"
})
It("should use the configured namespace as the default namespace", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(k.defaultNamespace).To(Equal("pulumi"))
})
})
}
commonChecks := func() {}

connectedChecks := func(expectedNS string) {
It("should have an initialized client", func() {
Expand All @@ -128,6 +136,12 @@ var _ = Describe("RPC:Configure", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(k.defaultNamespace).To(Equal(expectedNS))
})

It("should provide Helm settings", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(k.helmSettings).ToNot(BeNil())
})
}

clusterUnreachableChecks := func() {
Expand Down Expand Up @@ -160,6 +174,12 @@ var _ = Describe("RPC:Configure", func() {
})
commonChecks()
connectedChecks("other")

It("should set Helm's --kubeconfig", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(k.helmSettings.KubeConfig).ToNot(BeEmpty())
})
})
})

Expand Down Expand Up @@ -195,6 +215,64 @@ var _ = Describe("RPC:Configure", func() {
})
commonChecks()
connectedChecks("other")

It("should set Helm's --kubeconfig", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(k.helmSettings.KubeConfig).ToNot(BeEmpty())
})
})
})
})

Describe("Kube Context", func() {
Context("when configured to use a particular context", func() {
JustBeforeEach(func() {
req.Variables["kubernetes:config:context"] = "context2"
})
It("should use the configured context", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(k.helmSettings.KubeContext).To(Equal("context2"))
})
})
})

Describe("Kube Cluster", func() {
Context("when configured to use a particular cluster", func() {
JustBeforeEach(func() {
req.Variables["kubernetes:config:cluster"] = "cluster2"
})
It("should use the configured context", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
helmFlags := k.helmSettings.RESTClientGetter().(*genericclioptions.ConfigFlags)
Expect(helmFlags.ClusterName).To(PointTo(Equal("cluster2")))
})
})
})

Describe("Kube Client Settings", func() {
Context("when configured with Kube client settings", func() {
var kubeClientSettings *KubeClientSettings
BeforeEach(func() {
kubeClientSettings = &KubeClientSettings{
Burst: ptr.To(42),
QPS: ptr.To(42.),
Timeout: ptr.To(42),
}
})
JustBeforeEach(func() {
data, _ := json.Marshal(kubeClientSettings)
req.Variables["kubernetes:config:kubeClientSettings"] = string(data)
})
It("should use the configured settings", func() {
_, err := k.Configure(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
helmFlags := k.helmSettings.RESTClientGetter().(*genericclioptions.ConfigFlags)
Expect(k.helmSettings.BurstLimit).To(Equal(42))
Expect(k.helmSettings.QPS).To(Equal(float32(42.)))
Expect(helmFlags.Timeout).To(PointTo(Equal("42")))
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions provider/pkg/provider/provider_construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (k *kubeProvider) Construct(ctx context.Context, req *pulumirpc.ConstructRe
return nil, fmt.Errorf("configured Kubernetes cluster is unreachable: %s", k.clusterUnreachableReason)
}
contract.Assertf(k.defaultNamespace != "", "expected defaultNamespace")
contract.Assertf(k.helmDriver != "", "expected helmDriver")
contract.Assertf(k.helmSettings != nil, "expected helmSettings")

typ := req.GetType()
provider, found := k.getResourceProvider(typ)
Expand Down
14 changes: 14 additions & 0 deletions provider/pkg/provider/provider_construct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/clients"
providerresource "github.com/pulumi/pulumi-kubernetes/provider/v4/pkg/provider/resource"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/provider"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
helmcli "helm.sh/helm/v3/pkg/cli"
)

var _ = Describe("RPC:Construct", func() {
Expand Down Expand Up @@ -51,7 +53,10 @@ var _ = Describe("RPC:Construct", func() {

JustBeforeEach(func() {
k = pctx.NewProvider(opts...)
k.clientSet = &clients.DynamicClientSet{}
k.defaultNamespace = "default"
k.helmDriver = "memory"
k.helmSettings = helmcli.New()
})

Context("when the requested type is unknown", func() {
Expand All @@ -78,6 +83,7 @@ var _ = Describe("RPC:Construct", func() {
req.Type = "kubernetes:test:TestComponent"
req.Name = "testComponent"
})

It("should delegate to the provider", func() {
result, err := k.Construct(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expand All @@ -86,6 +92,14 @@ var _ = Describe("RPC:Construct", func() {
Expect(result.Urn).Should(Equal("urn:pulumi:test::test::test:TestComponent::testComponent"))
})

It("should provide options", func() {
_, err := k.Construct(context.Background(), req)
Expect(err).ShouldNot(HaveOccurred())
Expect(testComponent.opts.ClientSet).ShouldNot(BeNil())
Expect(testComponent.opts.DefaultNamespace).ShouldNot(BeEmpty())
Expect(testComponent.opts.HelmOptions).ShouldNot(BeNil())
})

Context("when clusterUnreachable is true", func() {
JustBeforeEach(func() {
k.clusterUnreachable = true
Expand Down

0 comments on commit 992e77f

Please sign in to comment.