Skip to content

Commit

Permalink
[tracegen] Add unit test for OTLP attributes flag (open-telemetry#11832)
Browse files Browse the repository at this point in the history
* [tracegen] Add unit test for OTLP attributes flag

* Address linter
  • Loading branch information
mx-psi committed Jul 11, 2022
1 parent 49fd4f7 commit 3bc0e6b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tracegen/internal/tracegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
"golang.org/x/time/rate"
)

var (
errFormatOTLPAttributes = fmt.Errorf("value should be of the format key=\"value\"")
errDoubleQuotesOTLPAttributes = fmt.Errorf("value should be a string wrapped in double quotes")
)

// Config describes the test scenario.
type Config struct {
WorkerCount int
Expand Down Expand Up @@ -55,11 +60,11 @@ func (v *KeyValue) String() string {
func (v *KeyValue) Set(s string) error {
kv := strings.SplitN(s, "=", 2)
if len(kv) != 2 {
return fmt.Errorf("value should be of the format key=\"value\"")
return errFormatOTLPAttributes
}
val := kv[1]
if len(val) < 2 || !strings.HasPrefix(val, "\"") || !strings.HasSuffix(val, "\"") {
return fmt.Errorf("value should be a string wrapped in double quotes")
return errDoubleQuotesOTLPAttributes
}

(*v)[kv[0]] = val[1 : len(val)-1]
Expand Down
63 changes: 63 additions & 0 deletions tracegen/internal/tracegen/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright The OpenTelemetry Authors
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tracegen

import (
"testing"

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

func TestKeyValueSet(t *testing.T) {
tests := []struct {
flag string
expected KeyValue
err error
}{
{
flag: "key=\"value\"",
expected: KeyValue(map[string]string{"key": "value"}),
},
{
flag: "key=\"\"",
expected: KeyValue(map[string]string{"key": ""}),
},
{
flag: "key=\"",
err: errDoubleQuotesOTLPAttributes,
},
{
flag: "key=value",
err: errDoubleQuotesOTLPAttributes,
},
{
flag: "key",
err: errFormatOTLPAttributes,
},
}

for _, tt := range tests {
t.Run(tt.flag, func(t *testing.T) {
kv := KeyValue(make(map[string]string))
err := kv.Set(tt.flag)
if err != nil || tt.err != nil {
assert.Equal(t, err, tt.err)
} else {
assert.Equal(t, tt.expected, kv)
}
})
}
}

0 comments on commit 3bc0e6b

Please sign in to comment.