github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/util/provider_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package util
    21  
    22  import (
    23  	"fmt"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	corev1 "k8s.io/api/core/v1"
    29  
    30  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    31  )
    32  
    33  var _ = Describe("provider util", func() {
    34  
    35  	buildNodes := func(provider string) *corev1.NodeList {
    36  		return &corev1.NodeList{
    37  			Items: []corev1.Node{
    38  				{
    39  					Spec: corev1.NodeSpec{
    40  						ProviderID: fmt.Sprintf("%s://blabla", provider),
    41  					},
    42  				},
    43  			},
    44  		}
    45  	}
    46  	It("GetK8sProvider", func() {
    47  		cases := []struct {
    48  			description    string
    49  			version        string
    50  			expectVersion  string
    51  			expectProvider K8sProvider
    52  			isCloud        bool
    53  			nodes          *corev1.NodeList
    54  		}{
    55  			{
    56  				"unknown provider without providerID and unique version identifier",
    57  				"v1.25.0",
    58  				"1.25.0",
    59  				UnknownProvider,
    60  				false,
    61  				buildNodes(""),
    62  			},
    63  			{
    64  				"EKS with unique version identifier",
    65  				"v1.25.0-eks-123456",
    66  				"1.25.0",
    67  				EKSProvider,
    68  				true,
    69  				buildNodes(""),
    70  			},
    71  			{
    72  				"EKS with providerID",
    73  				"1.25.0",
    74  				"1.25.0",
    75  				EKSProvider,
    76  				true,
    77  				buildNodes("aws"),
    78  			},
    79  			{
    80  				"GKE with unique version identifier",
    81  				"v1.24.9-gke.3200",
    82  				"1.24.9",
    83  				GKEProvider,
    84  				true,
    85  				buildNodes(""),
    86  			},
    87  			{
    88  				"GKE with providerID",
    89  				"v1.24.9",
    90  				"1.24.9",
    91  				GKEProvider,
    92  				true,
    93  				buildNodes("gce"),
    94  			},
    95  			{
    96  				"TKE with unique version identifier",
    97  				"v1.24.4-tke.5",
    98  				"1.24.4",
    99  				TKEProvider,
   100  				true,
   101  				buildNodes(""),
   102  			},
   103  			{
   104  				"TKE with providerID",
   105  				"v1.24.9",
   106  				"1.24.9",
   107  				TKEProvider,
   108  				true,
   109  				buildNodes("qcloud"),
   110  			},
   111  			{
   112  				"ACK with unique version identifier, as ACK don't have providerID",
   113  				"v1.24.6-aliyun.1",
   114  				"1.24.6",
   115  				ACKProvider,
   116  				true,
   117  				buildNodes(""),
   118  			},
   119  			{
   120  				"AKS with providerID, as AKS don't have unique version identifier",
   121  				"v1.24.9",
   122  				"1.24.9",
   123  				AKSProvider,
   124  				true,
   125  				buildNodes("azure"),
   126  			},
   127  		}
   128  
   129  		for _, c := range cases {
   130  			By(c.description)
   131  			Expect(GetK8sSemVer(c.version)).Should(Equal(c.expectVersion))
   132  			client := testing.FakeClientSet(c.nodes)
   133  			p, err := GetK8sProvider(c.version, client)
   134  			Expect(err).ShouldNot(HaveOccurred())
   135  			Expect(p).Should(Equal(c.expectProvider))
   136  			Expect(p.IsCloud()).Should(Equal(c.isCloud))
   137  		}
   138  	})
   139  })