2021-02-02 12:05:47 +01:00
|
|
|
package telegram_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/telegram"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
2021-06-22 00:12:03 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/mockable"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-09-28 12:42:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewExperimentMeasurer(t *testing.T) {
|
|
|
|
measurer := telegram.NewExperimentMeasurer(telegram.Config{})
|
|
|
|
if measurer.ExperimentName() != "telegram" {
|
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
|
|
|
if measurer.ExperimentVersion() != "0.2.0" {
|
|
|
|
t.Fatal("unexpected version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGood(t *testing.T) {
|
|
|
|
measurer := telegram.NewExperimentMeasurer(telegram.Config{})
|
|
|
|
measurement := new(model.Measurement)
|
|
|
|
err := measurer.Run(
|
|
|
|
context.Background(),
|
|
|
|
&mockable.Session{
|
|
|
|
MockableLogger: log.Log,
|
|
|
|
},
|
|
|
|
measurement,
|
|
|
|
model.NewPrinterCallbacks(log.Log),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tk := measurement.TestKeys.(*telegram.TestKeys)
|
|
|
|
if tk.Agent != "redirect" {
|
|
|
|
t.Fatal("unexpected Agent")
|
|
|
|
}
|
|
|
|
if tk.FailedOperation != nil {
|
|
|
|
t.Fatal("unexpected FailedOperation")
|
|
|
|
}
|
|
|
|
if tk.Failure != nil {
|
|
|
|
t.Fatal("unexpected Failure")
|
|
|
|
}
|
|
|
|
if len(tk.NetworkEvents) <= 0 {
|
|
|
|
t.Fatal("no NetworkEvents?!")
|
|
|
|
}
|
|
|
|
if len(tk.Queries) <= 0 {
|
|
|
|
t.Fatal("no Queries?!")
|
|
|
|
}
|
|
|
|
if len(tk.Requests) <= 0 {
|
|
|
|
t.Fatal("no Requests?!")
|
|
|
|
}
|
|
|
|
if len(tk.TCPConnect) <= 0 {
|
|
|
|
t.Fatal("no TCPConnect?!")
|
|
|
|
}
|
|
|
|
if len(tk.TLSHandshakes) <= 0 {
|
|
|
|
t.Fatal("no TLSHandshakes?!")
|
|
|
|
}
|
|
|
|
if tk.TelegramHTTPBlocking != false {
|
|
|
|
t.Fatal("unexpected TelegramHTTPBlocking")
|
|
|
|
}
|
|
|
|
if tk.TelegramTCPBlocking != false {
|
|
|
|
t.Fatal("unexpected TelegramTCPBlocking")
|
|
|
|
}
|
|
|
|
if tk.TelegramWebFailure != nil {
|
|
|
|
t.Fatal("unexpected TelegramWebFailure")
|
|
|
|
}
|
|
|
|
if tk.TelegramWebStatus != "ok" {
|
|
|
|
t.Fatal("unexpected TelegramWebStatus")
|
|
|
|
}
|
|
|
|
sk, err := measurer.GetSummaryKeys(measurement)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, ok := sk.(telegram.SummaryKeys); !ok {
|
|
|
|
t.Fatal("invalid type for summary keys")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWithNoAccessPointsBlocking(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50:443/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
Failure: nil, // this should be enough to declare success
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramHTTPBlocking == true {
|
|
|
|
t.Fatal("there should be no TelegramHTTPBlocking")
|
|
|
|
}
|
|
|
|
if tk.TelegramTCPBlocking == true {
|
|
|
|
t.Fatal("there should be no TelegramTCPBlocking")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWithNilFailedOperation(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50:443/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramHTTPBlocking == false {
|
|
|
|
t.Fatal("there should be TelegramHTTPBlocking")
|
|
|
|
}
|
|
|
|
if tk.TelegramTCPBlocking == true {
|
|
|
|
t.Fatal("there should be no TelegramTCPBlocking")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWithNonConnectFailedOperation(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
FailedOperation: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.ConnectOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50:443/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
FailedOperation: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.HTTPRoundTripOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramHTTPBlocking == false {
|
|
|
|
t.Fatal("there should be TelegramHTTPBlocking")
|
|
|
|
}
|
|
|
|
if tk.TelegramTCPBlocking == true {
|
|
|
|
t.Fatal("there should be no TelegramTCPBlocking")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWithAllConnectsFailed(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
FailedOperation: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.ConnectOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "POST"},
|
|
|
|
Target: "http://149.154.175.50:443/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
FailedOperation: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.ConnectOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramHTTPBlocking == false {
|
|
|
|
t.Fatal("there should be TelegramHTTPBlocking")
|
|
|
|
}
|
|
|
|
if tk.TelegramTCPBlocking == false {
|
|
|
|
t.Fatal("there should be TelegramTCPBlocking")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWebWithMixedResults(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "GET"},
|
|
|
|
Target: "http://web.telegram.org/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
FailedOperation: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.HTTPRoundTripOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
Failure: (func() *string {
|
2021-09-28 12:42:01 +02:00
|
|
|
s := netxlite.FailureEOFError
|
2021-02-02 12:05:47 +01:00
|
|
|
return &s
|
|
|
|
})(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "GET"},
|
|
|
|
Target: "https://web.telegram.org/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
HTTPResponseBody: `<title>Telegram Web</title>`,
|
|
|
|
HTTPResponseStatus: 200,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramWebStatus != "blocked" {
|
|
|
|
t.Fatal("TelegramWebStatus should be blocked")
|
|
|
|
}
|
2021-09-28 12:42:01 +02:00
|
|
|
if *tk.TelegramWebFailure != netxlite.FailureEOFError {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("invalid TelegramWebFailure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWeConfigureWebChecksToFailOnHTTPError(t *testing.T) {
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
called := &atomicx.Int64{}
|
|
|
|
failOnErrorHTTPS := &atomicx.Int64{}
|
|
|
|
failOnErrorHTTP := &atomicx.Int64{}
|
2021-02-02 12:05:47 +01:00
|
|
|
measurer := telegram.Measurer{
|
|
|
|
Config: telegram.Config{},
|
|
|
|
Getter: func(ctx context.Context, g urlgetter.Getter) (urlgetter.TestKeys, error) {
|
|
|
|
called.Add(1)
|
|
|
|
switch g.Target {
|
|
|
|
case "https://web.telegram.org/":
|
|
|
|
if g.Config.FailOnHTTPError {
|
|
|
|
failOnErrorHTTPS.Add(1)
|
|
|
|
}
|
|
|
|
case "http://web.telegram.org/":
|
|
|
|
if g.Config.FailOnHTTPError {
|
|
|
|
failOnErrorHTTP.Add(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return urlgetter.DefaultMultiGetter(ctx, g)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
sess := &mockable.Session{
|
|
|
|
MockableLogger: log.Log,
|
|
|
|
}
|
|
|
|
measurement := new(model.Measurement)
|
|
|
|
callbacks := model.NewPrinterCallbacks(log.Log)
|
|
|
|
if err := measurer.Run(ctx, sess, measurement, callbacks); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if called.Load() < 1 {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
if failOnErrorHTTPS.Load() != 1 {
|
|
|
|
t.Fatal("not configured fail on error for HTTPS")
|
|
|
|
}
|
|
|
|
if failOnErrorHTTP.Load() != 1 {
|
|
|
|
t.Fatal("not configured fail on error for HTTP")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWithMissingTitle(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "GET"},
|
|
|
|
Target: "http://web.telegram.org/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
HTTPResponseStatus: 200,
|
|
|
|
HTTPResponseBody: "<HTML><title>Telegram Web</title></HTML>",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "GET"},
|
|
|
|
Target: "http://web.telegram.org/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
HTTPResponseStatus: 200,
|
|
|
|
HTTPResponseBody: "<HTML><title>Antani Web</title></HTML>",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramWebStatus != "blocked" {
|
|
|
|
t.Fatal("TelegramWebStatus should be blocked")
|
|
|
|
}
|
|
|
|
if *tk.TelegramWebFailure != "telegram_missing_title_error" {
|
|
|
|
t.Fatal("invalid TelegramWebFailure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateWithAllGood(t *testing.T) {
|
|
|
|
tk := telegram.NewTestKeys()
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "GET"},
|
|
|
|
Target: "http://web.telegram.org/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
HTTPResponseStatus: 200,
|
|
|
|
HTTPResponseBody: "<HTML><title>Telegram Web</title></HTML>",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tk.Update(urlgetter.MultiOutput{
|
|
|
|
Input: urlgetter.MultiInput{
|
|
|
|
Config: urlgetter.Config{Method: "GET"},
|
|
|
|
Target: "http://web.telegram.org/",
|
|
|
|
},
|
|
|
|
TestKeys: urlgetter.TestKeys{
|
|
|
|
HTTPResponseStatus: 200,
|
|
|
|
HTTPResponseBody: "<HTML><title>Telegram Web</title></HTML>",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if tk.TelegramWebStatus != "ok" {
|
|
|
|
t.Fatal("TelegramWebStatus should be ok")
|
|
|
|
}
|
|
|
|
if tk.TelegramWebFailure != nil {
|
|
|
|
t.Fatal("invalid TelegramWebFailure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSummaryKeysInvalidType(t *testing.T) {
|
|
|
|
measurement := new(model.Measurement)
|
|
|
|
m := &telegram.Measurer{}
|
|
|
|
_, err := m.GetSummaryKeys(measurement)
|
|
|
|
if err.Error() != "invalid test keys type" {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSummaryKeysWorksAsIntended(t *testing.T) {
|
|
|
|
failure := io.EOF.Error()
|
|
|
|
tests := []struct {
|
|
|
|
tk telegram.TestKeys
|
|
|
|
isAnomaly bool
|
|
|
|
}{{
|
|
|
|
tk: telegram.TestKeys{},
|
|
|
|
isAnomaly: false,
|
|
|
|
}, {
|
|
|
|
tk: telegram.TestKeys{TelegramTCPBlocking: true},
|
|
|
|
isAnomaly: true,
|
|
|
|
}, {
|
|
|
|
tk: telegram.TestKeys{TelegramHTTPBlocking: true},
|
|
|
|
isAnomaly: true,
|
|
|
|
}, {
|
|
|
|
tk: telegram.TestKeys{TelegramWebFailure: &failure},
|
|
|
|
isAnomaly: true,
|
|
|
|
}}
|
|
|
|
for idx, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
|
|
|
|
m := &telegram.Measurer{}
|
|
|
|
measurement := &model.Measurement{TestKeys: &tt.tk}
|
|
|
|
got, err := m.GetSummaryKeys(measurement)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sk := got.(telegram.SummaryKeys)
|
|
|
|
if sk.IsAnomaly != tt.isAnomaly {
|
|
|
|
t.Fatal("unexpected isAnomaly value")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|