github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/openshift/item_test.go (about)

     1  package openshift
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/ghodss/yaml"
     8  	"github.com/opendevstack/tailor/internal/test/helper"
     9  )
    10  
    11  func TestNewResourceItem(t *testing.T) {
    12  	item := getItem(t, getBuildConfig(), "template")
    13  	if item.Kind != "BuildConfig" {
    14  		t.Errorf("Kind is %s but should be BuildConfig", item.Kind)
    15  	}
    16  	if item.Name != "foo" {
    17  		t.Errorf("Name is %s but should be foo", item.Name)
    18  	}
    19  	if item.Labels["app"] != "foo" {
    20  		t.Errorf("Label app is %s but should be foo", item.Labels["app"])
    21  	}
    22  }
    23  
    24  func getPlatformItem(t *testing.T, filename string) *ResourceItem {
    25  	return getItem(t, helper.ReadFixtureFile(t, filename), "platform")
    26  }
    27  
    28  func getTemplateItem(t *testing.T, filename string) *ResourceItem {
    29  	return getItem(t, helper.ReadFixtureFile(t, filename), "template")
    30  }
    31  
    32  func getItem(t *testing.T, input []byte, source string) *ResourceItem {
    33  	var f interface{}
    34  	err := yaml.Unmarshal(input, &f)
    35  	if err != nil {
    36  		t.Fatalf("Could not umarshal yaml: %v", err)
    37  	}
    38  	m := f.(map[string]interface{})
    39  	item, err := NewResourceItem(m, source)
    40  	if err != nil {
    41  		t.Errorf("Could not create item: %v", err)
    42  	}
    43  	return item
    44  }
    45  
    46  func getBuildConfig() []byte {
    47  	return []byte(
    48  		`apiVersion: v1
    49  kind: BuildConfig
    50  metadata:
    51    annotations: {}
    52    labels:
    53      app: foo
    54    name: foo
    55  spec:
    56    failedBuildsHistoryLimit: 5
    57    nodeSelector: null
    58    output:
    59      to:
    60        kind: ImageStreamTag
    61        name: foo:latest
    62    postCommit: {}
    63    resources: {}
    64    runPolicy: Serial
    65    source:
    66      binary: {}
    67      type: Binary
    68    strategy:
    69      dockerStrategy: {}
    70      type: Docker
    71    successfulBuildsHistoryLimit: 5
    72    triggers:
    73    - generic:
    74        secret: password
    75      type: Generic
    76    - imageChange: {}
    77      type: ImageChange
    78    - type: ConfigChange`)
    79  }
    80  
    81  func getRoute(host []byte) []byte {
    82  	config := []byte(
    83  		`apiVersion: v1
    84  kind: Route
    85  metadata:
    86    annotations: {}
    87    name: foo
    88  spec:
    89    host: HOST
    90    tls:
    91      insecureEdgeTerminationPolicy: Redirect
    92      termination: edge
    93    to:
    94      kind: Service
    95      name: foo
    96      weight: 100
    97    wildcardPolicy: None`)
    98  
    99  	return bytes.Replace(config, []byte("HOST"), host, -1)
   100  }