github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/field_map_test.go (about)

     1  // +build unit
     2  
     3  package kube
     4  
     5  import (
     6  	"testing"
     7  
     8  	jenkinsio_v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
     9  	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  func TestField(t *testing.T) {
    16  	RegisterFailHandler(Fail)
    17  	RunSpecs(t, "Pipeline Activity Integration Test Suite")
    18  }
    19  
    20  var _ = Describe("fieldMap", func() {
    21  	var pipelineActivity jenkinsio_v1.PipelineActivity
    22  	var fieldMap fieldMap
    23  	var err error
    24  
    25  	BeforeEach(func() {
    26  		pipelineActivity = jenkinsio_v1.PipelineActivity{
    27  			ObjectMeta: meta_v1.ObjectMeta{
    28  				Name: "test-pa",
    29  				Labels: map[string]string{
    30  					"lastCommitSHA": "5c25d20893323977de5606c10a377921fed99c1c",
    31  				},
    32  			},
    33  			Spec: jenkinsio_v1.PipelineActivitySpec{
    34  				LastCommitSHA: "5c25d20893323977de5606c10a377921fed99c1c",
    35  			},
    36  		}
    37  
    38  		fieldMap, err = newFieldMap(pipelineActivity)
    39  		Expect(err).Should(BeNil())
    40  	})
    41  
    42  	It("#Has returns true for existing fields", func() {
    43  		Expect(fieldMap.Has("metadata.name")).Should(BeTrue())
    44  		Expect(fieldMap.Has("spec.lastCommitSHA")).Should(BeTrue())
    45  	})
    46  
    47  	It("#Has returns false for non existing fields", func() {
    48  		Expect(fieldMap.Has("metadata.foo")).Should(BeFalse())
    49  		Expect(fieldMap.Has("foo")).Should(BeFalse())
    50  	})
    51  
    52  	It("#Has returns false for the empty string", func() {
    53  		Expect(fieldMap.Has("")).Should(BeFalse())
    54  	})
    55  
    56  	It("#Has returns false for non existing fields", func() {
    57  		Expect(fieldMap.Has("metadata.foo")).Should(BeFalse())
    58  		Expect(fieldMap.Has("foo")).Should(BeFalse())
    59  	})
    60  
    61  	It("#Has returns false for the empty string", func() {
    62  		Expect(fieldMap.Has("")).Should(BeFalse())
    63  	})
    64  
    65  	It("#Get returns the appropriate value for existing fields", func() {
    66  		Expect(fieldMap.Get("metadata.name")).Should(Equal("test-pa"))
    67  		Expect(fieldMap.Get("spec.lastCommitSHA")).Should(Equal("5c25d20893323977de5606c10a377921fed99c1c"))
    68  	})
    69  
    70  	It("#Get returns the empty string for non existing fields", func() {
    71  		Expect(fieldMap.Get("foo")).Should(Equal(""))
    72  	})
    73  })