github.com/GuanceCloud/cliutils@v1.1.21/point/env_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package point 7 8 import ( 9 "os" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestLoadEnvs(t *testing.T) { 16 cases := []struct { 17 name string 18 envs map[string]string 19 expect *Encoder 20 }{ 21 { 22 name: `set-pb-encoding`, 23 envs: map[string]string{EnvDefaultEncoding: "protobuf"}, 24 expect: func() *Encoder { 25 x := GetEncoder() 26 for _, opt := range []EncoderOption{WithEncEncoding(Protobuf)} { 27 opt(x) 28 } 29 return x 30 }(), 31 }, 32 33 { 34 name: `set-lp-encoding`, 35 envs: map[string]string{EnvDefaultEncoding: "lineprotocol"}, 36 expect: func() *Encoder { 37 x := GetEncoder() 38 for _, opt := range []EncoderOption{WithEncEncoding(LineProtocol)} { 39 opt(x) 40 } 41 return x 42 }(), 43 }, 44 45 { 46 name: `set-lp-encoding-2`, 47 envs: map[string]string{EnvDefaultEncoding: "lineproto"}, 48 expect: func() *Encoder { 49 x := GetEncoder() 50 for _, opt := range []EncoderOption{WithEncEncoding(LineProtocol)} { 51 opt(x) 52 } 53 return x 54 }(), 55 }, 56 } 57 58 for _, tc := range cases { 59 t.Run(tc.name, func(t *testing.T) { 60 for k, v := range tc.envs { 61 t.Setenv(k, v) 62 } 63 64 loadEnvs() 65 66 assert.Equal(t, tc.expect.String(), GetEncoder().String()) 67 68 for k := range tc.envs { 69 os.Unsetenv(k) 70 } 71 72 t.Cleanup(func() { 73 // reset them 74 DefaultEncoding = LineProtocol 75 }) 76 }) 77 } 78 }