sigs.k8s.io/external-dns@v0.14.1/endpoint/labels_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes 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 endpoint
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/suite"
    24  )
    25  
    26  type LabelsSuite struct {
    27  	suite.Suite
    28  	aesKey                       []byte
    29  	foo                          Labels
    30  	fooAsText                    string
    31  	fooAsTextWithQuotes          string
    32  	fooAsTextEncrypted           string
    33  	fooAsTextWithQuotesEncrypted string
    34  	barText                      string
    35  	barTextEncrypted             string
    36  	barTextAsMap                 Labels
    37  	noHeritageText               string
    38  	wrongHeritageText            string
    39  	multipleHeritageText         string // considered invalid
    40  }
    41  
    42  func (suite *LabelsSuite) SetupTest() {
    43  	suite.foo = map[string]string{
    44  		"owner":    "foo-owner",
    45  		"resource": "foo-resource",
    46  	}
    47  	suite.aesKey = []byte(")K_Fy|?Z.64#UuHm`}[d!GC%WJM_fs{_")
    48  	suite.fooAsText = "heritage=external-dns,external-dns/owner=foo-owner,external-dns/resource=foo-resource"
    49  	suite.fooAsTextWithQuotes = fmt.Sprintf(`"%s"`, suite.fooAsText)
    50  	suite.fooAsTextEncrypted = `+lvP8q9KHJ6BS6O81i2Q6DLNdf2JSKy8j/gbZKviTZlGYj7q+yDoYMgkQ1hPn6urtGllM5bfFMcaaHto52otQtiOYrX8990J3kQqg4s47m3bH3Ejl8RSxSSuWJM3HJtPghQzYg0/LSOsdQ0=`
    51  	suite.fooAsTextWithQuotesEncrypted = fmt.Sprintf(`"%s"`, suite.fooAsTextEncrypted)
    52  	suite.barTextAsMap = map[string]string{
    53  		"owner":    "bar-owner",
    54  		"resource": "bar-resource",
    55  		"new-key":  "bar-new-key",
    56  	}
    57  	suite.barText = "heritage=external-dns,,external-dns/owner=bar-owner,external-dns/resource=bar-resource,external-dns/new-key=bar-new-key,random=stuff,no-equal-sign,," // also has some random gibberish
    58  	suite.barTextEncrypted = "yi6vVATlgYN0enXBIupVK2atNUKtajofWMroWtvZjUanFZXlWvqjJPpjmMd91kv86bZj+syQEP0uR3TK6eFVV7oKFh/NxYyh238FjZ+25zlXW9TgbLoMalUNOkhKFdfXkLeeaqJjePB59t+kQBYX+ZEryK652asPs6M+xTIvtg07N7WWZ6SjJujm0RRISg=="
    59  	suite.noHeritageText = "external-dns/owner=random-owner"
    60  	suite.wrongHeritageText = "heritage=mate,external-dns/owner=random-owner"
    61  	suite.multipleHeritageText = "heritage=mate,heritage=external-dns,external-dns/owner=random-owner"
    62  }
    63  
    64  func (suite *LabelsSuite) TestSerialize() {
    65  	suite.Equal(suite.fooAsText, suite.foo.SerializePlain(false), "should serializeLabel")
    66  	suite.Equal(suite.fooAsTextWithQuotes, suite.foo.SerializePlain(true), "should serializeLabel")
    67  	suite.Equal(suite.fooAsText, suite.foo.Serialize(false, false, nil), "should serializeLabel")
    68  	suite.Equal(suite.fooAsTextWithQuotes, suite.foo.Serialize(true, false, nil), "should serializeLabel")
    69  	suite.Equal(suite.fooAsText, suite.foo.Serialize(false, false, suite.aesKey), "should serializeLabel")
    70  	suite.Equal(suite.fooAsTextWithQuotes, suite.foo.Serialize(true, false, suite.aesKey), "should serializeLabel")
    71  	suite.NotEqual(suite.fooAsText, suite.foo.Serialize(false, true, suite.aesKey), "should serializeLabel and encrypt")
    72  	suite.NotEqual(suite.fooAsTextWithQuotes, suite.foo.Serialize(true, true, suite.aesKey), "should serializeLabel and encrypt")
    73  }
    74  
    75  func (suite *LabelsSuite) TestEncryptionNonceReUsage() {
    76  	foo, err := NewLabelsFromString(suite.fooAsTextEncrypted, suite.aesKey)
    77  	suite.NoError(err, "should succeed for valid label text")
    78  	serialized := foo.Serialize(false, true, suite.aesKey)
    79  	suite.Equal(serialized, suite.fooAsTextEncrypted, "serialized result should be equal")
    80  }
    81  
    82  func (suite *LabelsSuite) TestDeserialize() {
    83  	foo, err := NewLabelsFromStringPlain(suite.fooAsText)
    84  	suite.NoError(err, "should succeed for valid label text")
    85  	suite.Equal(suite.foo, foo, "should reconstruct original label map")
    86  
    87  	foo, err = NewLabelsFromStringPlain(suite.fooAsTextWithQuotes)
    88  	suite.NoError(err, "should succeed for valid label text")
    89  	suite.Equal(suite.foo, foo, "should reconstruct original label map")
    90  
    91  	foo, err = NewLabelsFromString(suite.fooAsTextEncrypted, suite.aesKey)
    92  	suite.NoError(err, "should succeed for valid encrypted label text")
    93  	for key, val := range suite.foo {
    94  		suite.Equal(val, foo[key], "should contains all keys from original label map")
    95  	}
    96  
    97  	foo, err = NewLabelsFromString(suite.fooAsTextWithQuotesEncrypted, suite.aesKey)
    98  	suite.NoError(err, "should succeed for valid encrypted label text")
    99  	for key, val := range suite.foo {
   100  		suite.Equal(val, foo[key], "should contains all keys from original label map")
   101  	}
   102  
   103  	bar, err := NewLabelsFromStringPlain(suite.barText)
   104  	suite.NoError(err, "should succeed for valid label text")
   105  	suite.Equal(suite.barTextAsMap, bar, "should reconstruct original label map")
   106  
   107  	bar, err = NewLabelsFromString(suite.barText, suite.aesKey)
   108  	suite.NoError(err, "should succeed for valid encrypted label text")
   109  	suite.Equal(suite.barTextAsMap, bar, "should reconstruct original label map")
   110  
   111  	noHeritage, err := NewLabelsFromStringPlain(suite.noHeritageText)
   112  	suite.Equal(ErrInvalidHeritage, err, "should fail if no heritage is found")
   113  	suite.Nil(noHeritage, "should return nil")
   114  
   115  	wrongHeritage, err := NewLabelsFromStringPlain(suite.wrongHeritageText)
   116  	suite.Equal(ErrInvalidHeritage, err, "should fail if wrong heritage is found")
   117  	suite.Nil(wrongHeritage, "if error should return nil")
   118  
   119  	multipleHeritage, err := NewLabelsFromStringPlain(suite.multipleHeritageText)
   120  	suite.Equal(ErrInvalidHeritage, err, "should fail if multiple heritage is found")
   121  	suite.Nil(multipleHeritage, "if error should return nil")
   122  }
   123  
   124  func TestLabels(t *testing.T) {
   125  	suite.Run(t, new(LabelsSuite))
   126  }