github.com/kristofferahl/go-centry@v1.5.0/internal/pkg/config/annotations_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/franela/goblin"
     7  )
     8  
     9  func TestAnnotations(t *testing.T) {
    10  	g := Goblin(t)
    11  
    12  	g.Describe("ParseAnnotation", func() {
    13  		g.Describe("InvalidAnnotations", func() {
    14  			g.It("returns nil when line is not an annotation", func() {
    15  				annotation, err := ParseAnnotation("foo bar baz")
    16  				g.Assert(annotation == nil).IsTrue("expected no annotation")
    17  				g.Assert(err == nil).IsTrue("expected no error")
    18  			})
    19  
    20  			g.It("returns nil when annotation is missing centry prefix", func() {
    21  				annotation, err := ParseAnnotation("foo/bar=baz")
    22  				g.Assert(annotation == nil).IsTrue("expected no annotation")
    23  				g.Assert(err == nil).IsTrue("expected no error")
    24  			})
    25  
    26  			g.It("returns nil when annotation is missing slash", func() {
    27  				annotation, err := ParseAnnotation("centry_bar=baz")
    28  				g.Assert(annotation == nil).IsTrue("expected no annotation")
    29  				g.Assert(err == nil).IsTrue("expected no error")
    30  			})
    31  
    32  			g.It("returns nil when annotation is missing equals sign", func() {
    33  				annotation, err := ParseAnnotation("centry/bar_baz")
    34  				g.Assert(annotation == nil).IsTrue("expected no annotation")
    35  				g.Assert(err == nil).IsTrue("expected no error")
    36  			})
    37  
    38  			g.It("returns error when annotation only has equals sign before slash", func() {
    39  				annotation, err := ParseAnnotation("centry=bar/bar")
    40  				g.Assert(annotation == nil).IsTrue("expected no annotation")
    41  				g.Assert(err != nil).IsTrue("expected no error")
    42  			})
    43  		})
    44  
    45  		g.Describe("ValidAnnotations", func() {
    46  			g.It("returns annotation", func() {
    47  				annotation, err := ParseAnnotation("centry/foo=bar")
    48  				g.Assert(annotation != nil).IsTrue("expected annotation")
    49  				g.Assert(err == nil).IsTrue("expected no error")
    50  				g.Assert(annotation.Namespace).Equal("centry")
    51  				g.Assert(annotation.Key).Equal("foo")
    52  				g.Assert(annotation.Value).Equal("bar")
    53  			})
    54  
    55  			g.It("returns annotation when it contains multiple equal signs", func() {
    56  				annotation, err := ParseAnnotation("centry/foo=bar=baz")
    57  				g.Assert(annotation != nil).IsTrue("expected annotation")
    58  				g.Assert(err == nil).IsTrue("expected no error")
    59  				g.Assert(annotation.Namespace).Equal("centry")
    60  				g.Assert(annotation.Key).Equal("foo")
    61  				g.Assert(annotation.Value).Equal("bar=baz")
    62  			})
    63  
    64  			g.It("returns annotation when it contains multiple slashes", func() {
    65  				annotation, err := ParseAnnotation("centry/foo=bar/baz")
    66  				g.Assert(annotation != nil).IsTrue("expected annotation")
    67  				g.Assert(err == nil).IsTrue("expected no error")
    68  				g.Assert(annotation.Namespace).Equal("centry")
    69  				g.Assert(annotation.Key).Equal("foo")
    70  				g.Assert(annotation.Value).Equal("bar/baz")
    71  			})
    72  
    73  			g.It("returns annotation when namespace contains key[value]", func() {
    74  				annotation, err := ParseAnnotation("centry.key[value]/foo=bar")
    75  				g.Assert(annotation != nil).IsTrue("expected annotation")
    76  				g.Assert(err == nil).IsTrue("expected no error")
    77  				g.Assert(annotation.Namespace).Equal("centry.key")
    78  				g.Assert(annotation.NamespaceValues["key"]).Equal("value")
    79  				g.Assert(annotation.Key).Equal("foo")
    80  				g.Assert(annotation.Value).Equal("bar")
    81  			})
    82  
    83  			g.It("returns annotation when namespace contains key[dashed-value]", func() {
    84  				annotation, err := ParseAnnotation("centry.key[dashed-value]/foo=bar")
    85  				g.Assert(annotation != nil).IsTrue("expected annotation")
    86  				g.Assert(err == nil).IsTrue("expected no error")
    87  				g.Assert(annotation.Namespace).Equal("centry.key")
    88  				g.Assert(annotation.NamespaceValues["key"]).Equal("dashed-value")
    89  				g.Assert(annotation.Key).Equal("foo")
    90  				g.Assert(annotation.Value).Equal("bar")
    91  			})
    92  
    93  			g.It("returns annotation when namespace contains key[double-dashed-value]", func() {
    94  				annotation, err := ParseAnnotation("centry.key[double-dashed-value]/foo=bar")
    95  				g.Assert(annotation != nil).IsTrue("expected annotation")
    96  				g.Assert(err == nil).IsTrue("expected no error")
    97  				g.Assert(annotation.Namespace).Equal("centry.key")
    98  				g.Assert(annotation.NamespaceValues["key"]).Equal("double-dashed-value")
    99  				g.Assert(annotation.Key).Equal("foo")
   100  				g.Assert(annotation.Value).Equal("bar")
   101  			})
   102  
   103  			g.It("returns annotation when namespace contains multiple key[value]", func() {
   104  				annotation, err := ParseAnnotation("centry.key1[value1].key2[value2]/foo=bar")
   105  				g.Assert(annotation != nil).IsTrue("expected annotation")
   106  				g.Assert(err == nil).IsTrue("expected no error")
   107  				g.Assert(annotation.Namespace).Equal("centry.key1.key2")
   108  				g.Assert(annotation.NamespaceValues["key1"]).Equal("value1")
   109  				g.Assert(annotation.NamespaceValues["key2"]).Equal("value2")
   110  				g.Assert(annotation.Key).Equal("foo")
   111  				g.Assert(annotation.Value).Equal("bar")
   112  			})
   113  
   114  			g.It("returns annotation when namespace contains key[value] where value has special character", func() {
   115  				annotation, err := ParseAnnotation("centry.key1[value:1].key2[value_2]/foo=bar")
   116  				g.Assert(annotation != nil).IsTrue("expected annotation")
   117  				g.Assert(err == nil).IsTrue("expected no error")
   118  				g.Assert(annotation.Namespace).Equal("centry.key1.key2")
   119  				g.Assert(annotation.NamespaceValues["key1"]).Equal("value:1")
   120  				g.Assert(annotation.NamespaceValues["key2"]).Equal("value_2")
   121  				g.Assert(annotation.Key).Equal("foo")
   122  				g.Assert(annotation.Value).Equal("bar")
   123  			})
   124  		})
   125  	})
   126  }