| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func TestEnvironmentExpand(t *testing.T) { |
| 13 | s, err := store.Open(filepath.Join(t.TempDir(), "b.db"), make([]byte, 32)) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | defer s.Close() |
| 18 | ctx := context.Background() |
| 19 | tim, _ := s.UpsertUser(ctx, 1, "tim", "", false) |
| 20 | g, _ := s.CreateGroup(ctx, tim.ID, "Team", "team", "", access.RoleReadonly) |
| 21 | repo, _ := s.CreateRepo(ctx, "group", g.ID, "app", "", "master", "private") |
| 22 | put := func(kind string, id int64, n, v string, on bool) { |
| 23 | if err := s.PutSecret(ctx, kind, id, n, v, false); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | _ = s.SetSecretExpand(ctx, kind, id, n, on) |
| 27 | } |
| 28 | put("group", g.ID, "HOST", "db.local", false) |
| 29 | put("group", g.ID, "URL", "http://$HOST/$CI_PROJECT_NAME", true) |
| 30 | put("group", g.ID, "OVR", "grp-$HOST", true) |
| 31 | put("repo", repo.ID, "OVR", "repo-$HOST", false) // project wint, ook vlag uit |
| 32 | put("repo", repo.ID, "PASS", "pa$$w$HOST", false) |
| 33 | put("repo", repo.ID, "LIT", "$$x-${NOPE}", true) |
| 34 | put("repo", repo.ID, "JOBV", "s-$HOST", true) |
| 35 | r := &Runner{Store: s} |
| 36 | job := store.Job{Name: "t", Variables: map[string]string{"JOBV": "job-$HOST"}} |
| 37 | env, mask, err := r.environment(ctx, repo, store.Pipeline{Ref: "feature"}, job, "") |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | want := map[string]string{ |
| 42 | "URL": "http://db.local/app", "OVR": "repo-$HOST", "PASS": "pa$$w$HOST", |
| 43 | "LIT": "$x-${NOPE}", "JOBV": "job-$HOST", |
| 44 | } |
| 45 | for k, v := range want { |
| 46 | if env[k] != v { |
| 47 | t.Errorf("%s=%q want %q", k, env[k], v) |
| 48 | } |
| 49 | } |
| 50 | found := false |
| 51 | for _, m := range mask { |
| 52 | found = found || m == "http://db.local/app" |
| 53 | } |
| 54 | if !found { |
| 55 | t.Error("uitgebreide waarde niet gemaskeerd") |
| 56 | } |
| 57 | } |