config_diff_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package worker
  2. import (
  3. "sort"
  4. "testing"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestConfigDiff(t *testing.T) {
  8. Convey("When old and new configs are equal", t, func() {
  9. oldList := []mirrorConfig{
  10. {Name: "debian"},
  11. {Name: "debian-security"},
  12. {Name: "fedora"},
  13. {Name: "archlinux"},
  14. {Name: "AOSP"},
  15. {Name: "ubuntu"},
  16. }
  17. newList := make([]mirrorConfig, len(oldList))
  18. copy(newList, oldList)
  19. difference := diffMirrorConfig(oldList, newList)
  20. So(len(difference), ShouldEqual, 0)
  21. })
  22. Convey("When old config is empty", t, func() {
  23. newList := []mirrorConfig{
  24. {Name: "debian"},
  25. {Name: "debian-security"},
  26. {Name: "fedora"},
  27. {Name: "archlinux"},
  28. {Name: "AOSP"},
  29. {Name: "ubuntu"},
  30. }
  31. oldList := make([]mirrorConfig, 0)
  32. difference := diffMirrorConfig(oldList, newList)
  33. So(len(difference), ShouldEqual, len(newList))
  34. })
  35. Convey("When new config is empty", t, func() {
  36. oldList := []mirrorConfig{
  37. {Name: "debian"},
  38. {Name: "debian-security"},
  39. {Name: "fedora"},
  40. {Name: "archlinux"},
  41. {Name: "AOSP"},
  42. {Name: "ubuntu"},
  43. }
  44. newList := make([]mirrorConfig, 0)
  45. difference := diffMirrorConfig(oldList, newList)
  46. So(len(difference), ShouldEqual, len(oldList))
  47. })
  48. Convey("When giving two config lists with different names", t, func() {
  49. oldList := []mirrorConfig{
  50. {Name: "debian"},
  51. {Name: "debian-security"},
  52. {Name: "fedora"},
  53. {Name: "archlinux"},
  54. {Name: "AOSP", Env: map[string]string{"REPO": "/usr/bin/repo"}},
  55. {Name: "ubuntu"},
  56. }
  57. newList := []mirrorConfig{
  58. {Name: "debian"},
  59. {Name: "debian-cd"},
  60. {Name: "archlinuxcn"},
  61. {Name: "AOSP", Env: map[string]string{"REPO": "/usr/local/bin/aosp-repo"}},
  62. {Name: "ubuntu-ports"},
  63. }
  64. difference := diffMirrorConfig(oldList, newList)
  65. sort.Sort(sortableMirrorList(oldList))
  66. emptyList := []mirrorConfig{}
  67. for _, o := range oldList {
  68. keep := true
  69. for _, op := range difference {
  70. if (op.diffOp == diffDelete || op.diffOp == diffModify) &&
  71. op.mirCfg.Name == o.Name {
  72. keep = false
  73. break
  74. }
  75. }
  76. if keep {
  77. emptyList = append(emptyList, o)
  78. }
  79. }
  80. for _, op := range difference {
  81. if op.diffOp == diffAdd || op.diffOp == diffModify {
  82. emptyList = append(emptyList, op.mirCfg)
  83. }
  84. }
  85. sort.Sort(sortableMirrorList(emptyList))
  86. sort.Sort(sortableMirrorList(newList))
  87. So(emptyList, ShouldResemble, newList)
  88. })
  89. }