| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/registry" |
| 10 | ) |
| 11 | |
| 12 | // With generations on, the advisory does not dial the activator's own link |
| 13 | // at a managed route's canonical path (the checked dial would call it a peer |
| 14 | // mismatch every hour). Any other link, or a path that is not a link, is |
| 15 | // probed as before. |
| 16 | func TestSocketAdvisorySkipsTheActivatorsOwnLink(t *testing.T) { |
| 17 | dir := t.TempDir() |
| 18 | own := filepath.Join(dir, "own.sock") |
| 19 | foreign := filepath.Join(dir, "foreign.sock") |
| 20 | plain := filepath.Join(dir, "plain.sock") |
| 21 | for path, target := range map[string]string{own: "/run/carlos/g/aaaaaaaaaaaa.3.sock", foreign: "/run/carlos/elsewhere.sock"} { |
| 22 | if err := os.Symlink(target, path); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | routes := []registry.Route{ |
| 27 | {Host: "own.example.com", Kind: "instance", Hibernate: true, Socket: own}, |
| 28 | {Host: "foreign.example.com", Kind: "instance", Hibernate: true, Socket: foreign}, |
| 29 | {Host: "plain.example.com", Kind: "instance", Hibernate: true, Socket: plain}, |
| 30 | } |
| 31 | appSocket := func(string) (string, bool) { return "/run/carlos/g/aaaaaaaaaaaa.3.sock", true } |
| 32 | var probed []string |
| 33 | serving := advisorySocketServing(routes, appSocket, func(sock string) bool { |
| 34 | probed = append(probed, sock) |
| 35 | return true |
| 36 | }) |
| 37 | if serving(own) { |
| 38 | t.Fatal("the activator's own link was reported as served by a stray") |
| 39 | } |
| 40 | if !serving(foreign) || !serving(plain) { |
| 41 | t.Fatal("a foreign link or a plain path was not probed") |
| 42 | } |
| 43 | if fmt.Sprint(probed) != fmt.Sprint([]string{foreign, plain}) { |
| 44 | t.Fatalf("probed %v, want the foreign link and the plain path only", probed) |
| 45 | } |
| 46 | } |
| 47 | |