|
1 | 1 | package cleaner |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
4 | 7 | "testing" |
5 | 8 |
|
6 | | - "time" |
7 | | - |
8 | | - "github.com/deis/builder/pkg/sys" |
| 9 | + "github.com/arschles/assert" |
9 | 10 | "k8s.io/kubernetes/pkg/api" |
10 | | - "k8s.io/kubernetes/pkg/fields" |
11 | | - "k8s.io/kubernetes/pkg/labels" |
12 | | - "k8s.io/kubernetes/pkg/watch" |
13 | 11 | ) |
14 | 12 |
|
15 | | -type namespace struct { |
| 13 | +func TestGetDiff(t *testing.T) { |
| 14 | + nsList := []api.Namespace{ |
| 15 | + api.Namespace{ObjectMeta: api.ObjectMeta{Name: "app1"}}, |
| 16 | + api.Namespace{ObjectMeta: api.ObjectMeta{Name: "app2"}}, |
| 17 | + } |
| 18 | + dirList := []string{"app1", "app3"} |
| 19 | + diff := getDiff(nsList, dirList) |
| 20 | + assert.Equal(t, len(diff), 1, "number of items in the disjunction") |
16 | 21 | } |
17 | 22 |
|
18 | | -// Watch returns a watch.Interface that watches the requested namespaces. |
19 | | -func (r *namespace) Watch(label labels.Selector, field fields.Selector, resourceversion string) (watch.Interface, error) { |
20 | | - nst := watch.NewFake() |
21 | | - go func() { |
22 | | - nst.Add(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir1"}}) |
23 | | - nst.Modify(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir1"}}) |
24 | | - nst.Delete(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir1"}}) |
25 | | - nst.Modify(&api.Namespace{ObjectMeta: api.ObjectMeta{Name: "dir2"}}) |
26 | | - nst.Delete(nil) |
27 | | - nst.Add(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "dir3"}}) |
28 | | - nst.Delete(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "dir3"}}) |
29 | | - nst.Stop() |
30 | | - }() |
31 | | - return nst, nil |
| 23 | +func TestDirHasGitSuffix(t *testing.T) { |
| 24 | + assert.True(t, dirHasGitSuffix("a.git"), "'a.git' reported no git suffix") |
| 25 | + assert.False(t, dirHasGitSuffix("abc"), "'a' reported git suffix") |
32 | 26 | } |
33 | 27 |
|
34 | | -func (r *namespace) IsAnAPIObject() {} |
35 | | - |
36 | | -func TestCleanerRun(t *testing.T) { |
37 | | - ns := &namespace{} |
38 | | - fs := sys.NewFakeFS() |
39 | | - dirhome := "/home/git" |
40 | | - fs.Files["/home/git/dir1.git"] = []byte{} |
41 | | - fs.Files["/home/git/dir2.git"] = []byte{} |
42 | | - fs.Files["/home/git/dir3.git"] = []byte{} |
43 | | - go Run(dirhome, ns, fs) |
44 | | - time.Sleep(5 * time.Second) |
45 | | - // Namespace with name dir1 got deleted directory should be deleted |
46 | | - _, ok := fs.Files["/home/git/dir1.git"] |
47 | | - if ok { |
48 | | - t.Fatal("Test failed: Deleting a namespace should delete respective direcotry") |
| 28 | +func TestLocalDirs(t *testing.T) { |
| 29 | + wd, err := os.Getwd() |
| 30 | + assert.NoErr(t, err) |
| 31 | + pkgDir, err := filepath.Abs(wd + "/..") |
| 32 | + assert.NoErr(t, err) |
| 33 | + lDirs, err := localDirs(pkgDir, func(dir string) bool { |
| 34 | + // no directories with any dots in them |
| 35 | + return len(strings.Split(dir, ".")) == 1 |
| 36 | + }) |
| 37 | + assert.NoErr(t, err) |
| 38 | + |
| 39 | + expectedPackages := map[string]int{ |
| 40 | + "cleaner": 1, |
| 41 | + "conf": 1, |
| 42 | + "controller": 1, |
| 43 | + "env": 1, |
| 44 | + "git": 1, |
| 45 | + "gitreceive": 1, |
| 46 | + "healthsrv": 1, |
| 47 | + "k8s": 1, |
| 48 | + "sshd": 1, |
| 49 | + "storage": 1, |
| 50 | + "sys": 1, |
49 | 51 | } |
50 | | - // Namespace with name dir2 got modified directory should not be deleted |
51 | | - _, ok = fs.Files["/home/git/dir2.git"] |
52 | | - if !ok { |
53 | | - t.Fatal("Test failed: Modyfiying a namespace should not delete respective direcotry") |
| 52 | + |
| 53 | + actualPackages := map[string]int{} |
| 54 | + for _, lDir := range lDirs { |
| 55 | + actualPackages[lDir]++ |
54 | 56 | } |
55 | | - // Pod with name dir3 got deleted directory should not be deleted |
56 | | - _, ok = fs.Files["/home/git/dir3.git"] |
57 | | - if !ok { |
58 | | - t.Fatal("Test failed: Deleting a pod should not delete respective direcotry") |
| 57 | + assert.Equal(t, len(actualPackages), len(expectedPackages), "number of packages") |
| 58 | + for actualPackageName, actualNum := range actualPackages { |
| 59 | + if actualNum != 1 { |
| 60 | + t.Errorf("found %d %s packages", actualNum, actualPackageName) |
| 61 | + continue |
| 62 | + } |
| 63 | + expectedNum, ok := expectedPackages[actualPackageName] |
| 64 | + if !ok { |
| 65 | + t.Errorf("found unexpected package %s", actualPackageName) |
| 66 | + continue |
| 67 | + } |
| 68 | + if actualNum != expectedNum { |
| 69 | + t.Errorf("found %d %s packages, expected %d", actualNum, actualPackageName, expectedNum) |
| 70 | + continue |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestStripSuffixes(t *testing.T) { |
| 76 | + strs := []string{"a.git", "b.git", "c.git", "d"} |
| 77 | + newStrs := stripSuffixes(strs, dotGitSuffix) |
| 78 | + assert.Equal(t, len(newStrs), len(strs), "number of strings") |
| 79 | + for _, str := range newStrs { |
| 80 | + assert.False(t, strings.HasSuffix(str, dotGitSuffix), "string %s has suffix %s", str, dotGitSuffix) |
59 | 81 | } |
60 | 82 | } |
0 commit comments