Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: make bool checks more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Jul 29, 2020
1 parent 220fd42 commit 3881a99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
12 changes: 7 additions & 5 deletions cli/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package shell

import (
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -65,19 +66,20 @@ func GetEnv(envVar string, defaultValue string) string {
return defaultValue
}

// GetEnvBool returns an environment variable as boolean
func GetEnvBool(key string) bool {
// GetEnvBool returns an environment variable as boolean, returning also an error if
// and only if the variable is not present
func GetEnvBool(key string) (bool, error) {
s := os.Getenv(key)
if s == "" {
return false
return false, fmt.Errorf("The %s variable is not set", key)
}

v, err := strconv.ParseBool(s)
if err != nil {
return false
return false, nil
}

return v
return v, nil
}

// GetEnvInteger returns an environment variable as integer, including a default value
Expand Down
46 changes: 46 additions & 0 deletions cli/shell/shell_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package shell

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetEnvBool(t *testing.T) {
type test struct {
key string
value string
}

tests := []test{
{key: "should_be_true", value: "true"},
{key: "should_be_false", value: "false"},
{key: "should_be_empty", value: ""},
{key: "should_be_not_empty", value: "garbage"},
}

for _, test := range tests {
os.Setenv(test.key, test.value)

val, err := GetEnvBool(test.key)

if test.key == "should_be_empty" {
assert.NotNil(t, err)
assert.False(t, val)
} else if test.key == "should_be_not_empty" {
assert.Nil(t, err)
assert.False(t, val)
} else if test.key == "should_be_true" {
assert.Nil(t, err)
assert.True(t, val)
} else if test.key == "should_be_false" {
assert.Nil(t, err)
assert.False(t, val)
}
}
}
4 changes: 2 additions & 2 deletions e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func GetElasticArtifactURL(artifact string, version string, OS string, arch stri
return downloadURL, nil
}

useCISnapshots := shell.GetEnvBool("ELASTIC_AGENT_USE_CI_SNAPSHOTS")
useCISnapshots, _ := shell.GetEnvBool("ELASTIC_AGENT_USE_CI_SNAPSHOTS")
if useCISnapshots {
// We will use the snapshots produced by Beats CI
bucket := "beats-ci-artifacts"
Expand Down Expand Up @@ -115,7 +115,7 @@ func GetElasticArtifactURL(artifact string, version string, OS string, arch stri
return nil
}

err := backoff.Retry(apiStatus, exp)
err = backoff.Retry(apiStatus, exp)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 3881a99

Please sign in to comment.