func TestOutputService4ProtocolTestListsCase2(t *testing.T) { sess := session.New() svc := NewOutputService4ProtocolTest(sess, &aws.Config{Endpoint: aws.String("https://test")}) buf := bytes.NewReader([]byte("{\"ListMember\": [\"a\", null], \"ListMemberMap\": [{}, null, null, {}], \"ListMemberStruct\": [{}, null, null, {}]}")) req, out := svc.OutputService4TestCaseOperation2Request(nil) req.HTTPResponse = &http.Response{StatusCode: 200, Body: ioutil.NopCloser(buf), Header: http.Header{}} // set headers // unmarshal response jsonrpc.UnmarshalMeta(req) jsonrpc.Unmarshal(req) assert.NoError(t, req.Error) // assert response assert.NotNil(t, out) // ensure out variable is used assert.Equal(t, "a", *out.ListMember[0]) assert.Nil(t, out.ListMember[1]) assert.Nil(t, out.ListMemberMap[1]) assert.Nil(t, out.ListMemberMap[2]) assert.Nil(t, out.ListMemberStruct[1]) assert.Nil(t, out.ListMemberStruct[2]) }
func TestCanSupportStructWithNestedPointers(t *testing.T) { assert := assert.New(t) data := struct{ A *struct{ B int } }{} result, err := Search("A.B", data) assert.Nil(err) assert.Nil(result) }
func TestValidPrecompiledExpressionSearches(t *testing.T) { assert := assert.New(t) data := make(map[string]interface{}) data["foo"] = "bar" precompiled, err := Compile("foo") assert.Nil(err) result, err := precompiled.Search(data) assert.Nil(err) assert.Equal("bar", result) }
// Use DynamoDB methods for simplicity func TestPagination(t *testing.T) { db := dynamodb.New(unit.Session) tokens, pages, numPages, gotToEnd := []string{}, []string{}, 0, false reqNum := 0 resps := []*dynamodb.ListTablesOutput{ {TableNames: []*string{aws.String("Table1"), aws.String("Table2")}, LastEvaluatedTableName: aws.String("Table2")}, {TableNames: []*string{aws.String("Table3"), aws.String("Table4")}, LastEvaluatedTableName: aws.String("Table4")}, {TableNames: []*string{aws.String("Table5")}}, } db.Handlers.Send.Clear() // mock sending db.Handlers.Unmarshal.Clear() db.Handlers.UnmarshalMeta.Clear() db.Handlers.ValidateResponse.Clear() db.Handlers.Build.PushBack(func(r *request.Request) { in := r.Params.(*dynamodb.ListTablesInput) if in == nil { tokens = append(tokens, "") } else if in.ExclusiveStartTableName != nil { tokens = append(tokens, *in.ExclusiveStartTableName) } }) db.Handlers.Unmarshal.PushBack(func(r *request.Request) { r.Data = resps[reqNum] reqNum++ }) params := &dynamodb.ListTablesInput{Limit: aws.Int64(2)} err := db.ListTablesPages(params, func(p *dynamodb.ListTablesOutput, last bool) bool { numPages++ for _, t := range p.TableNames { pages = append(pages, *t) } if last { if gotToEnd { assert.Fail(t, "last=true happened twice") } gotToEnd = true } return true }) assert.Equal(t, []string{"Table2", "Table4"}, tokens) assert.Equal(t, []string{"Table1", "Table2", "Table3", "Table4", "Table5"}, pages) assert.Equal(t, 3, numPages) assert.True(t, gotToEnd) assert.Nil(t, err) assert.Nil(t, params.ExclusiveStartTableName) }
func TestCanSupportUserDefinedStructsRef(t *testing.T) { assert := assert.New(t) s := scalars{Foo: "one", Bar: "bar"} result, err := Search("Foo", &s) assert.Nil(err) assert.Equal("one", result) }
func TestCanSupportStructWithFilterProjection(t *testing.T) { assert := assert.New(t) data := sliceType{A: "foo", B: []scalars{scalars{"f1", "b1"}, scalars{"correct", "b2"}}} result, err := Search("B[? `true` ].Foo", data) assert.Nil(err) assert.Equal([]interface{}{"f1", "correct"}, result) }
func TestCanSupportStructWithSlice(t *testing.T) { assert := assert.New(t) data := sliceType{A: "foo", B: []scalars{scalars{"f1", "b1"}, scalars{"correct", "b2"}}} result, err := Search("B[-1].Foo", data) assert.Nil(err) assert.Equal("correct", result) }
func TestEC2RoleProviderExpiryWindowIsExpired(t *testing.T) { server := initTestServer("2014-12-16T01:51:37Z", false) defer server.Close() p := &ec2rolecreds.EC2RoleProvider{ Client: ec2metadata.New(session.New(), &aws.Config{Endpoint: aws.String(server.URL + "/latest")}), ExpiryWindow: time.Hour * 1, } p.CurrentTime = func() time.Time { return time.Date(2014, 12, 15, 0, 51, 37, 0, time.UTC) } assert.True(t, p.IsExpired(), "Expect creds to be expired before retrieve.") _, err := p.Retrieve() assert.Nil(t, err, "Expect no error, %v", err) assert.False(t, p.IsExpired(), "Expect creds to not be expired after retrieve.") p.CurrentTime = func() time.Time { return time.Date(2014, 12, 16, 0, 55, 37, 0, time.UTC) } assert.True(t, p.IsExpired(), "Expect creds to be expired.") }
func TestCanSupportStructWithOrExpressions(t *testing.T) { assert := assert.New(t) data := sliceType{A: "foo", C: nil} result, err := Search("C || A", data) assert.Nil(err) assert.Equal("foo", result) }
func TestCanSupportEmptyInterface(t *testing.T) { assert := assert.New(t) data := make(map[string]interface{}) data["foo"] = "bar" result, err := Search("foo", data) assert.Nil(err) assert.Equal("bar", result) }
func TestCanSupportFlattenNestedEmptySlice(t *testing.T) { assert := assert.New(t) data := nestedSlice{A: []sliceType{ {}, {B: []scalars{{Foo: "a"}}}, }} result, err := Search("A[].B[].Foo", data) assert.Nil(err) assert.Equal([]interface{}{"a"}, result) }
func TestCanSupportProjectionsWithStructs(t *testing.T) { assert := assert.New(t) data := nestedSlice{A: []sliceType{ {A: "first"}, {A: "second"}, {A: "third"}, }} result, err := Search("A[*].A", data) assert.Nil(err) assert.Equal([]interface{}{"first", "second", "third"}, result) }
// Use S3 for simplicity func TestPaginationTruncation(t *testing.T) { client := s3.New(unit.Session) reqNum := 0 resps := []*s3.ListObjectsOutput{ {IsTruncated: aws.Bool(true), Contents: []*s3.Object{{Key: aws.String("Key1")}}}, {IsTruncated: aws.Bool(true), Contents: []*s3.Object{{Key: aws.String("Key2")}}}, {IsTruncated: aws.Bool(false), Contents: []*s3.Object{{Key: aws.String("Key3")}}}, {IsTruncated: aws.Bool(true), Contents: []*s3.Object{{Key: aws.String("Key4")}}}, } client.Handlers.Send.Clear() // mock sending client.Handlers.Unmarshal.Clear() client.Handlers.UnmarshalMeta.Clear() client.Handlers.ValidateResponse.Clear() client.Handlers.Unmarshal.PushBack(func(r *request.Request) { r.Data = resps[reqNum] reqNum++ }) params := &s3.ListObjectsInput{Bucket: aws.String("bucket")} results := []string{} err := client.ListObjectsPages(params, func(p *s3.ListObjectsOutput, last bool) bool { results = append(results, *p.Contents[0].Key) return true }) assert.Equal(t, []string{"Key1", "Key2", "Key3"}, results) assert.Nil(t, err) // Try again without truncation token at all reqNum = 0 resps[1].IsTruncated = nil resps[2].IsTruncated = aws.Bool(true) results = []string{} err = client.ListObjectsPages(params, func(p *s3.ListObjectsOutput, last bool) bool { results = append(results, *p.Contents[0].Key) return true }) assert.Equal(t, []string{"Key1", "Key2"}, results) assert.Nil(t, err) }
func TestWillAutomaticallyCapitalizeFieldNames(t *testing.T) { assert := assert.New(t) s := scalars{Foo: "one", Bar: "bar"} // Note that there's a lower cased "foo" instead of "Foo", // but it should still correspond to the Foo field in the // scalars struct result, err := Search("foo", &s) assert.Nil(err) assert.Equal("one", result) }
func TestCopyDifferentStructs(t *testing.T) { type SrcFoo struct { A int B []*string C map[string]*int SrcUnique string SameNameDiffType int unexportedPtr *int ExportedPtr *int } type DstFoo struct { A int B []*string C map[string]*int DstUnique int SameNameDiffType string unexportedPtr *int ExportedPtr *int } // Create the initial value str1 := "hello" str2 := "bye bye" int1 := 1 int2 := 2 f1 := &SrcFoo{ A: 1, B: []*string{&str1, &str2}, C: map[string]*int{ "A": &int1, "B": &int2, }, SrcUnique: "unique", SameNameDiffType: 1, unexportedPtr: &int1, ExportedPtr: &int2, } // Do the copy var f2 DstFoo awsutil.Copy(&f2, f1) // Values are equal assert.Equal(t, f2.A, f1.A) assert.Equal(t, f2.B, f1.B) assert.Equal(t, f2.C, f1.C) assert.Equal(t, "unique", f1.SrcUnique) assert.Equal(t, 1, f1.SameNameDiffType) assert.Equal(t, 0, f2.DstUnique) assert.Equal(t, "", f2.SameNameDiffType) assert.Equal(t, int1, *f1.unexportedPtr) assert.Nil(t, f2.unexportedPtr) assert.Equal(t, int2, *f1.ExportedPtr) assert.Equal(t, int2, *f2.ExportedPtr) }
func TestSharedCredentialsProviderColonInCredFile(t *testing.T) { os.Clearenv() p := SharedCredentialsProvider{Filename: "example.ini", Profile: "with_colon"} creds, err := p.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match") assert.Empty(t, creds.SessionToken, "Expect no token") }
func TestSharedCredentialsProviderWithAWS_SHARED_CREDENTIALS_FILE(t *testing.T) { os.Clearenv() os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "example.ini") p := SharedCredentialsProvider{} creds, err := p.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match") assert.Equal(t, "token", creds.SessionToken, "Expect session token to match") }
func TestSharedCredentialsProviderIsExpired(t *testing.T) { os.Clearenv() p := SharedCredentialsProvider{Filename: "example.ini", Profile: ""} assert.True(t, p.IsExpired(), "Expect creds to be expired before retrieve") _, err := p.Retrieve() assert.Nil(t, err, "Expect no error") assert.False(t, p.IsExpired(), "Expect creds to not be expired after retrieve") }
func TestSlicePositiveStep(t *testing.T) { assert := assert.New(t) input := make([]interface{}, 5) input[0] = 0 input[1] = 1 input[2] = 2 input[3] = 3 input[4] = 4 result, err := slice(input, []sliceParam{sliceParam{0, true}, sliceParam{3, true}, sliceParam{1, true}}) assert.Nil(err) assert.Equal(input[:3], result) }
func TestSharedCredentialsProviderWithAWS_PROFILE(t *testing.T) { os.Clearenv() os.Setenv("AWS_PROFILE", "no_token") p := SharedCredentialsProvider{Filename: "example.ini", Profile: ""} creds, err := p.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match") assert.Empty(t, creds.SessionToken, "Expect no token") }
func TestEnvProviderAlternateNames(t *testing.T) { os.Clearenv() os.Setenv("AWS_ACCESS_KEY", "access") os.Setenv("AWS_SECRET_KEY", "secret") e := EnvProvider{} creds, err := e.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "access", creds.AccessKeyID, "Expected access key ID") assert.Equal(t, "secret", creds.SecretAccessKey, "Expected secret access key") assert.Empty(t, creds.SessionToken, "Expected no token") }
func TestAssumeRoleProvider(t *testing.T) { stub := &stubSTS{} p := &AssumeRoleProvider{ Client: stub, RoleARN: "roleARN", } creds, err := p.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "roleARN", creds.AccessKeyID, "Expect access key ID to be reflected role ARN") assert.Equal(t, "assumedSecretAccessKey", creds.SecretAccessKey, "Expect secret access key to match") assert.Equal(t, "assumedSessionToken", creds.SessionToken, "Expect session token to match") }
func TestEnvProviderRetrieve(t *testing.T) { os.Clearenv() os.Setenv("AWS_ACCESS_KEY_ID", "access") os.Setenv("AWS_SECRET_ACCESS_KEY", "secret") os.Setenv("AWS_SESSION_TOKEN", "token") e := EnvProvider{} creds, err := e.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "access", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match") assert.Equal(t, "token", creds.SessionToken, "Expect session token to match") }
func TestStaticProviderGet(t *testing.T) { s := StaticProvider{ Value: Value{ AccessKeyID: "AKID", SecretAccessKey: "SECRET", SessionToken: "", }, } creds, err := s.Retrieve() assert.Nil(t, err, "Expect no error") assert.Equal(t, "AKID", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "SECRET", creds.SecretAccessKey, "Expect secret access key to match") assert.Empty(t, creds.SessionToken, "Expect no session token") }
func TestEC2RoleProvider(t *testing.T) { server := initTestServer("2014-12-16T01:51:37Z", false) defer server.Close() p := &ec2rolecreds.EC2RoleProvider{ Client: ec2metadata.New(session.New(), &aws.Config{Endpoint: aws.String(server.URL + "/latest")}), } creds, err := p.Retrieve() assert.Nil(t, err, "Expect no error, %v", err) assert.Equal(t, "accessKey", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "secret", creds.SecretAccessKey, "Expect secret access key to match") assert.Equal(t, "token", creds.SessionToken, "Expect session token to match") }
func TestEnvProviderIsExpired(t *testing.T) { os.Clearenv() os.Setenv("AWS_ACCESS_KEY_ID", "access") os.Setenv("AWS_SECRET_ACCESS_KEY", "secret") os.Setenv("AWS_SESSION_TOKEN", "token") e := EnvProvider{} assert.True(t, e.IsExpired(), "Expect creds to be expired before retrieve.") _, err := e.Retrieve() assert.Nil(t, err, "Expect no error") assert.False(t, e.IsExpired(), "Expect creds to not be expired after retrieve.") }
func TestCredentialsGet(t *testing.T) { c := NewCredentials(&stubProvider{ creds: Value{ AccessKeyID: "AKID", SecretAccessKey: "SECRET", SessionToken: "", }, expired: true, }) creds, err := c.Get() assert.Nil(t, err, "Expected no error") assert.Equal(t, "AKID", creds.AccessKeyID, "Expect access key ID to match") assert.Equal(t, "SECRET", creds.SecretAccessKey, "Expect secret access key to match") assert.Empty(t, creds.SessionToken, "Expect session token to be empty") }
func TestClientNotOverrideDefaultHTTPClientTimeout(t *testing.T) { origClient := *http.DefaultClient http.DefaultClient.Transport = &http.Transport{} defer func() { http.DefaultClient = &origClient }() svc := ec2metadata.New(session.New()) assert.Equal(t, http.DefaultClient, svc.Config.HTTPClient) tr, ok := svc.Config.HTTPClient.Transport.(*http.Transport) assert.True(t, ok) assert.NotNil(t, tr) assert.Nil(t, tr.Dial) }
func TestCompliance(t *testing.T) { assert := assert.New(t) var complianceFiles []string err := filepath.Walk("compliance", func(path string, _ os.FileInfo, _ error) error { //if strings.HasSuffix(path, ".json") { if allowed(path) { complianceFiles = append(complianceFiles, path) } return nil }) if assert.Nil(err) { for _, filename := range complianceFiles { runComplianceTest(assert, filename) } } }
func TestCanLexTokens(t *testing.T) { assert := assert.New(t) lexer := NewLexer() for _, tt := range lexingTests { tokens, err := lexer.tokenize(tt.expression) if assert.Nil(err) { errMsg := fmt.Sprintf("Mismatch expected number of tokens: (expected: %s, actual: %s)", tt.expected, tokens) tt.expected = append(tt.expected, token{tEOF, "", len(tt.expression), 0}) if assert.Equal(len(tt.expected), len(tokens), errMsg) { for i, token := range tokens { expected := tt.expected[i] assert.Equal(expected, token, "Token not equal") } } } } }