github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/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  	"sort"
    19  	"testing"
    20  )
    21  
    22  /* func TestGetPodsIP(t *testing.T) {
    23  	restConfig, err := clientcmd.BuildConfigFromFlags("", common.KubeAdminConf)
    24  	if err != nil {
    25  		t.Errorf("failed to get rest config from file %s", common.KubeAdminConf)
    26  	}
    27  
    28  	kubeClientSet, err := kubernetes.NewForConfig(restConfig)
    29  	if err != nil {
    30  		t.Errorf("failed to create kubernetes client from file %s", common.KubeAdminConf)
    31  	}
    32  
    33  	tests := []struct {
    34  		testName  string
    35  		namespace string
    36  	}{
    37  		{
    38  			testName:  "default",
    39  			namespace: "default",
    40  		},
    41  	}
    42  
    43  	for _, tt := range tests {
    44  		t.Run(tt.testName, func(t *testing.T) {
    45  			_, err := GetPodsIP(context.Background(), kubeClientSet.CoreV1(), tt.namespace)
    46  			if err != nil {
    47  				t.Errorf("failed to get pods IP")
    48  			}
    49  		})
    50  	}
    51  }
    52  
    53  func TestGetNodesIP(t *testing.T) {
    54  	restConfig, err := clientcmd.BuildConfigFromFlags("", common.KubeAdminConf)
    55  	if err != nil {
    56  		t.Errorf("failed to get rest config from file %s", common.KubeAdminConf)
    57  	}
    58  
    59  	kubeClientSet, err := kubernetes.NewForConfig(restConfig)
    60  	if err != nil {
    61  		t.Errorf("failed to create kubernetes client from file %s", common.KubeAdminConf)
    62  	}
    63  
    64  	t.Run("GetNodesIP", func(t *testing.T) {
    65  		_, err := GetNodesIP(context.Background(), kubeClientSet.CoreV1())
    66  		if err != nil {
    67  			t.Errorf("failed to get nodes IP")
    68  		}
    69  	})
    70  }
    71  
    72  
    73  func TestGetDNSServiceAll(t *testing.T) {
    74  	restConfig, err := clientcmd.BuildConfigFromFlags("", common.KubeAdminConf)
    75  	if err != nil {
    76  		t.Errorf("failed to get rest config from file %s", common.KubeAdminConf)
    77  	}
    78  
    79  	kubeClientSet, err := kubernetes.NewForConfig(restConfig)
    80  	if err != nil {
    81  		t.Errorf("failed to create kubernetes client from file %s", common.KubeAdminConf)
    82  	}
    83  
    84  	t.Run("GetDNSServiceAll", func(t *testing.T) {
    85  		_, _, _, err := GetDNSServiceAll(context.Background(), kubeClientSet.CoreV1())
    86  		if err != nil {
    87  			t.Errorf("failed to get DNS Service")
    88  		}
    89  	})
    90  }*/
    91  
    92  func TestRemoveDuplicatesAndEmpty(t *testing.T) {
    93  	tests := []struct {
    94  		testName string
    95  		ipList   []string
    96  	}{
    97  		{
    98  			testName: "duplicates",
    99  			ipList:   []string{"192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.2"},
   100  		},
   101  		{
   102  			testName: "empty",
   103  			ipList:   []string{"192.168.1.2", "192.168.1.3", "", "192.168.1.5"},
   104  		},
   105  		{
   106  			testName: "duplicatesAndEmpty",
   107  			ipList:   []string{"192.168.1.2", "", "192.168.1.3", "", "192.168.1.4", "192.168.1.2"},
   108  		},
   109  	}
   110  
   111  	for _, tt := range tests {
   112  		t.Run(tt.testName, func(t *testing.T) {
   113  			ss := removeDuplicatesAndEmpty(tt.ipList)
   114  			sort.Strings(ss)
   115  			for i := 0; i < len(ss)-1; i++ {
   116  				if ss[i] == ss[i+1] || len(ss[i]) == 0 || len(ss[i+1]) == 0 {
   117  					t.Errorf("failed to remove duplicates and empty string")
   118  				}
   119  			}
   120  		})
   121  	}
   122  }