git_repo_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package git_repo
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "k8s.io/api/core/v1"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/kubernetes/pkg/volume"
  26. "k8s.io/kubernetes/pkg/volume/emptydir"
  27. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  28. "k8s.io/utils/exec"
  29. fakeexec "k8s.io/utils/exec/testing"
  30. )
  31. func newTestHost(t *testing.T) (string, volume.VolumeHost) {
  32. tempDir, err := ioutil.TempDir("/tmp", "git_repo_test.")
  33. if err != nil {
  34. t.Fatalf("can't make a temp rootdir: %v", err)
  35. }
  36. return tempDir, volumetest.NewFakeVolumeHost(tempDir, nil, emptydir.ProbeVolumePlugins())
  37. }
  38. func TestCanSupport(t *testing.T) {
  39. plugMgr := volume.VolumePluginMgr{}
  40. tempDir, host := newTestHost(t)
  41. defer os.RemoveAll(tempDir)
  42. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  43. plug, err := plugMgr.FindPluginByName("kubernetes.io/git-repo")
  44. if err != nil {
  45. t.Errorf("Can't find the plugin by name")
  46. }
  47. if plug.GetPluginName() != "kubernetes.io/git-repo" {
  48. t.Errorf("Wrong name: %s", plug.GetPluginName())
  49. }
  50. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{GitRepo: &v1.GitRepoVolumeSource{}}}}) {
  51. t.Errorf("Expected true")
  52. }
  53. }
  54. // Expected command
  55. type expectedCommand struct {
  56. // The git command
  57. cmd []string
  58. // The dir of git command is executed
  59. dir string
  60. }
  61. func TestPlugin(t *testing.T) {
  62. gitUrl := "https://github.com/kubernetes/kubernetes.git"
  63. revision := "2a30ce65c5ab586b98916d83385c5983edd353a1"
  64. scenarios := []struct {
  65. name string
  66. vol *v1.Volume
  67. expecteds []expectedCommand
  68. isExpectedFailure bool
  69. }{
  70. {
  71. name: "target-dir",
  72. vol: &v1.Volume{
  73. Name: "vol1",
  74. VolumeSource: v1.VolumeSource{
  75. GitRepo: &v1.GitRepoVolumeSource{
  76. Repository: gitUrl,
  77. Revision: revision,
  78. Directory: "target_dir",
  79. },
  80. },
  81. },
  82. expecteds: []expectedCommand{
  83. {
  84. cmd: []string{"git", "clone", "--", gitUrl, "target_dir"},
  85. dir: "",
  86. },
  87. {
  88. cmd: []string{"git", "checkout", revision},
  89. dir: "/target_dir",
  90. },
  91. {
  92. cmd: []string{"git", "reset", "--hard"},
  93. dir: "/target_dir",
  94. },
  95. },
  96. isExpectedFailure: false,
  97. },
  98. {
  99. name: "target-dir-no-revision",
  100. vol: &v1.Volume{
  101. Name: "vol1",
  102. VolumeSource: v1.VolumeSource{
  103. GitRepo: &v1.GitRepoVolumeSource{
  104. Repository: gitUrl,
  105. Directory: "target_dir",
  106. },
  107. },
  108. },
  109. expecteds: []expectedCommand{
  110. {
  111. cmd: []string{"git", "clone", "--", gitUrl, "target_dir"},
  112. dir: "",
  113. },
  114. },
  115. isExpectedFailure: false,
  116. },
  117. {
  118. name: "only-git-clone",
  119. vol: &v1.Volume{
  120. Name: "vol1",
  121. VolumeSource: v1.VolumeSource{
  122. GitRepo: &v1.GitRepoVolumeSource{
  123. Repository: gitUrl,
  124. },
  125. },
  126. },
  127. expecteds: []expectedCommand{
  128. {
  129. cmd: []string{"git", "clone", "--", gitUrl},
  130. dir: "",
  131. },
  132. },
  133. isExpectedFailure: false,
  134. },
  135. {
  136. name: "no-target-dir",
  137. vol: &v1.Volume{
  138. Name: "vol1",
  139. VolumeSource: v1.VolumeSource{
  140. GitRepo: &v1.GitRepoVolumeSource{
  141. Repository: gitUrl,
  142. Revision: revision,
  143. Directory: "",
  144. },
  145. },
  146. },
  147. expecteds: []expectedCommand{
  148. {
  149. cmd: []string{"git", "clone", "--", gitUrl},
  150. dir: "",
  151. },
  152. {
  153. cmd: []string{"git", "checkout", revision},
  154. dir: "/kubernetes",
  155. },
  156. {
  157. cmd: []string{"git", "reset", "--hard"},
  158. dir: "/kubernetes",
  159. },
  160. },
  161. isExpectedFailure: false,
  162. },
  163. {
  164. name: "current-dir",
  165. vol: &v1.Volume{
  166. Name: "vol1",
  167. VolumeSource: v1.VolumeSource{
  168. GitRepo: &v1.GitRepoVolumeSource{
  169. Repository: gitUrl,
  170. Revision: revision,
  171. Directory: ".",
  172. },
  173. },
  174. },
  175. expecteds: []expectedCommand{
  176. {
  177. cmd: []string{"git", "clone", "--", gitUrl, "."},
  178. dir: "",
  179. },
  180. {
  181. cmd: []string{"git", "checkout", revision},
  182. dir: "",
  183. },
  184. {
  185. cmd: []string{"git", "reset", "--hard"},
  186. dir: "",
  187. },
  188. },
  189. isExpectedFailure: false,
  190. },
  191. {
  192. name: "current-dir-mess",
  193. vol: &v1.Volume{
  194. Name: "vol1",
  195. VolumeSource: v1.VolumeSource{
  196. GitRepo: &v1.GitRepoVolumeSource{
  197. Repository: gitUrl,
  198. Revision: revision,
  199. Directory: "./.",
  200. },
  201. },
  202. },
  203. expecteds: []expectedCommand{
  204. {
  205. cmd: []string{"git", "clone", "--", gitUrl, "./."},
  206. dir: "",
  207. },
  208. {
  209. cmd: []string{"git", "checkout", revision},
  210. dir: "",
  211. },
  212. {
  213. cmd: []string{"git", "reset", "--hard"},
  214. dir: "",
  215. },
  216. },
  217. isExpectedFailure: false,
  218. },
  219. {
  220. name: "invalid-repository",
  221. vol: &v1.Volume{
  222. Name: "vol1",
  223. VolumeSource: v1.VolumeSource{
  224. GitRepo: &v1.GitRepoVolumeSource{
  225. Repository: "--foo",
  226. },
  227. },
  228. },
  229. isExpectedFailure: true,
  230. },
  231. {
  232. name: "invalid-revision",
  233. vol: &v1.Volume{
  234. Name: "vol1",
  235. VolumeSource: v1.VolumeSource{
  236. GitRepo: &v1.GitRepoVolumeSource{
  237. Repository: gitUrl,
  238. Revision: "--bar",
  239. },
  240. },
  241. },
  242. isExpectedFailure: true,
  243. },
  244. {
  245. name: "invalid-directory",
  246. vol: &v1.Volume{
  247. Name: "vol1",
  248. VolumeSource: v1.VolumeSource{
  249. GitRepo: &v1.GitRepoVolumeSource{
  250. Repository: gitUrl,
  251. Directory: "-b",
  252. },
  253. },
  254. },
  255. isExpectedFailure: true,
  256. },
  257. }
  258. for _, scenario := range scenarios {
  259. allErrs := doTestPlugin(scenario, t)
  260. if len(allErrs) == 0 && scenario.isExpectedFailure {
  261. t.Errorf("Unexpected success for scenario: %s", scenario.name)
  262. }
  263. if len(allErrs) > 0 && !scenario.isExpectedFailure {
  264. t.Errorf("Unexpected failure for scenario: %s - %+v", scenario.name, allErrs)
  265. }
  266. }
  267. }
  268. func doTestPlugin(scenario struct {
  269. name string
  270. vol *v1.Volume
  271. expecteds []expectedCommand
  272. isExpectedFailure bool
  273. }, t *testing.T) []error {
  274. allErrs := []error{}
  275. plugMgr := volume.VolumePluginMgr{}
  276. rootDir, host := newTestHost(t)
  277. defer os.RemoveAll(rootDir)
  278. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  279. plug, err := plugMgr.FindPluginByName("kubernetes.io/git-repo")
  280. if err != nil {
  281. allErrs = append(allErrs,
  282. fmt.Errorf("Can't find the plugin by name"))
  283. return allErrs
  284. }
  285. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
  286. mounter, err := plug.NewMounter(volume.NewSpecFromVolume(scenario.vol), pod, volume.VolumeOptions{})
  287. if err != nil {
  288. allErrs = append(allErrs,
  289. fmt.Errorf("Failed to make a new Mounter: %v", err))
  290. return allErrs
  291. }
  292. if mounter == nil {
  293. allErrs = append(allErrs,
  294. fmt.Errorf("Got a nil Mounter"))
  295. return allErrs
  296. }
  297. path := mounter.GetPath()
  298. suffix := fmt.Sprintf("pods/poduid/volumes/kubernetes.io~git-repo/%v", scenario.vol.Name)
  299. if !strings.HasSuffix(path, suffix) {
  300. allErrs = append(allErrs,
  301. fmt.Errorf("Got unexpected path: %s", path))
  302. return allErrs
  303. }
  304. // Test setUp()
  305. setUpErrs := doTestSetUp(scenario, mounter)
  306. allErrs = append(allErrs, setUpErrs...)
  307. if _, err := os.Stat(path); err != nil {
  308. if os.IsNotExist(err) {
  309. allErrs = append(allErrs,
  310. fmt.Errorf("SetUp() failed, volume path not created: %s", path))
  311. return allErrs
  312. } else {
  313. allErrs = append(allErrs,
  314. fmt.Errorf("SetUp() failed: %v", err))
  315. return allErrs
  316. }
  317. }
  318. // gitRepo volume should create its own empty wrapper path
  319. podWrapperMetadataDir := fmt.Sprintf("%v/pods/poduid/plugins/kubernetes.io~empty-dir/wrapped_%v", rootDir, scenario.vol.Name)
  320. if _, err := os.Stat(podWrapperMetadataDir); err != nil {
  321. if os.IsNotExist(err) {
  322. allErrs = append(allErrs,
  323. fmt.Errorf("SetUp() failed, empty-dir wrapper path is not created: %s", podWrapperMetadataDir))
  324. } else {
  325. allErrs = append(allErrs,
  326. fmt.Errorf("SetUp() failed: %v", err))
  327. }
  328. }
  329. unmounter, err := plug.NewUnmounter("vol1", types.UID("poduid"))
  330. if err != nil {
  331. allErrs = append(allErrs,
  332. fmt.Errorf("Failed to make a new Unmounter: %v", err))
  333. return allErrs
  334. }
  335. if unmounter == nil {
  336. allErrs = append(allErrs,
  337. fmt.Errorf("Got a nil Unmounter"))
  338. return allErrs
  339. }
  340. if err := unmounter.TearDown(); err != nil {
  341. allErrs = append(allErrs,
  342. fmt.Errorf("Expected success, got: %v", err))
  343. return allErrs
  344. }
  345. if _, err := os.Stat(path); err == nil {
  346. allErrs = append(allErrs,
  347. fmt.Errorf("TearDown() failed, volume path still exists: %s", path))
  348. } else if !os.IsNotExist(err) {
  349. allErrs = append(allErrs,
  350. fmt.Errorf("TearDown() failed: %v", err))
  351. }
  352. return allErrs
  353. }
  354. func doTestSetUp(scenario struct {
  355. name string
  356. vol *v1.Volume
  357. expecteds []expectedCommand
  358. isExpectedFailure bool
  359. }, mounter volume.Mounter) []error {
  360. expecteds := scenario.expecteds
  361. allErrs := []error{}
  362. // Construct combined outputs from expected commands
  363. var fakeOutputs []fakeexec.FakeCombinedOutputAction
  364. var fcmd fakeexec.FakeCmd
  365. for _, expected := range expecteds {
  366. if expected.cmd[1] == "clone" {
  367. fakeOutputs = append(fakeOutputs, func() ([]byte, error) {
  368. // git clone, it creates new dir/files
  369. os.MkdirAll(filepath.Join(fcmd.Dirs[0], expected.dir), 0750)
  370. return []byte{}, nil
  371. })
  372. } else {
  373. // git checkout || git reset, they create nothing
  374. fakeOutputs = append(fakeOutputs, func() ([]byte, error) {
  375. return []byte{}, nil
  376. })
  377. }
  378. }
  379. fcmd = fakeexec.FakeCmd{
  380. CombinedOutputScript: fakeOutputs,
  381. }
  382. // Construct fake exec outputs from fcmd
  383. var fakeAction []fakeexec.FakeCommandAction
  384. for i := 0; i < len(expecteds); i++ {
  385. fakeAction = append(fakeAction, func(cmd string, args ...string) exec.Cmd {
  386. return fakeexec.InitFakeCmd(&fcmd, cmd, args...)
  387. })
  388. }
  389. fake := fakeexec.FakeExec{
  390. CommandScript: fakeAction,
  391. }
  392. g := mounter.(*gitRepoVolumeMounter)
  393. g.exec = &fake
  394. g.SetUp(volume.MounterArgs{})
  395. if fake.CommandCalls != len(expecteds) {
  396. allErrs = append(allErrs,
  397. fmt.Errorf("unexpected command calls in scenario: expected %d, saw: %d", len(expecteds), fake.CommandCalls))
  398. }
  399. var expectedCmds [][]string
  400. for _, expected := range expecteds {
  401. expectedCmds = append(expectedCmds, expected.cmd)
  402. }
  403. if !reflect.DeepEqual(expectedCmds, fcmd.CombinedOutputLog) {
  404. allErrs = append(allErrs,
  405. fmt.Errorf("unexpected commands: %v, expected: %v", fcmd.CombinedOutputLog, expectedCmds))
  406. }
  407. var expectedPaths []string
  408. for _, expected := range expecteds {
  409. expectedPaths = append(expectedPaths, g.GetPath()+expected.dir)
  410. }
  411. if len(fcmd.Dirs) != len(expectedPaths) || !reflect.DeepEqual(expectedPaths, fcmd.Dirs) {
  412. allErrs = append(allErrs,
  413. fmt.Errorf("unexpected directories: %v, expected: %v", fcmd.Dirs, expectedPaths))
  414. }
  415. return allErrs
  416. }