diff --git a/go.mod b/go.mod index b722b21f057dfca9f4fb80529718a92e8136b064..2b74585239976cdab81b60a13fb2ed94d89fadaa 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module nestos-kubernetes-deployer go 1.17 require ( + github.com/agiledragon/gomonkey/v2 v2.12.0 github.com/clarketm/json v1.17.1 github.com/coreos/ignition/v2 v2.14.0 github.com/hashicorp/terraform-exec v0.17.2 diff --git a/go.sum b/go.sum index 8453a7cc5c0c7fb29d6ecba2c91d6042968c5f9e..45ee66791c21ce0c1cd8a010b260b52694b96334 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,10 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= +github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U= +github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/agiledragon/gomonkey/v2 v2.12.0 h1:ek0dYu9K1rSV+TgkW5LvNNPRWyDZVIxGMCFI6Pz9o38= +github.com/agiledragon/gomonkey/v2 v2.12.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= @@ -209,6 +213,7 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= @@ -239,6 +244,7 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -301,6 +307,8 @@ github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92 h1:OfRzdxCzDhp+rsK github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92/go.mod h1:7/OT02F6S6I7v6WXb+IjhMuZEYfH/RJ5RwEWnEo5BMg= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= @@ -524,6 +532,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/pkg/kubeclient/ctl_test.go b/pkg/kubeclient/ctl_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0798414f829543d14c7309e9f83ee2da478e2ae8 --- /dev/null +++ b/pkg/kubeclient/ctl_test.go @@ -0,0 +1,177 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 kubeclient + +import ( + "github.com/agiledragon/gomonkey/v2" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" + "log" + "testing" +) + +var ( + kubeconfigPath = "./kubeconfig" + yamlContent = "" + //namespace = "test_" +) + +func TestCreateClient(t *testing.T) { + t.Run("CreateClient_fail", func(t *testing.T) { + clientset, err := CreateClient(kubeconfigPath) + + if err != nil { + t.Errorf("CreateClient returned error: %v", err) + return + } + if clientset == nil { + t.Errorf("CreateClient is empty") + return + } + + log.Println("TestCreateClient success") + }) + + t.Run("CreateDynamicClient_fail", func(t *testing.T) { + CreateDynamicClient, err := CreateDynamicClient(kubeconfigPath) + if err != nil { + t.Errorf("CreateDynamicClient returned error: %v", err) + return + } + if CreateDynamicClient == nil { + t.Errorf("CreateDynamicClient is empty") + return + } + + log.Println("TestCreateDynamicClient success") + }) + + p := gomonkey.ApplyFunc(clientcmd.BuildConfigFromFlags, func(string, string) (*rest.Config, error) { + return &rest.Config{}, nil + }) + + defer p.Reset() + + t.Run("CreateClient_dyn_fail", func(t *testing.T) { + clientset, err := CreateClient(kubeconfigPath) + + if err != nil { + t.Errorf("CreateClient returned error: %v", err) + return + } + if clientset == nil { + t.Errorf("CreateClient is empty") + return + } + + log.Println("TestCreateClient success") + }) + + t.Run("CreateDynamicClient_dyn_fail", func(t *testing.T) { + CreateDynamicClient, err := CreateDynamicClient(kubeconfigPath) + if err != nil { + t.Errorf("CreateDynamicClient returned error: %v", err) + return + } + if CreateDynamicClient == nil { + t.Errorf("CreateDynamicClient is empty") + return + } + + log.Println("TestCreateDynamicClient success") + }) + + kp := gomonkey.ApplyFunc(kubernetes.NewForConfig, func(config *rest.Config) (*kubernetes.Clientset, error) { + return &kubernetes.Clientset{}, nil + }) + + defer kp.Reset() + + t.Run("CreateClient", func(t *testing.T) { + clientset, err := CreateClient(kubeconfigPath) + + if err != nil { + t.Errorf("CreateClient returned error: %v", err) + return + } + if clientset == nil { + t.Errorf("CreateClient is empty") + return + } + + log.Println("TestCreateClient success") + }) + + t.Run("CreateDynamicClient", func(t *testing.T) { + CreateDynamicClient, err := CreateDynamicClient(kubeconfigPath) + if err != nil { + t.Errorf("CreateDynamicClient returned error: %v", err) + return + } + if CreateDynamicClient == nil { + t.Errorf("CreateDynamicClient is empty") + return + } + + log.Println("TestCreateDynamicClient success") + }) + + t.Run("DeployCRD", func(t *testing.T) { + DeployCRD(yamlContent, kubeconfigPath) + }) + t.Run("DeployNamespace", func(t *testing.T) { + DeployNamespace(yamlContent, kubeconfigPath) + }) + + t.Run("DeployClusterRoleBinding", func(t *testing.T) { + DeployClusterRoleBinding(yamlContent, kubeconfigPath) + }) + + //t.Run("DeployDeployment", func(t *testing.T) { + // //patches := gomonkey.ApplyMethod(reflect.TypeOf(&appsv1.Deployment{}), "Create", func(_ *appsv1.Deployment, ctx context.Context, deployment *appsv1.Deployment, opts metav1.CreateOptions) (*appsv1.Deployment, error) { + // // return &appsv1.Deployment{ + // // ObjectMeta: metav1.ObjectMeta{ + // // Name: deployment.Name, + // // }, + // // }, nil + // //}) + // //defer patches.Reset() + // d := appsv1.Deployment{} + // t.Log(d) + // DeployDeployment(yamlContent, kubeconfigPath, namespace) + //}) + // + //t.Run("DeployDaemonSet", func(t *testing.T) { + // DeployDaemonSet(yamlContent, kubeconfigPath, namespace) + //}) + t.Run("ApplyHousekeeperCR", func(t *testing.T) { + ApplyHousekeeperCR(yamlContent, kubeconfigPath) + }) + t.Run("RunKubectlApplyWithYaml", func(t *testing.T) { + RunKubectlApplyWithYaml(yamlContent) + }) + +} + +func TestIsKubectlInstalled(t *testing.T) { + b := IsKubectlInstalled() + if !b { + t.Error("no install") + return + } + t.Log("IsKubectlInstalled success") +} diff --git a/vendor/modules.txt b/vendor/modules.txt index c224d174412373943d431ffaad05fcf62be018ba..892cc92cc2bb05c39722f0200448426cca8cef64 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -4,6 +4,10 @@ github.com/PuerkitoBio/purell # github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 ## explicit github.com/PuerkitoBio/urlesc +# github.com/agiledragon/gomonkey/v2 v2.12.0 +## explicit; go 1.14 +github.com/agiledragon/gomonkey/v2 +github.com/agiledragon/gomonkey/v2/creflect # github.com/clarketm/json v1.17.1 ## explicit github.com/clarketm/json