github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/helper/identifier_test.go (about) 1 package helper 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/observiq/carbon/entry" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestIdentifier(t *testing.T) { 12 os.Setenv("TEST_METADATA_PLUGIN_ENV", "foo") 13 defer os.Unsetenv("TEST_METADATA_PLUGIN_ENV") 14 15 cases := []struct { 16 name string 17 config IdentifierConfig 18 input *entry.Entry 19 expected *entry.Entry 20 }{ 21 { 22 "AddLabelLiteral", 23 func() IdentifierConfig { 24 cfg := NewIdentifierConfig() 25 cfg.Resource = map[string]ExprStringConfig{ 26 "key1": "value1", 27 } 28 return cfg 29 }(), 30 entry.New(), 31 func() *entry.Entry { 32 e := entry.New() 33 e.Resource = map[string]string{ 34 "key1": "value1", 35 } 36 return e 37 }(), 38 }, 39 { 40 "AddLabelExpr", 41 func() IdentifierConfig { 42 cfg := NewIdentifierConfig() 43 cfg.Resource = map[string]ExprStringConfig{ 44 "key1": `EXPR("start" + "end")`, 45 } 46 return cfg 47 }(), 48 entry.New(), 49 func() *entry.Entry { 50 e := entry.New() 51 e.Resource = map[string]string{ 52 "key1": "startend", 53 } 54 return e 55 }(), 56 }, 57 { 58 "AddLabelEnv", 59 func() IdentifierConfig { 60 cfg := NewIdentifierConfig() 61 cfg.Resource = map[string]ExprStringConfig{ 62 "key1": `EXPR(env("TEST_METADATA_PLUGIN_ENV"))`, 63 } 64 return cfg 65 }(), 66 entry.New(), 67 func() *entry.Entry { 68 e := entry.New() 69 e.Resource = map[string]string{ 70 "key1": "foo", 71 } 72 return e 73 }(), 74 }, 75 } 76 77 for _, tc := range cases { 78 t.Run(tc.name, func(t *testing.T) { 79 identifier, err := tc.config.Build() 80 require.NoError(t, err) 81 82 err = identifier.Identify(tc.input) 83 require.NoError(t, err) 84 require.Equal(t, tc.expected.Resource, tc.input.Resource) 85 }) 86 } 87 }