Skip to content

Commit

Permalink
flag test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tulsishah committed Sep 28, 2023
1 parent a52285f commit 13b649e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
7 changes: 7 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ func newApp() (app *cli.App) {
Usage: "File chunk size to read from GCS in one call. Need to specify the value in MB. ChunkSize less than 1MB is not supported",
},

cli.BoolFlag{
Name: "disable-auth",
Usage: "Disable authorization",
},

/////////////////////////
// Tuning
/////////////////////////
Expand Down Expand Up @@ -378,6 +383,7 @@ type flagStorage struct {
EgressBandwidthLimitBytesPerSecond float64
OpRateLimitHz float64
SequentialReadSizeMb int32
DisableAuth bool

// Tuning
MaxRetrySleep time.Duration
Expand Down Expand Up @@ -488,6 +494,7 @@ func populateFlags(c *cli.Context) (flags *flagStorage, err error) {
EgressBandwidthLimitBytesPerSecond: c.Float64("limit-bytes-per-sec"),
OpRateLimitHz: c.Float64("limit-ops-per-sec"),
SequentialReadSizeMb: int32(c.Int("sequential-read-size-mb")),
DisableAuth: c.Bool("disable-auth"),

// Tuning,
MaxRetrySleep: c.Duration("max-retry-sleep"),
Expand Down
5 changes: 5 additions & 0 deletions flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (t *FlagsTest) Defaults() {
ExpectEq(-1, f.OpRateLimitHz)
ExpectTrue(f.ReuseTokenFromUrl)
ExpectEq(nil, f.CustomEndpoint)
ExpectFalse(f.DisableAuth)

// Tuning
ExpectEq(4096, f.StatCacheCapacity)
Expand Down Expand Up @@ -111,6 +112,7 @@ func (t *FlagsTest) Bools() {
"debug_invariants",
"enable-nonexistent-type-cache",
"experimental-enable-json-read",
"disable-auth",
}

var args []string
Expand All @@ -132,6 +134,7 @@ func (t *FlagsTest) Bools() {
ExpectTrue(f.DebugInvariants)
ExpectTrue(f.EnableNonexistentTypeCache)
ExpectTrue(f.ExperimentalEnableJsonRead)
ExpectTrue(f.DisableAuth)

// --foo=false form
args = nil
Expand All @@ -148,6 +151,7 @@ func (t *FlagsTest) Bools() {
ExpectFalse(f.DebugHTTP)
ExpectFalse(f.DebugInvariants)
ExpectFalse(f.EnableNonexistentTypeCache)
ExpectFalse(f.DisableAuth)

// --foo=true form
args = nil
Expand All @@ -164,6 +168,7 @@ func (t *FlagsTest) Bools() {
ExpectTrue(f.DebugHTTP)
ExpectTrue(f.DebugInvariants)
ExpectTrue(f.EnableNonexistentTypeCache)
ExpectTrue(f.DisableAuth)
}

func (t *FlagsTest) DecimalNumbers() {
Expand Down
7 changes: 4 additions & 3 deletions internal/storage/storageutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type StorageClientConfig struct {
TokenUrl string
ReuseTokenFromUrl bool
ExperimentalEnableJsonRead bool
DisableAuth bool
}

func CreateHttpClient(storageClientConfig *StorageClientConfig) (httpClient *http.Client, err error) {
Expand Down Expand Up @@ -91,9 +92,9 @@ func CreateHttpClient(storageClientConfig *StorageClientConfig) (httpClient *htt
// is nil, it creates the token-source from the provided key-file or using ADC search
// order (https://cloud.google.com/docs/authentication/application-default-credentials#order).
func createTokenSource(storageClientConfig *StorageClientConfig) (tokenSrc oauth2.TokenSource, err error) {
if storageClientConfig.CustomEndpoint == nil {
// Create token src only if disable-auth flag is false.
if !storageClientConfig.DisableAuth {
return auth.GetTokenSource(context.Background(), storageClientConfig.KeyFile, storageClientConfig.TokenUrl, storageClientConfig.ReuseTokenFromUrl)
} else {
return oauth2.StaticTokenSource(&oauth2.Token{}), nil
}
return nil, nil
}
34 changes: 32 additions & 2 deletions internal/storage/storageutil/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,49 @@ type clientTest struct {

func init() { RegisterTestSuite(&clientTest{}) }

func (t *clientTest) TestCreateTokenSrcWithCustomEndpoint() {
func (t *clientTest) TestCreateTokenSrcWithCustomEndpointWhenDisableAuthIsTrue() {
url, err := url.Parse(CustomEndpoint)
AssertEq(nil, err)
sc := GetDefaultStorageClientConfig()
sc.CustomEndpoint = url
sc.DisableAuth = true

tokenSrc, err := createTokenSource(&sc)

ExpectEq(nil, err)
ExpectNe(nil, &tokenSrc)
}

func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNil() {
func (t *clientTest) TestCreateTokenSrcWithCustomEndpointWhenDisableAuthIsFalse() {
url, err := url.Parse(CustomEndpoint)
AssertEq(nil, err)
sc := GetDefaultStorageClientConfig()
sc.CustomEndpoint = url
sc.DisableAuth = false

// It will try to create the actual auth token and fail since key-file doesn't exist.
tokenSrc, err := createTokenSource(&sc)

ExpectNe(nil, err)
ExpectThat(err, oglematchers.Error(oglematchers.HasSubstr("no such file or directory")))
ExpectNe(nil, &tokenSrc)
}

func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNilAndDisableAuthIsTrue() {
sc := GetDefaultStorageClientConfig()
sc.CustomEndpoint = nil
sc.DisableAuth = true

tokenSrc, err := createTokenSource(&sc)

ExpectEq(nil, err)
ExpectEq(nil, tokenSrc)
}

func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNilAndDisableAuthIsFalse() {
sc := GetDefaultStorageClientConfig()
sc.CustomEndpoint = nil
sc.DisableAuth = false

// It will try to create the actual auth token and fail since key-file doesn't exist.
tokenSrc, err := createTokenSource(&sc)
Expand All @@ -55,6 +83,7 @@ func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNil() {

func (t *clientTest) TestCreateHttpClientWithHttp1() {
sc := GetDefaultStorageClientConfig() // By default http1 enabled
sc.DisableAuth = true

// Act: this method add tokenSource and clientOptions.
httpClient, err := CreateHttpClient(&sc)
Expand All @@ -67,6 +96,7 @@ func (t *clientTest) TestCreateHttpClientWithHttp1() {

func (t *clientTest) TestCreateHttpClientWithHttp2() {
sc := GetDefaultStorageClientConfig()
sc.DisableAuth = true

// Act: this method add tokenSource and clientOptions.
httpClient, err := CreateHttpClient(&sc)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func createStorageHandle(flags *flagStorage) (storageHandle storage.StorageHandl
TokenUrl: flags.TokenUrl,
ReuseTokenFromUrl: flags.ReuseTokenFromUrl,
ExperimentalEnableJsonRead: flags.ExperimentalEnableJsonRead,
DisableAuth: flags.DisableAuth,
}

storageHandle, err = storage.NewStorageHandle(context.Background(), storageClientConfig)
Expand Down

0 comments on commit 13b649e

Please sign in to comment.