Skip to content

Commit

Permalink
feat: switch to using api.dev.ooni.io
Browse files Browse the repository at this point in the history
* we switch to using api.dev.ooni.io for tests
* the orchestrate services have been switched to use api.ooni.org
  • Loading branch information
DecFox committed Feb 1, 2025
1 parent 62a2758 commit dd6185b
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 48 deletions.
2 changes: 1 addition & 1 deletion internal/engine/inputloader_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestTargetLoaderInputOrQueryBackendWithNoInput(t *testing.T) {
}
sess, err := engine.NewSession(context.Background(), engine.SessionConfig{
AvailableProbeServices: []model.OOAPIService{{
Address: "https://backend-hel.ooni.org/",
Address: "https://api.dev.ooni.io/",
Type: "https",
}},
KVStore: &kvstore.Memory{},
Expand Down
8 changes: 5 additions & 3 deletions internal/engine/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (s *Session) getAvailableProbeServicesUnlocked() []model.OOAPIService {

func (s *Session) initOrchestraClient(
ctx context.Context, clnt *probeservices.Client,
maybeLogin func(ctx context.Context) error,
maybeLogin func(ctx context.Context, baseURL string) error,
) (*probeservices.Client, error) {
// The original implementation has as its only use case that we
// were registering and logging in for sending an update regarding
Expand All @@ -644,10 +644,12 @@ func (s *Session) initOrchestraClient(
SoftwareVersion: "0.1.0-dev",
SupportedTests: []string{"web_connectivity"},
}
if err := clnt.MaybeRegister(ctx, meta); err != nil {

orchestrator := probeservices.DefaultOrchestrator()
if err := clnt.MaybeRegister(ctx, orchestrator.Address, meta); err != nil {
return nil, err
}
if err := maybeLogin(ctx); err != nil {
if err := maybeLogin(ctx, orchestrator.Address); err != nil {
return nil, err
}
return clnt, nil
Expand Down
10 changes: 5 additions & 5 deletions internal/engine/session_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestSessionTorArgsTorBinary(t *testing.T) {
}
sess, err := NewSession(context.Background(), SessionConfig{
AvailableProbeServices: []model.OOAPIService{{
Address: "https://backend-hel.ooni.org",
Address: "https://api.dev.ooni.io",
Type: "https",
}},
Logger: model.DiscardLogger,
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestSessionTorArgsTorBinary(t *testing.T) {
func newSessionForTestingNoLookupsWithProxyURL(t *testing.T, URL *url.URL) *Session {
sess, err := NewSession(context.Background(), SessionConfig{
AvailableProbeServices: []model.OOAPIService{{
Address: "https://backend-hel.ooni.org",
Address: "https://api.dev.ooni.io",
Type: "https",
}},
Logger: model.DiscardLogger,
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestInitOrchestraClientMaybeRegisterError(t *testing.T) {
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
clnt, err := probeservices.NewClient(sess, model.OOAPIService{
Address: "https://backend-hel.ooni.org/",
Address: "https://api.dev.ooni.io/",
Type: "https",
})
if err != nil {
Expand All @@ -204,15 +204,15 @@ func TestInitOrchestraClientMaybeLoginError(t *testing.T) {
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
clnt, err := probeservices.NewClient(sess, model.OOAPIService{
Address: "https://backend-hel.ooni.org/",
Address: "https://api.dev.ooni.io/",
Type: "https",
})
if err != nil {
t.Fatal(err)
}
expected := errors.New("mocked error")
outclnt, err := sess.initOrchestraClient(
ctx, clnt, func(context.Context) error {
ctx, clnt, func(context.Context, string) error {
return expected
},
)
Expand Down
18 changes: 18 additions & 0 deletions internal/probeservices/benchselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func Default() []model.OOAPIService {
}}
}

// DefaultOrchestrator returns the defaul orchestrate probe service
func DefaultOrchestrator() model.OOAPIService {
return model.OOAPIService{
Address: "https://api.ooni.org",
Type: "orchestrate",
}
}

// SortServices gives priority to https, then cloudfronted, then onion.
func SortServices(in []model.OOAPIService) (out []model.OOAPIService) {
for _, entry := range in {
Expand Down Expand Up @@ -59,6 +67,16 @@ func OnlyFallbacks(in []model.OOAPIService) (out []model.OOAPIService) {
return
}

// OnlyOrchestrate returns the orchestrate services only
func OnlyOrchestrate(in []model.OOAPIService) (out []model.OOAPIService) {
for _, entry := range in {
if entry.Type == "orchestrate" {
out = append(out, entry)
}
}
return
}

// Candidate is a candidate probe service.
type Candidate struct {
// Duration is the time it took to access the service.
Expand Down
2 changes: 1 addition & 1 deletion internal/probeservices/checkin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCheckIn(t *testing.T) {
}

client := newclient()
client.BaseURL = "https://backend-hel.ooni.org" // use the test infra
client.BaseURL = "https://api.dev.ooni.io" // use the test infra

ctx := context.Background()

Expand Down
8 changes: 6 additions & 2 deletions internal/probeservices/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// MaybeLogin performs login if necessary
func (c Client) MaybeLogin(ctx context.Context) error {
func (c Client) MaybeLogin(ctx context.Context, baseURL string) error {
state := c.StateFile.Get()
if state.Auth() != nil {
return nil // we're already good
Expand All @@ -20,7 +20,11 @@ func (c Client) MaybeLogin(ctx context.Context) error {
}
c.LoginCalls.Add(1)

URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/login", "")
// construct the URL to use
if baseURL == "" {
baseURL = c.BaseURL // fallback to the client BaseURL if the passed url is empty
}
URL, err := urlx.ResolveReference(baseURL, "/api/v1/login", "")
if err != nil {
return err
}
Expand Down
60 changes: 46 additions & 14 deletions internal/probeservices/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,50 @@ func TestMaybeLogin(t *testing.T) {
clnt := newclient()

// we need to register first because we don't have state yet
if err := clnt.MaybeRegister(context.Background(), MetadataFixture()); err != nil {
if err := clnt.MaybeRegister(context.Background(), "", MetadataFixture()); err != nil {
t.Fatal(err)
}

// now we try to login and get a token
if err := clnt.MaybeLogin(context.Background()); err != nil {
if err := clnt.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

// do this again, and later on we'll verify that we
// did actually issue just a single login call
if err := clnt.MaybeLogin(context.Background()); err != nil {
if err := clnt.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

// make sure we did call login just once: the second call
// should not invoke login because we have good state
if clnt.LoginCalls.Load() != 1 {
t.Fatal("called login API too many times")
}
})

// Let's also test that the passes baseURL is given precedence over the client baseURL
t.Run("is working with passed baseURL", func(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}

// create client
clnt := newEmptyClient()

// we need to register first because we don't have state yet
if err := clnt.MaybeRegister(context.Background(), "https://api.dev.ooni.io", MetadataFixture()); err != nil {
t.Fatal(err)
}

// now we try to login and get a token
if err := clnt.MaybeLogin(context.Background(), "https://api.dev.ooni.io"); err != nil {
t.Fatal(err)
}

// do this again, and later on we'll verify that we
// did actually issue just a single login call
if err := clnt.MaybeLogin(context.Background(), "https://api.dev.ooni.io"); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -75,18 +107,18 @@ func TestMaybeLogin(t *testing.T) {
}

// we need to register first because we don't have state yet
if err := client.MaybeRegister(context.Background(), MetadataFixture()); err != nil {
if err := client.MaybeRegister(context.Background(), "", MetadataFixture()); err != nil {
t.Fatal(err)
}

// now we try to login and get a token
if err := client.MaybeLogin(context.Background()); err != nil {
if err := client.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

// do this again, and later on we'll verify that we
// did actually issue just a single login call
if err := client.MaybeLogin(context.Background()); err != nil {
if err := client.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -129,18 +161,18 @@ func TestMaybeLogin(t *testing.T) {
}

// we need to register first because we don't have state yet
if err := client.MaybeRegister(context.Background(), MetadataFixture()); err != nil {
if err := client.MaybeRegister(context.Background(), "", MetadataFixture()); err != nil {
t.Fatal(err)
}

// now we try to login and get a token
if err := client.MaybeLogin(context.Background()); err != nil {
if err := client.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

// do this again, and later on we'll verify that we
// did actually issue just a single login call
if err := client.MaybeLogin(context.Background()); err != nil {
if err := client.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -182,7 +214,7 @@ func TestMaybeLogin(t *testing.T) {
}))

// now we try to login and get a token
err := client.MaybeLogin(context.Background())
err := client.MaybeLogin(context.Background(), "")

// we do expect an error
if !errors.Is(err, netxlite.ECONNRESET) {
Expand Down Expand Up @@ -228,7 +260,7 @@ func TestMaybeLogin(t *testing.T) {
}))

// now we try to login and get a token
err := client.MaybeLogin(context.Background())
err := client.MaybeLogin(context.Background(), "")

// we do expect an error
if err == nil || err.Error() != "unexpected end of JSON input" {
Expand Down Expand Up @@ -257,7 +289,7 @@ func TestMaybeLogin(t *testing.T) {

// now call login and we expect no error because we should
// already have what we need to perform a login
if err := clnt.MaybeLogin(context.Background()); err != nil {
if err := clnt.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

Expand All @@ -279,7 +311,7 @@ func TestMaybeLogin(t *testing.T) {
}

// now try to login and expect to see we've not registered yet
if err := clnt.MaybeLogin(context.Background()); !errors.Is(err, ErrNotRegistered) {
if err := clnt.MaybeLogin(context.Background(), ""); !errors.Is(err, ErrNotRegistered) {
t.Fatal("unexpected error", err)
}

Expand All @@ -306,7 +338,7 @@ func TestMaybeLogin(t *testing.T) {
}))

// now we try to login and get a token
err := client.MaybeLogin(context.Background())
err := client.MaybeLogin(context.Background(), "")

// we do expect an error
if err == nil || err.Error() != `parse "\t\t\t": net/url: invalid control character in URL` {
Expand Down
21 changes: 19 additions & 2 deletions internal/probeservices/probeservices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,31 @@ import (
"github.com/ooni/probe-cli/v3/internal/netxlite"
)

func newEmptyClient() *Client {
client, err := NewClient(
&mockable.Session{
MockableHTTPClient: http.DefaultClient,
MockableLogger: log.Log,
},
model.OOAPIService{
Address: "",
Type: "https",
},
)
if err != nil {
panic(err) // so fail the test
}
return client
}

func newclient() *Client {
client, err := NewClient(
&mockable.Session{
MockableHTTPClient: http.DefaultClient,
MockableLogger: log.Log,
},
model.OOAPIService{
Address: "https://backend-hel.ooni.org/",
Address: "https://api.dev.ooni.io",
Type: "https",
},
)
Expand Down Expand Up @@ -579,7 +596,7 @@ func TestGetCredsAndAuthNotLoggedIn(t *testing.T) {
}

clnt := newclient()
if err := clnt.MaybeRegister(context.Background(), MetadataFixture()); err != nil {
if err := clnt.MaybeRegister(context.Background(), "", MetadataFixture()); err != nil {
t.Fatal(err)
}
creds, auth, err := clnt.GetCredsAndAuth()
Expand Down
4 changes: 2 additions & 2 deletions internal/probeservices/psiphon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func TestFetchPsiphonConfig(t *testing.T) {
// psiphonflow is the flow with which we invoke the psiphon API
psiphonflow := func(t *testing.T, client *Client) ([]byte, error) {
// we need to make sure we're registered and logged in
if err := client.MaybeRegister(context.Background(), MetadataFixture()); err != nil {
if err := client.MaybeRegister(context.Background(), "", MetadataFixture()); err != nil {
t.Fatal(err)
}
if err := client.MaybeLogin(context.Background()); err != nil {
if err := client.MaybeLogin(context.Background(), ""); err != nil {
t.Fatal(err)
}

Expand Down
7 changes: 5 additions & 2 deletions internal/probeservices/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// MaybeRegister registers this client if not already registered
func (c Client) MaybeRegister(ctx context.Context, metadata model.OOAPIProbeMetadata) error {
func (c Client) MaybeRegister(ctx context.Context, baseURL string, metadata model.OOAPIProbeMetadata) error {
if !metadata.Valid() {
return ErrInvalidMetadata
}
Expand All @@ -32,7 +32,10 @@ func (c Client) MaybeRegister(ctx context.Context, metadata model.OOAPIProbeMeta
}

// construct the URL to use
URL, err := urlx.ResolveReference(c.BaseURL, "/api/v1/register", "")
if baseURL == "" {
baseURL = c.BaseURL // fallback to the client BaseURL if the passed url is empty
}
URL, err := urlx.ResolveReference(baseURL, "/api/v1/register", "")
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit dd6185b

Please sign in to comment.