mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 19:23:40 +00:00
kubeadm: increase ut converage for config/upgradeconfiguration
Signed-off-by: xin.li <xin.li@daocloud.io>
This commit is contained in:
parent
d3d06c3c7e
commit
a4fe397ebd
@ -17,10 +17,13 @@ limitations under the License.
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
|
"github.com/lithammer/dedent"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/utils/ptr"
|
"k8s.io/utils/ptr"
|
||||||
@ -104,3 +107,244 @@ func TestDocMapToUpgradeConfiguration(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadUpgradeConfigurationFromFile(t *testing.T) {
|
||||||
|
tmpdir, err := os.MkdirTemp("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := os.RemoveAll(tmpdir); err != nil {
|
||||||
|
t.Fatalf("Couldn't remove tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
filename := "kubeadmConfig"
|
||||||
|
filePath := filepath.Join(tmpdir, filename)
|
||||||
|
options := LoadOrDefaultConfigurationOptions{}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
cfgPath string
|
||||||
|
fileContents string
|
||||||
|
want *kubeadmapi.UpgradeConfiguration
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Config file does not exists",
|
||||||
|
cfgPath: "tmp",
|
||||||
|
want: nil,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Config file format is basic text",
|
||||||
|
cfgPath: filePath,
|
||||||
|
want: nil,
|
||||||
|
fileContents: "some-text",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unknown kind UpgradeConfiguration for kubeadm.k8s.io/unknown",
|
||||||
|
cfgPath: filePath,
|
||||||
|
fileContents: dedent.Dedent(`
|
||||||
|
apiVersion: kubeadm.k8s.io/unknown
|
||||||
|
kind: UpgradeConfiguration
|
||||||
|
`),
|
||||||
|
want: nil,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid kubeadm config",
|
||||||
|
cfgPath: filePath,
|
||||||
|
fileContents: dedent.Dedent(`
|
||||||
|
apiVersion: kubeadm.k8s.io/v1beta4
|
||||||
|
kind: UpgradeConfiguration`),
|
||||||
|
want: &kubeadmapi.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapi.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
Node: kubeadmapi.UpgradeNodeConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if tt.cfgPath == filePath {
|
||||||
|
err = os.WriteFile(tt.cfgPath, []byte(tt.fileContents), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't write content to file: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := os.RemoveAll(filePath); err != nil {
|
||||||
|
t.Fatalf("Couldn't remove filePath: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := LoadUpgradeConfigurationFromFile(tt.cfgPath, options)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("LoadUpgradeConfigurationFromFile() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if tt.want == nil && got != tt.want {
|
||||||
|
t.Errorf("LoadUpgradeConfigurationFromFile() got = %v, want %v", got, tt.want)
|
||||||
|
} else if tt.want != nil {
|
||||||
|
if diff := cmp.Diff(got, tt.want, cmpopts.IgnoreFields(kubeadmapi.UpgradeConfiguration{}, "Timeouts")); diff != "" {
|
||||||
|
t.Errorf("LoadUpgradeConfigurationFromFile returned unexpected diff (-want,+got):\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultedUpgradeConfiguration(t *testing.T) {
|
||||||
|
options := LoadOrDefaultConfigurationOptions{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
cfg *kubeadmapiv1.UpgradeConfiguration
|
||||||
|
want *kubeadmapi.UpgradeConfiguration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "config is empty",
|
||||||
|
cfg: &kubeadmapiv1.UpgradeConfiguration{},
|
||||||
|
want: &kubeadmapi.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapi.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
Node: kubeadmapi.UpgradeNodeConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "config has some fields configured",
|
||||||
|
cfg: &kubeadmapiv1.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapiv1.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(false),
|
||||||
|
},
|
||||||
|
Node: kubeadmapiv1.UpgradeNodeConfiguration{
|
||||||
|
EtcdUpgrade: ptr.To(false),
|
||||||
|
},
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
|
||||||
|
Kind: constants.UpgradeConfigurationKind,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &kubeadmapi.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapi.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(false),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
Node: kubeadmapi.UpgradeNodeConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(false),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, _ := DefaultedUpgradeConfiguration(tt.cfg, options)
|
||||||
|
if diff := cmp.Diff(got, tt.want, cmpopts.IgnoreFields(kubeadmapi.UpgradeConfiguration{}, "Timeouts")); diff != "" {
|
||||||
|
t.Errorf("DefaultedUpgradeConfiguration returned unexpected diff (-want,+got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadOrDefaultUpgradeConfiguration(t *testing.T) {
|
||||||
|
tmpdir, err := os.MkdirTemp("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := os.RemoveAll(tmpdir); err != nil {
|
||||||
|
t.Fatalf("Couldn't remove tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
filename := "kubeadmConfig"
|
||||||
|
filePath := filepath.Join(tmpdir, filename)
|
||||||
|
fileContents := dedent.Dedent(`
|
||||||
|
apiVersion: kubeadm.k8s.io/v1beta4
|
||||||
|
kind: UpgradeConfiguration
|
||||||
|
`)
|
||||||
|
err = os.WriteFile(filePath, []byte(fileContents), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't write content to file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
options := LoadOrDefaultConfigurationOptions{}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
cfgPath string
|
||||||
|
cfg *kubeadmapiv1.UpgradeConfiguration
|
||||||
|
want *kubeadmapi.UpgradeConfiguration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "cfgpPath is empty, the result should be obtained from cfg",
|
||||||
|
cfgPath: "",
|
||||||
|
cfg: &kubeadmapiv1.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapiv1.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(false),
|
||||||
|
},
|
||||||
|
Node: kubeadmapiv1.UpgradeNodeConfiguration{
|
||||||
|
EtcdUpgrade: ptr.To(false),
|
||||||
|
},
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
|
||||||
|
Kind: constants.UpgradeConfigurationKind,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &kubeadmapi.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapi.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(false),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
Node: kubeadmapi.UpgradeNodeConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(false),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "cfgpPath is not empty, the result should be obtained from the configuration file",
|
||||||
|
cfgPath: filePath,
|
||||||
|
cfg: &kubeadmapiv1.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapiv1.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(false),
|
||||||
|
},
|
||||||
|
Node: kubeadmapiv1.UpgradeNodeConfiguration{
|
||||||
|
EtcdUpgrade: ptr.To(false),
|
||||||
|
},
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
|
||||||
|
Kind: constants.UpgradeConfigurationKind,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: &kubeadmapi.UpgradeConfiguration{
|
||||||
|
Apply: kubeadmapi.UpgradeApplyConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
Node: kubeadmapi.UpgradeNodeConfiguration{
|
||||||
|
CertificateRenewal: ptr.To(true),
|
||||||
|
EtcdUpgrade: ptr.To(true),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, _ := LoadOrDefaultUpgradeConfiguration(tt.cfgPath, tt.cfg, options)
|
||||||
|
if diff := cmp.Diff(got, tt.want, cmpopts.IgnoreFields(kubeadmapi.UpgradeConfiguration{}, "Timeouts")); diff != "" {
|
||||||
|
t.Errorf("LoadOrDefaultUpgradeConfiguration returned unexpected diff (-want,+got):\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user