diff --git a/pkg/v1/tkg/client/upgrade_addon_test.go b/pkg/v1/tkg/client/upgrade_addon_test.go index 8fa430df28..f5c959d75a 100644 --- a/pkg/v1/tkg/client/upgrade_addon_test.go +++ b/pkg/v1/tkg/client/upgrade_addon_test.go @@ -4,11 +4,14 @@ package client_test import ( + "testing" "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/pkg/errors" + + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" capi "sigs.k8s.io/cluster-api/api/v1alpha3" @@ -17,6 +20,7 @@ import ( "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/clusterclient" "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants" "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/fakes" + fakehelper "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/fakes/helper" ) var _ = Describe("Unit tests for addons upgrade", func() { @@ -225,3 +229,97 @@ var _ = Describe("Unit tests for addons upgrade", func() { }) }) }) + +// Test_RetrieveProxySettings tests a previous regression where the proxy settings +// for a workload cluster was being incorrectly inherited from the management cluster. +// Specifically, this test validates retreiving proxy settings for a workload cluster +// with different settings from the management cluster. +func Test_RetrieveProxySettings(t *testing.T) { + RegisterTestingT(t) + g := NewWithT(t) + + tkgClient, err := CreateTKGClient("../fakes/config/config2.yaml", fakehelper.CreateTempTestingDirectory(), "../fakes/config/bom/tkg-bom-v1.3.1.yaml", 2*time.Millisecond) + g.Expect(err).To(BeNil()) + + regionalClusterClient := &fakes.ClusterClient{} + regionalClusterClient.ListClustersReturns([]capi.Cluster{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "regional-cluster-2", + Namespace: constants.DefaultNamespace, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "workload-cluster", + Namespace: constants.DefaultNamespace, + }, + }, + }, nil) + regionalClusterClient.GetResourceStub = func(obj interface{}, name string, namespace string, verifyFunc clusterclient.PostVerifyrFunc, pollOptions *clusterclient.PollOptions) error { + if name == constants.KappControllerConfigMapName { + cm := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: constants.KappControllerConfigMapName, + Namespace: constants.KappControllerNamespace, + }, + Data: map[string]string{ + "httpProxy": "http://10.0.0.1:8080", + "httpsProxy": "http://10.0.0.1:8080", + "noProxy": "127.0.0.1,foo.com", + }, + } + cm.DeepCopyInto(obj.(*corev1.ConfigMap)) + } + + return nil + } + + workloadClusterClient := &fakes.ClusterClient{} + workloadClusterClient.GetResourceStub = func(obj interface{}, name string, namespace string, verifyFunc clusterclient.PostVerifyrFunc, pollOptions *clusterclient.PollOptions) error { + if name == constants.KappControllerConfigMapName { + cm := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: constants.KappControllerConfigMapName, + Namespace: constants.KappControllerNamespace, + }, + Data: map[string]string{ + "httpProxy": "http://10.0.0.1:8081", + "httpsProxy": "http://10.0.0.1:8081", + "noProxy": "127.0.0.1,bar.com", + }, + } + cm.DeepCopyInto(obj.(*corev1.ConfigMap)) + } + + return nil + } + + // validate retrieving management cluster settings + err = tkgClient.RetrieveRegionalClusterConfiguration(regionalClusterClient) + g.Expect(err).To(BeNil()) + + httpProxy, err := tkgClient.TKGConfigReaderWriter().Get(constants.TKGHTTPProxy) + g.Expect(httpProxy).To(Equal("http://10.0.0.1:8080")) + g.Expect(err).To(BeNil()) + httpsProxy, err := tkgClient.TKGConfigReaderWriter().Get(constants.TKGHTTPSProxy) + g.Expect(httpsProxy).To(Equal("http://10.0.0.1:8080")) + g.Expect(err).To(BeNil()) + noProxy, err := tkgClient.TKGConfigReaderWriter().Get(constants.TKGNoProxy) + g.Expect(noProxy).To(Equal("127.0.0.1,foo.com")) + g.Expect(err).To(BeNil()) + + // validate retrieving workload cluster settings + err = tkgClient.RetrieveWorkloadClusterConfiguration(regionalClusterClient, workloadClusterClient, "workload-cluster", constants.DefaultNamespace) + g.Expect(err).To(BeNil()) + + httpProxy, err = tkgClient.TKGConfigReaderWriter().Get(constants.TKGHTTPProxy) + g.Expect(httpProxy).To(Equal("http://10.0.0.1:8081")) + g.Expect(err).To(BeNil()) + httpsProxy, err = tkgClient.TKGConfigReaderWriter().Get(constants.TKGHTTPSProxy) + g.Expect(httpsProxy).To(Equal("http://10.0.0.1:8081")) + g.Expect(err).To(BeNil()) + noProxy, err = tkgClient.TKGConfigReaderWriter().Get(constants.TKGNoProxy) + g.Expect(noProxy).To(Equal("127.0.0.1,bar.com")) + g.Expect(err).To(BeNil()) +}