From e6227a95beea94024f41de8abb434b06d0c7ac20 Mon Sep 17 00:00:00 2001 From: Marc Ole Bulling Date: Mon, 17 May 2021 11:04:39 +0200 Subject: [PATCH] Fixed AWS tests, refactoring --- .github/workflows/test-code.yml | 7 ++- build/generateCoverage.sh | 2 +- build/go.mod | 1 + cmd/gokapi/Main.go | 4 +- go.mod | 1 + go.sum | 22 ++++++++- internal/environment/Environment_test.go | 4 ++ internal/storage/FileServing_test.go | 40 +++++++++++++++- internal/storage/cloudstorage/aws/AwsS3.go | 11 ++--- .../storage/cloudstorage/aws/AwsS3_mock.go | 3 +- .../storage/cloudstorage/aws/AwsS3_slim.go | 2 +- .../storage/cloudstorage/aws/AwsS3_test.go | 28 +++++++++-- internal/storage/test/config.json | 1 + internal/storage/test/fileupload.jpg | 1 + .../testconfiguration/TestConfiguration.go | 46 ++++++++++++------- .../TestConfiguration_test.go | 2 +- 16 files changed, 135 insertions(+), 40 deletions(-) create mode 100755 internal/storage/test/config.json create mode 100755 internal/storage/test/fileupload.jpg diff --git a/.github/workflows/test-code.yml b/.github/workflows/test-code.yml index 3494f8a..8951e41 100644 --- a/.github/workflows/test-code.yml +++ b/.github/workflows/test-code.yml @@ -12,8 +12,11 @@ jobs: - uses: actions/setup-go@v2 with: go-version: '^1.16.4' - - run: go test ./... --tags=test,awsmock + - run: go test ./... -parallel 8 --tags=test,awsmock - run: go clean -testcache - - run: go test ./... --tags=test,noaws + - run: go test ./... -parallel 8 --tags=test,noaws - run: go clean -testcache - run: go test ./... --tags=test,noaws,integration + - run: go clean -testcache + - run: GOKAPI_AWS_BUCKET="gokapi" GOKAPI_AWS_REGION="eu-central-1" GOKAPI_AWS_KEY="keyid" GOKAPI_AWS_KEY_SECRET="secret" go test ./... --tags=test,awstest + diff --git a/build/generateCoverage.sh b/build/generateCoverage.sh index 95a8ceb..85ef253 100644 --- a/build/generateCoverage.sh +++ b/build/generateCoverage.sh @@ -1,7 +1,7 @@ #!/bin/sh cd .. go test ./... -parallel 8 --tags=test,integration,noaws -coverprofile=/tmp/coverage1.out -go test ./... -parallel 8 -coverprofile=/tmp/coverage2.out --tags=test,awsmock +GOKAPI_AWS_BUCKET="gokapi" GOKAPI_AWS_REGION="eu-central-1" GOKAPI_AWS_KEY="keyid" GOKAPI_AWS_KEY_SECRET="secret" go test ./... -parallel 8 -coverprofile=/tmp/coverage2.out --tags=test,awstest which gocovmerge > /dev/null if [ $? -eq 0 ]; then diff --git a/build/go.mod b/build/go.mod index 09ff9d0..c984f25 100644 --- a/build/go.mod +++ b/build/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/aws/aws-sdk-go v1.38.36 + github.com/johannesboyne/gofakes3 v0.0.0-20210415062230-4b6b67a85d38 golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d gopkg.in/yaml.v2 v2.2.8 ) diff --git a/cmd/gokapi/Main.go b/cmd/gokapi/Main.go index 0d29efe..4dcf772 100644 --- a/cmd/gokapi/Main.go +++ b/cmd/gokapi/Main.go @@ -6,6 +6,7 @@ Main routine import ( "Gokapi/internal/configuration" + "Gokapi/internal/configuration/cloudconfig" "Gokapi/internal/environment" "Gokapi/internal/helper" "Gokapi/internal/storage" @@ -36,7 +37,8 @@ func main() { resetPassword(passedFlags) createSsl(passedFlags) - if aws.Init() { + cConfig, ok := cloudconfig.Load() + if ok && aws.Init(cConfig.Aws) { fmt.Println("Saving new files to cloud storage") } else { fmt.Println("Saving new files to local storage") diff --git a/go.mod b/go.mod index 09ff9d0..c984f25 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/aws/aws-sdk-go v1.38.36 + github.com/johannesboyne/gofakes3 v0.0.0-20210415062230-4b6b67a85d38 golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index f6bdb47..6543f32 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,37 @@ +github.com/aws/aws-sdk-go v1.17.4/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.38.36 h1:MiqzQY/IOFTX/jmGse7ThafD0eyOC4TrCLv2KY1v+bI= github.com/aws/aws-sdk-go v1.38.36/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/kurin/blazer v0.5.3 h1:SAgYv0TKU0kN/ETfO5ExjNAPyMt2FocO2s/UlCHfjAk= -github.com/kurin/blazer v0.5.3/go.mod h1:4FCXMUWo9DllR2Do4TtBd377ezyAJ51vB5uTBjt0pGU= +github.com/johannesboyne/gofakes3 v0.0.0-20210415062230-4b6b67a85d38 h1:RzxIE+fiv4JCG5pPjTLWdegsdoDCQHZEE+ByYC49Y0Y= +github.com/johannesboyne/gofakes3 v0.0.0-20210415062230-4b6b67a85d38/go.mod h1:Zj9d90chLFOXPNj/m+HfCAFx1s8zSue9HiqC/hbHLS0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8= +github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8= +github.com/shabbyrobe/gocovmerge v0.0.0-20180507124511-f6ea450bfb63 h1:J6qvD6rbmOil46orKqJaRPG+zTpoGlBTUdyv8ki63L0= +github.com/shabbyrobe/gocovmerge v0.0.0-20180507124511-f6ea450bfb63/go.mod h1:n+VKSARF5y/tS9XFSP7vWDfS+GUC5vs/YT7M5XDTUEM= +github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190310074541-c10a0554eabf/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -28,6 +41,11 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190308174544-00c44ba9c14f h1:SUQ6L9W8e5xt2GFO9s+i18JGITAfem+a0AQuFU8Ls74= +golang.org/x/tools v0.0.0-20190308174544-00c44ba9c14f/go.mod h1:25r3+/G6/xytQM8iWZKq3Hn0kr0rgFKPUNVEL/dr3z4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/environment/Environment_test.go b/internal/environment/Environment_test.go index ccb012d..d7929e9 100644 --- a/internal/environment/Environment_test.go +++ b/internal/environment/Environment_test.go @@ -33,6 +33,10 @@ func TestEnvLoad(t *testing.T) { } func TestIsAwsProvided(t *testing.T) { + os.Unsetenv("GOKAPI_AWS_BUCKET") + os.Unsetenv("GOKAPI_AWS_REGION") + os.Unsetenv("GOKAPI_AWS_KEY") + os.Unsetenv("GOKAPI_AWS_KEY_SECRET") env := New() test.IsEqualBool(t, env.IsAwsProvided(), false) os.Setenv("GOKAPI_AWS_BUCKET", "test") diff --git a/internal/storage/FileServing_test.go b/internal/storage/FileServing_test.go index 5bc454d..1436393 100644 --- a/internal/storage/FileServing_test.go +++ b/internal/storage/FileServing_test.go @@ -2,6 +2,7 @@ package storage import ( "Gokapi/internal/configuration" + "Gokapi/internal/configuration/cloudconfig" "Gokapi/internal/models" "Gokapi/internal/storage/cloudstorage/aws" "Gokapi/internal/test" @@ -19,8 +20,15 @@ import ( func TestMain(m *testing.M) { testconfiguration.Create(true) configuration.Load() + var testserver *httptest.Server + if testconfiguration.UseMockS3Server() { + testserver = testconfiguration.StartS3TestServer() + } exitVal := m.Run() testconfiguration.Delete() + if testserver != nil { + testserver.Close() + } os.Exit(exitVal) } @@ -133,6 +141,10 @@ func TestNewFile(t *testing.T) { if aws.IsIncludedInBuild { testconfiguration.EnableS3() + config, ok := cloudconfig.Load() + test.IsEqualBool(t, ok, true) + ok = aws.Init(config.Aws) + test.IsEqualBool(t, ok, true) file, err = NewFile(bytes.NewReader(content), &header, request) test.IsNil(t, err) test.IsEqualString(t, file.Name, "bigfile") @@ -160,12 +172,20 @@ func TestServeFile(t *testing.T) { if aws.IsIncludedInBuild { testconfiguration.EnableS3() + config, ok := cloudconfig.Load() + test.IsEqualBool(t, ok, true) + ok = aws.Init(config.Aws) + test.IsEqualBool(t, ok, true) r = httptest.NewRequest("GET", "/upload", nil) w = httptest.NewRecorder() file, result = GetFile("awsTest1234567890123") test.IsEqualBool(t, result, true) ServeFile(file, w, r, false) - test.ResponseBodyContains(t, w, "https://redirect.url") + if aws.IsMockApi { + test.ResponseBodyContains(t, w, "https://redirect.url") + } else { + test.ResponseBodyContains(t, w, "