diff --git a/pkg/kubelet/kubeletconfig/configfiles/configfiles_test.go b/pkg/kubelet/kubeletconfig/configfiles/configfiles_test.go index 5e3763e128d..6df84272d0c 100644 --- a/pkg/kubelet/kubeletconfig/configfiles/configfiles_test.go +++ b/pkg/kubelet/kubeletconfig/configfiles/configfiles_test.go @@ -30,6 +30,7 @@ import ( kubeletscheme "k8s.io/kubernetes/pkg/kubelet/apis/config/scheme" utiltest "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/test" utilfs "k8s.io/kubernetes/pkg/util/filesystem" + "k8s.io/utils/ptr" ) const configDir = "/test-config-dir" @@ -53,53 +54,53 @@ func TestLoad(t *testing.T) { // empty file { desc: "empty file", - file: newString(``), + file: ptr.To(``), err: "was empty", }, // invalid format { desc: "invalid yaml", - file: newString(`*`), + file: ptr.To(`*`), err: "failed to decode", }, { desc: "invalid json", - file: newString(`{*`), + file: ptr.To(`{*`), err: "failed to decode", }, // invalid object { desc: "missing kind", - file: newString(`{"apiVersion":"kubelet.config.k8s.io/v1beta1"}`), + file: ptr.To(`{"apiVersion":"kubelet.config.k8s.io/v1beta1"}`), err: "failed to decode", }, { desc: "missing version", - file: newString(`{"kind":"KubeletConfiguration"}`), + file: ptr.To(`{"kind":"KubeletConfiguration"}`), err: "failed to decode", }, { desc: "unregistered kind", - file: newString(`{"kind":"BogusKind","apiVersion":"kubelet.config.k8s.io/v1beta1"}`), + file: ptr.To(`{"kind":"BogusKind","apiVersion":"kubelet.config.k8s.io/v1beta1"}`), err: "failed to decode", }, { desc: "unregistered version", - file: newString(`{"kind":"KubeletConfiguration","apiVersion":"bogusversion"}`), + file: ptr.To(`{"kind":"KubeletConfiguration","apiVersion":"bogusversion"}`), err: "failed to decode", }, // empty object with correct kind and version should result in the defaults for that kind and version { desc: "default from yaml", - file: newString(`kind: KubeletConfiguration + file: ptr.To(`kind: KubeletConfiguration apiVersion: kubelet.config.k8s.io/v1beta1`), expect: newConfig(t), skipOnWindows: true, }, { desc: "default from json", - file: newString(`{"kind":"KubeletConfiguration","apiVersion":"kubelet.config.k8s.io/v1beta1"}`), + file: ptr.To(`{"kind":"KubeletConfiguration","apiVersion":"kubelet.config.k8s.io/v1beta1"}`), expect: newConfig(t), skipOnWindows: true, }, @@ -107,7 +108,7 @@ apiVersion: kubelet.config.k8s.io/v1beta1`), // relative path { desc: "yaml, relative path is resolved", - file: newString(fmt.Sprintf(`kind: KubeletConfiguration + file: ptr.To(fmt.Sprintf(`kind: KubeletConfiguration apiVersion: kubelet.config.k8s.io/v1beta1 staticPodPath: %s`, relativePath)), expect: func() *kubeletconfig.KubeletConfiguration { @@ -119,7 +120,7 @@ staticPodPath: %s`, relativePath)), }, { desc: "json, relative path is resolved", - file: newString(fmt.Sprintf(`{"kind":"KubeletConfiguration","apiVersion":"kubelet.config.k8s.io/v1beta1","staticPodPath":"%s"}`, relativePath)), + file: ptr.To(fmt.Sprintf(`{"kind":"KubeletConfiguration","apiVersion":"kubelet.config.k8s.io/v1beta1","staticPodPath":"%s"}`, relativePath)), expect: func() *kubeletconfig.KubeletConfiguration { kc := newConfig(t) kc.StaticPodPath = filepath.Join(configDir, relativePath) @@ -130,7 +131,7 @@ staticPodPath: %s`, relativePath)), { // This should fail from v1beta2+ desc: "duplicate field", - file: newString(fmt.Sprintf(`kind: KubeletConfiguration + file: ptr.To(fmt.Sprintf(`kind: KubeletConfiguration apiVersion: kubelet.config.k8s.io/v1beta1 staticPodPath: %s staticPodPath: %s/foo`, relativePath, relativePath)), @@ -146,7 +147,7 @@ staticPodPath: %s/foo`, relativePath, relativePath)), { // This should fail from v1beta2+ desc: "unknown field", - file: newString(`kind: KubeletConfiguration + file: ptr.To(`kind: KubeletConfiguration apiVersion: kubelet.config.k8s.io/v1beta1 foo: bar`), // err: "found unknown field: foo", @@ -224,10 +225,6 @@ func TestResolveRelativePaths(t *testing.T) { } } -func newString(s string) *string { - return &s -} - func addFile(fs utilfs.Filesystem, path string, fileContent string) error { dir := filepath.Dir(path) tmpFile, err := fs.TempFile(dir, "tmp_"+filepath.Base(path)) diff --git a/pkg/kubelet/kuberuntime/helpers_linux_test.go b/pkg/kubelet/kuberuntime/helpers_linux_test.go index 70add049b80..2142d03a56a 100644 --- a/pkg/kubelet/kuberuntime/helpers_linux_test.go +++ b/pkg/kubelet/kuberuntime/helpers_linux_test.go @@ -81,7 +81,7 @@ func TestGetSeccompProfile(t *testing.T) { }, { description: "pod seccomp profile set to SeccompProfileTypeLocalhost returns 'localhost/' + LocalhostProfile", - podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("filename")}}, + podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("filename")}}, expectedProfile: &runtimeapi.SecurityProfile{ ProfileType: runtimeapi.SecurityProfile_Localhost, LocalhostRef: seccompLocalhostRef("filename"), @@ -99,7 +99,7 @@ func TestGetSeccompProfile(t *testing.T) { }, { description: "container seccomp profile set to SeccompProfileTypeLocalhost returns 'localhost/' + LocalhostProfile", - containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("filename2")}}, + containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("filename2")}}, expectedProfile: &runtimeapi.SecurityProfile{ ProfileType: runtimeapi.SecurityProfile_Localhost, LocalhostRef: seccompLocalhostRef("filename2"), @@ -113,8 +113,8 @@ func TestGetSeccompProfile(t *testing.T) { }, { description: "prioritise container field over pod field", - podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("field-pod-profile.json")}}, - containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("field-cont-profile.json")}}, + podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("field-pod-profile.json")}}, + containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("field-cont-profile.json")}}, containerName: "container1", expectedProfile: &runtimeapi.SecurityProfile{ ProfileType: runtimeapi.SecurityProfile_Localhost, @@ -181,7 +181,7 @@ func TestGetSeccompProfileDefaultSeccomp(t *testing.T) { }, { description: "pod seccomp profile set to SeccompProfileTypeLocalhost returns 'localhost/' + LocalhostProfile", - podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("filename")}}, + podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("filename")}}, expectedProfile: &runtimeapi.SecurityProfile{ ProfileType: runtimeapi.SecurityProfile_Localhost, LocalhostRef: seccompLocalhostRef("filename"), @@ -199,7 +199,7 @@ func TestGetSeccompProfileDefaultSeccomp(t *testing.T) { }, { description: "container seccomp profile set to SeccompProfileTypeLocalhost returns 'localhost/' + LocalhostProfile", - containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("filename2")}}, + containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("filename2")}}, expectedProfile: &runtimeapi.SecurityProfile{ ProfileType: runtimeapi.SecurityProfile_Localhost, LocalhostRef: seccompLocalhostRef("filename2"), @@ -213,8 +213,8 @@ func TestGetSeccompProfileDefaultSeccomp(t *testing.T) { }, { description: "prioritise container field over pod field", - podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("field-pod-profile.json")}}, - containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: getLocal("field-cont-profile.json")}}, + podSc: &v1.PodSecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("field-pod-profile.json")}}, + containerSc: &v1.SecurityContext{SeccompProfile: &v1.SeccompProfile{Type: v1.SeccompProfileTypeLocalhost, LocalhostProfile: ptr.To("field-cont-profile.json")}}, containerName: "container1", expectedProfile: &runtimeapi.SecurityProfile{ ProfileType: runtimeapi.SecurityProfile_Localhost, @@ -234,10 +234,6 @@ func TestGetSeccompProfileDefaultSeccomp(t *testing.T) { } } -func getLocal(v string) *string { - return &v -} - func TestSharesToMilliCPU(t *testing.T) { knownMilliCPUToShares := map[int64]int64{ 0: 2, diff --git a/staging/src/k8s.io/apimachinery/pkg/util/dump/dump_test.go b/staging/src/k8s.io/apimachinery/pkg/util/dump/dump_test.go index b597f5c6324..d31850ebaff 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/dump/dump_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/dump/dump_test.go @@ -19,16 +19,14 @@ package dump import ( "fmt" "testing" + + "k8s.io/utils/ptr" ) func ptrint(i int) *int { return &i } -func ptrstr(s string) *string { - return &s -} - // custom type to test Stringer interface on non-pointer receiver. type customString string @@ -109,7 +107,7 @@ func TestPretty(t *testing.T) { {bool(true), "(bool) true\n"}, {bool(false), "(bool) false\n"}, {string("test"), "(string) (len=4) \"test\"\n"}, - {ptrstr("test"), "(*string)((len=4) \"test\")\n"}, + {ptr.To("test"), "(*string)((len=4) \"test\")\n"}, {[1]string{"arr"}, "([1]string) (len=1) {\n (string) (len=3) \"arr\"\n}\n"}, {[]string{"slice"}, "([]string) (len=1) {\n (string) (len=5) \"slice\"\n}\n"}, {tcs, "(dump.customString) (len=4) \"test\"\n"}, @@ -188,7 +186,7 @@ func TestForHash(t *testing.T) { {bool(true), "(bool)true"}, {bool(false), "(bool)false"}, {string("test"), "(string)test"}, - {ptrstr("test"), "(*string)test"}, + {ptr.To("test"), "(*string)test"}, {[1]string{"arr"}, "([1]string)[arr]"}, {[]string{"slice"}, "([]string)[slice]"}, {tcs, "(dump.customString)test"}, @@ -267,7 +265,7 @@ func TestOneLine(t *testing.T) { {bool(true), "(bool)true"}, {bool(false), "(bool)false"}, {string("test"), "(string)test"}, - {ptrstr("test"), "(*string)test"}, + {ptr.To("test"), "(*string)test"}, {[1]string{"arr"}, "([1]string)[arr]"}, {[]string{"slice"}, "([]string)[slice]"}, {tcs, "(dump.customString)test"}, diff --git a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/marker_test.go b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/marker_test.go index 003ac06cb79..c86f1e1d49c 100644 --- a/staging/src/k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/marker_test.go +++ b/staging/src/k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/marker_test.go @@ -25,12 +25,9 @@ import ( externalexternal "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external/external" "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external2" "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external3" + "k8s.io/utils/ptr" ) -func getPointerFromString(s string) *string { - return &s -} - var ( defaultInt32 int32 = 32 defaultInt64 int64 = 64 @@ -49,7 +46,7 @@ func Test_Marker(t *testing.T) { StringDefault: "bar", StringEmptyDefault: "", StringEmpty: "", - StringPointer: getPointerFromString("default"), + StringPointer: ptr.To("default"), Int64: &defaultInt64, Int32: &defaultInt32, IntDefault: 1, @@ -59,8 +56,8 @@ func Test_Marker(t *testing.T) { FloatEmptyDefault: 0.0, FloatEmpty: 0.0, List: []Item{ - getPointerFromString("foo"), - getPointerFromString("bar"), + ptr.To("foo"), + ptr.To("bar"), }, Sub: &SubStruct{ S: "foo", @@ -94,7 +91,7 @@ func Test_Marker(t *testing.T) { "foo", }, Map: map[string]Item{ - "foo": getPointerFromString("bar"), + "foo": ptr.To("bar"), }, StructMap: map[string]SubStruct{ "foo": { @@ -108,7 +105,7 @@ func Test_Marker(t *testing.T) { I: 1, }, }, - AliasPtr: getPointerFromString("banana"), + AliasPtr: ptr.To("banana"), }, }, { @@ -121,7 +118,7 @@ func Test_Marker(t *testing.T) { StringDefault: "changed", StringEmptyDefault: "", StringEmpty: "", - StringPointer: getPointerFromString("default"), + StringPointer: ptr.To("default"), Int64: &defaultInt64, Int32: &defaultInt32, IntDefault: 5, @@ -131,8 +128,8 @@ func Test_Marker(t *testing.T) { FloatEmptyDefault: 0.0, FloatEmpty: 0.0, List: []Item{ - getPointerFromString("foo"), - getPointerFromString("bar"), + ptr.To("foo"), + ptr.To("bar"), }, Sub: &SubStruct{ S: "foo", @@ -166,7 +163,7 @@ func Test_Marker(t *testing.T) { I: 1, }, Map: map[string]Item{ - "foo": getPointerFromString("bar"), + "foo": ptr.To("bar"), }, StructMap: map[string]SubStruct{ "foo": { @@ -180,7 +177,7 @@ func Test_Marker(t *testing.T) { I: 1, }, }, - AliasPtr: getPointerFromString("banana"), + AliasPtr: ptr.To("banana"), }, }, { @@ -188,14 +185,14 @@ func Test_Marker(t *testing.T) { in: Defaulted{ List: []Item{ nil, - getPointerFromString("bar"), + ptr.To("bar"), }, }, out: Defaulted{ StringDefault: "bar", StringEmptyDefault: "", StringEmpty: "", - StringPointer: getPointerFromString("default"), + StringPointer: ptr.To("default"), Int64: &defaultInt64, Int32: &defaultInt32, IntDefault: 1, @@ -205,8 +202,8 @@ func Test_Marker(t *testing.T) { FloatEmptyDefault: 0.0, FloatEmpty: 0.0, List: []Item{ - getPointerFromString("apple"), - getPointerFromString("bar"), + ptr.To("apple"), + ptr.To("bar"), }, Sub: &SubStruct{ S: "foo", @@ -240,7 +237,7 @@ func Test_Marker(t *testing.T) { I: 1, }, Map: map[string]Item{ - "foo": getPointerFromString("bar"), + "foo": ptr.To("bar"), }, StructMap: map[string]SubStruct{ "foo": { @@ -254,7 +251,7 @@ func Test_Marker(t *testing.T) { I: 1, }, }, - AliasPtr: getPointerFromString("banana"), + AliasPtr: ptr.To("banana"), }, }, { @@ -262,14 +259,14 @@ func Test_Marker(t *testing.T) { in: Defaulted{ Map: map[string]Item{ "foo": nil, - "bar": getPointerFromString("banana"), + "bar": ptr.To("banana"), }, }, out: Defaulted{ StringDefault: "bar", StringEmptyDefault: "", StringEmpty: "", - StringPointer: getPointerFromString("default"), + StringPointer: ptr.To("default"), Int64: &defaultInt64, Int32: &defaultInt32, IntDefault: 1, @@ -279,8 +276,8 @@ func Test_Marker(t *testing.T) { FloatEmptyDefault: 0.0, FloatEmpty: 0.0, List: []Item{ - getPointerFromString("foo"), - getPointerFromString("bar"), + ptr.To("foo"), + ptr.To("bar"), }, Sub: &SubStruct{ S: "foo", @@ -314,8 +311,8 @@ func Test_Marker(t *testing.T) { I: 1, }, Map: map[string]Item{ - "foo": getPointerFromString("apple"), - "bar": getPointerFromString("banana"), + "foo": ptr.To("apple"), + "bar": ptr.To("banana"), }, StructMap: map[string]SubStruct{ "foo": { @@ -329,7 +326,7 @@ func Test_Marker(t *testing.T) { I: 1, }, }, - AliasPtr: getPointerFromString("banana"), + AliasPtr: ptr.To("banana"), }, }, } @@ -387,7 +384,7 @@ func Test_DefaultingReference(t *testing.T) { AliasOverride: Item(&SomeDefault), AliasConvertDefaultPointer: &dv, AliasPointerDefault: &dv, - PointerAliasDefault: Item(getPointerFromString("apple")), + PointerAliasDefault: Item(ptr.To("apple")), AliasNonPointer: SomeValue, AliasPointer: &SomeValue, SymbolReference: SomeDefault,