sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/tags_test.go (about)

     1  /*
     2  Copyright 2019 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 converters
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/onsi/gomega"
    23  	"k8s.io/utils/ptr"
    24  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    25  )
    26  
    27  func Test_TagsToMap(t *testing.T) {
    28  	cases := []struct {
    29  		tags   infrav1.Tags
    30  		expect map[string]*string
    31  	}{
    32  		{
    33  			tags:   nil,
    34  			expect: nil,
    35  		},
    36  		{
    37  			tags:   infrav1.Tags{},
    38  			expect: map[string]*string{},
    39  		},
    40  		{
    41  			tags:   infrav1.Tags{"env": "prod"},
    42  			expect: map[string]*string{"env": ptr.To("prod")},
    43  		},
    44  	}
    45  
    46  	for _, c := range cases {
    47  		c := c
    48  		t.Run("name", func(t *testing.T) {
    49  			t.Parallel()
    50  			g := gomega.NewGomegaWithT(t)
    51  			result := TagsToMap(c.tags)
    52  			g.Expect(c.expect).To(gomega.BeEquivalentTo(result))
    53  		})
    54  	}
    55  }
    56  
    57  func Test_MapToTags(t *testing.T) {
    58  	cases := []struct {
    59  		tags   map[string]*string
    60  		expect infrav1.Tags
    61  	}{
    62  		{
    63  			tags:   nil,
    64  			expect: nil,
    65  		},
    66  		{
    67  			tags:   map[string]*string{},
    68  			expect: infrav1.Tags{},
    69  		},
    70  		{
    71  			tags:   map[string]*string{"env": ptr.To("prod")},
    72  			expect: infrav1.Tags{"env": "prod"},
    73  		},
    74  	}
    75  
    76  	for _, c := range cases {
    77  		c := c
    78  		t.Run("name", func(t *testing.T) {
    79  			t.Parallel()
    80  			g := gomega.NewGomegaWithT(t)
    81  			result := MapToTags(c.tags)
    82  			g.Expect(c.expect).To(gomega.BeEquivalentTo(result))
    83  		})
    84  	}
    85  }
    86  
    87  // convert the value to map then back to tags
    88  // and make sure that the value we get is strictly equal
    89  // to the original value (ie. round trip conversion).
    90  func Test_TagsToMapRoundTrip(t *testing.T) {
    91  	cases := []struct {
    92  		tags   infrav1.Tags
    93  		expect infrav1.Tags
    94  	}{
    95  		{
    96  			tags:   nil,
    97  			expect: nil,
    98  		},
    99  		{
   100  			tags:   infrav1.Tags{},
   101  			expect: infrav1.Tags{},
   102  		},
   103  		{
   104  			tags:   infrav1.Tags{"env": "prod"},
   105  			expect: infrav1.Tags{"env": "prod"},
   106  		},
   107  	}
   108  
   109  	for _, c := range cases {
   110  		c := c
   111  		t.Run("name", func(t *testing.T) {
   112  			t.Parallel()
   113  			g := gomega.NewGomegaWithT(t)
   114  			result := MapToTags(TagsToMap(c.tags))
   115  			g.Expect(c.expect).To(gomega.BeEquivalentTo(result))
   116  		})
   117  	}
   118  }
   119  
   120  // convert the value to tags then back to map
   121  // and make sure that the value we get is strictly equal
   122  // to the original value (ie. round trip conversion).
   123  func Test_MapToTagsMapRoundTrip(t *testing.T) {
   124  	cases := []struct {
   125  		tags   map[string]*string
   126  		expect map[string]*string
   127  	}{
   128  		{
   129  			tags:   nil,
   130  			expect: nil,
   131  		},
   132  		{
   133  			tags:   map[string]*string{},
   134  			expect: map[string]*string{},
   135  		},
   136  		{
   137  			tags:   map[string]*string{"env": ptr.To("prod")},
   138  			expect: map[string]*string{"env": ptr.To("prod")},
   139  		},
   140  	}
   141  
   142  	for _, c := range cases {
   143  		c := c
   144  		t.Run("name", func(t *testing.T) {
   145  			t.Parallel()
   146  			g := gomega.NewGomegaWithT(t)
   147  			result := TagsToMap(MapToTags(c.tags))
   148  			g.Expect(c.expect).To(gomega.BeEquivalentTo(result))
   149  		})
   150  	}
   151  }