k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/prometheus/experimental_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package prometheus
    18  
    19  import (
    20  	"testing"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  func Test_nameAndZone(t *testing.T) {
    27  	tests := []struct {
    28  		name     string
    29  		pv       corev1.PersistentVolume
    30  		wantName string
    31  		wantZone string
    32  		wantErr  bool
    33  	}{
    34  		{
    35  			name: "gce",
    36  			pv: corev1.PersistentVolume{
    37  				ObjectMeta: v1.ObjectMeta{
    38  					Labels: map[string]string{
    39  						"topology.kubernetes.io/zone": "us-zone-9",
    40  					},
    41  				},
    42  				Spec: corev1.PersistentVolumeSpec{
    43  					PersistentVolumeSource: corev1.PersistentVolumeSource{
    44  						GCEPersistentDisk: &corev1.GCEPersistentDiskVolumeSource{
    45  							PDName: "name-1",
    46  						},
    47  					},
    48  				},
    49  			},
    50  			wantName: "name-1",
    51  			wantZone: "us-zone-9",
    52  		},
    53  		{
    54  			name: "gce legacy label",
    55  			pv: corev1.PersistentVolume{
    56  				ObjectMeta: v1.ObjectMeta{
    57  					Labels: map[string]string{
    58  						"failure-domain.beta.kubernetes.io/zone": "us-zone-9",
    59  					},
    60  				},
    61  				Spec: corev1.PersistentVolumeSpec{
    62  					PersistentVolumeSource: corev1.PersistentVolumeSource{
    63  						GCEPersistentDisk: &corev1.GCEPersistentDiskVolumeSource{
    64  							PDName: "name-1",
    65  						},
    66  					},
    67  				},
    68  			},
    69  			wantName: "name-1",
    70  			wantZone: "us-zone-9",
    71  		},
    72  		{
    73  			name: "csi",
    74  			pv: corev1.PersistentVolume{
    75  				Spec: corev1.PersistentVolumeSpec{
    76  					PersistentVolumeSource: corev1.PersistentVolumeSource{
    77  						CSI: &corev1.CSIPersistentVolumeSource{
    78  							VolumeHandle: "projects/project-1234/zones/us-zone-9/disks/name-1",
    79  						},
    80  					},
    81  				},
    82  			},
    83  			wantName: "name-1",
    84  			wantZone: "us-zone-9",
    85  		},
    86  		{
    87  			name: "csi bad volumehandle",
    88  			pv: corev1.PersistentVolume{
    89  				Spec: corev1.PersistentVolumeSpec{
    90  					PersistentVolumeSource: corev1.PersistentVolumeSource{
    91  						CSI: &corev1.CSIPersistentVolumeSource{
    92  							VolumeHandle: "blah",
    93  						},
    94  					},
    95  				},
    96  			},
    97  			wantErr: true,
    98  		},
    99  		{
   100  			name: "unknown",
   101  			pv: corev1.PersistentVolume{
   102  				Spec: corev1.PersistentVolumeSpec{
   103  					PersistentVolumeSource: corev1.PersistentVolumeSource{
   104  						Glusterfs: &corev1.GlusterfsPersistentVolumeSource{},
   105  					},
   106  				},
   107  			},
   108  			wantErr: true,
   109  		},
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			gotName, gotZone, err := nameAndZone(tt.pv)
   114  			if (err != nil) != tt.wantErr {
   115  				t.Errorf("nameAndZone() error = %v, wantErr %v", err, tt.wantErr)
   116  				return
   117  			}
   118  			if gotName != tt.wantName {
   119  				t.Errorf("nameAndZone() gotName = %v, want %v", gotName, tt.wantName)
   120  			}
   121  			if gotZone != tt.wantZone {
   122  				t.Errorf("nameAndZone() gotZone = %v, want %v", gotZone, tt.wantZone)
   123  			}
   124  		})
   125  	}
   126  }