mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
kubeadm: increase ut coverage for app/util
Signed-off-by: xin.li <xin.li@daocloud.io>
This commit is contained in:
parent
002b0f0003
commit
36904475db
89
cmd/kubeadm/app/util/copy_test.go
Normal file
89
cmd/kubeadm/app/util/copy_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user