procfs_linux_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // +build linux
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package procfs
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "os/signal"
  19. "path/filepath"
  20. "runtime"
  21. "syscall"
  22. "testing"
  23. "time"
  24. "github.com/stretchr/testify/assert"
  25. )
  26. func verifyContainerName(procCgroupText, expectedName string, expectedErr bool, t *testing.T) {
  27. name, err := containerNameFromProcCgroup(procCgroupText)
  28. if expectedErr && err == nil {
  29. t.Errorf("Expected error but did not get error in verifyContainerName")
  30. return
  31. } else if !expectedErr && err != nil {
  32. t.Errorf("Expected no error, but got error %+v in verifyContainerName", err)
  33. return
  34. } else if expectedErr {
  35. return
  36. }
  37. if name != expectedName {
  38. t.Errorf("Expected container name %s but got name %s", expectedName, name)
  39. }
  40. }
  41. func TestContainerNameFromProcCgroup(t *testing.T) {
  42. procCgroupValid := "2:devices:docker/kubelet"
  43. verifyContainerName(procCgroupValid, "docker/kubelet", false, t)
  44. procCgroupEmpty := ""
  45. verifyContainerName(procCgroupEmpty, "", true, t)
  46. content, err := ioutil.ReadFile("example_proc_cgroup")
  47. if err != nil {
  48. t.Errorf("Could not read example /proc cgroup file")
  49. }
  50. verifyContainerName(string(content), "/user/1000.user/c1.session", false, t)
  51. procCgroupNoDevice := "2:freezer:docker/kubelet\n5:cpuacct:pkg/kubectl"
  52. verifyContainerName(procCgroupNoDevice, "", true, t)
  53. procCgroupInvalid := "devices:docker/kubelet\ncpuacct:pkg/kubectl"
  54. verifyContainerName(procCgroupInvalid, "", true, t)
  55. }
  56. func TestPidOf(t *testing.T) {
  57. if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
  58. t.Skipf("not supported on GOOS=%s", runtime.GOOS)
  59. }
  60. pids, err := PidOf(filepath.Base(os.Args[0]))
  61. assert.Empty(t, err)
  62. assert.NotZero(t, pids)
  63. assert.Contains(t, pids, os.Getpid())
  64. }
  65. func TestPKill(t *testing.T) {
  66. if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
  67. t.Skipf("not supported on GOOS=%s", runtime.GOOS)
  68. }
  69. sig := syscall.SIGCONT
  70. c := make(chan os.Signal, 1)
  71. signal.Notify(c, sig)
  72. defer signal.Stop(c)
  73. PKill(os.Args[0], sig)
  74. select {
  75. case s := <-c:
  76. if s != sig {
  77. t.Fatalf("signal was %v, want %v", s, sig)
  78. }
  79. case <-time.After(1 * time.Second):
  80. t.Fatalf("timeout waiting for %v", sig)
  81. }
  82. }