|  | 
|  | 1 | +/* | 
|  | 2 | +   Copyright The SpinKube 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 main_test | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"reflect" | 
|  | 21 | +	"testing" | 
|  | 22 | + | 
|  | 23 | +	"github.com/spf13/afero" | 
|  | 24 | +	main "github.com/spinkube/runtime-class-manager/cmd/node-installer" | 
|  | 25 | +	"github.com/spinkube/runtime-class-manager/internal/preset" | 
|  | 26 | +	tests "github.com/spinkube/runtime-class-manager/tests/node-installer" | 
|  | 27 | +	"github.com/stretchr/testify/require" | 
|  | 28 | +) | 
|  | 29 | + | 
|  | 30 | +func Test_DetectDistro(t *testing.T) { | 
|  | 31 | +	type args struct { | 
|  | 32 | +		config main.Config | 
|  | 33 | +		hostFs afero.Fs | 
|  | 34 | +	} | 
|  | 35 | +	tests := []struct { | 
|  | 36 | +		name       string | 
|  | 37 | +		args       args | 
|  | 38 | +		wantErr    bool | 
|  | 39 | +		wantPreset preset.Settings | 
|  | 40 | +	}{ | 
|  | 41 | +		{ | 
|  | 42 | +			"config_override", | 
|  | 43 | +			args{ | 
|  | 44 | +				main.Config{ | 
|  | 45 | +					struct { | 
|  | 46 | +						Name       string | 
|  | 47 | +						ConfigPath string | 
|  | 48 | +					}{"containerd", preset.MicroK8s.ConfigPath}, | 
|  | 49 | +					struct { | 
|  | 50 | +						Path      string | 
|  | 51 | +						AssetPath string | 
|  | 52 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 53 | +					struct{ RootPath string }{""}, | 
|  | 54 | +				}, | 
|  | 55 | +				tests.FixtureFs("../../testdata/node-installer/distros/default"), | 
|  | 56 | +			}, | 
|  | 57 | +			false, | 
|  | 58 | +			preset.MicroK8s, | 
|  | 59 | +		}, | 
|  | 60 | +		{ | 
|  | 61 | +			"config_not_found_fallback_default", | 
|  | 62 | +			args{ | 
|  | 63 | +				main.Config{ | 
|  | 64 | +					struct { | 
|  | 65 | +						Name       string | 
|  | 66 | +						ConfigPath string | 
|  | 67 | +					}{"containerd", "/etc/containerd/not_found.toml"}, | 
|  | 68 | +					struct { | 
|  | 69 | +						Path      string | 
|  | 70 | +						AssetPath string | 
|  | 71 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 72 | +					struct{ RootPath string }{""}, | 
|  | 73 | +				}, | 
|  | 74 | +				tests.FixtureFs("../../testdata/node-installer/distros/default"), | 
|  | 75 | +			}, | 
|  | 76 | +			false, | 
|  | 77 | +			preset.Default.WithConfigPath("/etc/containerd/not_found.toml"), | 
|  | 78 | +		}, | 
|  | 79 | +		{ | 
|  | 80 | +			"unsupported", | 
|  | 81 | +			args{ | 
|  | 82 | +				main.Config{ | 
|  | 83 | +					struct { | 
|  | 84 | +						Name       string | 
|  | 85 | +						ConfigPath string | 
|  | 86 | +					}{"containerd", ""}, | 
|  | 87 | +					struct { | 
|  | 88 | +						Path      string | 
|  | 89 | +						AssetPath string | 
|  | 90 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 91 | +					struct{ RootPath string }{""}, | 
|  | 92 | +				}, | 
|  | 93 | +				tests.FixtureFs("../../testdata/node-installer/distros/unsupported"), | 
|  | 94 | +			}, | 
|  | 95 | +			true, | 
|  | 96 | +			preset.Default, | 
|  | 97 | +		}, | 
|  | 98 | +		{ | 
|  | 99 | +			"microk8s", | 
|  | 100 | +			args{ | 
|  | 101 | +				main.Config{ | 
|  | 102 | +					struct { | 
|  | 103 | +						Name       string | 
|  | 104 | +						ConfigPath string | 
|  | 105 | +					}{"containerd", ""}, | 
|  | 106 | +					struct { | 
|  | 107 | +						Path      string | 
|  | 108 | +						AssetPath string | 
|  | 109 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 110 | +					struct{ RootPath string }{""}, | 
|  | 111 | +				}, | 
|  | 112 | +				tests.FixtureFs("../../testdata/node-installer/distros/microk8s"), | 
|  | 113 | +			}, | 
|  | 114 | +			false, | 
|  | 115 | +			preset.MicroK8s, | 
|  | 116 | +		}, | 
|  | 117 | +		{ | 
|  | 118 | +			"k0s", | 
|  | 119 | +			args{ | 
|  | 120 | +				main.Config{ | 
|  | 121 | +					struct { | 
|  | 122 | +						Name       string | 
|  | 123 | +						ConfigPath string | 
|  | 124 | +					}{"containerd", ""}, | 
|  | 125 | +					struct { | 
|  | 126 | +						Path      string | 
|  | 127 | +						AssetPath string | 
|  | 128 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 129 | +					struct{ RootPath string }{""}, | 
|  | 130 | +				}, | 
|  | 131 | +				tests.FixtureFs("../../testdata/node-installer/distros/k0s"), | 
|  | 132 | +			}, | 
|  | 133 | +			false, | 
|  | 134 | +			preset.K0s, | 
|  | 135 | +		}, | 
|  | 136 | +		{ | 
|  | 137 | +			"k3s", | 
|  | 138 | +			args{ | 
|  | 139 | +				main.Config{ | 
|  | 140 | +					struct { | 
|  | 141 | +						Name       string | 
|  | 142 | +						ConfigPath string | 
|  | 143 | +					}{"containerd", ""}, | 
|  | 144 | +					struct { | 
|  | 145 | +						Path      string | 
|  | 146 | +						AssetPath string | 
|  | 147 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 148 | +					struct{ RootPath string }{""}, | 
|  | 149 | +				}, | 
|  | 150 | +				tests.FixtureFs("../../testdata/node-installer/distros/k3s"), | 
|  | 151 | +			}, | 
|  | 152 | +			false, | 
|  | 153 | +			preset.K3s, | 
|  | 154 | +		}, | 
|  | 155 | +		{ | 
|  | 156 | +			"rke2", | 
|  | 157 | +			args{ | 
|  | 158 | +				main.Config{ | 
|  | 159 | +					struct { | 
|  | 160 | +						Name       string | 
|  | 161 | +						ConfigPath string | 
|  | 162 | +					}{"containerd", ""}, | 
|  | 163 | +					struct { | 
|  | 164 | +						Path      string | 
|  | 165 | +						AssetPath string | 
|  | 166 | +					}{"/opt/kwasm", "/assets"}, | 
|  | 167 | +					struct{ RootPath string }{""}, | 
|  | 168 | +				}, | 
|  | 169 | +				tests.FixtureFs("../../testdata/node-installer/distros/rke2"), | 
|  | 170 | +			}, | 
|  | 171 | +			false, | 
|  | 172 | +			preset.RKE2, | 
|  | 173 | +		}, | 
|  | 174 | +	} | 
|  | 175 | +	for _, tt := range tests { | 
|  | 176 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 177 | +			preset, err := main.DetectDistro(tt.args.config, tt.args.hostFs) | 
|  | 178 | +			if tt.wantErr { | 
|  | 179 | +				require.Error(t, err) | 
|  | 180 | +			} else { | 
|  | 181 | +				require.NoError(t, err) | 
|  | 182 | +				require.Equal(t, tt.wantPreset.ConfigPath, preset.ConfigPath) | 
|  | 183 | +				require.Equal(t, reflect.ValueOf(tt.wantPreset.Setup), reflect.ValueOf(preset.Setup)) | 
|  | 184 | +				require.Equal(t, reflect.ValueOf(tt.wantPreset.Restarter), reflect.ValueOf(preset.Restarter)) | 
|  | 185 | +			} | 
|  | 186 | +		}) | 
|  | 187 | +	} | 
|  | 188 | +} | 
0 commit comments