Skip to content

Commit

Permalink
chore: implement code internal faker s3 (hadenlabs#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
luismayta committed May 8, 2022
1 parent 430a49e commit 8a5fd4f
Show file tree
Hide file tree
Showing 9 changed files with 671 additions and 65 deletions.
42 changes: 42 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/bxcodec/faker/v3 v3.6.0
github.com/caarlos0/env/v6 v6.7.1
github.com/go-playground/validator/v10 v10.9.0
github.com/gruntwork-io/terratest v0.40.7
github.com/joho/godotenv v1.4.0
github.com/lithammer/shortuuid/v3 v3.0.7
github.com/pkg/errors v0.9.1
Expand All @@ -16,16 +17,57 @@ require (
)

require (
cloud.google.com/go v0.83.0 // indirect
cloud.google.com/go/storage v1.10.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.40.56 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.5.9 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.13.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.47.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
546 changes: 545 additions & 1 deletion go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions internal/app/external/faker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Generator() {
_ = fakerTag.AddProvider("userNameFaker", func(v reflect.Value) (interface{}, error) {
return User().Name(), nil
_ = fakerTag.AddProvider("ServerNameFaker", func(v reflect.Value) (interface{}, error) {
return Server().Name(), nil
})
}
19 changes: 19 additions & 0 deletions internal/app/external/faker/s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package faker

import (
"github.com/lithammer/shortuuid/v3"
)

type FakeS3 interface {
Name() string // Name server
}

type fakeS3 struct{}

func Bucket() FakeS3 {
return fakeS3{}
}

func (n fakeS3) Name() string {
return shortuuid.New()
}
12 changes: 12 additions & 0 deletions internal/app/external/faker/s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package faker

import (
"testing"

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

func TestS3FakeBucketName(t *testing.T) {
name := Bucket().Name()
assert.NotEmpty(t, name)
}
36 changes: 36 additions & 0 deletions internal/app/external/faker/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package faker

import (
"crypto/rand"
"fmt"
"math/big"

"github.com/lithammer/shortuuid/v3"

"strings"

"github.com/hadenlabs/terraform-aws-iam-s3-user/internal/errors"
)

type FakeServer interface {
Name() string // Name server
}

type fakeServer struct{}

func Server() FakeServer {
return fakeServer{}
}

var (
names = []string{"optimusprime", "wheeljack", "bumblebee"}
)

func (n fakeServer) Name() string {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(names))))
if err != nil {
panic(errors.New(errors.ErrorUnknown, err.Error()))
}
nameuuid := fmt.Sprintf("%s-%s", names[num.Int64()], shortuuid.New())
return strings.ToLower(nameuuid)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import (
)

func TestFakeUserName(t *testing.T) {
name := User().Name()
name := Server().Name()
namePrefix := strings.Split(name, "-")[0]
assert.Contains(t, names, namePrefix, namePrefix)
}

func TestFakeUserFirstName(t *testing.T) {
assert.Contains(t, firstNames, User().FirstName())
}

func TestFakeUserPath(t *testing.T) {
assert.Contains(t, paths, User().Path())
func TestFakeUserNameLower(t *testing.T) {
name := Server().Name()
assert.Equal(t, strings.ToLower(name), name)
}
55 changes: 0 additions & 55 deletions internal/app/external/faker/user.go

This file was deleted.

11 changes: 11 additions & 0 deletions internal/testutil/structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package testutil

// Server is the kind of error.
type Server string

// Servers Types.
const (
Company Server = "evilcorp"
Environment Server = "testing"
Stage Server = "test"
)

0 comments on commit 8a5fd4f

Please sign in to comment.