package isolatedjob import ( "bytes" "encoding/json" "errors" "io" "os/exec" "path/filepath" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/testci" ) const guardModule = "amadan.net/oficina/equipo" // providerAdapter is the one package tree that may hold AWS SDK types. const providerAdapter = guardModule + "/internal/isolatedjob/awsjob" // providerAllowed are the composition points that wire the adapter in. Only // commands belong here: a library package that needs AWS is a leak. Slice // 1b adds cmd/equipo-vm-init and cmd/equipo-hosted-image. var providerAllowed = []string{ guardModule + "/cmd/equipo-hosted-controller", } // TestProviderTypesStayInAdapter keeps the AWS SDK and the adapter out of // every package of this module except the adapter itself and the commands // in providerAllowed. Test files count: a test that imports the SDK is a // package that depends on it. func TestProviderTypesStayInAdapter(t *testing.T) { for _, p := range providerAllowed { if !strings.HasPrefix(p, guardModule+"/cmd/") { t.Errorf("providerAllowed holds %s; only cmd/ composition points may depend on the AWS SDK or awsjob", p) } } // go list reads every package in the tree, which go test cannot see, // so declare the whole tree: a new or edited package reruns the guard. testci.RepoFiles(t) out, err := exec.Command("go", "list", "-test", "-json=ImportPath,Dir,Deps,Module", guardModule+"/...").Output() if err != nil { var exit *exec.ExitError if errors.As(err, &exit) { t.Fatalf("go list: %v\n%s", err, exit.Stderr) } t.Fatalf("go list: %v", err) } var pkgs []listedPackage dec := json.NewDecoder(bytes.NewReader(out)) for { var p listedPackage if err := dec.Decode(&p); err == io.EOF { break } else if err != nil { t.Fatalf("go list output: %v", err) } pkgs = append(pkgs, p) } offenders, adapterSeen := providerOffenders(pkgs, providerAllowed) if len(offenders) > 0 { t.Errorf("packages outside %s depend on the AWS SDK or the adapter:\n %s", providerAdapter, strings.Join(offenders, "\n ")) } if !adapterSeen { t.Errorf("go list showed no %s package depending on github.com/aws/; the guard is not seeing the adapter", providerAdapter) } } // listedPackage is what the guard reads of one go list -test entry. type listedPackage struct { ImportPath string Dir string // the directory of the package's source files Deps []string Module *struct{ Path, Dir string } } // providerOffenders returns each module package outside the adapter and // allowed that depends on the AWS SDK or the adapter, with the dependency // that pulled it in, and whether any adapter package depends on the SDK. // No entry is skipped, and none is judged by its import path string. func providerOffenders(pkgs []listedPackage, allowed []string) (offenders []string, adapterSeen bool) { for _, p := range pkgs { if p.Module == nil || p.Module.Path != guardModule { continue } base := basePackage(p) pulled := providerDep(p.Deps) if inAdapter(base) { if strings.HasPrefix(pulled, "github.com/aws/") { adapterSeen = true } continue } if pulled == "" || slices.Contains(allowed, base) { continue } line := base + " (via " + pulled + ")" if !slices.Contains(offenders, line) { offenders = append(offenders, line) } } return offenders, adapterSeen } // basePackage names the package an entry belongs to by the directory of // its source, not by its import path. go list -test gives a test variant // "q [p.test]" q's directory, the external test package "p_test [p.test]" // and the generated test main "p.test" p's directory, and an ordinary // package its own, whatever its name ends with. So a real package at // p.test or p_test is judged as itself, and generated entries as the // package they test. An entry outside the module directory keeps its own // path. func basePackage(p listedPackage) string { rel, err := filepath.Rel(p.Module.Dir, p.Dir) if err != nil || p.Module.Dir == "" || p.Dir == "" || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { return plainPath(p.ImportPath) } if rel == "." { return p.Module.Path } return p.Module.Path + "/" + filepath.ToSlash(rel) } // plainPath drops the " [p.test]" go list adds to a test variant. func plainPath(path string) string { path, _, _ = strings.Cut(path, " ") return path } func inAdapter(path string) bool { return path == providerAdapter || strings.HasPrefix(path, providerAdapter+"/") } // providerDep returns the first dependency that is the AWS SDK or the // adapter, or "" when there is none. func providerDep(deps []string) string { for _, d := range deps { if strings.HasPrefix(d, "github.com/aws/") || inAdapter(plainPath(d)) { return d } } return "" } // TestProviderGuardClassifies runs the guard's judgement on a made-up go // list answer, so the cases a real tree may not hold are covered: an // ordinary package whose name ends in _test beside the adapter, an // external test package that imports the adapter, and a generated test main. func TestProviderGuardClassifies(t *testing.T) { const root = "/m" mod := &struct{ Path, Dir string }{guardModule, root} other := &struct{ Path, Dir string }{"example.com/other", "/o"} dir := func(rel string) string { return root + "/" + rel } sdk := "github.com/aws/aws-sdk-go-v2/aws" adapter, adapterDir := providerAdapter, dir("internal/isolatedjob/awsjob") sibling, siblingDir := guardModule+"/internal/isolatedjob/awsjob_test", dir("internal/isolatedjob/awsjob_test") job, jobDir := guardModule+"/internal/isolatedjob", dir("internal/isolatedjob") ctl, ctlDir := guardModule+"/internal/hostedctl", dir("internal/hostedctl") controller := guardModule + "/cmd/equipo-hosted-controller" pkgs := []listedPackage{ // The adapter, its variants and its generated test main: allowed. {ImportPath: adapter, Dir: adapterDir, Deps: []string{sdk}, Module: mod}, {ImportPath: adapter + " [" + adapter + ".test]", Dir: adapterDir, Deps: []string{sdk}, Module: mod}, {ImportPath: adapter + "_test [" + adapter + ".test]", Dir: adapterDir, Deps: []string{sdk, adapter + " [" + adapter + ".test]"}, Module: mod}, {ImportPath: adapter + ".test", Dir: adapterDir, Deps: []string{sdk}, Module: mod}, // An ordinary package named like an external test, beside the adapter. {ImportPath: sibling, Dir: siblingDir, Deps: []string{sdk}, Module: mod}, {ImportPath: sibling + " [" + sibling + ".test]", Dir: siblingDir, Deps: []string{sdk}, Module: mod}, // isolatedjob's generated test main, and a real package with the same // import path string in a directory of its own. {ImportPath: job, Dir: jobDir, Deps: []string{"fmt"}, Module: mod}, {ImportPath: job + " [" + job + ".test]", Dir: jobDir, Deps: []string{"fmt"}, Module: mod}, {ImportPath: job + ".test", Dir: jobDir, Deps: []string{"fmt", "testing"}, Module: mod}, {ImportPath: job + ".test", Dir: dir("internal/isolatedjob.test"), Deps: []string{sdk}, Module: mod}, // An external test package that imports the adapter. {ImportPath: ctl, Dir: ctlDir, Deps: []string{"fmt"}, Module: mod}, {ImportPath: ctl + "_test [" + ctl + ".test]", Dir: ctlDir, Deps: []string{adapter + " [" + ctl + ".test]"}, Module: mod}, {ImportPath: ctl + ".test", Dir: ctlDir, Deps: []string{adapter + " [" + ctl + ".test]"}, Module: mod}, {ImportPath: guardModule + "/internal/isolatedjob/awsjobx", Dir: dir("internal/isolatedjob/awsjobx"), Deps: []string{adapter}, Module: mod}, {ImportPath: controller, Dir: dir("cmd/equipo-hosted-controller"), Deps: []string{sdk, adapter}, Module: mod}, {ImportPath: "example.com/other/aws", Dir: "/o/aws", Deps: []string{sdk}, Module: other}, } got, seen := providerOffenders(pkgs, []string{controller}) want := []string{ sibling + " (via " + sdk + ")", job + ".test (via " + sdk + ")", ctl + " (via " + adapter + " [" + ctl + ".test])", guardModule + "/internal/isolatedjob/awsjobx (via " + adapter + ")", } if !slices.Equal(got, want) { t.Errorf("offenders =\n %s\nwant\n %s", strings.Join(got, "\n "), strings.Join(want, "\n ")) } if !seen { t.Errorf("adapterSeen = false, want true") } }