Skip to content

Commit

Permalink
Test retrieval of configuration variables
Browse files Browse the repository at this point in the history
JIRA: LMCROSSITXSADEPLOY-1456
  • Loading branch information
nictas committed Mar 19, 2019
1 parent f291ed8 commit 03f355c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 29 deletions.
5 changes: 2 additions & 3 deletions commands/base_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"net/http"
"net/http/cookiejar"
"os"
"strings"
"time"
"unicode"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/restclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
Expand Down Expand Up @@ -214,11 +214,10 @@ func (c *BaseCommand) GetUsername() (string, error) {

// GetDeployServiceURL returns the deploy service URL
func (c *BaseCommand) GetDeployServiceURL() (string, error) {
deployServiceURL := os.Getenv(DeployServiceURLEnv)
deployServiceURL := configuration.GetTargetURL()
if deployServiceURL == "" {
return c.deployServiceURLCalculator.ComputeDeployServiceURL()
}
ui.Say(fmt.Sprintf("Attention: You've specified a custom Deploy Service URL (%s) via the environment variable \"%s\". The application listening on that URL may be outdated, contain bugs or unreleased features or may even be modified by a potentially untrused person. Use at your own risk.\n", deployServiceURL, DeployServiceURLEnv))
return deployServiceURL, nil
}

Expand Down
28 changes: 5 additions & 23 deletions commands/file_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/cloudfoundry/cli/cf/terminal"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
Expand All @@ -18,11 +18,6 @@ import (
"golang.org/x/sync/errgroup"
)

const (
defaultChunkSizeInMb = 45
chunkSizeInMbEnv = "CHUNK_SIZE_IN_MB"
)

//FileUploader uploads files for the service with the specified service ID
type FileUploader struct {
files []string
Expand Down Expand Up @@ -79,7 +74,7 @@ func (f *FileUploader) UploadFiles() ([]*models.FileMetadata, ExecutionStatus) {
uploadedFiles := []*models.FileMetadata{}
uploadedFiles = append(uploadedFiles, alreadyUploadedFiles...)
if len(filesToUpload) != 0 {
chunkSizeInMb := getChunkSizeInMb()
chunkSizeInMB := configuration.GetChunkSizeInMB()
ui.Say("Uploading %d files...", len(filesToUpload))

// Iterate over all files to be uploaded
Expand All @@ -93,7 +88,7 @@ func (f *FileUploader) UploadFiles() ([]*models.FileMetadata, ExecutionStatus) {
ui.Say(" " + fullPath)

// Upload the file
uploaded, err := uploadInChunks(fullPath, fileToUpload, chunkSizeInMb, f.mtaClient)
uploaded, err := uploadInChunks(fullPath, fileToUpload, chunkSizeInMB, f.mtaClient)
if err != nil {
ui.Failed("Could not upload file %s: %s", terminal.EntityNameColor(fileToUpload.Name()), err.Error())
return nil, Failure
Expand All @@ -105,9 +100,9 @@ func (f *FileUploader) UploadFiles() ([]*models.FileMetadata, ExecutionStatus) {
return uploadedFiles, Success
}

func uploadInChunks(fullPath string, fileToUpload os.File, chunkSizeInMb uint64, mtaClient mtaclient.MtaClientOperations) ([]*models.FileMetadata, error) {
func uploadInChunks(fullPath string, fileToUpload os.File, chunkSizeInMB uint64, mtaClient mtaclient.MtaClientOperations) ([]*models.FileMetadata, error) {
// Upload the file
fileToUploadParts, err := util.SplitFile(fullPath, chunkSizeInMb)
fileToUploadParts, err := util.SplitFile(fullPath, chunkSizeInMB)
if err != nil {
return nil, fmt.Errorf("Could not process file %q: %v", fullPath, baseclient.NewClientError(err))
}
Expand Down Expand Up @@ -201,16 +196,3 @@ func isFileAlreadyUploaded(newFilePath string, fileInfo os.FileInfo, oldFiles []
}
return false
}

func getChunkSizeInMb() uint64 {
chunkSizeInMb, isSet := os.LookupEnv(chunkSizeInMbEnv)
if isSet {
parsedChunkSizeInMb, err := strconv.ParseUint(chunkSizeInMb, 10, 64)
if err == nil && parsedChunkSizeInMb != 0 {
ui.Say("Attention: You've specified a custom chunk size (%d MB) via the environment variable \"%s\".", parsedChunkSizeInMb, chunkSizeInMbEnv)
return parsedChunkSizeInMb
}
ui.Warn("Attention: You've specified an INVALID custom chunk size (%s) via the environment variable \"%s\". Using default: %d", chunkSizeInMb, chunkSizeInMbEnv, defaultChunkSizeInMb)
}
return defaultChunkSizeInMb
}
34 changes: 34 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package configuration

import (
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
"os"
"strconv"
)

const (
ChunkSizeInMBEnv = "CHUNK_SIZE_IN_MB"
TargetURLEnv = "DEPLOY_SERVICE_URL"
DefaultChunkSizeInMB = uint64(45)
)

func GetChunkSizeInMB() uint64 {
chunkSizeInMb, isSet := os.LookupEnv(ChunkSizeInMBEnv)
if isSet {
parsedChunkSizeInMb, err := strconv.ParseUint(chunkSizeInMb, 10, 64)
if err == nil && parsedChunkSizeInMb != 0 {
ui.Say("Attention: You've specified a custom chunk size (%d MB) via the environment variable \"%s\".", parsedChunkSizeInMb, ChunkSizeInMBEnv)
return parsedChunkSizeInMb
}
ui.Warn("Attention: You've specified an INVALID custom chunk size (%s) via the environment variable \"%s\". Using default: %d", chunkSizeInMb, ChunkSizeInMBEnv, DefaultChunkSizeInMB)
}
return DefaultChunkSizeInMB
}

func GetTargetURL() string {
targetURL := os.Getenv(TargetURLEnv)
if targetURL != "" {
ui.Say("Attention: You've specified a custom Deploy Service URL (%s) via the environment variable \"%s\". The application listening on that URL may be outdated, contain bugs or unreleased features or may even be modified by a potentially untrused person. Use at your own risk.\n", targetURL, TargetURLEnv)
}
return targetURL
}
13 changes: 13 additions & 0 deletions configuration/configuration_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package configuration_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestUtil(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Configuration Suite")
}
76 changes: 76 additions & 0 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package configuration_test

import (
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
"strconv"
)

var _ = Describe("Configuration", func() {

BeforeEach(func() {
ui.DisableTerminalOutput(true)
})

Describe("GetTargetURL", func() {

BeforeEach(func() {
os.Unsetenv(configuration.TargetURLEnv)
})

Context("with a set environment variable", func() {
It("should return its value", func() {
targetURL := "http://my-multiapps-controller.domain.com"
os.Setenv(configuration.TargetURLEnv, targetURL)
Expect(configuration.GetTargetURL()).To(Equal(targetURL))
})
})
Context("without a set environment variable", func() {
It("should return an empty string", func() {
Expect(configuration.GetTargetURL()).To(BeEmpty())
})
})

})

Describe("GetChunkSizeInMB", func() {

BeforeEach(func() {
os.Unsetenv(configuration.TargetURLEnv)
})

Context("with a set environment variable", func() {
Context("containing a positive integer", func() {
It("should return its value", func() {
chunkSizeInMB := uint64(5)
os.Setenv(configuration.ChunkSizeInMBEnv, strconv.Itoa(int(chunkSizeInMB)))
Expect(configuration.GetChunkSizeInMB()).To(Equal(chunkSizeInMB))
})
})
Context("containing zero", func() {
It("should return the default value", func() {
chunkSizeInMB := 0
os.Setenv(configuration.ChunkSizeInMBEnv, strconv.Itoa(chunkSizeInMB))
Expect(configuration.GetChunkSizeInMB()).To(Equal(configuration.DefaultChunkSizeInMB))
})
})
Context("containing a string", func() {
It("should return the default value", func() {
chunkSizeInMB := "abc"
os.Setenv(configuration.ChunkSizeInMBEnv, chunkSizeInMB)
Expect(configuration.GetChunkSizeInMB()).To(Equal(configuration.DefaultChunkSizeInMB))
})
})
})
Context("without a set environment variable", func() {
It("should return the default value", func() {
Expect(configuration.GetChunkSizeInMB()).To(Equal(configuration.DefaultChunkSizeInMB))
})
})

})

})
6 changes: 3 additions & 3 deletions util/file_splitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func generateHash() string {
}

// SplitFile ...
func SplitFile(filePath string, fileChunkSizeInMb uint64) ([]string, error) {
if fileChunkSizeInMb == 0 {
func SplitFile(filePath string, fileChunkSizeInMB uint64) ([]string, error) {
if fileChunkSizeInMB == 0 {
return []string{filePath}, nil
}

Expand All @@ -31,7 +31,7 @@ func SplitFile(filePath string, fileChunkSizeInMb uint64) ([]string, error) {
fileInfo, _ := file.Stat()

var fileSize = uint64(fileInfo.Size())
var fileChunkSize = toBytes(fileChunkSizeInMb)
var fileChunkSize = toBytes(fileChunkSizeInMB)

// calculate total number of parts the file will be chunked into
totalPartsNum := uint64(math.Ceil(float64(fileSize) / float64(fileChunkSize)))
Expand Down

0 comments on commit 03f355c

Please sign in to comment.