...

Source file src/github.com/containerd/ttrpc/metadata_test.go

Documentation: github.com/containerd/ttrpc

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package ttrpc
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  )
    23  
    24  func TestMetadataGet(t *testing.T) {
    25  	metadata := make(MD)
    26  	metadata.Set("foo", "1", "2")
    27  
    28  	if list, ok := metadata.Get("foo"); !ok {
    29  		t.Error("key not found")
    30  	} else if len(list) != 2 {
    31  		t.Errorf("unexpected number of values: %d", len(list))
    32  	} else if list[0] != "1" {
    33  		t.Errorf("invalid metadata value at 0: %s", list[0])
    34  	} else if list[1] != "2" {
    35  		t.Errorf("invalid metadata value at 1: %s", list[1])
    36  	}
    37  }
    38  
    39  func TestMetadataGetInvalidKey(t *testing.T) {
    40  	metadata := make(MD)
    41  	metadata.Set("foo", "1", "2")
    42  
    43  	if _, ok := metadata.Get("invalid"); ok {
    44  		t.Error("found invalid key")
    45  	}
    46  }
    47  
    48  func TestMetadataUnset(t *testing.T) {
    49  	metadata := make(MD)
    50  	metadata.Set("foo", "1", "2")
    51  	metadata.Set("foo")
    52  
    53  	if _, ok := metadata.Get("foo"); ok {
    54  		t.Error("key not deleted")
    55  	}
    56  }
    57  
    58  func TestMetadataReplace(t *testing.T) {
    59  	metadata := make(MD)
    60  	metadata.Set("foo", "1", "2")
    61  	metadata.Set("foo", "3", "4")
    62  
    63  	if list, ok := metadata.Get("foo"); !ok {
    64  		t.Error("key not found")
    65  	} else if len(list) != 2 {
    66  		t.Errorf("unexpected number of values: %d", len(list))
    67  	} else if list[0] != "3" {
    68  		t.Errorf("invalid metadata value at 0: %s", list[0])
    69  	} else if list[1] != "4" {
    70  		t.Errorf("invalid metadata value at 1: %s", list[1])
    71  	}
    72  }
    73  
    74  func TestMetadataAppend(t *testing.T) {
    75  	metadata := make(MD)
    76  	metadata.Set("foo", "1")
    77  	metadata.Append("foo", "2")
    78  	metadata.Append("bar", "3")
    79  
    80  	if list, ok := metadata.Get("foo"); !ok {
    81  		t.Error("key not found")
    82  	} else if len(list) != 2 {
    83  		t.Errorf("unexpected number of values: %d", len(list))
    84  	} else if list[0] != "1" {
    85  		t.Errorf("invalid metadata value at 0: %s", list[0])
    86  	} else if list[1] != "2" {
    87  		t.Errorf("invalid metadata value at 1: %s", list[1])
    88  	}
    89  
    90  	if list, ok := metadata.Get("bar"); !ok {
    91  		t.Error("key not found")
    92  	} else if list[0] != "3" {
    93  		t.Errorf("invalid value: %s", list[0])
    94  	}
    95  }
    96  
    97  func TestMetadataContext(t *testing.T) {
    98  	metadata := make(MD)
    99  	metadata.Set("foo", "bar")
   100  
   101  	ctx := WithMetadata(context.Background(), metadata)
   102  
   103  	if bar, ok := GetMetadataValue(ctx, "foo"); !ok {
   104  		t.Error("metadata not found")
   105  	} else if bar != "bar" {
   106  		t.Errorf("invalid metadata value: %q", bar)
   107  	}
   108  }
   109  

View as plain text