package mirror import ( "context" "net/url" "path/filepath" "testing" "github.com/grmrgecko/repo-sync/internal/testrepos" ) // TestChildEntry verifies anchor resolution against a directory URL. func TestChildEntry(t *testing.T) { base, _ := url.Parse("http://example.com/repos/") cases := []struct { href string want string isDir bool }{ {"el9/", "el9", true}, {"file.rpm", "file.rpm", false}, {"/repos/el8/", "el8", true}, {"../", "", false}, {"?C=N;O=D", "", false}, {"http://other.example.com/repos/x/", "", false}, {"/outside/", "", false}, {"deep/nested/", "", false}, {"#anchor", "", false}, } for _, c := range cases { got, isDir := childEntry(base, c.href) if got != c.want || isDir != c.isDir { t.Errorf("childEntry(%q) = %q,%v, want %q,%v", c.href, got, isDir, c.want, c.isDir) } } } // TestDiscoverRepos verifies RPM repositories are found through directory // listings and that the depth limit is honored. func TestDiscoverRepos(t *testing.T) { www := t.TempDir() testrepos.BuildRPMRepo(t, filepath.Join(www, "a", "repo1")) testrepos.BuildRPMRepo(t, filepath.Join(www, "b", "nested", "repo2")) testrepos.BuildRPMRepo(t, filepath.Join(www, "deep", "x", "y", "z", "repo3")) srv := testrepos.ServeDir(t, www) found, err := discoverRepos(context.Background(), srv.URL, RepoRPM, 3) if err != nil { t.Fatal(err) } want := []string{srv.URL + "/a/repo1", srv.URL + "/b/nested/repo2"} if len(found) != len(want) { t.Fatalf("found %v, want %v", found, want) } for i := range want { if found[i] != want[i] { t.Errorf("found[%d] = %q, want %q", i, found[i], want[i]) } } // A deeper limit reaches the third repository. found, err = discoverRepos(context.Background(), srv.URL, RepoRPM, 6) if err != nil { t.Fatal(err) } if len(found) != 3 { t.Errorf("depth 6 found %v, want 3 repositories", found) } } // TestDiscoverArchRepos verifies pacman repositories are recognized by // their database files. func TestDiscoverArchRepos(t *testing.T) { www := t.TempDir() testrepos.BuildArchRepo(t, filepath.Join(www, "core", "os", "x86_64"), "core") testrepos.BuildArchRepo(t, filepath.Join(www, "extra", "os", "x86_64"), "extra") srv := testrepos.ServeDir(t, www) found, err := discoverRepos(context.Background(), srv.URL, RepoArch, 4) if err != nil { t.Fatal(err) } want := []string{srv.URL + "/core/os/x86_64", srv.URL + "/extra/os/x86_64"} if len(found) != len(want) { t.Fatalf("found %v, want %v", found, want) } for i := range want { if found[i] != want[i] { t.Errorf("found[%d] = %q, want %q", i, found[i], want[i]) } } } // TestDiscoverApkRepos verifies Alpine repositories are recognized by // their index files, matching the arch directories under a release tree. func TestDiscoverApkRepos(t *testing.T) { www := t.TempDir() testrepos.BuildApkRepo(t, filepath.Join(www, "v3.24", "community", "x86_64")) testrepos.BuildApkRepo(t, filepath.Join(www, "v3.24", "community", "aarch64")) srv := testrepos.ServeDir(t, www) found, err := discoverRepos(context.Background(), srv.URL+"/v3.24/community", RepoApk, 1) if err != nil { t.Fatal(err) } want := []string{srv.URL + "/v3.24/community/aarch64", srv.URL + "/v3.24/community/x86_64"} if len(found) != len(want) { t.Fatalf("found %v, want %v", found, want) } for i := range want { if found[i] != want[i] { t.Errorf("found[%d] = %q, want %q", i, found[i], want[i]) } } } // TestDiscoverDebRepos verifies suite directories are recognized by their // release files. func TestDiscoverDebRepos(t *testing.T) { www := t.TempDir() testrepos.BuildDebRepo(t, filepath.Join(www, "debian")) testrepos.BuildFlatDebRepo(t, filepath.Join(www, "flat")) srv := testrepos.ServeDir(t, www) found, err := discoverRepos(context.Background(), srv.URL, RepoDeb, 4) if err != nil { t.Fatal(err) } want := []string{srv.URL + "/debian/dists/test", srv.URL + "/flat"} if len(found) != len(want) { t.Fatalf("found %v, want %v", found, want) } for i := range want { if found[i] != want[i] { t.Errorf("found[%d] = %q, want %q", i, found[i], want[i]) } } }