| 1 | package isolatedjob |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/testci" |
| 15 | ) |
| 16 | |
| 17 | const guardModule = "amadan.net/oficina/equipo" |
| 18 | |
| 19 | // providerAdapter is the one package tree that may hold AWS SDK types. |
| 20 | const providerAdapter = guardModule + "/internal/isolatedjob/awsjob" |
| 21 | |
| 22 | // providerAllowed are the composition points that wire the adapter in. Only |
| 23 | // commands belong here: a library package that needs AWS is a leak. Slice |
| 24 | // 1b adds cmd/equipo-vm-init and cmd/equipo-hosted-image. |
| 25 | var providerAllowed = []string{ |
| 26 | guardModule + "/cmd/equipo-hosted-controller", |
| 27 | } |
| 28 | |
| 29 | // TestProviderTypesStayInAdapter keeps the AWS SDK and the adapter out of |
| 30 | // every package of this module except the adapter itself and the commands |
| 31 | // in providerAllowed. Test files count: a test that imports the SDK is a |
| 32 | // package that depends on it. |
| 33 | func TestProviderTypesStayInAdapter(t *testing.T) { |
| 34 | for _, p := range providerAllowed { |
| 35 | if !strings.HasPrefix(p, guardModule+"/cmd/") { |
| 36 | t.Errorf("providerAllowed holds %s; only cmd/ composition points may depend on the AWS SDK or awsjob", p) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // go list reads every package in the tree, which go test cannot see, |
| 41 | // so declare the whole tree: a new or edited package reruns the guard. |
| 42 | testci.RepoFiles(t) |
| 43 | out, err := exec.Command("go", "list", "-test", "-json=ImportPath,Dir,Deps,Module", guardModule+"/...").Output() |
| 44 | if err != nil { |
| 45 | var exit *exec.ExitError |
| 46 | if errors.As(err, &exit) { |
| 47 | t.Fatalf("go list: %v\n%s", err, exit.Stderr) |
| 48 | } |
| 49 | t.Fatalf("go list: %v", err) |
| 50 | } |
| 51 | |
| 52 | var pkgs []listedPackage |
| 53 | dec := json.NewDecoder(bytes.NewReader(out)) |
| 54 | for { |
| 55 | var p listedPackage |
| 56 | if err := dec.Decode(&p); err == io.EOF { |
| 57 | break |
| 58 | } else if err != nil { |
| 59 | t.Fatalf("go list output: %v", err) |
| 60 | } |
| 61 | pkgs = append(pkgs, p) |
| 62 | } |
| 63 | offenders, adapterSeen := providerOffenders(pkgs, providerAllowed) |
| 64 | if len(offenders) > 0 { |
| 65 | t.Errorf("packages outside %s depend on the AWS SDK or the adapter:\n %s", providerAdapter, strings.Join(offenders, "\n ")) |
| 66 | } |
| 67 | if !adapterSeen { |
| 68 | t.Errorf("go list showed no %s package depending on github.com/aws/; the guard is not seeing the adapter", providerAdapter) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // listedPackage is what the guard reads of one go list -test entry. |
| 73 | type listedPackage struct { |
| 74 | ImportPath string |
| 75 | Dir string // the directory of the package's source files |
| 76 | Deps []string |
| 77 | Module *struct{ Path, Dir string } |
| 78 | } |
| 79 | |
| 80 | // providerOffenders returns each module package outside the adapter and |
| 81 | // allowed that depends on the AWS SDK or the adapter, with the dependency |
| 82 | // that pulled it in, and whether any adapter package depends on the SDK. |
| 83 | // No entry is skipped, and none is judged by its import path string. |
| 84 | func providerOffenders(pkgs []listedPackage, allowed []string) (offenders []string, adapterSeen bool) { |
| 85 | for _, p := range pkgs { |
| 86 | if p.Module == nil || p.Module.Path != guardModule { |
| 87 | continue |
| 88 | } |
| 89 | base := basePackage(p) |
| 90 | pulled := providerDep(p.Deps) |
| 91 | if inAdapter(base) { |
| 92 | if strings.HasPrefix(pulled, "github.com/aws/") { |
| 93 | adapterSeen = true |
| 94 | } |
| 95 | continue |
| 96 | } |
| 97 | if pulled == "" || slices.Contains(allowed, base) { |
| 98 | continue |
| 99 | } |
| 100 | line := base + " (via " + pulled + ")" |
| 101 | if !slices.Contains(offenders, line) { |
| 102 | offenders = append(offenders, line) |
| 103 | } |
| 104 | } |
| 105 | return offenders, adapterSeen |
| 106 | } |
| 107 | |
| 108 | // basePackage names the package an entry belongs to by the directory of |
| 109 | // its source, not by its import path. go list -test gives a test variant |
| 110 | // "q [p.test]" q's directory, the external test package "p_test [p.test]" |
| 111 | // and the generated test main "p.test" p's directory, and an ordinary |
| 112 | // package its own, whatever its name ends with. So a real package at |
| 113 | // p.test or p_test is judged as itself, and generated entries as the |
| 114 | // package they test. An entry outside the module directory keeps its own |
| 115 | // path. |
| 116 | func basePackage(p listedPackage) string { |
| 117 | rel, err := filepath.Rel(p.Module.Dir, p.Dir) |
| 118 | if err != nil || p.Module.Dir == "" || p.Dir == "" || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { |
| 119 | return plainPath(p.ImportPath) |
| 120 | } |
| 121 | if rel == "." { |
| 122 | return p.Module.Path |
| 123 | } |
| 124 | return p.Module.Path + "/" + filepath.ToSlash(rel) |
| 125 | } |
| 126 | |
| 127 | // plainPath drops the " [p.test]" go list adds to a test variant. |
| 128 | func plainPath(path string) string { |
| 129 | path, _, _ = strings.Cut(path, " ") |
| 130 | return path |
| 131 | } |
| 132 | |
| 133 | func inAdapter(path string) bool { |
| 134 | return path == providerAdapter || strings.HasPrefix(path, providerAdapter+"/") |
| 135 | } |
| 136 | |
| 137 | // providerDep returns the first dependency that is the AWS SDK or the |
| 138 | // adapter, or "" when there is none. |
| 139 | func providerDep(deps []string) string { |
| 140 | for _, d := range deps { |
| 141 | if strings.HasPrefix(d, "github.com/aws/") || inAdapter(plainPath(d)) { |
| 142 | return d |
| 143 | } |
| 144 | } |
| 145 | return "" |
| 146 | } |
| 147 | |
| 148 | // TestProviderGuardClassifies runs the guard's judgement on a made-up go |
| 149 | // list answer, so the cases a real tree may not hold are covered: an |
| 150 | // ordinary package whose name ends in _test beside the adapter, an |
| 151 | // external test package that imports the adapter, and a generated test main. |
| 152 | func TestProviderGuardClassifies(t *testing.T) { |
| 153 | const root = "/m" |
| 154 | mod := &struct{ Path, Dir string }{guardModule, root} |
| 155 | other := &struct{ Path, Dir string }{"example.com/other", "/o"} |
| 156 | dir := func(rel string) string { return root + "/" + rel } |
| 157 | sdk := "github.com/aws/aws-sdk-go-v2/aws" |
| 158 | adapter, adapterDir := providerAdapter, dir("internal/isolatedjob/awsjob") |
| 159 | sibling, siblingDir := guardModule+"/internal/isolatedjob/awsjob_test", dir("internal/isolatedjob/awsjob_test") |
| 160 | job, jobDir := guardModule+"/internal/isolatedjob", dir("internal/isolatedjob") |
| 161 | ctl, ctlDir := guardModule+"/internal/hostedctl", dir("internal/hostedctl") |
| 162 | controller := guardModule + "/cmd/equipo-hosted-controller" |
| 163 | pkgs := []listedPackage{ |
| 164 | // The adapter, its variants and its generated test main: allowed. |
| 165 | {ImportPath: adapter, Dir: adapterDir, Deps: []string{sdk}, Module: mod}, |
| 166 | {ImportPath: adapter + " [" + adapter + ".test]", Dir: adapterDir, Deps: []string{sdk}, Module: mod}, |
| 167 | {ImportPath: adapter + "_test [" + adapter + ".test]", Dir: adapterDir, Deps: []string{sdk, adapter + " [" + adapter + ".test]"}, Module: mod}, |
| 168 | {ImportPath: adapter + ".test", Dir: adapterDir, Deps: []string{sdk}, Module: mod}, |
| 169 | // An ordinary package named like an external test, beside the adapter. |
| 170 | {ImportPath: sibling, Dir: siblingDir, Deps: []string{sdk}, Module: mod}, |
| 171 | {ImportPath: sibling + " [" + sibling + ".test]", Dir: siblingDir, Deps: []string{sdk}, Module: mod}, |
| 172 | // isolatedjob's generated test main, and a real package with the same |
| 173 | // import path string in a directory of its own. |
| 174 | {ImportPath: job, Dir: jobDir, Deps: []string{"fmt"}, Module: mod}, |
| 175 | {ImportPath: job + " [" + job + ".test]", Dir: jobDir, Deps: []string{"fmt"}, Module: mod}, |
| 176 | {ImportPath: job + ".test", Dir: jobDir, Deps: []string{"fmt", "testing"}, Module: mod}, |
| 177 | {ImportPath: job + ".test", Dir: dir("internal/isolatedjob.test"), Deps: []string{sdk}, Module: mod}, |
| 178 | // An external test package that imports the adapter. |
| 179 | {ImportPath: ctl, Dir: ctlDir, Deps: []string{"fmt"}, Module: mod}, |
| 180 | {ImportPath: ctl + "_test [" + ctl + ".test]", Dir: ctlDir, Deps: []string{adapter + " [" + ctl + ".test]"}, Module: mod}, |
| 181 | {ImportPath: ctl + ".test", Dir: ctlDir, Deps: []string{adapter + " [" + ctl + ".test]"}, Module: mod}, |
| 182 | {ImportPath: guardModule + "/internal/isolatedjob/awsjobx", Dir: dir("internal/isolatedjob/awsjobx"), Deps: []string{adapter}, Module: mod}, |
| 183 | {ImportPath: controller, Dir: dir("cmd/equipo-hosted-controller"), Deps: []string{sdk, adapter}, Module: mod}, |
| 184 | {ImportPath: "example.com/other/aws", Dir: "/o/aws", Deps: []string{sdk}, Module: other}, |
| 185 | } |
| 186 | got, seen := providerOffenders(pkgs, []string{controller}) |
| 187 | want := []string{ |
| 188 | sibling + " (via " + sdk + ")", |
| 189 | job + ".test (via " + sdk + ")", |
| 190 | ctl + " (via " + adapter + " [" + ctl + ".test])", |
| 191 | guardModule + "/internal/isolatedjob/awsjobx (via " + adapter + ")", |
| 192 | } |
| 193 | if !slices.Equal(got, want) { |
| 194 | t.Errorf("offenders =\n %s\nwant\n %s", strings.Join(got, "\n "), strings.Join(want, "\n ")) |
| 195 | } |
| 196 | if !seen { |
| 197 | t.Errorf("adapterSeen = false, want true") |
| 198 | } |
| 199 | } |
| 200 | |