helpers_linux_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // +build linux
  2. /*
  3. Copyright 2017 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 dockershim
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/stretchr/testify/require"
  23. "k8s.io/api/core/v1"
  24. )
  25. func TestGetSeccompSecurityOpts(t *testing.T) {
  26. tests := []struct {
  27. msg string
  28. seccompProfile string
  29. expectedOpts []string
  30. }{{
  31. msg: "No security annotations",
  32. seccompProfile: "",
  33. expectedOpts: []string{"seccomp=unconfined"},
  34. }, {
  35. msg: "Seccomp unconfined",
  36. seccompProfile: "unconfined",
  37. expectedOpts: []string{"seccomp=unconfined"},
  38. }, {
  39. msg: "Seccomp default",
  40. seccompProfile: v1.SeccompProfileRuntimeDefault,
  41. expectedOpts: nil,
  42. }, {
  43. msg: "Seccomp deprecated default",
  44. seccompProfile: v1.DeprecatedSeccompProfileDockerDefault,
  45. expectedOpts: nil,
  46. }}
  47. for i, test := range tests {
  48. opts, err := getSeccompSecurityOpts(test.seccompProfile, '=')
  49. assert.NoError(t, err, "TestCase[%d]: %s", i, test.msg)
  50. assert.Len(t, opts, len(test.expectedOpts), "TestCase[%d]: %s", i, test.msg)
  51. for _, opt := range test.expectedOpts {
  52. assert.Contains(t, opts, opt, "TestCase[%d]: %s", i, test.msg)
  53. }
  54. }
  55. }
  56. func TestLoadSeccompLocalhostProfiles(t *testing.T) {
  57. tmpdir, err := ioutil.TempDir("", "seccomp-local-profile-test")
  58. require.NoError(t, err)
  59. defer os.RemoveAll(tmpdir)
  60. testProfile := `{"foo": "bar"}`
  61. err = ioutil.WriteFile(filepath.Join(tmpdir, "test"), []byte(testProfile), 0644)
  62. require.NoError(t, err)
  63. tests := []struct {
  64. msg string
  65. seccompProfile string
  66. expectedOpts []string
  67. expectErr bool
  68. }{{
  69. msg: "Seccomp localhost/test profile should return correct seccomp profiles",
  70. seccompProfile: "localhost/" + filepath.Join(tmpdir, "test"),
  71. expectedOpts: []string{`seccomp={"foo":"bar"}`},
  72. expectErr: false,
  73. }, {
  74. msg: "Non-existent profile should return error",
  75. seccompProfile: "localhost/" + filepath.Join(tmpdir, "fixtures/non-existent"),
  76. expectedOpts: nil,
  77. expectErr: true,
  78. }, {
  79. msg: "Relative profile path should return error",
  80. seccompProfile: "localhost/fixtures/test",
  81. expectedOpts: nil,
  82. expectErr: true,
  83. }}
  84. for i, test := range tests {
  85. opts, err := getSeccompSecurityOpts(test.seccompProfile, '=')
  86. if test.expectErr {
  87. assert.Error(t, err, fmt.Sprintf("TestCase[%d]: %s", i, test.msg))
  88. continue
  89. }
  90. assert.NoError(t, err, "TestCase[%d]: %s", i, test.msg)
  91. assert.Len(t, opts, len(test.expectedOpts), "TestCase[%d]: %s", i, test.msg)
  92. for _, opt := range test.expectedOpts {
  93. assert.Contains(t, opts, opt, "TestCase[%d]: %s", i, test.msg)
  94. }
  95. }
  96. }