github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/cluster-runtime/installer_test.go (about) 1 // Copyright © 2022 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 package clusterruntime 15 16 import ( 17 "net" 18 "reflect" 19 "testing" 20 21 containerruntime "github.com/sealerio/sealer/pkg/container-runtime" 22 "github.com/sealerio/sealer/pkg/infradriver" 23 "github.com/sealerio/sealer/pkg/registry" 24 "github.com/sealerio/sealer/pkg/runtime" 25 v1 "github.com/sealerio/sealer/types/api/v1" 26 v2 "github.com/sealerio/sealer/types/api/v2" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestInstaller_GetCurrentDriver(t *testing.T) { 32 type fields struct { 33 RuntimeConfig RuntimeConfig 34 infraDriver infradriver.InfraDriver 35 containerRuntimeInstaller containerruntime.Installer 36 clusterRuntimeType string 37 hooks map[Phase]HookConfigList 38 regConfig v2.Registry 39 } 40 var tests []struct { 41 name string 42 fields fields 43 want registry.Driver 44 want1 runtime.Driver 45 wantErr bool 46 } 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 i := &Installer{ 50 RuntimeConfig: tt.fields.RuntimeConfig, 51 infraDriver: tt.fields.infraDriver, 52 containerRuntimeInstaller: tt.fields.containerRuntimeInstaller, 53 clusterRuntimeType: tt.fields.clusterRuntimeType, 54 hooks: tt.fields.hooks, 55 regConfig: tt.fields.regConfig, 56 } 57 got, got1, err := i.GetCurrentDriver() 58 if (err != nil) != tt.wantErr { 59 t.Errorf("GetCurrentDriver() error = %v, wantErr %v", err, tt.wantErr) 60 return 61 } 62 if !reflect.DeepEqual(got, tt.want) { 63 t.Errorf("GetCurrentDriver() got = %v, want %v", got, tt.want) 64 } 65 if !reflect.DeepEqual(got1, tt.want1) { 66 t.Errorf("GetCurrentDriver() got1 = %v, want %v", got1, tt.want1) 67 } 68 }) 69 } 70 } 71 72 func TestInstaller_setNodeLabels(t *testing.T) { 73 type fields struct { 74 RuntimeConfig RuntimeConfig 75 infraDriver infradriver.InfraDriver 76 containerRuntimeInstaller containerruntime.Installer 77 clusterRuntimeType string 78 hooks map[Phase]HookConfigList 79 regConfig v2.Registry 80 } 81 type args struct { 82 hosts []net.IP 83 driver runtime.Driver 84 } 85 var tests []struct { 86 name string 87 fields fields 88 args args 89 wantErr bool 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 i := &Installer{ 94 RuntimeConfig: tt.fields.RuntimeConfig, 95 infraDriver: tt.fields.infraDriver, 96 containerRuntimeInstaller: tt.fields.containerRuntimeInstaller, 97 clusterRuntimeType: tt.fields.clusterRuntimeType, 98 hooks: tt.fields.hooks, 99 regConfig: tt.fields.regConfig, 100 } 101 if err := i.setNodeLabels(tt.args.hosts, tt.args.driver); (err != nil) != tt.wantErr { 102 t.Errorf("setNodeLabels() error = %v, wantErr %v", err, tt.wantErr) 103 } 104 }) 105 } 106 } 107 108 func Test_chooseCRIInstaller(t *testing.T) { 109 type args struct { 110 containerRuntime string 111 } 112 tests := []struct { 113 name string 114 args args 115 want v2.ContainerRuntimeConfig 116 wantErr bool 117 }{ 118 { 119 name: "test for choose docker", 120 args: args{ 121 containerRuntime: "docker", 122 }, 123 want: v2.ContainerRuntimeConfig{Type: "docker"}, 124 wantErr: false, 125 }, 126 { 127 name: "test for choose containerd", 128 args: args{ 129 containerRuntime: "containerd", 130 }, 131 want: v2.ContainerRuntimeConfig{Type: "containerd"}, 132 wantErr: false, 133 }, 134 } 135 for _, tt := range tests { 136 t.Run(tt.name, func(t *testing.T) { 137 infraDriver, err := getDefaultCluster() 138 if err != nil { 139 assert.Error(t, err) 140 } 141 142 got, err := getCRIInstaller(tt.args.containerRuntime, infraDriver) 143 if err != nil { 144 t.Errorf("chooseCRIInstaller() error = %v, wantErr %v", err, tt.wantErr) 145 return 146 } 147 info, err := got.GetInfo() 148 if err != nil { 149 assert.Error(t, err) 150 } 151 152 if !reflect.DeepEqual(info.Type, tt.want.Type) { 153 t.Errorf("chooseCRIInstaller() got = %v, want %v", info.Type, tt.want) 154 } 155 }) 156 } 157 } 158 159 func getDefaultCluster() (infradriver.InfraDriver, error) { 160 cluster := &v2.Cluster{ 161 Spec: v2.ClusterSpec{ 162 Image: "kubernetes:v1.19.8", 163 Env: []string{"key1=value1", "key2=value2;value3"}, 164 SSH: v1.SSH{ 165 User: "root", 166 Passwd: "test123", 167 Port: "22", 168 Pk: "xxx", 169 PkPasswd: "xxx", 170 }, 171 Hosts: []v2.Host{ 172 { 173 IPS: []net.IP{net.IPv4(192, 168, 0, 2)}, 174 Roles: []string{"master"}, 175 Env: []string{"etcd-dir=/data/etcd"}, 176 SSH: v1.SSH{ 177 User: "root", 178 Passwd: "test456", 179 Port: "22", 180 }, 181 }, 182 { 183 IPS: []net.IP{net.IPv4(192, 168, 0, 3)}, 184 Roles: []string{"node", "db"}, 185 Env: []string{"test_node_env_key=test_node_env_value"}, 186 }, 187 }, 188 }, 189 } 190 cluster.APIVersion = "sealer.io/v2" 191 cluster.Kind = "Cluster" 192 cluster.Name = "my-cluster" 193 194 return infradriver.NewInfraDriver(cluster) 195 }