|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + |
| 8 | + spritzv1 "spritz.sh/operator/api/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func TestParseCSV(t *testing.T) { |
| 12 | + if parseCSV("") != nil { |
| 13 | + t.Fatal("expected nil for empty CSV") |
| 14 | + } |
| 15 | + got := parseCSV("/home/dev, /home/spritz") |
| 16 | + if len(got) != 2 { |
| 17 | + t.Fatalf("expected 2 entries, got %d", len(got)) |
| 18 | + } |
| 19 | + if got[0] != "/home/dev" || got[1] != "/home/spritz" { |
| 20 | + t.Fatalf("unexpected values: %v", got) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestParseNodeSelector(t *testing.T) { |
| 25 | + selector, err := parseNodeSelector("spritz.sh/storage-ready=true,zone=fsn1") |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("unexpected error: %v", err) |
| 28 | + } |
| 29 | + if selector["spritz.sh/storage-ready"] != "true" || selector["zone"] != "fsn1" { |
| 30 | + t.Fatalf("unexpected selector: %v", selector) |
| 31 | + } |
| 32 | + |
| 33 | + if _, err := parseNodeSelector("missingequals"); err == nil { |
| 34 | + t.Fatal("expected error for invalid selector entry") |
| 35 | + } |
| 36 | + if _, err := parseNodeSelector("=novalue"); err == nil { |
| 37 | + t.Fatal("expected error for empty key") |
| 38 | + } |
| 39 | + if _, err := parseNodeSelector("key="); err == nil { |
| 40 | + t.Fatal("expected error for empty value") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestBuildHomeMountsDefault(t *testing.T) { |
| 45 | + mounts := buildHomeMounts() |
| 46 | + if len(mounts) != 1 { |
| 47 | + t.Fatalf("expected 1 mount, got %d", len(mounts)) |
| 48 | + } |
| 49 | + if mounts[0].Name != "home" { |
| 50 | + t.Fatalf("unexpected mount name: %s", mounts[0].Name) |
| 51 | + } |
| 52 | + if mounts[0].MountPath != repoInitHomeDir { |
| 53 | + t.Fatalf("unexpected mount path: %s", mounts[0].MountPath) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestBuildPodSecurityContext(t *testing.T) { |
| 58 | + if ctx := buildPodSecurityContext(false, false); ctx != nil { |
| 59 | + t.Fatal("expected nil security context when no shared mounts or repo init") |
| 60 | + } |
| 61 | + |
| 62 | + ctx := buildPodSecurityContext(true, false) |
| 63 | + if ctx == nil || ctx.FSGroup == nil || *ctx.FSGroup != repoInitGroupID { |
| 64 | + t.Fatalf("expected fsGroup %d when shared mounts enabled, got %+v", repoInitGroupID, ctx) |
| 65 | + } |
| 66 | + |
| 67 | + ctx = buildPodSecurityContext(false, true) |
| 68 | + if ctx == nil || ctx.FSGroup == nil || *ctx.FSGroup != repoInitGroupID { |
| 69 | + t.Fatalf("expected fsGroup %d when repo init present, got %+v", repoInitGroupID, ctx) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestBuildRepoInitContainerDedupesHomeMount(t *testing.T) { |
| 74 | + spritz := &spritzv1.Spritz{ |
| 75 | + Spec: spritzv1.SpritzSpec{ |
| 76 | + Repo: &spritzv1.SpritzRepo{ |
| 77 | + URL: "https://github.com/example/repo.git", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + homeMounts := buildHomeMounts() |
| 83 | + repos := repoEntries(spritz) |
| 84 | + containers, _, err := buildRepoInitContainers(spritz, repos, homeMounts) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("unexpected error: %v", err) |
| 87 | + } |
| 88 | + if len(containers) == 0 { |
| 89 | + t.Fatal("expected repo init container") |
| 90 | + } |
| 91 | + |
| 92 | + count := 0 |
| 93 | + for _, mount := range containers[0].VolumeMounts { |
| 94 | + if mount.MountPath == repoInitHomeDir { |
| 95 | + count++ |
| 96 | + } |
| 97 | + } |
| 98 | + if count != 1 { |
| 99 | + t.Fatalf("expected single %s mount, got %d", repoInitHomeDir, count) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestRepoDirNeedsWorkspaceMountHonorsSharedMounts(t *testing.T) { |
| 104 | + mountRoots := []corev1.VolumeMount{ |
| 105 | + {Name: "shared", MountPath: "/shared"}, |
| 106 | + } |
| 107 | + if repoDirNeedsWorkspaceMount("/shared/repo", mountRoots) { |
| 108 | + t.Fatal("expected repo dir under shared mount to skip workspace mount") |
| 109 | + } |
| 110 | + if repoDirNeedsWorkspaceMount("/workspace/repo", mountRoots) { |
| 111 | + t.Fatal("expected repo dir under /workspace to skip workspace mount") |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestValidateRepoDir(t *testing.T) { |
| 116 | + cases := []struct { |
| 117 | + name string |
| 118 | + dir string |
| 119 | + wantErr bool |
| 120 | + }{ |
| 121 | + {"empty ok", "", false}, |
| 122 | + {"relative ok", "spritz", false}, |
| 123 | + {"relative nested ok", "project/app", false}, |
| 124 | + {"relative up invalid", "../etc", true}, |
| 125 | + {"relative up nested invalid", "foo/../../etc", true}, |
| 126 | + {"absolute workspace ok", "/workspace/spritz", false}, |
| 127 | + {"absolute workspace nested ok", "/workspace/spritz/app", false}, |
| 128 | + {"absolute escape invalid", "/etc", true}, |
| 129 | + {"absolute escape via traversal invalid", "/workspace/../etc", true}, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tc := range cases { |
| 133 | + t.Run(tc.name, func(t *testing.T) { |
| 134 | + err := validateRepoDir(tc.dir) |
| 135 | + if tc.wantErr && err == nil { |
| 136 | + t.Fatalf("expected error for %s", tc.dir) |
| 137 | + } |
| 138 | + if !tc.wantErr && err != nil { |
| 139 | + t.Fatalf("unexpected error for %s: %v", tc.dir, err) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments