github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/pkg/labels/name_test.go (about)

     1  package labels_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/caos/orbos/pkg/labels"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  func expectValidNameLabels(t *testing.T, name string) *labels.Name {
    12  	l, err := labels.ForName(validComponentLabels(t), name)
    13  	if err != nil {
    14  		t.Fatal()
    15  	}
    16  	return l
    17  }
    18  
    19  func validNameLabels(t *testing.T) *labels.Name {
    20  	return expectValidNameLabels(t, "testcase")
    21  }
    22  
    23  func TestNameLabels_Equal(t *testing.T) {
    24  	expectValueEquality(
    25  		t,
    26  		validNameLabels(t),
    27  		validNameLabels(t),
    28  		expectValidNameLabels(t, "somethingElse"))
    29  }
    30  
    31  const validLabels = `app.kubernetes.io/name: testcase
    32  app.kubernetes.io/component: testSuite
    33  caos.ch/kind: testing.caos.ch/TestSuite
    34  caos.ch/apiversion: v1
    35  app.kubernetes.io/managed-by: TEST_OPERATOR_LABELS
    36  app.kubernetes.io/version: v123.4.5
    37  app.kubernetes.io/part-of: ORBOS
    38  `
    39  
    40  func TestNameLabels_MarshalYAML(t *testing.T) {
    41  	marshalled, err := yaml.Marshal(validNameLabels(t))
    42  	if err != nil {
    43  		t.Error(err, "expected successful mashalling")
    44  	}
    45  
    46  	if strings.TrimSpace(string(marshalled)) != strings.TrimSpace(validLabels) {
    47  		t.Errorf("expected \n%s\n but got \n%s\n", validLabels, string(marshalled))
    48  	}
    49  }
    50  
    51  func TestNameLabels_UnmarshalYAML(t *testing.T) {
    52  	name := &labels.Name{}
    53  	if err := yaml.Unmarshal([]byte(validLabels), name); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	reMarshalled, err := yaml.Marshal(name)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	got := string(reMarshalled)
    63  	if got != validLabels {
    64  		t.Errorf("expected %s but got %s", validLabels, got)
    65  	}
    66  }