github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/debug/clusterinfo/clusterinfo_test.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package clusterinfo
    16  
    17  import (
    18  	"context"
    19  	"sort"
    20  	"testing"
    21  
    22  	"k8s.io/client-go/kubernetes"
    23  	"k8s.io/client-go/tools/clientcmd"
    24  
    25  	"github.com/alibaba/sealer/common"
    26  )
    27  
    28  func TestGetPodsIP(t *testing.T) {
    29  	restConfig, err := clientcmd.BuildConfigFromFlags("", common.KubeAdminConf)
    30  	if err != nil {
    31  		t.Errorf("failed to get rest config from file %s", common.KubeAdminConf)
    32  	}
    33  
    34  	kubeClientSet, err := kubernetes.NewForConfig(restConfig)
    35  	if err != nil {
    36  		t.Errorf("failed to create kubernetes client from file %s", common.KubeAdminConf)
    37  	}
    38  
    39  	tests := []struct {
    40  		testName  string
    41  		namespace string
    42  	}{
    43  		{
    44  			testName:  "default",
    45  			namespace: "default",
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		t.Run(tt.testName, func(t *testing.T) {
    51  			_, err := GetPodsIP(context.Background(), kubeClientSet.CoreV1(), tt.namespace)
    52  			if err != nil {
    53  				t.Errorf("failed to get pods IP")
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func TestGetNodesIP(t *testing.T) {
    60  	restConfig, err := clientcmd.BuildConfigFromFlags("", common.KubeAdminConf)
    61  	if err != nil {
    62  		t.Errorf("failed to get rest config from file %s", common.KubeAdminConf)
    63  	}
    64  
    65  	kubeClientSet, err := kubernetes.NewForConfig(restConfig)
    66  	if err != nil {
    67  		t.Errorf("failed to create kubernetes client from file %s", common.KubeAdminConf)
    68  	}
    69  
    70  	t.Run("GetNodesIP", func(t *testing.T) {
    71  		_, err := GetNodesIP(context.Background(), kubeClientSet.CoreV1())
    72  		if err != nil {
    73  			t.Errorf("failed to get nodes IP")
    74  		}
    75  	})
    76  }
    77  
    78  func TestGetDNSServiceAll(t *testing.T) {
    79  	restConfig, err := clientcmd.BuildConfigFromFlags("", common.KubeAdminConf)
    80  	if err != nil {
    81  		t.Errorf("failed to get rest config from file %s", common.KubeAdminConf)
    82  	}
    83  
    84  	kubeClientSet, err := kubernetes.NewForConfig(restConfig)
    85  	if err != nil {
    86  		t.Errorf("failed to create kubernetes client from file %s", common.KubeAdminConf)
    87  	}
    88  
    89  	t.Run("GetDNSServiceAll", func(t *testing.T) {
    90  		_, _, _, err := GetDNSServiceAll(context.Background(), kubeClientSet.CoreV1())
    91  		if err != nil {
    92  			t.Errorf("failed to get DNS Service")
    93  		}
    94  	})
    95  }
    96  
    97  func TestRemoveDuplicatesAndEmpty(t *testing.T) {
    98  	tests := []struct {
    99  		testName string
   100  		ipList   []string
   101  	}{
   102  		{
   103  			testName: "duplicates",
   104  			ipList:   []string{"192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.2"},
   105  		},
   106  		{
   107  			testName: "empty",
   108  			ipList:   []string{"192.168.1.2", "192.168.1.3", "", "192.168.1.5"},
   109  		},
   110  		{
   111  			testName: "duplicatesAndEmpty",
   112  			ipList:   []string{"192.168.1.2", "", "192.168.1.3", "", "192.168.1.4", "192.168.1.2"},
   113  		},
   114  	}
   115  
   116  	for _, tt := range tests {
   117  		t.Run(tt.testName, func(t *testing.T) {
   118  			ss := removeDuplicatesAndEmpty(tt.ipList)
   119  			sort.Strings(ss)
   120  			for i := 0; i < len(ss)-1; i++ {
   121  				if ss[i] == ss[i+1] || len(ss[i]) == 0 || len(ss[i+1]) == 0 {
   122  					t.Errorf("failed to remove duplicates and empty string")
   123  				}
   124  			}
   125  		})
   126  	}
   127  }