ooni-probe-cli/internal/engine/mockable/mockable.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

145 lines
4.4 KiB
Go

// Package mockable contains mockable objects
package mockable
import (
"context"
"net/http"
"net/url"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/model"
)
// Session allows to mock sessions.
type Session struct {
MockableTestHelpers map[string][]model.OOAPIService
MockableHTTPClient *http.Client
MockableLogger model.Logger
MockableMaybeResolverIP string
MockableProbeASNString string
MockableProbeCC string
MockableProbeIP string
MockableProbeNetworkName string
MockableProxyURL *url.URL
MockableFetchPsiphonConfigResult []byte
MockableFetchPsiphonConfigErr error
MockableFetchTorTargetsResult map[string]model.OOAPITorTarget
MockableFetchTorTargetsErr error
MockableFetchURLListResult []model.OOAPIURLInfo
MockableFetchURLListErr error
MockableResolverIP string
MockableSoftwareName string
MockableSoftwareVersion string
MockableTempDir string
MockableTorArgs []string
MockableTorBinary string
MockableUserAgent string
}
// GetTestHelpersByName implements ExperimentSession.GetTestHelpersByName
func (sess *Session) GetTestHelpersByName(name string) ([]model.OOAPIService, bool) {
services, okay := sess.MockableTestHelpers[name]
return services, okay
}
// DefaultHTTPClient implements ExperimentSession.DefaultHTTPClient
func (sess *Session) DefaultHTTPClient() *http.Client {
return sess.MockableHTTPClient
}
// FetchPsiphonConfig implements ExperimentSession.FetchPsiphonConfig
func (sess *Session) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
return sess.MockableFetchPsiphonConfigResult, sess.MockableFetchPsiphonConfigErr
}
// FetchTorTargets implements ExperimentSession.TorTargets
func (sess *Session) FetchTorTargets(
ctx context.Context, cc string) (map[string]model.OOAPITorTarget, error) {
return sess.MockableFetchTorTargetsResult, sess.MockableFetchTorTargetsErr
}
// FetchURLList implements ExperimentSession.FetchURLList.
func (sess *Session) FetchURLList(
ctx context.Context, config model.OOAPIURLListConfig) ([]model.OOAPIURLInfo, error) {
return sess.MockableFetchURLListResult, sess.MockableFetchURLListErr
}
// KeyValueStore returns the configured key-value store.
func (sess *Session) KeyValueStore() model.KeyValueStore {
return &kvstore.Memory{}
}
// Logger implements ExperimentSession.Logger
func (sess *Session) Logger() model.Logger {
return sess.MockableLogger
}
// MaybeResolverIP implements ExperimentSession.MaybeResolverIP.
func (sess *Session) MaybeResolverIP() string {
return sess.MockableMaybeResolverIP
}
// ProbeASNString implements ExperimentSession.ProbeASNString
func (sess *Session) ProbeASNString() string {
return sess.MockableProbeASNString
}
// ProbeCC implements ExperimentSession.ProbeCC
func (sess *Session) ProbeCC() string {
return sess.MockableProbeCC
}
// ProbeIP implements ExperimentSession.ProbeIP
func (sess *Session) ProbeIP() string {
return sess.MockableProbeIP
}
// ProbeNetworkName implements ExperimentSession.ProbeNetworkName
func (sess *Session) ProbeNetworkName() string {
return sess.MockableProbeNetworkName
}
// ProxyURL implements ExperimentSession.ProxyURL
func (sess *Session) ProxyURL() *url.URL {
return sess.MockableProxyURL
}
// ResolverIP implements ExperimentSession.ResolverIP
func (sess *Session) ResolverIP() string {
return sess.MockableResolverIP
}
// SoftwareName implements ExperimentSession.SoftwareName
func (sess *Session) SoftwareName() string {
return sess.MockableSoftwareName
}
// SoftwareVersion implements ExperimentSession.SoftwareVersion
func (sess *Session) SoftwareVersion() string {
return sess.MockableSoftwareVersion
}
// TempDir implements ExperimentSession.TempDir
func (sess *Session) TempDir() string {
return sess.MockableTempDir
}
// TorArgs implements ExperimentSession.TorArgs.
func (sess *Session) TorArgs() []string {
return sess.MockableTorArgs
}
// TorBinary implements ExperimentSession.TorBinary.
func (sess *Session) TorBinary() string {
return sess.MockableTorBinary
}
// UserAgent implements ExperimentSession.UserAgent
func (sess *Session) UserAgent() string {
return sess.MockableUserAgent
}
var _ model.ExperimentSession = &Session{}
var _ probeservices.Session = &Session{}