From 36904475db40b3c90dca36c389766e31d409fb08 Mon Sep 17 00:00:00 2001 From: "xin.li" Date: Fri, 9 Feb 2024 11:39:19 +0800 Subject: [PATCH] kubeadm: increase ut coverage for app/util Signed-off-by: xin.li --- cmd/kubeadm/app/util/copy_test.go | 89 ++++++++++++++++++++++++++++ cmd/kubeadm/app/util/marshal_test.go | 32 ++++++++++ 2 files changed, 121 insertions(+) create mode 100644 cmd/kubeadm/app/util/copy_test.go diff --git a/cmd/kubeadm/app/util/copy_test.go b/cmd/kubeadm/app/util/copy_test.go new file mode 100644 index 00000000000..ccc721fdf0e --- /dev/null +++ b/cmd/kubeadm/app/util/copy_test.go @@ -0,0 +1,89 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "os" + "testing" +) + +func TestCopyFile(t *testing.T) { + tmpdir, err := os.MkdirTemp("", "") + if err != nil { + t.Fatal("Failed to create temporary directory") + } + defer func() { + err = os.RemoveAll(tmpdir) + if err != nil { + t.Fatal("Failed to remove temporary directory") + } + }() + tmpfile, err := os.CreateTemp(tmpdir, "") + if err != nil { + t.Fatalf("Failed to create temporary file") + } + restrictedFile, err := os.CreateTemp(tmpdir, "") + if err != nil { + t.Fatal("Failed to create temporary restricted file") + } + err = restrictedFile.Chmod(0000) + if err != nil { + t.Fatal("Failed to change file mode") + } + + type args struct { + src string + dest string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "src file does not exist", + args: args{ + src: "foo", + dest: tmpdir + "bar", + }, + wantErr: true, + }, + { + name: "src file exists", + args: args{ + src: tmpfile.Name(), + dest: tmpdir + "bar", + }, + wantErr: false, + }, + { + name: "src file cannot be read or written", + args: args{ + src: restrictedFile.Name(), + dest: tmpdir + "bar", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := CopyFile(tt.args.src, tt.args.dest); (err != nil) != tt.wantErr { + t.Errorf("CopyFile() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/cmd/kubeadm/app/util/marshal_test.go b/cmd/kubeadm/app/util/marshal_test.go index ee3aac68aac..e8631c6ba22 100644 --- a/cmd/kubeadm/app/util/marshal_test.go +++ b/cmd/kubeadm/app/util/marshal_test.go @@ -431,3 +431,35 @@ func TestGroupVersionKindsHasResetConfiguration(t *testing.T) { }) } } + +func TestGroupVersionKindsHasClusterConfiguration(t *testing.T) { + tests := []struct { + name string + gvks []schema.GroupVersionKind + expected bool + }{ + { + name: "does not have ClusterConfiguraiton", + gvks: []schema.GroupVersionKind{ + {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"}, + }, + expected: false, + }, + { + name: "has ClusterConfiguraiton", + gvks: []schema.GroupVersionKind{ + {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"}, + {Group: "foo.k8s.io", Version: "v1", Kind: "ClusterConfiguration"}, + }, + expected: true, + }, + } + for _, rt := range tests { + t.Run(rt.name, func(t *testing.T) { + actual := GroupVersionKindsHasClusterConfiguration(rt.gvks...) + if rt.expected != actual { + t.Errorf("expected gvks to have a ClusterConfiguration: %t\n\tactual: %t\n", rt.expected, actual) + } + }) + } +}