...

Source file src/go.opencensus.io/trace/tracestate/tracestate_test.go

Documentation: go.opencensus.io/trace/tracestate

     1  // Copyright 2018, OpenCensus Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tracestate
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func checkFront(t *testing.T, tracestate *Tracestate, wantKey, testname string) {
    23  	gotKey := tracestate.entries[0].Key
    24  	if gotKey != wantKey {
    25  		t.Errorf("test:%s: first entry in the list: got %q want %q", testname, gotKey, wantKey)
    26  	}
    27  }
    28  
    29  func checkBack(t *testing.T, tracestate *Tracestate, wantKey, testname string) {
    30  	gotKey := tracestate.entries[len(tracestate.entries)-1].Key
    31  	if gotKey != wantKey {
    32  		t.Errorf("test:%s: last entry in the list: got %q want %q", testname, gotKey, wantKey)
    33  	}
    34  }
    35  
    36  func checkSize(t *testing.T, tracestate *Tracestate, wantSize int, testname string) {
    37  	if gotSize := len(tracestate.entries); gotSize != wantSize {
    38  		t.Errorf("test:%s: size of the list: got %q want %q", testname, gotSize, wantSize)
    39  	}
    40  }
    41  
    42  func (ts *Tracestate) get(key string) (string, bool) {
    43  	if ts == nil {
    44  		return "", false
    45  	}
    46  	for _, entry := range ts.entries {
    47  		if entry.Key == key {
    48  			return entry.Value, true
    49  		}
    50  	}
    51  	return "", false
    52  }
    53  
    54  func checkKeyValue(t *testing.T, tracestate *Tracestate, key, wantValue, testname string) {
    55  	wantOk := true
    56  	if wantValue == "" {
    57  		wantOk = false
    58  	}
    59  	gotValue, gotOk := tracestate.get(key)
    60  	if wantOk != gotOk || gotValue != wantValue {
    61  		t.Errorf("test:%s: get value for key=%s failed: got %q want %q", testname, key, gotValue, wantValue)
    62  	}
    63  }
    64  
    65  func checkError(t *testing.T, tracestate *Tracestate, err error, testname, msg string) {
    66  	if err != nil {
    67  		t.Errorf("test:%s: %s: tracestate=%v, error= %v", testname, msg, tracestate, err)
    68  	}
    69  }
    70  
    71  func wantError(t *testing.T, tracestate *Tracestate, err error, testname, msg string) {
    72  	if err == nil {
    73  		t.Errorf("test:%s: %s: tracestate=%v, error=%v", testname, msg, tracestate, err)
    74  	}
    75  }
    76  
    77  func TestCreateWithNullParent(t *testing.T) {
    78  	key1, value1 := "hello", "world"
    79  	testname := "TestCreateWithNullParent"
    80  
    81  	entry := Entry{key1, value1}
    82  	tracestate, err := New(nil, entry)
    83  	checkError(t, tracestate, err, testname, "create failed from null parent")
    84  	checkKeyValue(t, tracestate, key1, value1, testname)
    85  }
    86  
    87  func TestCreateFromParentWithSingleKey(t *testing.T) {
    88  	key1, value1, key2, value2 := "hello", "world", "foo", "bar"
    89  	testname := "TestCreateFromParentWithSingleKey"
    90  
    91  	entry1 := Entry{key1, value1}
    92  	entry2 := Entry{key2, value2}
    93  	parent, _ := New(nil, entry1)
    94  	tracestate, err := New(parent, entry2)
    95  
    96  	checkError(t, tracestate, err, testname, "create failed from parent with single key")
    97  	checkKeyValue(t, tracestate, key2, value2, testname)
    98  	checkFront(t, tracestate, key2, testname)
    99  	checkBack(t, tracestate, key1, testname)
   100  }
   101  
   102  func TestCreateFromParentWithDoubleKeys(t *testing.T) {
   103  	key1, value1, key2, value2, key3, value3 := "hello", "world", "foo", "bar", "bar", "baz"
   104  	testname := "TestCreateFromParentWithDoubleKeys"
   105  
   106  	entry1 := Entry{key1, value1}
   107  	entry2 := Entry{key2, value2}
   108  	entry3 := Entry{key3, value3}
   109  	parent, _ := New(nil, entry2, entry1)
   110  	tracestate, err := New(parent, entry3)
   111  
   112  	checkError(t, tracestate, err, testname, "create failed from parent with double keys")
   113  	checkKeyValue(t, tracestate, key3, value3, testname)
   114  	checkFront(t, tracestate, key3, testname)
   115  	checkBack(t, tracestate, key1, testname)
   116  }
   117  
   118  func TestCreateFromParentWithExistingKey(t *testing.T) {
   119  	key1, value1, key2, value2, key3, value3 := "hello", "world", "foo", "bar", "hello", "baz"
   120  	testname := "TestCreateFromParentWithExistingKey"
   121  
   122  	entry1 := Entry{key1, value1}
   123  	entry2 := Entry{key2, value2}
   124  	entry3 := Entry{key3, value3}
   125  	parent, _ := New(nil, entry2, entry1)
   126  	tracestate, err := New(parent, entry3)
   127  
   128  	checkError(t, tracestate, err, testname, "create failed with an existing key")
   129  	checkKeyValue(t, tracestate, key3, value3, testname)
   130  	checkFront(t, tracestate, key3, testname)
   131  	checkBack(t, tracestate, key2, testname)
   132  	checkSize(t, tracestate, 2, testname)
   133  }
   134  
   135  func TestImplicitImmutableTracestate(t *testing.T) {
   136  	key1, value1, key2, value2, key3, value3 := "hello", "world", "hello", "bar", "foo", "baz"
   137  	testname := "TestImplicitImmutableTracestate"
   138  
   139  	entry1 := Entry{key1, value1}
   140  	entry2 := Entry{key2, value2}
   141  	parent, _ := New(nil, entry1)
   142  	tracestate, err := New(parent, entry2)
   143  
   144  	checkError(t, tracestate, err, testname, "create failed")
   145  	checkKeyValue(t, tracestate, key2, value2, testname)
   146  	checkKeyValue(t, parent, key2, value1, testname)
   147  
   148  	// Get and update entries.
   149  	entries := tracestate.Entries()
   150  	entry := Entry{key3, value3}
   151  	entries = append(entries, entry)
   152  
   153  	// Check Tracestate does not have key3.
   154  	checkKeyValue(t, tracestate, key3, "", testname)
   155  	// Check that we added the key3 in the entries
   156  	tracestate, err = New(nil, entries...)
   157  	checkError(t, tracestate, err, testname, "create failed")
   158  	checkKeyValue(t, tracestate, key3, value3, testname)
   159  }
   160  
   161  func TestKeyWithValidChar(t *testing.T) {
   162  	testname := "TestKeyWithValidChar"
   163  
   164  	arrayRune := []rune("")
   165  	for c := 'a'; c <= 'z'; c++ {
   166  		arrayRune = append(arrayRune, c)
   167  	}
   168  	for c := '0'; c <= '9'; c++ {
   169  		arrayRune = append(arrayRune, c)
   170  	}
   171  	arrayRune = append(arrayRune, '_')
   172  	arrayRune = append(arrayRune, '-')
   173  	arrayRune = append(arrayRune, '*')
   174  	arrayRune = append(arrayRune, '/')
   175  	key := string(arrayRune)
   176  	entry := Entry{key, "world"}
   177  	tracestate, err := New(nil, entry)
   178  
   179  	checkError(t, tracestate, err, testname, "create failed when the key contains all valid characters")
   180  }
   181  
   182  func TestKeyWithInvalidChar(t *testing.T) {
   183  	testname := "TestKeyWithInvalidChar"
   184  
   185  	keys := []string{"1ab", "1ab2", "Abc", " abc", "a=b"}
   186  
   187  	for _, key := range keys {
   188  		entry := Entry{key, "world"}
   189  		tracestate, err := New(nil, entry)
   190  		wantError(t, tracestate, err, testname, fmt.Sprintf(
   191  			"create did not err with invalid key=%q", key))
   192  	}
   193  }
   194  
   195  func TestNilKey(t *testing.T) {
   196  	testname := "TestNilKey"
   197  
   198  	entry := Entry{"", "world"}
   199  	tracestate, err := New(nil, entry)
   200  	wantError(t, tracestate, err, testname, "create did not err when the key is nil (\"\")")
   201  }
   202  
   203  func TestValueWithInvalidChar(t *testing.T) {
   204  	testname := "TestValueWithInvalidChar"
   205  
   206  	keys := []string{"A=B", "A,B", "AB "}
   207  
   208  	for _, value := range keys {
   209  		entry := Entry{"hello", value}
   210  		tracestate, err := New(nil, entry)
   211  		wantError(t, tracestate, err, testname,
   212  			fmt.Sprintf("create did not err when the value is invalid (%q)", value))
   213  	}
   214  }
   215  
   216  func TestNilValue(t *testing.T) {
   217  	testname := "TestNilValue"
   218  
   219  	tracestate, err := New(nil, Entry{"hello", ""})
   220  	wantError(t, tracestate, err, testname, "create did not err when the value is nil (\"\")")
   221  }
   222  
   223  func TestInvalidKeyLen(t *testing.T) {
   224  	testname := "TestInvalidKeyLen"
   225  
   226  	arrayRune := []rune("")
   227  	for i := 0; i <= keyMaxSize+1; i++ {
   228  		arrayRune = append(arrayRune, 'a')
   229  	}
   230  	key := string(arrayRune)
   231  	tracestate, err := New(nil, Entry{key, "world"})
   232  
   233  	wantError(t, tracestate, err, testname,
   234  		fmt.Sprintf("create did not err when the length (%d) of the key is larger than max (%d)",
   235  			len(key), keyMaxSize))
   236  }
   237  
   238  func TestInvalidValueLen(t *testing.T) {
   239  	testname := "TestInvalidValueLen"
   240  
   241  	arrayRune := []rune("")
   242  	for i := 0; i <= valueMaxSize+1; i++ {
   243  		arrayRune = append(arrayRune, 'a')
   244  	}
   245  	value := string(arrayRune)
   246  	tracestate, err := New(nil, Entry{"hello", value})
   247  
   248  	wantError(t, tracestate, err, testname,
   249  		fmt.Sprintf("create did not err when the length (%d) of the value is larger than max (%d)",
   250  			len(value), valueMaxSize))
   251  }
   252  
   253  func TestCreateFromArrayWithOverLimitKVPairs(t *testing.T) {
   254  	testname := "TestCreateFromArrayWithOverLimitKVPairs"
   255  
   256  	entries := []Entry{}
   257  	for i := 0; i <= maxKeyValuePairs; i++ {
   258  		key := fmt.Sprintf("a%db", i)
   259  		entry := Entry{key, "world"}
   260  		entries = append(entries, entry)
   261  	}
   262  	tracestate, err := New(nil, entries...)
   263  	wantError(t, tracestate, err, testname,
   264  		fmt.Sprintf("create did not err when the number (%d) of key-value pairs is larger than max (%d)",
   265  			len(entries), maxKeyValuePairs))
   266  }
   267  
   268  func TestCreateFromEmptyArray(t *testing.T) {
   269  	testname := "TestCreateFromEmptyArray"
   270  
   271  	tracestate, err := New(nil, nil...)
   272  	checkError(t, tracestate, err, testname,
   273  		"failed to create nil tracestate")
   274  }
   275  
   276  func TestCreateFromParentWithOverLimitKVPairs(t *testing.T) {
   277  	testname := "TestCreateFromParentWithOverLimitKVPairs"
   278  
   279  	entries := []Entry{}
   280  	for i := 0; i < maxKeyValuePairs; i++ {
   281  		key := fmt.Sprintf("a%db", i)
   282  		entry := Entry{key, "world"}
   283  		entries = append(entries, entry)
   284  	}
   285  	parent, err := New(nil, entries...)
   286  
   287  	checkError(t, parent, err, testname, fmt.Sprintf("create failed to add %d key-value pair", maxKeyValuePairs))
   288  
   289  	// Add one more to go over the limit
   290  	key := fmt.Sprintf("a%d", maxKeyValuePairs)
   291  	tracestate, err := New(parent, Entry{key, "world"})
   292  	wantError(t, tracestate, err, testname,
   293  		fmt.Sprintf("create did not err when attempted to exceed the key-value pair limit of %d", maxKeyValuePairs))
   294  }
   295  
   296  func TestCreateFromArrayWithDuplicateKeys(t *testing.T) {
   297  	key1, value1, key2, value2, key3, value3 := "hello", "world", "foo", "bar", "hello", "baz"
   298  	testname := "TestCreateFromArrayWithDuplicateKeys"
   299  
   300  	entry1 := Entry{key1, value1}
   301  	entry2 := Entry{key2, value2}
   302  	entry3 := Entry{key3, value3}
   303  	tracestate, err := New(nil, entry1, entry2, entry3)
   304  
   305  	wantError(t, tracestate, err, testname,
   306  		"create did not err when entries contained duplicate keys")
   307  }
   308  
   309  func TestEntriesWithNil(t *testing.T) {
   310  	ts, err := New(nil)
   311  	if err != nil {
   312  		t.Fatal(err)
   313  	}
   314  	if got, want := len(ts.Entries()), 0; got != want {
   315  		t.Errorf("zero value should have no entries, got %v; want %v", got, want)
   316  	}
   317  }
   318  

View as plain text